From c5765c20bc38647b7cf0a76429566023db7f0625 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 16 Dec 2024 20:34:28 +0100 Subject: [PATCH 1/4] =?UTF-8?q?delay=20NetXCmd=C2=B4s=20instead=20of=20dro?= =?UTF-8?q?pping=20them=20when=20the=20buffer=20is=20full?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit credits and many thanks to Hanicef! https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2568 --- src/d_clisrv.c | 73 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 13 deletions(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 5b307f160..86a5ab2ca 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -187,6 +187,16 @@ typedef struct textcmdtic_s struct textcmdtic_s *next; } textcmdtic_t; +typedef struct textcmdbuf_s textcmdbuf_t; + +struct textcmdbuf_s +{ + textcmdbuf_t *next; + UINT8 cmd[MAXTEXTCMD]; +}; + +static textcmdbuf_t *textcmdbuf[MAXSPLITSCREENPLAYERS]; + ticcmd_t netcmds[TICQUEUE][MAXPLAYERS]; static textcmdtic_t *textcmds[TEXTCMD_HASH_SIZE] = {NULL}; @@ -271,25 +281,53 @@ void RegisterNetXCmd(netxcmd_t id, void (*cmd_f)(UINT8 **p, INT32 playernum)) listnetxcmd[id] = cmd_f; } -void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_t nparam) +static void WriteNetXCmd(UINT8 *cmd, netxcmd_t id, const void *param, size_t nparam) { - if ((localtextcmd[playerid])[0]+2+nparam > MAXTEXTCMD) - { - // for future reference: if (cht_debug) != debug disabled. - CONS_Alert(CONS_ERROR, M_GetText("NetXCmd buffer full, cannot add netcmd %d! (size: %d, needed: %s)\n"), id, localtextcmd[playerid][0], sizeu1(nparam)); - return; - } - - localtextcmd[playerid][0]++; - localtextcmd[playerid][localtextcmd[playerid][0]] = (UINT8)id; - + cmd[0]++; + cmd[cmd[0]] = (UINT8)id; if (param && nparam) { - M_Memcpy(&localtextcmd[playerid][localtextcmd[playerid][0] + 1], param, nparam); - localtextcmd[playerid][0] = (UINT8)(localtextcmd[playerid][0] + (UINT8)nparam); + M_Memcpy(&cmd[cmd[0]+1], param, nparam); + cmd[0] = (UINT8)(cmd[0] + (UINT8)nparam); } } +void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_t nparam) +{ + if (localtextcmd[playerid][0]+2+nparam > MAXTEXTCMD) + { + textcmdbuf_t *buf = textcmdbuf[playerid]; + + // for future reference: if (cv_debug) != debug disabled. + CONS_Alert(CONS_NOTICE, M_GetText("NetXCmd buffer full, delaying netcmd %d... (size: %d, needed: %s)\n"), id, localtextcmd[playerid][0], sizeu1(nparam)); + if (buf == NULL) + { + textcmdbuf[playerid] = Z_Malloc(sizeof(textcmdbuf_t), PU_STATIC, NULL); + textcmdbuf[playerid]->cmd[0] = 0; + textcmdbuf[playerid]->next = NULL; + WriteNetXCmd(textcmdbuf[playerid]->cmd, id, param, nparam); + return; + } + + while (buf->next != NULL) + buf = buf->next; + + if (buf->cmd[0]+2+nparam > MAXTEXTCMD) + { + WriteNetXCmd(buf->cmd, id, param, nparam); + } + else + { + buf->next = Z_Malloc(sizeof(textcmdbuf_t), PU_STATIC, NULL); + buf->next->cmd[0] = 0; + buf->next->next = NULL; + WriteNetXCmd(buf->next->cmd, id, param, nparam); + } + return; + } + WriteNetXCmd(localtextcmd[playerid], id, param, nparam); +} + /*UINT8 GetFreeXCmdSize(UINT8 playerid) { // -1 for the size and another -1 for the ID. @@ -5937,7 +5975,16 @@ static void CL_SendClientCmd(void) M_Memcpy(netbuffer->u.textcmd, localtextcmd[i], localtextcmd[i][0]+1); // All extra data have been sent if (HSendPacket(servernode, true, 0, localtextcmd[i][0]+1)) // Send can fail... + { localtextcmd[i][0] = 0; + if (textcmdbuf[i] != NULL) + { + textcmdbuf_t *buf = textcmdbuf[i]; + M_Memcpy(localtextcmd[i], textcmdbuf[i]->cmd, textcmdbuf[i]->cmd[0]+1); + textcmdbuf[i] = textcmdbuf[i]->next; + Z_Free(buf); + } + } } } } From 13579785968104d7b092da4c0cbeb31fa6a2cb16 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 16 Dec 2024 20:40:36 +0100 Subject: [PATCH 2/4] init textcmdbuf to NULL just in case --- src/d_clisrv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 86a5ab2ca..22139ad02 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -195,7 +195,7 @@ struct textcmdbuf_s UINT8 cmd[MAXTEXTCMD]; }; -static textcmdbuf_t *textcmdbuf[MAXSPLITSCREENPLAYERS]; +static textcmdbuf_t *textcmdbuf[MAXSPLITSCREENPLAYERS] = {NULL}; ticcmd_t netcmds[TICQUEUE][MAXPLAYERS]; static textcmdtic_t *textcmds[TEXTCMD_HASH_SIZE] = {NULL}; From 3174d8fb1c0480abac3bb1c790391d22e81aa81f Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 18 Dec 2024 18:57:02 +0100 Subject: [PATCH 3/4] Fix segfault when sending excessively large netcmds https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2568/diffs?commit_id=457b761d9301b1904c414a8e8ba908f74b9ab959 --- src/d_clisrv.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 22139ad02..50e4a9183 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -298,6 +298,12 @@ void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_ { textcmdbuf_t *buf = textcmdbuf[playerid]; + if (2+nparam > MAXTEXTCMD) + { + CONS_Alert(CONS_ERROR, M_GetText("packet too large to fit NetXCmd, cannot add netcmd %d! (size: %s, max: %d)\n"), id, sizeu1(2+nparam), MAXTEXTCMD); + return; + } + // for future reference: if (cv_debug) != debug disabled. CONS_Alert(CONS_NOTICE, M_GetText("NetXCmd buffer full, delaying netcmd %d... (size: %d, needed: %s)\n"), id, localtextcmd[playerid][0], sizeu1(nparam)); if (buf == NULL) @@ -313,16 +319,16 @@ void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_ buf = buf->next; if (buf->cmd[0]+2+nparam > MAXTEXTCMD) - { - WriteNetXCmd(buf->cmd, id, param, nparam); - } - else { buf->next = Z_Malloc(sizeof(textcmdbuf_t), PU_STATIC, NULL); buf->next->cmd[0] = 0; buf->next->next = NULL; WriteNetXCmd(buf->next->cmd, id, param, nparam); } + else + { + WriteNetXCmd(buf->cmd, id, param, nparam); + } return; } WriteNetXCmd(localtextcmd[playerid], id, param, nparam); From 0290c738e1d2ee2e72beb5ed6a57c20e7a6e41ee Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 22 Dec 2024 18:38:09 +0100 Subject: [PATCH 4/4] fix compile --- src/p_mobj.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/p_mobj.c b/src/p_mobj.c index e68288f2b..03e7e9f99 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -3234,7 +3234,6 @@ void P_DestroyRobots(void) // the below is chasecam only, if you're curious. check out P_CalcPostImg in p_user.c for first person static void P_CalcChasePostImg(player_t *player, camera_t *thiscam) { - boolean itsatwodlevel = false; const boolean flipcam = (player->pflags & PF_FLIPCAM && !(player->pflags & PF_NIGHTSMODE) && player->mo->eflags & MFE_VERTICALFLIP); UINT16 postimgflags = 0; UINT8 i; @@ -3244,21 +3243,6 @@ static void P_CalcChasePostImg(player_t *player, camera_t *thiscam) if (flipcam) postimgflags |= POSTIMG_FLIP; - if (twodlevel) - itsatwodlevel = true; - else - { - for (i = 0; i <= splitscreen; i++) - { - if (thiscam == &camera[i] && players[displayplayers[i]].mo - && (players[displayplayers[i]].mo->flags2 & MF2_TWOD)) - { - itsatwodlevel = true; - break; - } - } - } - if (player->awayviewtics && player->awayviewmobj && !P_MobjWasRemoved(player->awayviewmobj)) // Camera must obviously exist { camera_t dummycam; @@ -3300,12 +3284,30 @@ static void P_CalcChasePostImg(player_t *player, camera_t *thiscam) // Process the mobj-ish required functions of the camera boolean P_CameraThinker(player_t *player, camera_t *thiscam, boolean resetcalled) { + boolean itsatwodlevel = false; + UINT8 i; + // This can happen when joining if (thiscam->subsector == NULL || thiscam->subsector->sector == NULL) return true; P_CalcChasePostImg(player, thiscam); + if (twodlevel) + itsatwodlevel = true; + else + { + for (i = 0; i <= splitscreen; i++) + { + if (thiscam == &camera[i] && players[displayplayers[i]].mo + && (players[displayplayers[i]].mo->flags2 & MF2_TWOD)) + { + itsatwodlevel = true; + break; + } + } + } + if (thiscam->momx || thiscam->momy) { if (!P_TryCameraMove(thiscam->x + thiscam->momx, thiscam->y + thiscam->momy, thiscam)) // Never fails for 2D mode. @@ -3333,7 +3335,7 @@ boolean P_CameraThinker(player_t *player, camera_t *thiscam, boolean resetcalled } if (!itsatwodlevel) - P_CheckCameraPosition(thiscam->x, thiscam->y, thiscam); + P_CheckCameraPosition(thiscam->x, thiscam->y, thiscam); thiscam->subsector = R_PointInSubsectorFast(thiscam->x, thiscam->y); thiscam->floorz = tmfloorz;