Skip to content

Commit

Permalink
Give bots a chance to clearly shutdown after a match
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Aug 10, 2024
1 parent 260a96b commit 9fc9b4e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion RLBotCS/Server/FlatBuffersSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ internal class FlatBuffersSession
private bool _renderingIsEnabled;

private int _spawnId;
private bool _sessionForceClosed;
private bool _closed;

private readonly FlatBufferBuilder _messageBuilder = new(1 << 10);

Expand Down Expand Up @@ -284,6 +286,7 @@ await SendPayloadToClientAsync(
break;
case SessionMessage.StopMatch m
when m.Force || (_connectionEstablished && _closeAfterMatch):
_sessionForceClosed = m.Force;
return;
}
}
Expand All @@ -292,6 +295,11 @@ private async Task HandleClientMessages()
{
await foreach (TypedPayload message in _socketSpecReader.ReadAllAsync())
{
// if the session is closed, ignore any incoming messages
// this should allow the client to close cleanly
if (_closed)
continue;

bool keepRunning = await ParseClientMessage(message);
if (keepRunning)
continue;
Expand Down Expand Up @@ -349,7 +357,11 @@ public void Cleanup()
// if an exception was thrown, the client disconnected first
// remove this session from the server
_rlbotServer.TryWrite(new SessionClosed(_clientId));
_client.Close();

// if we're trying to shutdown cleanly,
// let the bot finish sending messages and close the connection itself
if (!_sessionForceClosed)
_client.Close();
}
}
}

0 comments on commit 9fc9b4e

Please sign in to comment.