Skip to content

Commit

Permalink
fix: Telepathy updated to latest version. Threads are closed properly…
Browse files Browse the repository at this point in the history
… now.
  • Loading branch information
miwarnec committed Jan 27, 2020
1 parent ef7993c commit 4007423
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions Assets/Mirror/Runtime/Transport/Telepathy/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,23 @@ void ReceiveThreadFunction(string ip, int port)
Logger.LogError("Client Recv Exception: " + exception);
}

// sendthread might be waiting on ManualResetEvent,
// so let's make sure to end it if the connection
// closed.
// otherwise the send thread would only end if it's
// actually sending data while the connection is
// closed.
sendThread?.Interrupt();
// close client BEFORE stopping send thread. otherwise the send
// thread might wait until client is closed.
client?.Close();

// SendThread might be waiting on ManualResetEvent.
// call .Set() to interrupt the WaitOne()
sendPending.Set(); // interrupt SendThread WaitOne()

// now wait until the thread finished gracefully
// => it's not blocking on socket because we closed it
// => it's not blocking on ManualResetEvent because we set it
// so it will end.
sendThread?.Join();

// Connect might have failed. thread might have been closed.
// let's reset connecting state no matter what.
_Connecting = false;

// if we got here then we are done. ReceiveLoop cleans up already,
// but we may never get there if connect fails. so let's clean up
// here too.
client?.Close();
}

public void Connect(string ip, int port)
Expand Down Expand Up @@ -163,9 +164,9 @@ public void Disconnect()

// wait until thread finished. this is the only way to guarantee
// that we can call Connect() again immediately after Disconnect
// -> calling .Join would sometimes wait forever, e.g. when
// calling Disconnect while trying to connect to a dead end
receiveThread?.Interrupt();
// => Calling .Close will end the thread
// => Calling .Join is the only safe way to wait until it's done
receiveThread?.Join();

// we interrupted the receive Thread, so we can't guarantee that
// connecting was reset. let's do it manually.
Expand Down

0 comments on commit 4007423

Please sign in to comment.