📄 hatcp.c
字号:
if(!(ha->baseChan.status&NWST_LISTEN)) {#ifdef NW_DEBUG_OUTPUT Printf( "\n Set listen mode\n");#endif 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 // belongs to master /* * Waiting for the connection request */#ifdef NW_DEBUG_OUTPUT Printf( "\n Listener: Waiting for the connection request...\n" );#endif if ( (ret = nw_accept(&ha->baseChan, &_ch, timeout)) != MCO_S_OK ) {#ifdef NW_DEBUG_OUTPUT if(ret != MCO_E_NW_TIMEOUT) Printf("Error accepting connection %d\n", ret); else Printf("\nConnection timeout\n");#endif return ret; }#ifdef NW_DEBUG_OUTPUT Printf("connection accepted\n");#endif/* * Creating and initializing the channel descriptor */ ch = (nw_channel_h)malloc(sizeof(nw_channel_t)); // create the new channel descriptor memcpy(ch,&_ch,sizeof(nw_channel_t)); // copy the descriptor from stack to the new instance#ifndef _WIN32 ch->cancel_socket = INVALID_SOCKET_VALUE;#endif ch->mco_channel.fsend = (mco_xstream_write)mco_nw_send; // set pointer to virtual HA send-receive methods ch->mco_channel.frecv = (mco_xstream_read)mco_nw_recv; // to it's internal descriptor ch->db = db; sprintf(name, "mco_replica_%d", ch->index); // generate the name of the replica/* * Connection request is just arrived, attaching to replica */#ifdef NW_DEBUG_OUTPUT Printf( " Attaching to replica, please wait...\n" );#endif init_time = mco_system_get_current_time(); conn_flag = 0; ret = mco_HA_attach_replica( db, &ch->mco_channel, params, replica_name, ErrorHandler ); init_time = mco_system_get_current_time() - init_time; conn_flag = 0;#ifdef NW_DEBUG_OUTPUT if(ret) Printf("Error attaching to replica %d\n", ret);#endif return ret;}/*************************************************************************** * * Attaches master to the replica. * * IN OUT mco_db_h* db - pointer to database handle * IN const char*conn_string - interconnect dependent connection-string. * IN const mco_connection_param_t* params - connection & transaction timeouts. * OUT MCO_E_HA_REPLICA_STOP_REASON* stop_reason - the reason why the replica is stopped. * IN const char* db_name - database name. * IN mco_dictionary_h dict - pointer to database dictionary * IN void* mem_ptr - pointer to the memory allocated for database copy * IN uint4 total_size - size of allocated memory * IN unsigned long timeout - wait-for-connection timeout * IN OUT void * arg - user defined argument. In this context it implements special mode flag: * flag != 0 - replica loads master's database and then becomes master * * Description: * This function includes connection algotrithm dependent on the type of a communication * channel. It attaches the replica to the connected master. * * Returns MSO_S_OK if successful or error code (see above). */MCO_RET mco_nw_attach_master( mco_db_h* db, const char* conn_string, const mco_connection_param_t* params, MCO_E_HA_REPLICA_STOP_REASON* stop_reason, const char* db_name, mco_dictionary_h dict, void* mem_ptr, uint4 total_size, timer_unit timeout, void * arg){ MCO_RET ret; char name[NW_MAX_NAMELENGTH]; ha_h ha = (ha_h)arg; *stop_reason = MCO_HA_REPLICA_HANDSHAKE_FAILED; commit_initialized++;/* * If the base channel still belongs to master then delete all replicas & close all * master's channels */ if ( ha->baseChan.status & NWST_IS_MASTER ) { mco_nw_close( 0 ); }/* * If transport layer isn't initialized * then initialize it */ if ( !(ha->baseChan.status & NWST_INITIALIZED) ) {#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: %d\n",ret);#endif return ret; }#ifdef NW_DEBUG_OUTPUT Printf("done\n");#endif } else { nw_close( &ha->baseChan ); }/* For POSIX/Linux only. WIN32 uses events. */#ifndef _WIN32 ha->baseChan.cancel_socket = INVALID_SOCKET_VALUE; /* if "replica cancellation" feature is enabled then create channel for the cancel point */ if(cancel_flag) { printf("*** connect to cancellation socket... "); if( (ret = nw_connect_cancel_point( &ha->baseChan, CANCELPOINT_ADDR, timeout ) ) != MCO_S_OK ) { printf("can't connect to cancellation socket %ld\n", ret); EXIT(-1); } else { printf("connected. ***\n"); } }#endif //_WIN32/* * Connecting to master */#ifdef NW_DEBUG_OUTPUT Printf("Connecting...\n");#endif if ( (ret=nw_connect(&ha->baseChan, conn_string, timeout)) != MCO_S_OK ) {#ifdef NW_DEBUG_OUTPUT Printf("Error connecting: %d\n", ret);#endif return ret; }#ifdef NW_DEBUG_OUTPUT Printf("done\n");#endif/* * Initializing channel descriptor */ ha->baseChan.mco_channel.fsend = (mco_xstream_write)mco_nw_send; // set pointer to virtual HA send-receive methods ha->baseChan.mco_channel.frecv = (mco_xstream_read)mco_nw_recv; // to it's internal descriptor/* * Connection request is accepted, attaching to master */#ifdef NW_DEBUG_OUTPUT Printf( " Attaching to master...\n" );#endif/* This sample code compiles the unique replica database name from parameter "db_name" and unique replica index obtained from master. please remove the line below if you want to replicate existing database and use "db_name" instead of "name" *///*** sprintf(name, "%s%d", db_name, (int)ha->baseChan.index); strcpy((char*)db_name,name); reallocate_index(ha->baseChan.index); mco_db_kill(name);//*** Printf("DB name = %s\n",name); ha->baseChan.index = -1; ret = mco_HA_attach_master( db, // pointer to database handle (mco_channel_h) &ha->baseChan, // pointer to the communication channel params, // connection & transaction timeouts stop_reason, // stop reason name, // database name dict, // pointer to database dictionary mem_ptr, // pointer to the memory allocated for database copy total_size, // size of allocated memory (uint2)((ha->replicaMode &MCO_HAFLAG_FORCE_MASTER) | MCO_HAFLAG_REPLICA_NOTIFICATION) ); nw_close(&ha->baseChan); commit_initialized = 0; return ret;}/*************************************************************************** * * Closes communication channel * * IN OUT void * arg - user defined argument. Not used in this context * * Description: * If called by the replica the function closes the communication channel. * if called by the master then disconnects all replicas * connected to the master and closes all master/replica communication channels * * Returns MSO_S_OK if successful or error code (see above). */MCO_RET mco_nw_close (void *arg){ nw_channel_h ch; short nr; MCO_RET rc; ha_h ha = (ha_h)arg; if ( ha->baseChan.status & NWST_IS_MASTER ) { nw_close(&ha->baseChan); if(ha->isMainMaster != 0) { // if (main master) for(nr=mco_HA_get_number_of_replicas((mco_db_h)ha->baseChan.db); nr; nr-- ) { Printf("\nMaster: detaching replica %d ...", nr-1); ch = (nw_channel_h)mco_HA_get_io_channel((mco_db_h)ha->baseChan.db, (int2)(nr-1)); if ((rc = mco_HA_detach_replica((mco_db_h)ha->baseChan.db, (mco_channel_h)ch)) != MCO_S_OK) { Printf( "failed (%d)\n", rc ); } /* no need to close channel, it's closed by ErrorHandler */ } } } nw_close(&ha->baseChan); return MCO_S_OK;}#endif// CFG_TCP_SOCKET_CHANNEL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -