gdle/Source/ServerAPI.cpp

138 lines
2.4 KiB
C++
Raw Permalink Normal View History

2018-03-29 17:01:57 -04:00
2019-07-07 20:56:15 +00:00
#include <StdAfx.h>
2018-03-29 17:01:57 -04:00
#include "ServerAPI.h"
#include "Server.h"
extern void InitPhatSDK();
extern void CleanupPhatSDK();
#if defined(WIN32) && defined(_WINDLL)
bool g_bStarted = false;
CPhatServer *g_pPhatServer = NULL;
HINSTANCE g_hInstance = NULL;
HANDLE g_hQuitEvent = NULL;
HANDLE g_hServerThread = NULL;
2019-07-07 20:56:15 +00:00
uint32_t WINAPI MainServerThread(LPVOID)
2018-03-29 17:01:57 -04:00
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
srand((unsigned int)time(NULL));
Random::Init();
g_pGlobals = new CGlobals();
g_Logger.Open();
WSADATA wsaData;
USHORT wVersionRequested = 0x0202;
WSAStartup(wVersionRequested, &wsaData);
InitPhatSDK();
2019-07-07 20:56:15 +00:00
extern uint64_t g_RandomAdminPassword;
g_RandomAdminPassword = ((uint64_t)Random::GenUInt(0, 0xFFFFFFF0) << 32) | Random::GenUInt(0, 0xFFFFFFF0);
2018-03-29 17:01:57 -04:00
LOG(UserInterface, Normal, "Welcome to GDL - Classic Dereth!\n");
unsigned long serverIP = GetLocalIP();
int serverPort = 9050;
g_pPhatServer = new CPhatServer();
while (WaitForSingleObject(g_hQuitEvent, 0) != WAIT_OBJECT_0)
{
g_pPhatServer->Tick();
}
SafeDelete(g_pPhatServer);
CleanupPhatSDK();
WSACleanup();
SafeDelete(g_pGlobals);
g_Logger.Close();
#if !defined(QUICKSTART) && defined(_DEBUG) // check for memory leaks
if (_CrtDumpMemoryLeaks())
OutputDebugString("Memory leak found!\n");
else
OutputDebugString("No memory leaks found!\n");
#endif
return 0;
}
extern "C"
{
int _IsStarted()
{
return g_bStarted ? 1 : 0;
}
int _StartServer()
{
if (g_bStarted)
return 0;
g_hQuitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!g_hQuitEvent)
return -1;
g_hServerThread = CreateThread(NULL, 0, MainServerThread, NULL, 0, NULL);
if (!g_hServerThread)
{
CloseHandle(g_hQuitEvent);
return -1;
}
g_bStarted = true;
return 0;
}
void _StopServer()
{
if (!g_bStarted)
return;
SetEvent(g_hQuitEvent);
if (g_hServerThread)
{
WaitForSingleObject(g_hServerThread, 60000);
CloseHandle(g_hServerThread);
g_hServerThread = NULL;
}
if (g_hQuitEvent)
{
CloseHandle(g_hQuitEvent);
g_hQuitEvent = NULL;
}
g_bStarted = false;
}
}
2019-07-07 20:56:15 +00:00
BOOL WINAPI DllMain(HINSTANCE hInstance, uint32_t reason, LPVOID your_mom)
2018-03-29 17:01:57 -04:00
{
BOOL success = TRUE;
switch (reason)
{
case DLL_PROCESS_ATTACH:
g_hInstance = hInstance;
DisableThreadLibraryCalls(hInstance);
break;
case DLL_PROCESS_DETACH:
_StopServer();
break;
}
return success;
}
#endif