Skip to content

Commit

Permalink
- refactored the exit calls out of the networking code
Browse files Browse the repository at this point in the history
These ones were particularly bad examples of misusing the exit handlers by temporarily installing one themselves and then calling exit to clean stuff up.

Now they just return an error code to D_DoomMain to perform a regular exit.
  • Loading branch information
coelckers committed Oct 6, 2019
1 parent 96006eb commit 0a611e1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
14 changes: 8 additions & 6 deletions src/d_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const FIWADInfo *D_FindIWAD(TArray<FString> &wadfiles, const char *iwad, const c

// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------

void D_CheckNetGame ();
bool D_CheckNetGame ();
void D_ProcessEvents ();
void G_BuildTiccmd (ticcmd_t* cmd);
void D_DoAdvanceDemo ();
Expand Down Expand Up @@ -2353,7 +2353,7 @@ void I_Quit()
//
//==========================================================================

static void D_DoomMain_Internal (void)
static int D_DoomMain_Internal (void)
{
int p;
const char *v;
Expand Down Expand Up @@ -2710,7 +2710,10 @@ static void D_DoomMain_Internal (void)
{
if (!batchrun) Printf ("D_CheckNetGame: Checking network game status.\n");
StartScreen->LoadingStatus ("Checking network game status.", 0x3f);
D_CheckNetGame ();
if (!D_CheckNetGame ())
{
return 0;
}
}

// [SP] Force vanilla transparency auto-detection to re-detect our game lumps now
Expand Down Expand Up @@ -2746,7 +2749,7 @@ static void D_DoomMain_Internal (void)

if (Args->CheckParm("-norun") || batchrun)
{
return;
return 1337; // special exit
}

V_Init2();
Expand Down Expand Up @@ -2842,8 +2845,7 @@ int D_DoomMain()
int ret = 0;
try
{
D_DoomMain_Internal();
ret = 1337;
ret = D_DoomMain_Internal();
}
catch (std::exception &error)
{
Expand Down
11 changes: 9 additions & 2 deletions src/d_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ static void SendSetup (uint32_t playersdetected[MAXNETNODES], uint8_t gotsetup[M
// Works out player numbers among the net participants
//

void D_CheckNetGame (void)
bool D_CheckNetGame (void)
{
const char *v;
int i;
Expand All @@ -1674,8 +1674,13 @@ void D_CheckNetGame (void)
"\nIf the game is running well below expected speeds, use netmode 0 (P2P) instead.\n");
}

int result = I_InitNetwork ();
// I_InitNetwork sets doomcom and netgame
if (I_InitNetwork ())
if (result == -1)
{
return false;
}
else if (result > 0)
{
// For now, stop auto selecting PacketServer, as it's more likely to cause confusion.
//NetMode = NET_PacketServer;
Expand Down Expand Up @@ -1739,6 +1744,8 @@ void D_CheckNetGame (void)

if (!batchrun) Printf ("player %i of %i (%i nodes)\n",
consoleplayer+1, doomcom.numplayers, doomcom.numnodes);

return true;
}


Expand Down
36 changes: 17 additions & 19 deletions src/i_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ bool Host_SendAllHere (void *userdata)
return gotack[MAXNETNODES] == doomcom.numnodes - 1;
}

void HostGame (int i)
bool HostGame (int i)
{
PreGamePacket packet;
int numplayers;
Expand All @@ -667,7 +667,6 @@ void HostGame (int i)
if (numplayers > MAXNETNODES)
{
I_FatalError("You cannot host a game with %d players. The limit is currently %d.", numplayers, MAXNETNODES);
return;
}

if (numplayers == 1)
Expand All @@ -677,7 +676,7 @@ void HostGame (int i)
doomcom.id = DOOMCOM_ID;
doomcom.numplayers = doomcom.numnodes = 1;
doomcom.consoleplayer = 0;
return;
return true;
}

StartNetwork (false);
Expand All @@ -689,14 +688,13 @@ void HostGame (int i)

doomcom.numnodes = 1;

atterm(SendAbort);

StartScreen->NetInit ("Waiting for players", numplayers);

// Wait for numplayers-1 different connections
if (!StartScreen->NetLoop (Host_CheckForConnects, (void *)(intptr_t)numplayers))
{
exit(0);
SendAbort();
return false;
}

// Now inform everyone of all machines involved in the game
Expand All @@ -706,11 +704,10 @@ void HostGame (int i)

if (!StartScreen->NetLoop (Host_SendAllHere, (void *)gotack))
{
exit(0);
SendAbort();
return false;
}

popterm ();

// Now go
StartScreen->NetMessage ("Go");
packet.Fake = PRE_FAKE;
Expand All @@ -735,6 +732,7 @@ void HostGame (int i)
{
sendplayer[i] = i;
}
return true;
}

// This routine is used by a guest to notify the host of its presence.
Expand Down Expand Up @@ -844,7 +842,7 @@ bool Guest_WaitForOthers (void *userdata)
return false;
}

void JoinGame (int i)
bool JoinGame (int i)
{
if ((i == Args->NumArgs() - 1) ||
(Args->GetArg(i+1)[0] == '-') ||
Expand All @@ -858,28 +856,28 @@ void JoinGame (int i)
sendplayer[1] = 0;
doomcom.numnodes = 2;

atterm(SendAbort);

// Let host know we are here
StartScreen->NetInit ("Contacting host", 0);

if (!StartScreen->NetLoop (Guest_ContactHost, NULL))
{
exit(0);
SendAbort();
return false;
}

// Wait for everyone else to connect
if (!StartScreen->NetLoop (Guest_WaitForOthers, 0))
{
exit(0);
SendAbort();
return false;
}

popterm ();


StartScreen->NetMessage ("Total players: %d", doomcom.numnodes);

doomcom.id = DOOMCOM_ID;
doomcom.numplayers = doomcom.numnodes;
return true;
}

static int PrivateNetOf(in_addr in)
Expand Down Expand Up @@ -939,7 +937,7 @@ static bool NodesOnSameNetwork()
//
// Returns true if packet server mode might be a good idea.
//
bool I_InitNetwork (void)
int I_InitNetwork (void)
{
int i;
const char *v;
Expand Down Expand Up @@ -969,11 +967,11 @@ bool I_InitNetwork (void)
// player x: -join <player 1's address>
if ( (i = Args->CheckParm ("-host")) )
{
HostGame (i);
if (!HostGame (i)) return -1;
}
else if ( (i = Args->CheckParm ("-join")) )
{
JoinGame (i);
if (!JoinGame (i)) return -1;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/i_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define __I_NET_H__

// Called by D_DoomMain.
bool I_InitNetwork (void);
int I_InitNetwork (void);
void I_NetCmd (void);

#endif

0 comments on commit 0a611e1

Please sign in to comment.