Skip to content

Commit

Permalink
NetManager now blocks the main thread when waiting for the network th…
Browse files Browse the repository at this point in the history
…read to shut down. (space-wizards#633)
  • Loading branch information
Acruid authored and PJB3005 committed Aug 3, 2018
1 parent 830ff0f commit dcb22bf
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions SS14.Shared/Network/NetManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Lidgren.Network;
using SS14.Shared.Configuration;
using SS14.Shared.Interfaces.Configuration;
Expand Down Expand Up @@ -150,7 +151,19 @@ public void Shutdown(string reason)
foreach (var kvChannel in _channels)
DisconnectChannel(kvChannel.Value, reason);

// request shutdown of the netPeer
_netPeer.Shutdown(reason);

// wait for the network thread to finish its work (like flushing packets and gracefully disconnecting)
// Lidgren does not expose the thread, so we can't join or or anything
// pretty much have to poll every so often and wait for it to finish before continuing
// when the network thread is finished, it will change status from ShutdownRequested to NotRunning
while (_netPeer.Status == NetPeerStatus.ShutdownRequested)
{
// sleep the thread for an arbitrary length so it isn't spinning in the while loop as much
Thread.Sleep(50);
}

_strings.Reset();
}

Expand Down

0 comments on commit dcb22bf

Please sign in to comment.