From 854e43ea7c7cf3b3eb81e7388958f6546ce28ab6 Mon Sep 17 00:00:00 2001
From: LJ Sonic <lamr@free.fr>
Date: Thu, 25 Mar 2021 22:28:07 +0100
Subject: [PATCH] Kick clients if they take too long to download a Lua file

---
 src/d_netfil.c | 17 +++++++++++++++++
 src/d_netfil.h |  1 +
 2 files changed, 18 insertions(+)

diff --git a/src/d_netfil.c b/src/d_netfil.c
index e15c15bd54..50dc2ba608 100644
--- a/src/d_netfil.c
+++ b/src/d_netfil.c
@@ -570,6 +570,7 @@ static void SV_PrepareSendLuaFileToNextNode(void)
 				I_Error("Failed to send a PT_SENDINGLUAFILE packet\n"); // !!! Todo: Handle failure a bit better lol
 
 			luafiletransfers->nodestatus[i] = LFTNS_ASKED;
+			luafiletransfers->nodetimeouts[i] = I_GetTime() + 30 * TICRATE;
 
 			return;
 		}
@@ -930,6 +931,22 @@ void FileSendTicker(void)
 	filetx_t *f;
 	INT32 packetsent, ram, i, j;
 
+	// If someone is taking too long to download, kick them with a timeout
+	// to prevent blocking the rest of the server...
+	if (luafiletransfers)
+	{
+		for (i = 1; i < MAXNETNODES; i++)
+		{
+			luafiletransfernodestatus_t status = luafiletransfers->nodestatus[i];
+
+			if (status != LFTNS_NONE && status != LFTNS_WAITING && status != LFTNS_SENT
+				&& I_GetTime() > luafiletransfers->nodetimeouts[i])
+			{
+				Net_ConnectionTimeout(i);
+			}
+		}
+	}
+
 	if (!filestosend) // No file to send
 		return;
 
diff --git a/src/d_netfil.h b/src/d_netfil.h
index 2e656e0f60..1581494777 100644
--- a/src/d_netfil.h
+++ b/src/d_netfil.h
@@ -100,6 +100,7 @@ typedef struct luafiletransfer_s
 	INT32 id; // Callback ID
 	boolean ongoing;
 	luafiletransfernodestatus_t nodestatus[MAXNETNODES];
+	tic_t nodetimeouts[MAXNETNODES];
 	struct luafiletransfer_s *next;
 } luafiletransfer_t;
 
-- 
GitLab