// *************************************************************** // Function: Main.cpp // *************************************************************** #define VLD_MAX_DATA_DUMP 0 #include "../../src/VLD/vld.h" #ifdef WIN32 #include "XCrash/ExceptionHandler.h" #endif #include "Main.h" //global variables see the header file for the meaning of each one GWLPLog gCoreLog; GWLPLog gScriptLog; Server *gpGameServer=NULL; Server *gpInternalComm=NULL; ScriptManager *gpScriptMan=NULL; GroupPermissions *gpPermissionGroups=NULL; PacketMan gPacketManager; MYSQL_SERVER gDatabaseInfo; GWLPDatabase gDatabase; SERVER_CONFIG gServerConfig; hash_map gInstances; ItemDefManager gItemDefMan; IDManager gInstanceIds; SERVER_STATS gServerStats; void *gpAppMutex; bool gShuttingDown=false; bool gShutdownOK=false; char gCmdLine[250]; #ifdef WIN32 //function declarations for functions referenced in this file with the function in the file. BOOL WINAPI HandlerRoutine(DWORD dwCtrlType); #endif int RealMain( int argc, char *argv[] ); void Restart(); #ifdef WIN32 #define REI RecordExceptionInfo(GetExceptionInformation(), "Main") #else #define REI true #endif //************************************ // Name: main // // Entry point for the program. // // Parameters: // argc - Number of command line arguments // argv[] - Command line arguments // // Return: // 0 - no errors // <0 - errors //************************************ int main( int argc, char *argv[] ) { __try { strcpy(gCmdLine, argv[0]); //If we have a command line argument we must run the code that launches the server again if (argc>1) { #ifdef WIN32 { while(OpenMutex(MUTEX_ALL_ACCESS, false, "gwlpgameserver")!=NULL) { Sleep(1); } //Start GW PROCESS_INFORMATION p; STARTUPINFO si; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory(&p, sizeof(p)); char c[100]; strcpy(c, argv[0]); if (CreateProcess(0, c, 0, 0, false, 0, 0, 0, &si, &p)==0) { return -2; } } #endif return 0; } //Prevent more than one login server from running #ifdef WIN32 { gpAppMutex=OpenMutex(MUTEX_ALL_ACCESS, false, "gwlpgameserver"); if (gpAppMutex!=NULL) { return -1; } gpAppMutex=CreateMutex(NULL, true, "gwlpgameserver"); } #endif //set-up so shutdown is called when the application is closed #ifdef WIN32 { SetConsoleCtrlHandler(HandlerRoutine, true); } #endif RealMain(argc, argv); } __except(REI) { gCoreLog.LOCATION; gCoreLog.error("CRASH!! Attempting to restart server!"); Restart(); } } //************************************ // Name: AccessInstance // // Accesses an instance. // // Parameters: // instanceId - Instance id // // Return: // NOT NULL - Pointer to instance with instanceId // NULL - No instance found //************************************ Instance* AccessInstance(long instanceId) { hash_map::iterator itr=gInstances.find(instanceId); if (itr!=gInstances.end()) { return &itr->second; } return NULL; } #ifdef WIN32 //************************************ // Name: HandlerRoutine // // Called when the user closes the application on Windows. // // Parameters: // dwCtrlType - event // // Return: // - true //************************************ BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) { switch(dwCtrlType) { case CTRL_CLOSE_EVENT: gShuttingDown=true; break; case CTRL_LOGOFF_EVENT: gShuttingDown=true; break; case CTRL_SHUTDOWN_EVENT: gShuttingDown=true; break; } while(!gShutdownOK){Sleep(90);} return true; } #endif //************************************ // Name: IncrementPlayerCount // // Increments our current player count and tells the login server. // // Parameters: // // Return: // //************************************ void IncrementPlayerCount() { //Increment our counters gServerStats.currentPlayers++; if (gServerStats.currentPlayers>gServerStats.maxPlayerPeak) { gServerStats.maxPlayerPeak=gServerStats.currentPlayers; } //Inform the login server of the current number of players unsigned char pck[4]={GS_UPDATE_PLAYER_COUNT, 2}; memcpy(pck+2, &gServerStats.currentPlayers, 2); NetMessage s; s.networkId=gpInternalComm->GetClientModeId(); s.pData=pck; s.dataSize=4; gpInternalComm->SendData(&s); } //************************************ // Name: DecrementPlayerCount // // Subtracts one from our player count tells the login server. // // Parameters: // // Return: // //************************************ void DecrementPlayerCount() { //Increment our counters gServerStats.currentPlayers--; //Inform the login server of the current number of players unsigned char pck[4]={GS_UPDATE_PLAYER_COUNT, 2}; memcpy(pck+2, &gServerStats.currentPlayers, 2); NetMessage s; s.networkId=gpInternalComm->GetClientModeId(); s.pData=pck; s.dataSize=4; gpInternalComm->SendData(&s); }