int CEmsInfo::ReceiveStaRequestProcess(CProtocol &cProto) { std::vector<char> vecPayload; CSessionInfo *cSessionInfo = NULL; CStmReqApi cDecApi; char *szPayload = NULL; char chFlag = 0; time_t tm = 0; tm = time(NULL); chFlag = cProto.GetFlag(); if(chFlag != CProtocol::FLAG_REQUEST){ STA_LOG(STA_ERR,"Invalid Flag(flag=%d)\n",chFlag); return STA_NOK; } cProto.GetPayload(vecPayload); cSessionInfo = new CSessionInfo(); cSessionInfo->SetReceiveTime(tm); szPayload = new char[vecPayload.size() + 1]; strncpy(szPayload, &vecPayload[0], vecPayload.size()); szPayload[vecPayload.size()] = '\0'; cSessionInfo->SetReceiveData(szPayload, vecPayload.size()); DEBUG_LOG("RECEIVE DATA (Wait 3 time)\n"); m_lstPendingQueue.push_back(cSessionInfo); return STA_OK; }
int CEmsInfo::CollectProcess() { int nRet = 0; time_t receiveTime = 0; time_t currentTime = 0; list<CSessionInfo*>::iterator iter; CSessionInfo *cSession = NULL; currentTime = time(NULL); for(iter=m_lstPendingQueue.begin();iter != m_lstPendingQueue.end();){ cSession = *iter; receiveTime = cSession->GetReceiveTime(); if((currentTime - receiveTime) > RECEIVE_WAIT_TIME){ DEBUG_LOG("RECEIVE PROCESS\n"); /* receive process */ nRet = CollectAnswer(cSession); if(nRet != STA_OK){ STA_LOG(STA_ERR,"STS Collect failed(nRet=%d)\n",nRet); } m_lstPendingQueue.erase(iter++); delete cSession; } else { iter++; } } return STA_OK; }
//UINT WINAPI AcceptThread(LPVOID lpParameter) DWORD WINAPI AcceptThread(LPVOID lpParameter) { int nLen = sizeof(SOCKADDR_IN); char szMsg[64]; SOCKET Accept; SOCKADDR_IN Address; while (TRUE) { Accept = accept(g_ssock, (struct sockaddr FAR *)&Address, &nLen); if (g_fTerminated) return 0; CSessionInfo* pNewUserInfo = (CSessionInfo*)GlobalAlloc(GPTR, sizeof(CSessionInfo)); if (pNewUserInfo) { pNewUserInfo->sock = Accept; CreateIoCompletionPort((HANDLE)pNewUserInfo->sock, g_hIOCP, (DWORD)pNewUserInfo, 0); if (g_xSessionList.AddNewNode(pNewUserInfo)) { int zero = 0; setsockopt(pNewUserInfo->sock, SOL_SOCKET, SO_SNDBUF, (char *)&zero, sizeof(zero) ); // ORZ: pNewUserInfo->Recv(); UpdateStatusBar(TRUE); szMsg[0] = '%'; szMsg[1] = 'O'; char *pszPos = ValToAnsiStr((int)Accept, &szMsg[2]); *pszPos++ = '/'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_net, pszPos); *pszPos++ = '.'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_host, pszPos); *pszPos++ = '.'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_lh, pszPos); *pszPos++ = '.'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_impno, pszPos); *pszPos++ = '$'; *pszPos = '\0'; SendExToServer(szMsg); } } } return 0; }
DWORD WINAPI ServerWorkerThread(LPVOID CompletionPortID) { DWORD dwBytesTransferred = 0; CSessionInfo* pSessionInfo = NULL; _LPTCOMPLETIONPORT lpPerIoData = NULL; char szPacket[DATA_BUFSIZE * 2]; char szMsg[32]; char *pszPos; while (TRUE) { if ( GetQueuedCompletionStatus( (HANDLE)CompletionPortID, &dwBytesTransferred, (LPDWORD)&pSessionInfo, (LPOVERLAPPED *)&lpPerIoData, INFINITE) == 0) { if (g_fTerminated) return 0; if (pSessionInfo) { szMsg[0] = '%'; szMsg[1] = 'X'; char *pszPos = ValToAnsiStr((int)pSessionInfo->sock, &szMsg[2]); *pszPos++ = '$'; *pszPos = '\0'; SendExToServer(szMsg); g_xSessionList.RemoveNodeByData(pSessionInfo); closesocket(pSessionInfo->sock); pSessionInfo->sock = INVALID_SOCKET; UpdateStatusBar(FALSE); GlobalFree(pSessionInfo); } continue; } if (g_fTerminated) return 0; if (dwBytesTransferred == 0) { szMsg[0] = '%'; szMsg[1] = 'X'; char *pszPos = ValToAnsiStr((int)pSessionInfo->sock, &szMsg[2]); *pszPos++ = '$'; *pszPos = '\0'; SendExToServer(szMsg); g_xSessionList.RemoveNodeByData(pSessionInfo); closesocket(pSessionInfo->sock); pSessionInfo->sock = INVALID_SOCKET; UpdateStatusBar(FALSE); GlobalFree(pSessionInfo); continue; } // ORZ: pSessionInfo->bufLen += dwBytesTransferred; while ( pSessionInfo->HasCompletionPacket() ) { szPacket[0] = '%'; szPacket[1] = 'A'; pszPos = ValToAnsiStr( (int) pSessionInfo->sock, &szPacket[2] ); *pszPos++ = '/'; pszPos = pSessionInfo->ExtractPacket( pszPos ); *pszPos++ = '$'; *pszPos = '\0'; SendExToServer( szPacket ); } // ORZ: if ( pSessionInfo->Recv() == SOCKET_ERROR && WSAGetLastError() != ERROR_IO_PENDING ) { InsertLogMsg(_TEXT("WSARecv() failed")); CloseSession(pSessionInfo->sock); continue; } } return 0; }
//UINT WINAPI AcceptThread(LPVOID lpParameter) //logingate接受线程 DWORD WINAPI AcceptThread(LPVOID lpParameter) { int nLen = sizeof(SOCKADDR_IN); char szMsg[128] = {0}; SOCKET Accept; SOCKADDR_IN Address; while (TRUE) { Accept = accept(g_ssock, (struct sockaddr FAR *)&Address, &nLen); if (g_fTerminated) return 0; CSessionInfo* pNewUserInfo = CSessionInfo::ObjPool().createObject();//(CSessionInfo*)GlobalAlloc(GPTR, sizeof(CSessionInfo)); if (pNewUserInfo) { pNewUserInfo->sock = Accept; CreateIoCompletionPort((HANDLE)pNewUserInfo->sock, g_hIOCP, (DWORD)pNewUserInfo, 0); if (g_xSessionList.AddNewNode(pNewUserInfo)) { int zero = 0; setsockopt(pNewUserInfo->sock, SOL_SOCKET, SO_SNDBUF, (char *)&zero, sizeof(zero) ); zero = 0; setsockopt( pNewUserInfo->sock, SOL_SOCKET, SO_RCVBUF, (char*)&zero, sizeof(zero)); int nodelay = 1; setsockopt( pNewUserInfo->sock, IPPROTO_TCP, TCP_NODELAY, (char*)&nodelay, sizeof(nodelay) ); // ORZ:接受数据 int retcode = pNewUserInfo->Recv(); if ( (retcode == SOCKET_ERROR) && (WSAGetLastError() != WSA_IO_PENDING) ) { continue; } UpdateStatusBar(TRUE); szMsg[0] = '%'; szMsg[1] = 'O'; char *pszPos = ValToAnsiStr((int)Accept, &szMsg[2]); *pszPos++ = '/'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_net, pszPos); *pszPos++ = '.'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_host, pszPos); *pszPos++ = '.'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_lh, pszPos); *pszPos++ = '.'; pszPos = ValToAnsiStr((int)Address.sin_addr.s_impno, pszPos); *pszPos++ = '$'; *pszPos = '\0'; //发送给loginsrv更新 SendExToServer(szMsg); } } } return 0; }