From 495abcbe7b721fa31fa2519035860548f71832ce Mon Sep 17 00:00:00 2001 From: Spoike Date: Sat, 8 Jan 2022 09:59:46 +0000 Subject: [PATCH] Fix up mapcluster functionality for windows. git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@6150 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- engine/client/cl_main.c | 2 +- engine/client/sys_win.c | 53 +++++---------------------------- engine/common/sys_win_threads.c | 27 +++++++++++++---- engine/server/server.h | 1 - engine/server/sv_cluster.c | 7 +++++ engine/server/sv_sys_win.c | 41 ++----------------------- 6 files changed, 39 insertions(+), 92 deletions(-) diff --git a/engine/client/cl_main.c b/engine/client/cl_main.c index 3cd814cd2..f19c27960 100644 --- a/engine/client/cl_main.c +++ b/engine/client/cl_main.c @@ -3224,7 +3224,7 @@ void CL_ConnectionlessPacket (void) { if (CL_IsPendingServerAddress(&net_from)) { - if (!NET_EnsureRoute(cls.sockets, "redir", cls.servername, &net_from)) + if (!NET_EnsureRoute(cls.sockets, "redir", cls.servername, &adr)) Con_Printf ("Unable to redirect to %s\n", data); else { diff --git a/engine/client/sys_win.c b/engine/client/sys_win.c index 5ecdef513..e7f294d9e 100644 --- a/engine/client/sys_win.c +++ b/engine/client/sys_win.c @@ -2176,50 +2176,7 @@ char *Sys_ConsoleInput (void) #ifdef SUBSERVERS if (SSV_IsSubServer()) - { - DWORD avail; - static char text[1024]; - static int textpos = 0; - - HANDLE input = GetStdHandle(STD_INPUT_HANDLE); - for (;;) - { - if (!PeekNamedPipe(input, NULL, 0, NULL, &avail, NULL)) - { - SV_FinalMessage("Cluster shut down\n"); - Cmd_ExecuteString("quit force", RESTRICT_LOCAL); - } - else if (avail) - { - if (avail > sizeof(text)-1-textpos) - avail = sizeof(text)-1-textpos; - if (ReadFile(input, text+textpos, avail, &avail, NULL)) - { - textpos += avail; - while(textpos >= 2) - { - unsigned short len = text[0] | (text[1]<<8); - if (textpos >= len && len >= 2) - { - memcpy(net_message.data, text+2, len-2); - net_message.cursize = len-2; - MSG_BeginReading (msg_nullnetprim); - - SSV_ReadFromControlServer(); - - memmove(text, text+len, textpos - len); - textpos -= len; - } - else - break; - } - } - } - else - break; - } return NULL; - } #endif @@ -2380,8 +2337,11 @@ qboolean Sys_InitTerminal (void) houtput = GetStdHandle (STD_OUTPUT_HANDLE); } - GetConsoleMode(hinput, &m); - SetConsoleMode(hinput, m | 0x40 | 0x80); + if (hinput) + { + GetConsoleMode(hinput, &m); + SetConsoleMode(hinput, m | 0x40 | 0x80); + } return true; } @@ -4322,8 +4282,9 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin { isDedicated = isClusterSlave = true; #ifdef _DEBUG - MessageBox(0, "New cluster slave starting\nAttach to process now, if desired.", "FTEQW", 0); + MessageBox(0, "New cluster slave starting\nAttach to process now, if desired.", "FTEQW Debug Build", 0); #endif + SSV_SetupControlPipe(Sys_GetStdInOutStream()); } #endif #endif diff --git a/engine/common/sys_win_threads.c b/engine/common/sys_win_threads.c index 8922b6186..4c5b72e03 100644 --- a/engine/common/sys_win_threads.c +++ b/engine/common/sys_win_threads.c @@ -474,7 +474,16 @@ static int QDECL Sys_MSV_ReadBytes (struct vfsfile_s *file, void *buffer, int by DWORD avail; //trying to do this stuff without blocking is a real pain. if (!PeekNamedPipe(s->inpipe, NULL, 0, NULL, &avail, NULL)) + { + /*switch(GetLastError()) + { + case ERROR_BROKEN_PIPE: + case ERROR_PIPE_NOT_CONNECTED: + case ERROR_INVALID_HANDLE: + break; + }*/ return -1; //EOF + } if (avail) { if (avail > bytestoread) @@ -551,12 +560,20 @@ vfsfile_t *Sys_ForkServer(void) return &ctx->pub; } -void Sys_InstructMaster(sizebuf_t *cmd) +vfsfile_t *Sys_GetStdInOutStream(void) { - //FIXME: this is blocking. this is bad if the target is also blocking while trying to write to us. - HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); - DWORD written = 0; - WriteFile(output, cmd->data, cmd->cursize, &written, NULL); + winsubserver_t *ctx = Z_Malloc(sizeof(*ctx)); + ctx->inpipe = GetStdHandle(STD_INPUT_HANDLE); + ctx->outpipe = GetStdHandle(STD_OUTPUT_HANDLE); + + //cause some errors if something else uses them (also avoid problems when closing). + SetStdHandle(STD_INPUT_HANDLE, NULL); + SetStdHandle(STD_OUTPUT_HANDLE, NULL); + + ctx->pub.ReadBytes = Sys_MSV_ReadBytes; + ctx->pub.WriteBytes = Sys_MSV_WriteBytes; + ctx->pub.Close = Sys_MSV_Close; + return &ctx->pub; } #endif diff --git a/engine/server/server.h b/engine/server/server.h index c471c17f8..fa0914df2 100644 --- a/engine/server/server.h +++ b/engine/server/server.h @@ -1240,7 +1240,6 @@ void SSV_SavePlayerStats(client_t *cl, int reason); //initial, periodic (in case void SSV_RequestShutdown(void); //asks the cluster to not send us new players vfsfile_t *Sys_ForkServer(void); -void Sys_InstructMaster(sizebuf_t *cmd); //first two bytes will always be the length of the data vfsfile_t *Sys_GetStdInOutStream(void); //obtains a bi-directional pipe for reading/writing via stdin/stdout. make sure the system code won't be using it. qboolean MSV_NewNetworkedNode(vfsfile_t *stream, qbyte *reqstart, qbyte *buffered, size_t buffersize, const char *remoteaddr); //call to register a pipe to a newly discovered node. diff --git a/engine/server/sv_cluster.c b/engine/server/sv_cluster.c index 5c4b17d77..a671cdcb6 100644 --- a/engine/server/sv_cluster.c +++ b/engine/server/sv_cluster.c @@ -1241,8 +1241,15 @@ void MSV_PollSlaves(void) } if (error) { + error = isClusterSlave; SSV_SetupControlPipe(NULL); inbuffersize = 0; + + if (error) + { + SV_FinalMessage("Cluster shut down\n"); + Cmd_ExecuteString("quit\n", RESTRICT_LOCAL); + } } } else if (msv_loop_to_ss) diff --git a/engine/server/sv_sys_win.c b/engine/server/sv_sys_win.c index 4a2d06a19..dbdf36a16 100644 --- a/engine/server/sv_sys_win.c +++ b/engine/server/sv_sys_win.c @@ -921,46 +921,7 @@ char *Sys_ConsoleInput (void) #ifdef SUBSERVERS if (SSV_IsSubServer()) - { - DWORD avail; - static char text[1024]; - static int textpos = 0; - - HANDLE input = GetStdHandle(STD_INPUT_HANDLE); - if (!PeekNamedPipe(input, NULL, 0, NULL, &avail, NULL)) - { - SV_FinalMessage("Cluster shut down\n"); - Cmd_ExecuteString("quit force", RESTRICT_LOCAL); - } - else if (avail) - { - if (avail > sizeof(text)-1-textpos) - avail = sizeof(text)-1-textpos; - if (ReadFile(input, text+textpos, avail, &avail, NULL)) - { - textpos += avail; - while(textpos >= 2) - { - unsigned short len = text[0] | (text[1]<<8); - if (textpos >= len && len >= 2) - { - memcpy(net_message.data, text+2, len-2); - net_message.cursize = len-2; - MSG_BeginReading (msg_nullnetprim); - - SSV_ReadFromControlServer(); - - memmove(text, text+len, textpos - len); - textpos -= len; - } - else - break; - } - } - - } return NULL; - } #endif if (isPlugin) @@ -1670,6 +1631,8 @@ int main (int argc, char **argv) #ifdef SUBSERVERS isClusterSlave = COM_CheckParm("-clusterslave"); + if (isClusterSlave) + SSV_SetupControlPipe(Sys_GetStdInOutStream()); #endif #ifdef USESERVICE if (!SSV_IsSubServer() && StartServiceCtrlDispatcher( DispatchTable))