DWORD WINAPI IocpServerWorker(LPVOID p) { SYSTEM_INFO SystemInfo; DWORD ThreadID; SOCKET Accept; int nRet; int ClientIndex; SOCKADDR_IN cAddr; IN_ADDR cInAddr; int cAddrlen = sizeof( cAddr ); LPPER_SOCKET_CONTEXT lpPerSocketContext = NULL; DWORD RecvBytes; DWORD Flags=0; InitializeCriticalSection(&criti); GetSystemInfo(&SystemInfo); g_dwThreadCount = SystemInfo.dwNumberOfProcessors * 2; __try { g_CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); if ( g_CompletionPort == NULL) { LogAdd("CreateIoCompletionPort failed with error: %d", GetLastError()); __leave; } for(DWORD dwCPU = 0; dwCPU < g_dwThreadCount; dwCPU++) { HANDLE ThreadHandle; // Create a server worker thread and pass the completion port to the thread. ThreadHandle = CreateThread(NULL, 0, ServerWorkerThread, g_CompletionPort, 0, &ThreadID); if ( ThreadHandle == NULL) { LogAdd("CreateThread() failed with error %d", GetLastError()); __leave; } g_ThreadHandles[dwCPU] = ThreadHandle; CloseHandle(ThreadHandle); } if (!CreateListenSocket() ) __leave; while(TRUE) { Accept = WSAAccept(g_Listen, (LPSOCKADDR)&cAddr, &cAddrlen, NULL, 0); if (Accept==SOCKET_ERROR) { EnterCriticalSection(&criti); LogAdd("WSAAccept() failed with error %d", WSAGetLastError()); LeaveCriticalSection(&criti); continue; } EnterCriticalSection(&criti); memcpy( &cInAddr, &cAddr.sin_addr.s_addr, 4 ); ClientIndex = gObjAddSearch(Accept, inet_ntoa(cInAddr) ); if ( ClientIndex == -1 ) { LogAddL("error-L2 : ClientIndex = -1"); closesocket(Accept); LeaveCriticalSection(&criti); continue; } if (UpdateCompletionPort(Accept, ClientIndex, 1) == 0 ) { LogAddL("error-L1 : %d %d CreateIoCompletionPort failed with error %d", Accept, ClientIndex, GetLastError() ); closesocket(Accept); LeaveCriticalSection(&criti); continue; } if (gObjAdd(Accept, inet_ntoa(cInAddr), ClientIndex) == -1 ) { LogAddL("error-L1 : %d %d gObjAdd() failed with error %d", Accept, ClientIndex, GetLastError() ); LeaveCriticalSection(&criti); closesocket(Accept); continue; } memset(&gObj[ClientIndex].PerSocketContext->IOContext[0].Overlapped, 0, sizeof(OVERLAPPED)); memset(&gObj[ClientIndex].PerSocketContext->IOContext[1].Overlapped, 0, sizeof(OVERLAPPED)); gObj[ClientIndex].PerSocketContext->IOContext[0].wsabuf.buf = gObj[ClientIndex].PerSocketContext->IOContext[0].Buffer; gObj[ClientIndex].PerSocketContext->IOContext[0].wsabuf.len = MAX_IO_BUFFER_SIZE; gObj[ClientIndex].PerSocketContext->IOContext[0].nTotalBytes = 0; gObj[ClientIndex].PerSocketContext->IOContext[0].nSentBytes = 0; gObj[ClientIndex].PerSocketContext->IOContext[0].nWaitIO = 0; gObj[ClientIndex].PerSocketContext->IOContext[0].nSecondOfs = 0; gObj[ClientIndex].PerSocketContext->IOContext[0].IOOperation = RECV_IO; gObj[ClientIndex].PerSocketContext->IOContext[1].wsabuf.buf = gObj[ClientIndex].PerSocketContext->IOContext[0].Buffer; gObj[ClientIndex].PerSocketContext->IOContext[1].wsabuf.len = MAX_IO_BUFFER_SIZE; gObj[ClientIndex].PerSocketContext->IOContext[1].nTotalBytes= 0; gObj[ClientIndex].PerSocketContext->IOContext[1].nSentBytes = 0; gObj[ClientIndex].PerSocketContext->IOContext[1].nWaitIO = 0; gObj[ClientIndex].PerSocketContext->IOContext[1].nSecondOfs = 0; gObj[ClientIndex].PerSocketContext->IOContext[1].IOOperation = SEND_IO; gObj[ClientIndex].PerSocketContext->m_socket = Accept; gObj[ClientIndex].PerSocketContext->nIndex = ClientIndex; nRet = WSARecv(Accept, &(gObj[ClientIndex].PerSocketContext->IOContext[0].wsabuf), 1, &RecvBytes, &Flags, &(gObj[ClientIndex].PerSocketContext->IOContext[0].Overlapped), NULL); if( nRet == SOCKET_ERROR && WSAGetLastError() != ERROR_IO_PENDING ) { LogAdd("error-L1 : WSARecv() failed with error %d", WSAGetLastError()); gObj[ClientIndex].PerSocketContext->IOContext[0].nWaitIO = 4; CloseClient(gObj[ClientIndex].PerSocketContext, FALSE); LeaveCriticalSection(&criti); continue; } gObj[ClientIndex].PerSocketContext->IOContext[0].nWaitIO = 1; gObj[ClientIndex].PerSocketContext->dwIOCount++; LeaveCriticalSection(&criti); SCPJoinResultSend(ClientIndex, 1); } } __finally { if( g_CompletionPort ) { for (DWORD i = 0; i < g_dwThreadCount; i++) PostQueuedCompletionStatus(g_CompletionPort, 0, 0, NULL); } if( g_CompletionPort ) { CloseHandle(g_CompletionPort); g_CompletionPort = NULL; } if (g_Listen != INVALID_SOCKET) { closesocket(g_Listen); g_Listen = INVALID_SOCKET; } } return TRUE; }
void GJJoinIdPassRequest(LPSDHP_IDPASS lpMsgIdPass,int aIndex ) { char szId[MAX_IDSTRING+1]; #if (S6EP2 == 1 || EX700==1) char szPass[20+1]; #else char szPass[MAX_IDSTRING+1]; #endif char szIpAddress[17]; char szJoominNumber[21]=""; BYTE result=0x01; int sbOfs=0; int vNumber=0; int UserNumber=-1; int DBNumber; SDHP_IDPASSRESULT spResult; memset(szId, 0, sizeof(szId)); memset(szPass, 0, sizeof(szPass)); memset(szIpAddress, 0, sizeof(szIpAddress)); memcpy(szId,lpMsgIdPass->Id,MAX_IDSTRING); memcpy(szPass,lpMsgIdPass->Pass,20); BuxConvert(szId, MAX_IDSTRING); BuxConvert(szPass, 20); char Block='0'; if( SQLSyntexCheck(szId) == FALSE || SQLSyntexCheck(szPass) == FALSE || SpaceSyntexCheck(szId) == FALSE) { result = 0x02; } else { EnterCriticalSection(&g_JSData.m_critJoinUser); int nRet = IsUser(szId,szPass,szJoominNumber,Block,DBNumber); if( nRet == 0 ) { result = 0; } if(gObjSearchUser(szId) == -1) { if( nRet == 1 && Block > '0' ) { if( Block == 'A' ) { result=0x0E; } else if( Block == 'B' ) { result=0x0F; } else result=0x05; } else if(nRet == 1) { UserNumber = gObjAdd(szId,szPass,DBNumber,aIndex,lpMsgIdPass->IpAddress,lpMsgIdPass->Number); if(UserNumber < 0) { result = 0x04; } else { gObj[UserNumber].bloc_code = Block; memcpy(gObj[UserNumber].JoomingNumber,szJoominNumber,20); g_Window.ServerLogAdd(ST_JOINSERVER,"[ %s ] User login",szId); } } else if(nRet == 0) { result = 0x00; } else { result = 0x02; } } else { if( result == 0 ) { result = 0x03; } else { result = 0x06; } g_Window.ServerLogAdd(ST_JOINSERVER,"[ %s ] [ERROR] User Connected login",szId); } LeaveCriticalSection(&g_JSData.m_critJoinUser); } spResult.h.size = sizeof( spResult ); spResult.h.c = PMHC_BYTE; spResult.h.headcode = 0x01; spResult.result = result; spResult.Number = lpMsgIdPass->Number; spResult.UserNumber = UserNumber; spResult.DBNumber = DBNumber; memcpy(spResult.JoominNumber, szJoominNumber, 20); memcpy(spResult.Id, szId, MAX_IDSTRING); DataSend(aIndex,(LPBYTE)&spResult,spResult.h.size); }