Skip to content

Commit

Permalink
netplay: Handle NETplayerClientDisconnect on NETsend in a delayed action
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Oct 14, 2023
1 parent f3e7a65 commit 67bfe93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/netplay/netplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,16 @@ size_t NETgetStatistic(NetStatisticType type, bool sent, bool isTotal)
return nStatsLastSec.*statsType.*statisticType - nStatsSecondLastSec.*statsType.*statisticType;
}

static std::list<std::function<void()>> netSendDelayedActions;

void NETsendProcessDelayedActions()
{
for (auto action : netSendDelayedActions)
{
action();
}
netSendDelayedActions.clear();
}

// ////////////////////////////////////////////////////////////////////////
// Send a message to a player, option to guarantee message
Expand Down Expand Up @@ -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);
});
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/netplay/netplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/netplay/nettypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 67bfe93

Please sign in to comment.