BOOL KApexProxy::Connect(const char szIP[], int nPort) { BOOL bResult = false; BOOL bRetCode = false; timeval TimeoutValue = {0, 0}; KG_SocketConnector Connector; assert(m_piSocketStream == NULL); m_piSocketStream = Connector.Connect(szIP, nPort); KG_PROCESS_ERROR(m_piSocketStream); bRetCode = m_piSocketStream->SetTimeout(&TimeoutValue); KGLOG_PROCESS_ERROR(bRetCode); m_bSendErrorFlag = false; bResult = true; Exit0: if (!bResult) { KG_COM_RELEASE(m_piSocketStream); } KGLogPrintf( KGLOG_INFO, "[ApexProxy] Connect to %s : %d ... ... [%s]", szIP, nPort, bResult ? "Ok" : "Failed" ); return bResult; }
BOOL KLogClient::Connect() { BOOL bResult = false; BOOL bRetCode = false; KG_SocketConnector Connector; struct timeval TimeVal; IKG_SocketStream* piSocketStream = NULL; IKG_Buffer* piPackage = NULL; G2L_HANDSHAKE_REQUEST* pHandshake = NULL; piSocketStream = Connector.Connect(m_szLogServerAddr, m_nRelayPort); KG_PROCESS_ERROR(piSocketStream); TimeVal.tv_sec = 0; TimeVal.tv_usec = 10000; bRetCode = piSocketStream->SetTimeout(&TimeVal); KGLOG_PROCESS_ERROR(bRetCode); m_bSocketError = false; //m_nWorldIndex = 0; m_nLastSendPacketTime = g_pSO3World->m_nCurrentTime; // 初始化的一些操作,注意多线程 piPackage = KG_MemoryCreateBuffer((unsigned)sizeof(G2L_HANDSHAKE_REQUEST)); KGLOG_PROCESS_ERROR(piPackage); pHandshake = (G2L_HANDSHAKE_REQUEST*)piPackage->GetData(); KGLOG_PROCESS_ERROR(pHandshake); pHandshake->wProtocolID = g2l_handshake_request; pHandshake->nServerTime = (int)time(NULL); pHandshake->nServerIndexInGC = g_pSO3World->m_nServerIndexInGC; bRetCode = piSocketStream->Send(piPackage); KGLOG_PROCESS_ERROR(bRetCode == 1); // 小心: 这里不能写成 "m_piSocket = piSocket; m_piSocket->AddRef(); ", 那样会导致线程安全隐患 piSocketStream->AddRef(); m_piSocketStream = piSocketStream; KGLogPrintf( KGLOG_INFO, "Connect to log server %s:%d ... ... [OK]", m_szLogServerAddr, m_nRelayPort ); bResult = true; Exit0: KG_COM_RELEASE(piPackage); KG_COM_RELEASE(piSocketStream); return bResult; }
BOOL KLogServerAgency::LoginServer(const char cszIP[], int nPort, const char cszIdentity[]) { BOOL bResult = false; BOOL bRetCode = false; IKG_Buffer* piSendBuffer = NULL; KGCSLogServerLogin* pLogin = NULL; KG_SocketConnector Connector; struct timeval TimeVal; assert(m_piSocket == NULL); m_piSocket = Connector.Connect(cszIP, nPort); KG_PROCESS_ERROR(m_piSocket); TimeVal.tv_sec = 2; TimeVal.tv_usec = 0; bRetCode = m_piSocket->SetTimeout(&TimeVal); KGLOG_PROCESS_ERROR(bRetCode); piSendBuffer = KG_MemoryCreateBuffer((unsigned)sizeof(KGCSLogServerLogin)); KGLOG_PROCESS_ERROR(piSendBuffer); pLogin = (KGCSLogServerLogin*)piSendBuffer->GetData(); KGLOG_PROCESS_ERROR(pLogin); pLogin->ProtocolType = LOGC2S_LOG_LOGIN; pLogin->ulIdentity = 0; strncpy(pLogin->szGatewayName, cszIdentity, sizeof(pLogin->szGatewayName)); pLogin->szGatewayName[sizeof(pLogin->szGatewayName) - 1] = '\0'; bRetCode = Send(piSendBuffer); KGLOG_PROCESS_ERROR(bRetCode); bResult = true; Exit0: KGLogPrintf( KGLOG_INFO, "Connect to log server %s:%d ... ... [%s]", cszIP, nPort, bResult ? "OK" : "Failed" ); if (!bResult) { KG_COM_RELEASE(m_piSocket); } KG_COM_RELEASE(piSendBuffer); return bResult; }