From 18bfc3266aa30c81acb6ae91926231ba29126329 Mon Sep 17 00:00:00 2001
From: LJ Sonic <lamr@free.fr>
Date: Mon, 9 Jan 2023 22:14:58 +0100
Subject: [PATCH] Remove reboundstore timeout hack

---
 src/netcode/d_clisrv.c    | 50 ++++++++-------------------------------
 src/netcode/d_clisrv.h    |  1 +
 src/netcode/d_net.c       | 19 +++++----------
 src/netcode/net_command.c | 10 ++++++++
 src/netcode/net_command.h |  1 +
 src/netcode/protocol.h    |  1 -
 6 files changed, 28 insertions(+), 54 deletions(-)

diff --git a/src/netcode/d_clisrv.c b/src/netcode/d_clisrv.c
index 6680c7e42f..81d7e55435 100644
--- a/src/netcode/d_clisrv.c
+++ b/src/netcode/d_clisrv.c
@@ -479,6 +479,15 @@ void CL_RemovePlayer(INT32 playernum, kickreason_t reason)
 		P_CheckRacers();
 }
 
+void CL_HandleTimeout(void)
+{
+	LUA_HookBool(false, HOOK(GameQuit));
+	D_QuitNetGame();
+	CL_Reset();
+	D_StartTitle();
+	M_StartMessage(M_GetText("Server Timeout\n\nPress Esc\n"), NULL, MM_NOTHING);
+}
+
 void CL_Reset(void)
 {
 	if (metalrecording)
@@ -1363,21 +1372,6 @@ static void PT_ServerShutdown(SINT8 node)
 	M_StartMessage(M_GetText("Server has shutdown\n\nPress Esc\n"), NULL, MM_NOTHING);
 }
 
-/** Called when a PT_NODETIMEOUT packet is received
-  *
-  * \param node The packet sender (should be the server)
-  *
-  */
-static void PT_NodeTimeout(SINT8 node)
-{
-	(void)node;
-	LUA_HookBool(false, HOOK(GameQuit));
-	D_QuitNetGame();
-	CL_Reset();
-	D_StartTitle();
-	M_StartMessage(M_GetText("Server Timeout\n\nPress Esc\n"), NULL, MM_NOTHING);
-}
-
 static void PT_Login(SINT8 node, INT32 netconsole)
 {
 	(void)node;
@@ -1415,32 +1409,10 @@ static void PT_Login(SINT8 node, INT32 netconsole)
 static void PT_ClientQuit(SINT8 node, INT32 netconsole)
 {
 	if (client)
-	{
-		if (node == servernode && cl_mode != CL_SEARCHING && netbuffer->packettype == PT_NODETIMEOUT)
-			PT_NodeTimeout(node);
 		return;
-	}
 
-	// nodeingame will be put false in the execution of kick command
-	// this allow to send some packets to the quitting client to have their ack back
 	if (netnodes[node].ingame && netconsole != -1 && playeringame[netconsole])
-	{
-		UINT8 kickmsg;
-
-		if (netbuffer->packettype == PT_NODETIMEOUT)
-			kickmsg = KICK_MSG_TIMEOUT;
-		else
-			kickmsg = KICK_MSG_PLAYER_QUIT;
-		kickmsg |= KICK_MSG_KEEP_BODY;
-
-		SendKick(netconsole, kickmsg);
-
-		if (netnodes[node].player2 != -1 && netnodes[node].player2 >= 0
-			&& playeringame[(UINT8)netnodes[node].player2])
-		{
-			SendKick(netnodes[node].player2, kickmsg);
-		}
-	}
+		SendKicksForNode(node, KICK_MSG_PLAYER_QUIT | KICK_MSG_KEEP_BODY);
 
 	Net_CloseConnection(node);
 	netnodes[node].ingame = false;
@@ -1517,7 +1489,6 @@ static void HandlePacketFromAwayNode(SINT8 node)
 		case PT_FILEACK        : PT_FileAck        (node    ); break;
 		case PT_FILERECEIVED   : PT_FileReceived   (node    ); break;
 		case PT_REQUESTFILE    : PT_RequestFile    (node    ); break;
-		case PT_NODETIMEOUT    : PT_ClientQuit     (node, -1); break;
 		case PT_CLIENTQUIT     : PT_ClientQuit     (node, -1); break;
 		case PT_SERVERTICS     : PT_ServerTics     (node, -1); break;
 		case PT_CLIENTJOIN     : PT_ClientJoin     (node    ); break;
@@ -1566,7 +1537,6 @@ static void HandlePacketFromPlayer(SINT8 node)
 		case PT_TEXTCMD            : PT_TextCmd            (node, netconsole); break;
 		case PT_TEXTCMD2           : PT_TextCmd            (node, netconsole); break;
 		case PT_LOGIN              : PT_Login              (node, netconsole); break;
-		case PT_NODETIMEOUT        : PT_ClientQuit         (node, netconsole); break;
 		case PT_CLIENTQUIT         : PT_ClientQuit         (node, netconsole); break;
 		case PT_CANRECEIVEGAMESTATE: PT_CanReceiveGamestate(node            ); break;
 		case PT_ASKLUAFILE         : PT_AskLuaFile         (node            ); break;
diff --git a/src/netcode/d_clisrv.h b/src/netcode/d_clisrv.h
index 9cb3ef3e81..0abd638ce9 100644
--- a/src/netcode/d_clisrv.h
+++ b/src/netcode/d_clisrv.h
@@ -94,6 +94,7 @@ void CL_RemoveSplitscreenPlayer(void);
 void CL_Reset(void);
 void CL_ClearPlayer(INT32 playernum);
 void CL_RemovePlayer(INT32 playernum, kickreason_t reason);
+void CL_HandleTimeout(void);
 // Is there a game running
 boolean Playing(void);
 
diff --git a/src/netcode/d_net.c b/src/netcode/d_net.c
index 9723422824..3c16392d01 100644
--- a/src/netcode/d_net.c
+++ b/src/netcode/d_net.c
@@ -27,6 +27,7 @@
 #include "d_netfil.h"
 #include "d_clisrv.h"
 #include "tic_command.h"
+#include "net_command.h"
 #include "../z_zone.h"
 #include "i_tcp.h"
 #include "../d_main.h" // srb2home
@@ -461,14 +462,10 @@ void Net_ConnectionTimeout(INT32 node)
 		return;
 	nodes[node].flags |= NF_TIMEOUT;
 
-	// Send a very special packet to self (hack the reboundstore queue)
-	// Main code will handle it
-	reboundstore[rebound_head].packettype = PT_NODETIMEOUT;
-	reboundstore[rebound_head].ack = 0;
-	reboundstore[rebound_head].ackreturn = 0;
-	reboundstore[rebound_head].u.textcmd[0] = (UINT8)node;
-	reboundsize[rebound_head] = (INT16)(BASEPACKETSIZE + 1);
-	rebound_head = (rebound_head+1) % MAXREBOUND;
+	if (server)
+		SendKicksForNode(node, KICK_MSG_TIMEOUT | KICK_MSG_KEEP_BODY);
+	else
+		CL_HandleTimeout();
 
 	// Do not redo it quickly (if we do not close connection it is
 	// for a good reason!)
@@ -782,7 +779,6 @@ static const char *packettypename[NUMPACKETTYPE] =
 	"TEXTCMD",
 	"TEXTCMD2",
 	"CLIENTJOIN",
-	"NODETIMEOUT",
 	"LOGIN",
 	"TELLFILESNEEDED",
 	"MOREFILESNEEDED",
@@ -1065,10 +1061,7 @@ boolean HGetPacket(void)
 	{
 		M_Memcpy(netbuffer, &reboundstore[rebound_tail], reboundsize[rebound_tail]);
 		doomcom->datalength = reboundsize[rebound_tail];
-		if (netbuffer->packettype == PT_NODETIMEOUT)
-			doomcom->remotenode = netbuffer->u.textcmd[0];
-		else
-			doomcom->remotenode = 0;
+		doomcom->remotenode = 0;
 
 		rebound_tail = (rebound_tail+1) % MAXREBOUND;
 #ifdef DEBUGFILE
diff --git a/src/netcode/net_command.c b/src/netcode/net_command.c
index 95a49a9592..13477d42d5 100644
--- a/src/netcode/net_command.c
+++ b/src/netcode/net_command.c
@@ -315,3 +315,13 @@ void SendKick(UINT8 playernum, UINT8 msg)
 	buf[1] = msg;
 	SendNetXCmd(XD_KICK, &buf, 2);
 }
+
+void SendKicksForNode(SINT8 node, UINT8 msg)
+{
+	if (!netnodes[node].ingame)
+		return;
+
+	for (INT32 playernum = netnodes[node].player; playernum != -1; playernum = netnodes[node].player2)
+		if (playernum != -1 && playeringame[playernum])
+			SendKick(playernum, msg);
+}
diff --git a/src/netcode/net_command.h b/src/netcode/net_command.h
index 1fc5035c81..f1b4b2f3f8 100644
--- a/src/netcode/net_command.h
+++ b/src/netcode/net_command.h
@@ -58,5 +58,6 @@ size_t TotalTextCmdPerTic(tic_t tic);
 
 void PT_TextCmd(SINT8 node, INT32 netconsole);
 void SendKick(UINT8 playernum, UINT8 msg);
+void SendKicksForNode(SINT8 node, UINT8 msg);
 
 #endif
diff --git a/src/netcode/protocol.h b/src/netcode/protocol.h
index cbeb37f36c..374cc5bffd 100644
--- a/src/netcode/protocol.h
+++ b/src/netcode/protocol.h
@@ -85,7 +85,6 @@ typedef enum
 	PT_TEXTCMD,       // Extra text commands from the client.
 	PT_TEXTCMD2,      // Splitscreen text commands.
 	PT_CLIENTJOIN,    // Client wants to join; used in start game.
-	PT_NODETIMEOUT,   // Packet sent to self if the connection times out.
 
 	PT_LOGIN,         // Login attempt from the client.
 
-- 
GitLab