From 9291416a90749b22b8c971a89a5ea39e3fe0955c Mon Sep 17 00:00:00 2001
From: LJ Sonic <lamr@free.fr>
Date: Sat, 14 Jan 2023 19:01:36 +0100
Subject: [PATCH] Split SV_SendTics into functions

---
 src/netcode/net_command.c | 21 +++++++++
 src/netcode/net_command.h |  1 +
 src/netcode/tic_command.c | 95 ++++++++++++++++++---------------------
 3 files changed, 66 insertions(+), 51 deletions(-)

diff --git a/src/netcode/net_command.c b/src/netcode/net_command.c
index 98c578b797..11b351af73 100644
--- a/src/netcode/net_command.c
+++ b/src/netcode/net_command.c
@@ -304,6 +304,27 @@ void PT_TextCmd(SINT8 node, INT32 netconsole)
 	}
 }
 
+void SV_WriteNetCommandsForTic(tic_t tic, UINT8 **buf)
+{
+	UINT8 *numcmds;
+
+	numcmds = (*buf)++;
+	*numcmds = 0;
+	for (INT32 i = 0; i < MAXPLAYERS; i++)
+	{
+		UINT8 *cmd = D_GetExistingTextcmd(tic, i);
+		INT32 size = cmd ? cmd[0] : 0;
+
+		if ((!i || playeringame[i]) && size)
+		{
+			(*numcmds)++;
+			WRITEUINT8(*buf, i);
+			M_Memcpy(*buf, cmd, size + 1);
+			*buf += size + 1;
+		}
+	}
+}
+
 void CL_CopyNetCommandsFromServerPacket(tic_t tic)
 {
 	servertics_pak *packet = &netbuffer->u.serverpak;
diff --git a/src/netcode/net_command.h b/src/netcode/net_command.h
index 0e8339ef7c..cc26aeb0ef 100644
--- a/src/netcode/net_command.h
+++ b/src/netcode/net_command.h
@@ -57,6 +57,7 @@ void ExtraDataTicker(void);
 size_t TotalTextCmdPerTic(tic_t tic);
 
 void PT_TextCmd(SINT8 node, INT32 netconsole);
+void SV_WriteNetCommandsForTic(tic_t tic, UINT8 **buf);
 void CL_CopyNetCommandsFromServerPacket(tic_t tic);
 void CL_SendNetCommands(void);
 void SendKick(UINT8 playernum, UINT8 msg);
diff --git a/src/netcode/tic_command.c b/src/netcode/tic_command.c
index 6b237ed614..bf8c5d21d4 100644
--- a/src/netcode/tic_command.c
+++ b/src/netcode/tic_command.c
@@ -298,16 +298,56 @@ void CL_SendClientCmd(void)
 		CL_SendNetCommands();
 }
 
+// PT_SERVERTICS packets can grow too large for a single UDP packet,
+// So this checks how many tics worth of data can be sent in one packet.
+// The rest can be sent later, usually the next tic.
+static tic_t SV_CalculateNumTicsForPacket(SINT8 nodenum, tic_t firsttic, tic_t lasttic)
+{
+	size_t size = BASESERVERTICSSIZE;
+	tic_t tic;
+
+	for (tic = firsttic; tic < lasttic; tic++)
+	{
+		size += sizeof (ticcmd_t) * doomcom->numslots;
+		size += TotalTextCmdPerTic(tic);
+
+		if (size > software_MAXPACKETLENGTH)
+		{
+			DEBFILE(va("packet too large (%s) at tic %d (should be from %d to %d)\n",
+				sizeu1(size), tic, firsttic, lasttic));
+			lasttic = tic;
+
+			// too bad: too much player have send extradata and there is too
+			//          much data in one tic.
+			// To avoid it put the data on the next tic. (see getpacket
+			// textcmd case) but when numplayer changes the computation can be different
+			if (lasttic == firsttic)
+			{
+				if (size > MAXPACKETLENGTH)
+					I_Error("Too many players: can't send %s data for %d players to node %d\n"
+							"Well sorry nobody is perfect....\n",
+							sizeu1(size), doomcom->numslots, nodenum);
+				else
+				{
+					lasttic++; // send it anyway!
+					DEBFILE("sending it anyway\n");
+				}
+			}
+			break;
+		}
+	}
+
+	return lasttic - firsttic;
+}
+
 // send the server packet
 // send tic from firstticstosend to maketic-1
 void SV_SendTics(void)
 {
 	tic_t realfirsttic, lasttictosend, i;
 	UINT32 n;
-	INT32 j;
 	size_t packsize;
 	UINT8 *bufpos;
-	UINT8 *ntextcmd;
 
 	// send to all client but not to me
 	// for each node create a packet with x tics and send it
@@ -336,38 +376,7 @@ void SV_SendTics(void)
 			if (realfirsttic < firstticstosend)
 				realfirsttic = firstticstosend;
 
-			// compute the length of the packet and cut it if too large
-			packsize = BASESERVERTICSSIZE;
-			for (i = realfirsttic; i < lasttictosend; i++)
-			{
-				packsize += sizeof (ticcmd_t) * doomcom->numslots;
-				packsize += TotalTextCmdPerTic(i);
-
-				if (packsize > software_MAXPACKETLENGTH)
-				{
-					DEBFILE(va("packet too large (%s) at tic %d (should be from %d to %d)\n",
-						sizeu1(packsize), i, realfirsttic, lasttictosend));
-					lasttictosend = i;
-
-					// too bad: too much player have send extradata and there is too
-					//          much data in one tic.
-					// To avoid it put the data on the next tic. (see getpacket
-					// textcmd case) but when numplayer changes the computation can be different
-					if (lasttictosend == realfirsttic)
-					{
-						if (packsize > MAXPACKETLENGTH)
-							I_Error("Too many players: can't send %s data for %d players to node %d\n"
-							        "Well sorry nobody is perfect....\n",
-							        sizeu1(packsize), doomcom->numslots, n);
-						else
-						{
-							lasttictosend++; // send it anyway!
-							DEBFILE("sending it anyway\n");
-						}
-					}
-					break;
-				}
-			}
+			lasttictosend = realfirsttic + SV_CalculateNumTicsForPacket(n, realfirsttic, lasttictosend);
 
 			// Send the tics
 			netbuffer->packettype = PT_SERVERTICS;
@@ -383,23 +392,7 @@ void SV_SendTics(void)
 
 			// add textcmds
 			for (i = realfirsttic; i < lasttictosend; i++)
-			{
-				ntextcmd = bufpos++;
-				*ntextcmd = 0;
-				for (j = 0; j < MAXPLAYERS; j++)
-				{
-					UINT8 *textcmd = D_GetExistingTextcmd(i, j);
-					INT32 size = textcmd ? textcmd[0] : 0;
-
-					if ((!j || playeringame[j]) && size)
-					{
-						(*ntextcmd)++;
-						WRITEUINT8(bufpos, j);
-						M_Memcpy(bufpos, textcmd, size + 1);
-						bufpos += size + 1;
-					}
-				}
-			}
+				SV_WriteNetCommandsForTic();
 			packsize = bufpos - (UINT8 *)&(netbuffer->u);
 
 			HSendPacket(n, false, 0, packsize);
-- 
GitLab