no message

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@1226 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
driAn
2007-08-11 17:22:19 +00:00
parent 176fea2e8d
commit f97ca126f0
2 changed files with 138 additions and 0 deletions
+20
View File
@@ -2,6 +2,9 @@
// runs dedicated or as client coroutine
#include "cube.h"
#include "servercontroller.h"
servercontroller *svcctrl = NULL;
#define valid_client(c) (clients.inrange(c) && clients[c]->type!=ST_EMPTY)
#define valid_flag(f) (f >= 0 && f < 2)
@@ -1674,6 +1677,7 @@ void serverslice(uint timeout) // main server update, called from cube main lo
{
#ifdef STANDALONE
int nextmillis = (int)enet_time_get();
if(svcctrl) svcctrl->keepalive();
#else
int nextmillis = isdedicated ? (int)enet_time_get() : lastmillis;
#endif
@@ -1773,6 +1777,7 @@ void serverslice(uint timeout) // main server update, called from cube main lo
void cleanupserver()
{
if(serverhost) enet_host_destroy(serverhost);
if(svcctrl) svcctrl->stop();
}
#ifndef STANDALONE
@@ -1834,6 +1839,21 @@ int main(int argc, char **argv)
int uprate = 0, maxcl = DEFAULTCLIENTS, scthreshold = -5;
char *sdesc = "", *ip = "", *master = NULL, *passwd = "", *maprot = "", *adminpasswd = NULL, *srvmsg = NULL;
if(isdedicated && !svcctrl)
{
#ifdef WIN32
svcctrl = new winservice();
#endif
if(svcctrl)
{
svcctrl->argc = argc; svcctrl->argv = argv;
int c = svcctrl->start();
#ifdef WIN32
return c;
#endif
}
}
for(int i = 1; i<argc; i++)
{
char *a = &argv[i][2];
+118
View File
@@ -0,0 +1,118 @@
// code for running the server as a background process/daemon/service
struct servercontroller
{
virtual bool start() = 0;
virtual void keepalive() = 0;
virtual void stop() = 0;
int argc;
char **argv;
};
#ifdef WIN32
struct winservice : servercontroller
{
SERVICE_STATUS_HANDLE statushandle;
SERVICE_STATUS status;
HANDLE stopevent;
winservice() { callbacks::svc = this; };
~winservice()
{
if(status.dwCurrentState != SERVICE_STOPPED) stop();
callbacks::svc = NULL;
}
bool start() // starts the server again on a new thread and returns once the windows service has stopped
{
SERVICE_TABLE_ENTRY dispatchtable[] = { "assaultcubeserver", (LPSERVICE_MAIN_FUNCTION)callbacks::main, NULL, NULL };
return StartServiceCtrlDispatcher(dispatchtable);
}
void keepalive()
{
report(SERVICE_RUNNING, 0);
handleevents();
};
void stop()
{
report(SERVICE_STOP_PENDING, 0);
if(stopevent) CloseHandle(stopevent);
status.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
report(SERVICE_STOPPED, 0);
}
void handleevents()
{
if(WaitForSingleObject(stopevent, 0) == WAIT_OBJECT_0) stop();
}
void WINAPI requesthandler(DWORD ctrl)
{
switch(ctrl)
{
case SERVICE_CONTROL_STOP:
report(SERVICE_STOP_PENDING, 0);
SetEvent(stopevent);
return;
default: break;
}
report(status.dwCurrentState, 0);
}
void report(DWORD state, DWORD wait)
{
status.dwCurrentState = state;
status.dwWaitHint = wait;
status.dwWin32ExitCode = NO_ERROR;
status.dwControlsAccepted = SERVICE_START_PENDING == state || SERVICE_STOP_PENDING == state ? 0 : SERVICE_ACCEPT_STOP;
if(state == SERVICE_RUNNING || state == SERVICE_STOPPED) status.dwCheckPoint = 0;
else status.dwCheckPoint++;
SetServiceStatus(statushandle, &status);
}
int WINAPI svcmain() // new server thread's entry point
{
statushandle = RegisterServiceCtrlHandler("assaultcubeserver", (LPHANDLER_FUNCTION)callbacks::requesthandler);
if(!statushandle) return EXIT_FAILURE;
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
status.dwServiceSpecificExitCode = 0;
report(SERVICE_START_PENDING, 3000);
stopevent = CreateEvent(NULL, true, false, NULL);
if(!stopevent) { stop(); return EXIT_FAILURE; }
extern int main(int argc, char **argv);
return main(0, NULL); // todo args
}
struct callbacks
{
static winservice *svc;
static int WINAPI main() { return svc->svcmain(); };
static void WINAPI requesthandler(DWORD request) { svc->requesthandler(request); };
};
/*void log(const char *msg, bool error)
{
HANDLE eventsrc = RegisterEventSource(NULL, "AC Server");
if(eventsrc)
{
int eventid = ((error ? 0x11 : 0x1) << 10) & (0x1 << 9) & (FACILITY_NULL << 6) & 0x1; // TODO: create event definitions
LPCTSTR msgs[1] = { msg };
int r = ReportEvent(eventsrc, error ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE, 0, 4, NULL, 1, 0, msgs, NULL);
DeregisterEventSource(eventsrc);
}
}*/
};
winservice *winservice::callbacks::svc = (winservice *)NULL;
#elif __GNUC__
struct unixdaemon : servercontroller
{
// UNDONE, AH
}
#endif