// Server is started, lets run our loop :D void CWorldServer::ServerLoop( ) { fd_set fds; int activity; maxfd = 0; sockaddr_in ClientInfo; SOCKET NewSocket; timeval timeout; maxfd = sock; OnServerStep(); //LMA BEGIN //MySQL Ping (every hour) //20070623, 221000 UINT time_last_ping=clock(); UINT delay_ping=3600000; //LMA END do { //LMA BEGIN //MySQL Ping //20070623, 221000 UINT etime = (UINT)round((clock( ) - time_last_ping)); if(etime>=delay_ping) { time_last_ping=clock(); Ping(); } //LMA END timeout.tv_sec = 0; timeout.tv_usec = 1000; NewSocket = INVALID_SOCKET; FD_ZERO( &fds ); pthread_mutex_lock( &PlayerMutex ); if(!Config.usethreads) FillFDS( &fds ); FD_SET( sock, &fds ); activity = select( maxfd+1, &fds, NULL, NULL, &timeout ); if ( activity == 0 ) { pthread_mutex_unlock( &PlayerMutex ); #ifdef _WIN32 Sleep(1); #else usleep(1); #endif continue; } if ( activity < 0 && errno != EINTR ) { #ifdef _WIN32 Log( MSG_ERROR, "Select command failed. Error #%i", WSAGetLastError() ); #else Log( MSG_ERROR, "Select command failed. Error #%i", errno ); #endif isActive = false; } if ( FD_ISSET( sock, &fds ) && ConnectedClients < 1024 ) { int clientinfolen = sizeof( sockaddr_in ); #ifdef _WIN32 NewSocket = accept( sock, (sockaddr*)&ClientInfo, (int*)&clientinfolen ); #else NewSocket = accept( sock, (sockaddr*)&ClientInfo, (socklen_t*)&clientinfolen ); #endif // TODO: check if server is full if (NewSocket != INVALID_SOCKET) { if(!isBanned(&ClientInfo)) AddUser( NewSocket, &ClientInfo ); else { Log( MSG_WARNING, "Banned client tried to connect: %s", inet_ntoa( ClientInfo.sin_addr ) ); close( NewSocket ); } } else { #ifdef _WIN32 Log( MSG_ERROR, "Error accepting socket: %i", WSAGetLastError() ); #else Log( MSG_ERROR, "Error accepting socket: %i", errno ); #endif } } if(!Config.usethreads) HandleClients( &fds ); pthread_mutex_unlock( &PlayerMutex ); #ifdef _WIN32 Sleep(1); #else usleep(1); #endif } while( isActive ); }
// Server is started, lets run our loop :D void CServerSocket::ServerLoop( ) { fd_set fds; int activity; maxfd = 0; sockaddr_in ClientInfo; SOCKET NewSocket; timeval timeout; maxfd = sock; OnServerStep(); do { if (!Ping( )) isActive = false; timeout.tv_sec = 0; timeout.tv_usec = 1000; NewSocket = INVALID_SOCKET; FD_ZERO( &fds ); if(!Config.usethreads) FillFDS( &fds ); FD_SET( sock, &fds ); activity = select( maxfd+1, &fds, NULL, NULL, &timeout ); if ( activity == 0 ) { // continue; FD_ZERO( &fds ); if(!Config.usethreads) FillFDS( &fds ); FD_SET( sckISC, &fds ); activity = select( maxfd+1, &fds, NULL, NULL, &timeout ); if ( activity == 0 )continue; if ( activity < 0 && errno != EINTR ) { #ifdef _WIN32 Log( MSG_ERROR, "Select command failed. Error #%i", WSAGetLastError() ); #else Log( MSG_ERROR, "Select command failed. Error #%i", errno ); #endif isActive = false; } if ( FD_ISSET( sckISC, &fds ) ) { int clientinfolen = sizeof( sockaddr_in ); #ifdef _WIN32 NewSocket = accept( sckISC, (sockaddr*)&ClientInfo, (int*)&clientinfolen ); #else NewSocket = accept( sckISC, (sockaddr*)&ClientInfo, (socklen_t*)&clientinfolen ); #endif // TODO: check if server is full if (NewSocket != INVALID_SOCKET) { AddUser( NewSocket, &ClientInfo, true ); } else { #ifdef _WIN32 Log( MSG_ERROR, "Error accepting socket: %i", WSAGetLastError() ); #else Log( MSG_ERROR, "Error accepting socket: %i", errno ); #endif } } } if ( activity < 0 && errno != EINTR ) { #ifdef _WIN32 Log( MSG_ERROR, "Select command failed. Error #%i", WSAGetLastError() ); #else Log( MSG_ERROR, "Select command failed. Error #%i", errno ); #endif isActive = false; } if ( FD_ISSET( sock, &fds ) ) { int clientinfolen = sizeof( sockaddr_in ); #ifdef _WIN32 NewSocket = accept( sock, (sockaddr*)&ClientInfo, (int*)&clientinfolen ); #else NewSocket = accept( sock, (sockaddr*)&ClientInfo, (socklen_t*)&clientinfolen ); #endif // TODO: check if server is full if (NewSocket != INVALID_SOCKET) AddUser( NewSocket, &ClientInfo, false ); else { #ifdef _WIN32 Log( MSG_ERROR, "Error accepting socket: %i", WSAGetLastError() ); #else Log( MSG_ERROR, "Error accepting socket: %i", errno ); #endif } } if(!Config.usethreads) HandleClients( &fds ); } while( isActive ); }