- 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.
This commit is contained in:
Christoph Oelckers 2019-11-01 07:26:49 +01:00
parent 67acad3984
commit 6c5d9c3507
8 changed files with 104 additions and 73 deletions

View file

@ -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();

View file

@ -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"
@ -447,6 +452,13 @@ int main(int argc, char *argv[])
{
// 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);

View file

@ -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,

View file

@ -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
//-------------------------------------------------------------------------------------------------

View file

@ -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)

View file

@ -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,

View file

@ -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

View file

@ -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