From c29130c792ea51771c3d591179b5e55b0bd9f7db Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 7 Feb 2022 23:52:42 +1100 Subject: [PATCH] Disconnect immediately after send fails instead of always waiting 30 seconds Should mostly fix situation where after server is closed, game will should still take up to 30 seconds to actually disconnect, especially for LAN/localhost servers I tested unplugging ethernet and disconnecting from wifi, then reconnecting a couple of seconds later, and in both cases the game was able to survive connection dropout without being disconnected from the server. So hopefully this doesn't cause any issues --- src/Server.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Server.c b/src/Server.c index 8ee65a153..1073106a7 100644 --- a/src/Server.c +++ b/src/Server.c @@ -215,7 +215,7 @@ static cc_uint8 net_readBuffer[4096 * 5]; static cc_uint8 net_writeBuffer[131]; static cc_uint8* net_readCurrent; -static cc_bool net_writeFailed; +static cc_result net_writeFailure; static double lastPacket; static cc_uint8 lastOpcode; @@ -335,6 +335,12 @@ static void MPConnection_SendPosition(Vec3 pos, float yaw, float pitch) { Net_SendPacket(); } +static void MPConnection_Disconnect(void) { + static const cc_string title = String_FromConst("Disconnected!"); + static const cc_string reason = String_FromConst("You've lost connection to the server"); + Game_Disconnect(&title, &reason); +} + static void MPConnection_CheckDisconnection(void) { static const cc_string title = String_FromConst("Disconnected!"); static const cc_string reason = String_FromConst("You've lost connection to the server"); @@ -346,8 +352,8 @@ static void MPConnection_CheckDisconnection(void) { /* poll read returns true when socket is closed */ selectRes = Socket_Poll(net_socket, SOCKET_POLL_READ, &poll_read); - if (net_writeFailed || availRes || selectRes || (pending == 0 && poll_read)) { - Game_Disconnect(&title, &reason); + if (net_writeFailure || availRes || selectRes || (pending == 0 && poll_read)) { + MPConnection_Disconnect(); } } @@ -432,6 +438,11 @@ static void MPConnection_Tick(struct ScheduledTask* task) { } net_readCurrent = net_readBuffer + remaining; + if (net_writeFailure) { + Platform_Log1("Error from send: %i", &net_writeFailure); + MPConnection_Disconnect(); + } + /* Network is ticked 60 times a second. We only send position updates 20 times a second */ if ((ticks % 3) == 0) { TexturePack_CheckPending(); @@ -459,7 +470,9 @@ static void MPConnection_SendData(const cc_uint8* data, cc_uint32 len) { } /* NOTE: Not immediately disconnecting here, as otherwise we sometimes miss out on kick messages */ - if (res || !wrote) { net_writeFailed = true; return; } + if (res) { net_writeFailure = res; return; } + if (!wrote) { net_writeFailure = ERR_INVALID_ARGUMENT; return; } + data += wrote; len -= wrote; } } @@ -519,7 +532,7 @@ static void OnInit(void) { static void OnReset(void) { if (Server.IsSinglePlayer) return; - net_writeFailed = false; + net_writeFailure = 0; OnClose(); }