From 6c5d9c3507b47970c762ea5af0cd11f55822e323 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 1 Nov 2019 07:26:49 +0100 Subject: [PATCH] - moved the net init code out of the game frontends to avoid having to call atexit for their deinit function. When doing this during startup it can be done by regular cleanup measures. This also moves two larger chunks of networking code out of game.cpp. Nevertheless, the fact that enet is a very dirty library which directly depends on Windows types is a big problem because it bleeds Windows definitions everywhere thanks to poor abstraction in all relevant layers. --- source/blood/src/network.cpp | 4 +++- source/build/src/sdlayer.cpp | 17 ++++++++++++++- source/duke3d/src/game.cpp | 36 ++---------------------------- source/duke3d/src/network.cpp | 34 +++++++++++++++++++++++++++++ source/duke3d/src/network.h | 6 +++++ source/rr/src/game.cpp | 41 ++++------------------------------- source/rr/src/net.cpp | 33 ++++++++++++++++++++++++++++ source/rr/src/net.h | 6 +++++ 8 files changed, 104 insertions(+), 73 deletions(-) diff --git a/source/blood/src/network.cpp b/source/blood/src/network.cpp index 213b45cf9..0cc79072b 100644 --- a/source/blood/src/network.cpp +++ b/source/blood/src/network.cpp @@ -39,6 +39,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "sound.h" #include "view.h" +extern bool gHaveNetworking; + BEGIN_BLD_NS char packet[576]; @@ -972,7 +974,7 @@ void netInitialize(bool bConsole) netResetToSinglePlayer(); return; } - if (enet_initialize() != 0) + if (!gHaveNetworking) { initprintf("An error occurred while initializing ENet.\n"); netResetToSinglePlayer(); diff --git a/source/build/src/sdlayer.cpp b/source/build/src/sdlayer.cpp index 9bb18b331..9d9536e84 100644 --- a/source/build/src/sdlayer.cpp +++ b/source/build/src/sdlayer.cpp @@ -30,8 +30,13 @@ #include "i_specialpaths.h" #include "inputstate.h" #include "c_cvars.h" +#ifndef NETCODE_DISABLE +#include "enet.h" +#endif #include "../../glbackend/glbackend.h" +bool gHaveNetworking; + #ifdef USE_OPENGL # include "glbuild.h" # include "glsurface.h" @@ -446,7 +451,14 @@ int main(int argc, char *argv[]) try { // Write to the DOCUMENTS directory, not the game directory - + + // Initialize the netcode here, because enet pulls in a lot of headers that pollute the namespace. +#ifndef NETCODE_DISABLE + gHaveNetworking = !enet_initialize(); + if (!gHaveNetworking) + initprintf("An error occurred while initializing ENet.\n"); +#endif + FString logpath = M_GetDocumentsPath() + "demolition.log"; OSD_SetLogFile(logpath); r = CONFIG_Init(); @@ -461,6 +473,9 @@ int main(int argc, char *argv[]) // Just let the rest of the function execute. r = exit.Reason(); } +#ifndef NETCODE_DISABLE + if (gHaveNetworking) enet_deinitialize(); +#endif #if defined(HAVE_GTK2) gtkbuild_exit(r); diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index 9d008b507..cfc30390f 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -4747,22 +4747,8 @@ void G_HandleLocalKeys(void) } G_AddUserQuote(*CombatMacros[ridiculeNum-1]); + Net_SendTaunt(ridiculeNum); -#ifndef NETCODE_DISABLE - tempbuf[0] = PACKET_MESSAGE; - tempbuf[1] = 255; - tempbuf[2] = 0; - Bstrcat(tempbuf+2,*CombatMacros[ridiculeNum-1]); - - ridiculeNum = 2+ strlen(*CombatMacros[ridiculeNum-1]); - - tempbuf[ridiculeNum++] = myconnectindex; - - if (g_netClient) - enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(&tempbuf[0], ridiculeNum, 0)); - else if (g_netServer) - enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(&tempbuf[0], ridiculeNum, 0)); -#endif pus = NUMPAGES; pub = NUMPAGES; @@ -4772,19 +4758,7 @@ void G_HandleLocalKeys(void) // Not SHIFT -- that is, either some ALT or WIN. if (G_StartRTS(ridiculeNum, 1)) { -#ifndef NETCODE_DISABLE - if ((g_netServer || ud.multimode > 1)) - { - tempbuf[0] = PACKET_RTS; - tempbuf[1] = ridiculeNum; - tempbuf[2] = myconnectindex; - - if (g_netClient) - enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(&tempbuf[0], 3, 0)); - else if (g_netServer) - enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(&tempbuf[0], 3, 0)); - } -#endif + Net_SendRTS(ridiculeNum); pus = NUMPAGES; pub = NUMPAGES; @@ -6245,12 +6219,6 @@ EDUKE32_STATIC_ASSERT(sizeof(DukePlayer_t)%4 == 0); int app_main() { -#ifndef NETCODE_DISABLE - if (enet_initialize() != 0) - initprintf("An error occurred while initializing ENet.\n"); - else atexit(enet_deinitialize); -#endif - OSD_SetFunctions(GAME_drawosdchar, GAME_drawosdstr, GAME_drawosdcursor, diff --git a/source/duke3d/src/network.cpp b/source/duke3d/src/network.cpp index 5357759ac..aadfaeba4 100644 --- a/source/duke3d/src/network.cpp +++ b/source/duke3d/src/network.cpp @@ -5223,6 +5223,40 @@ void Net_NotifyNewGame() // The client didn't load the map until G_EnterLevel } + +void Net_SendTaunt(int ridiculeNum) +{ + tempbuf[0] = PACKET_MESSAGE; + tempbuf[1] = 255; + tempbuf[2] = 0; + Bstrcat(tempbuf + 2, *CombatMacros[ridiculeNum - 1]); + + ridiculeNum = 2 + strlen(*CombatMacros[ridiculeNum - 1]); + + tempbuf[ridiculeNum++] = myconnectindex; + + if (g_netClient) + enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(&tempbuf[0], ridiculeNum, 0)); + else if (g_netServer) + enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(&tempbuf[0], ridiculeNum, 0)); + +} + +void Net_SendRTS(int ridiculeNum) +{ + if ((g_netServer || ud.multimode > 1)) + { + tempbuf[0] = PACKET_RTS; + tempbuf[1] = ridiculeNum; + tempbuf[2] = myconnectindex; + + if (g_netClient) + enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(&tempbuf[0], 3, 0)); + else if (g_netServer) + enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(&tempbuf[0], 3, 0)); + } +} + #endif //------------------------------------------------------------------------------------------------- diff --git a/source/duke3d/src/network.h b/source/duke3d/src/network.h index ed45b5545..eef81fa79 100644 --- a/source/duke3d/src/network.h +++ b/source/duke3d/src/network.h @@ -296,6 +296,9 @@ void DumpMapStateHistory(); void Net_WaitForInitialSnapshot(); +void Net_SendTaunt(int ridiculeNum); +void Net_SendRTS(int ridiculeNum); + #else // note: don't include faketimerhandler in this @@ -345,6 +348,9 @@ void Net_WaitForInitialSnapshot(); #define Net_AddWorldToInitialSnapshot(...) ((void)0) #define DumpMapStateHistory(...) ((void)0) +#define Net_SendTaunt(...) ((void)0) +#define Net_SendRTS(...) ((void)0) + diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index d472be5bb..8fb2fdac8 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -6260,23 +6260,8 @@ void G_HandleLocalKeys(void) } G_AddUserQuote(*CombatMacros[ridiculeNum-1]); - -#ifndef NETCODE_DISABLE - tempbuf[0] = PACKET_MESSAGE; - tempbuf[1] = 255; - tempbuf[2] = 0; - Bstrcat(tempbuf+2,*CombatMacros[ridiculeNum-1]); - - ridiculeNum = 2 + strlen(*CombatMacros[ridiculeNum - 1]); - - tempbuf[ridiculeNum++] = myconnectindex; - - if (g_netClient) - enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(tempbuf, ridiculeNum, 0)); - else if (g_netServer) - enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(tempbuf, ridiculeNum, 0)); -#endif - pus = NUMPAGES; + Net_SendTaunt(ridiculeNum); + pus = NUMPAGES; pub = NUMPAGES; return; @@ -6285,20 +6270,8 @@ void G_HandleLocalKeys(void) // Not SHIFT -- that is, either some ALT or WIN. if (G_StartRTS(ridiculeNum, 1)) { -#ifndef NETCODE_DISABLE - if ((g_netServer || ud.multimode > 1)) - { - tempbuf[0] = PACKET_RTS; - tempbuf[1] = ridiculeNum; - tempbuf[2] = myconnectindex; - - if (g_netClient) - enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(tempbuf, 3, 0)); - else if (g_netServer) - enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(tempbuf, 3, 0)); - } -#endif - pus = NUMPAGES; + Net_SendRTS(ridiculeNum); + pus = NUMPAGES; pub = NUMPAGES; return; @@ -7616,12 +7589,6 @@ EDUKE32_STATIC_ASSERT(sizeof(DukePlayer_t)%4 == 0); int app_main() { playing_rr = 1; -#ifndef NETCODE_DISABLE - if (enet_initialize() != 0) - initprintf("An error occurred while initializing ENet.\n"); - else atexit(enet_deinitialize); -#endif - OSD_SetFunctions(GAME_drawosdchar, GAME_drawosdstr, GAME_drawosdcursor, diff --git a/source/rr/src/net.cpp b/source/rr/src/net.cpp index fa5349739..3ccfafad2 100644 --- a/source/rr/src/net.cpp +++ b/source/rr/src/net.cpp @@ -4062,6 +4062,39 @@ void Net_ReceiveMapVoteCancel(uint8_t *pbuf) voting = -1; } +void Net_SendTaunt(int ridiculeNum) +{ + tempbuf[0] = PACKET_MESSAGE; + tempbuf[1] = 255; + tempbuf[2] = 0; + Bstrcat(tempbuf + 2, *CombatMacros[ridiculeNum - 1]); + + ridiculeNum = 2 + strlen(*CombatMacros[ridiculeNum - 1]); + + tempbuf[ridiculeNum++] = myconnectindex; + + if (g_netClient) + enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(&tempbuf[0], ridiculeNum, 0)); + else if (g_netServer) + enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(&tempbuf[0], ridiculeNum, 0)); + +} + +void Net_SendRTS(int ridiculeNum) +{ + if ((g_netServer || ud.multimode > 1)) + { + tempbuf[0] = PACKET_RTS; + tempbuf[1] = ridiculeNum; + tempbuf[2] = myconnectindex; + + if (g_netClient) + enet_peer_send(g_netClientPeer, CHAN_CHAT, enet_packet_create(&tempbuf[0], 3, 0)); + else if (g_netServer) + enet_host_broadcast(g_netServer, CHAN_CHAT, enet_packet_create(&tempbuf[0], 3, 0)); + } +} + #endif // !defined NETCODE_DISABLE END_RR_NS diff --git a/source/rr/src/net.h b/source/rr/src/net.h index d56829c33..f678c4ea0 100644 --- a/source/rr/src/net.h +++ b/source/rr/src/net.h @@ -306,6 +306,9 @@ void Net_Update(void); void Net_PostPacket(ENetPacket *packet); void faketimerhandler(void); +void Net_SendTaunt(int ridiculeNum); +void Net_SendRTS(int ridiculeNum); + #else /* NETCODE_ENABLE is not defined */ @@ -392,6 +395,9 @@ void faketimerhandler(void); #define Net_NotifyNewGame(...) ((void)0) +#define Net_SendTaunt(...) ((void)0) +#define Net_SendRTS(...) ((void)0) + #endif END_RR_NS