⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 haudp.c

📁 extremeDB s sample code,useful for you
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************* *                                                                 * *  haudp.c                                                        * *                                                                 * *  This file is a part of the eXtremeDB-HA Application Framework  * *  Copyright (c) 2001-2006 McObject LLC                           *  *  All Rights Reserved                                            * *                                                                 * *******************************************************************//* * ++ *  * PROJECT:   eXtremeDB(tm) (c) McObject LLC * * SUBSYSTEM: HA support * * MODULE:    haudp.c * * ABSTRACT:  Low level OS dependent IPC. Channel-over-UDP * * * VERSION:   1.0 * * HISTORY: *            1.0- 1 SS     21-Jul-2003 Created it was *                 2 SS      8-Nov-2003 Ported to Solaris *                 3 SS     12-Nov-2003 Ported to WinCE * * -- */#define INCLUDE_SOCKETS#include "interface.h"#ifdef CFG_UDP_SOCKET_CHANNEL#ifdef __cplusplus  extern "C" { #endifextern  int2  allocate_index();extern  void  deallocate_index(int2 index);extern  void  reallocate_index(int2 index);extern  void  nw_attach_buffer( nw_channel_h chan, PVOID buffer, uint2 bufsize);extern  int           cancel_flag;    // turns on "replica cancellation" featureextern uint4          NumBytes;extern  int           conn_flag;extern  timer_unit    init_time;extern  int           commit_initialized;extern  udp_msg_buf_t msgbuf;externMCO_RET nw_send_message( nw_channel_h chan,                           const void* buffer,  int2 buflen,                           long       timeout);externMCO_RET nw_recv_message( nw_channel_h  chan,                          const void* buffer,  int2 buflen,                          PSHORT    recvlen,  long timeout);#ifdef __cplusplus  }#endif/*************************************************************************** * Interface layer. Used by master - replica interconnections.             *  * The interface is simplified. All the internal protocol details are      * * hidden behind this layer from the application and MCOdb HA              *  ***************************************************************************//****************************************************************************** *  * Send method of HA runtime. *  * IN nw_channel_h  ch      - pointer to a channel descriptor. * IN const void *  buffer  - buffer of data to send. * IN unsigned      buflen  - number of bytes to send * IN timer_unit    timeout - wait-for-send-completion timeout. * * Description: * This send function is used implicitly by master-replica interconnection channel. * The pointers to this function MUST be set to HA internal descriptor mco_channel_t * in each channel descriptor. * * Returns the actual length of sent data or -1 if error. */static int mco_nw_send(                    nw_channel_h  ch,                    const void*   buffer,                    unsigned      buflen,                    timer_unit    timeout                    ){  MCO_RET ret;  if (      (ret=nw_send_message(ch, buffer, (int2)buflen, timeout)      ) == MCO_S_OK      )     return buflen;  else return -1;}/****************************************************************************** * *  Receive method of HA runtime. * * IN  nw_channel_h ch      - pointer to a channel descriptor. * OUT void *       buffer  - buffer for data to receive. * IN unsigned      buflen  - number of bytes to receive. * IN  timer_unit   timeout - wait-for-receive-completion timeout. * * Description: * This receive function is used implicitly by master-replica interconnection channel * mco_nw_recv MUST read exactly max_bytes !!! * The pointers to this function MUST be set to HA internal descriptor mco_channel_t * in each channel descriptor. * * Returns the actual length of sent data or -1 if error. */static int mco_nw_recv(                    nw_channel_h  ch,                    const void*   buffer,                    unsigned      buflen,                    timer_unit    timeout                    ){  MCO_RET ret;  int2    len;  if (       (ret=nw_recv_message(                            ch,                            buffer,                            (int2)buflen,                             &len,                            timeout       )     ) == MCO_S_OK )     return (int)len;  else return -1;}/*************************************************************************** * * HA error handler. * * IN ha_error_h  HAerror - pointer to ha_error_t structure described in mcoha.h. * The structure contains error code that caused the handler call & pointer to  * the descriptor of communication channel of the deleted replica. If the pointer not * equal to NULL then replica was deleted from master's list of replicas. * * Description: * HA error handler callback - closes replica channel & deallocates memory * used by it's descriptor */static void  ErrorHandler( ha_error_h  HAerror){  nw_channel_h ch = (nw_channel_h)HAerror->IOchannel;  uint4        addr;#ifdef  NW_DEBUG_OUTPUT        Printf("Master: HA error handler called, errcode = %d\n", HAerror->errcode);#endif    if(ch != 0) {   // if (replica was deleted) {}      addr = ntohl(ch->inaddr);#ifdef  NW_DEBUG_OUTPUT      Printf("Master: replica addr %s port %d\n",        inet_ntoa(*((struct in_addr*)&addr)),        ntohs(ch->sin.sin_port));#endif      nw_close((nw_channel_h)HAerror->IOchannel); // close communication channel      free(HAerror->IOchannel);  // free the memory allocated by the descriptor    }}/*************************************************************************** * * Attaches replica to the master. * * IN	mco_db_h db       - database descriptor pointer. * IN	const char *      - devname. * IN   const mco_connection_param_t* params - connection & transaction timeouts. * IN	const char * replica_name - replica name (optional). * IN unsigned long timeout       - wait-for-connection timeout * IN OUT void * arg              - user defined argument. In this context * it is used as main/slave master flag in multimaster mode * (for shared memory only) * * Description: * This function includes connection algotrithm dependent on the type of a communication * channel. It attaches connected replica to HA subsystem. * * Returns MSO_S_OK if successful or error code (see above). */MCO_RET mco_nw_attach_replica(                        mco_db_h       db,                        const char*    devname,              		const          mco_connection_param_t * params,             		const char*    replica_name,              		timer_unit     timeout,              		void *         arg){  MCO_RET         ret;  nw_channel_t    _ch;        // copy of IO channel in the stack  nw_channel_h	  ch;         // pointer to the new IO channel  ha_h            ha = (ha_h)arg;/* * If the transport protocol isn't initialized   * then initialize it */    if ( !(ha->baseChan.status&NWST_INITIALIZED) )  {      ha->baseChan.db      = db;               // database handle. Is used by mco_nw_close()      ha->baseChan.bufsize = sizeof(udp_msg_buf_t);#ifdef NW_DEBUG_OUTPUT      Printf( "\n  Initializing master communication channel's context, please wait...\n" );#endif      if( (ret = nw_init(&ha->baseChan)) != MCO_S_OK) {#ifdef NW_DEBUG_OUTPUT        Printf("Error initializing");#endif        return ret;      }    }/* For POSIX/Linux only. WIN32 uses events. */#ifndef _WIN32   ha->baseChan.cancel_socket = INVALID_SOCKET_VALUE;#endif //_WIN32/* * If master's listener channel isn't initialized   * then open listener channel */    if(!(ha->baseChan.status&NWST_LISTEN)) {      if ( (ret=nw_listen(&ha->baseChan, devname)) != MCO_S_OK )      {#ifdef  NW_DEBUG_OUTPUT        Printf("Error listening: %d\n", ret);#endif        return ret;      }#ifdef  NW_DEBUG_OUTPUT      Printf("done\n");#endif    }    ha->baseChan.db     = db;               // database handle. Is used by mco_nw_close()    ha->baseChan.status |= NWST_IS_MASTER;  // ID_MASTER flag used by function mco_nw_close()                                            // to check whether the transport layer still

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -