diff --git a/lib/netplay/netplay.cpp b/lib/netplay/netplay.cpp index 3f8adc54f69..d9092d4f50a 100644 --- a/lib/netplay/netplay.cpp +++ b/lib/netplay/netplay.cpp @@ -1709,6 +1709,16 @@ size_t NETgetStatistic(NetStatisticType type, bool sent, bool isTotal) return nStatsLastSec.*statsType.*statisticType - nStatsSecondLastSec.*statsType.*statisticType; } +static std::list> netSendDelayedActions; + +void NETsendProcessDelayedActions() +{ + for (auto action : netSendDelayedActions) + { + action(); + } + netSendDelayedActions.clear(); +} // //////////////////////////////////////////////////////////////////////// // Send a message to a player, option to guarantee message @@ -1774,8 +1784,10 @@ bool NETsend(NETQUEUE queue, NetMessage const *message) debug(LOG_ERROR, "Failed to send message (type: %" PRIu8 ", rawLen: %zu, compressedRawLen: %zu) to %" PRIu8 ": %s", message->type, message->rawLen(), compressedRawLen, player, strSockError(getSockErr())); if (!isTmpQueue) { - NETlogEntry("client disconnect?", SYNC_FLAG, player); - NETplayerClientDisconnect(player); + netSendDelayedActions.push_back([player]() { + NETlogEntry("client disconnect?", SYNC_FLAG, player); + NETplayerClientDisconnect(player); + }); } } } diff --git a/lib/netplay/netplay.h b/lib/netplay/netplay.h index 07dce367019..1353c1287b9 100644 --- a/lib/netplay/netplay.h +++ b/lib/netplay/netplay.h @@ -371,6 +371,7 @@ extern bool netGameserverPortOverride; // = false; (for cli override) // functions available to you. int NETinit(bool bFirstCall); WZ_DECL_NONNULL(2) bool NETsend(NETQUEUE queue, NetMessage const *message); ///< send to player, or broadcast if player == NET_ALL_PLAYERS. +void NETsendProcessDelayedActions(); WZ_DECL_NONNULL(1, 2) bool NETrecvNet(NETQUEUE *queue, uint8_t *type); ///< recv a message from the net queues if possible. WZ_DECL_NONNULL(1, 2) bool NETrecvGame(NETQUEUE *queue, uint8_t *type); ///< recv a message from the game queues which is sceduled to execute by time, if possible. void NETflush(); ///< Flushes any data stuck in compression buffers. diff --git a/lib/netplay/nettypes.cpp b/lib/netplay/nettypes.cpp index 2518be5394d..5b7d7543878 100644 --- a/lib/netplay/nettypes.cpp +++ b/lib/netplay/nettypes.cpp @@ -727,6 +727,9 @@ bool NETend() // We have ended the serialisation, so mark the direction invalid NETsetPacketDir(PACKET_INVALID); + // Process any delayed actions from the NETsend call + NETsendProcessDelayedActions(); + if (queueInfo.queueType == QUEUE_GAME_FORCED) // If true, we must be the host, inserting a GAME_PLAYER_LEFT into the other player's game queue. Since they left, they're not around to complain about us messing with their queue, which would normally cause a desynch. { // Almost duplicate code from NETflushGameQueues() in here.