// *************************************************************** // Function: Main.cpp // *************************************************************** //Load the Visual Leak Detector stuff #define VLD_MAX_DATA_DUMP 0 #include "../../src/VLD/vld.h" //Load our global header file #include "Main.h" #include "Config.h" #ifdef WIN32 #include "XCrash/ExceptionHandler.h" #endif //OS neutral #include "../../src/Log.h" #include "../../src/timer.h" GWLPLog gCoreLog; Server *gpLoginServer=NULL; Server *gpInternalComm=NULL; PacketMan gPacketManager; MYSQL_SERVER gDatabaseInfo; SERVER_DATA gData; GWLPDatabase gDatabase; void *gpAppMutex; hash_map gBruteForceTracker; SERVER_CONFIG gServerConfig; SERVER_STATUS gServerStatus; bool gShuttingDown=false; bool gShutdownOK=false; char gCmdLine[250]; int Initialize(int argc, char* argv[]); void Restart(); #ifdef WIN32 //************************************ // Name: HandlerRoutine // // Called when the user clicks the close button in Windows on the window // // 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; } return true; } #endif #ifdef WIN32 #define REI RecordExceptionInfo(GetExceptionInformation(), "Main") #else #define REI true #endif //************************************ // Name: main // // The entry point of the program. Sets up crash handler and initializes the server. // // 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, "gwlploginserver")!=NULL) { Sleep(1); } //Start GW PROCESS_INFORMATION p; STARTUPINFO si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&p, sizeof(p)); char temp[100]; strcpy(temp, argv[0]); if (CreateProcess(0, temp, 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, "gwlploginserver"); if (gpAppMutex!=NULL) { return -1; } gpAppMutex=CreateMutex(NULL, true, "gwlploginserver"); } #endif #ifdef WIN32 //set-up so shutdown is called when the application is closed SetConsoleCtrlHandler(HandlerRoutine, true); #endif //Call our main program xD Initialize(argc, argv); } __except(REI) { gCoreLog.LOCATION; gCoreLog.error("CRASH!! Attempting to restart server!"); Restart(); } return 0; }