From: Matthew Endsley Date: Thu, 4 Feb 2021 21:54:15 +0000 (-0800) Subject: Flush outgoing packets before shutting closing socket X-Git-Tag: 1.0.0~20^2^2 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=6992dd4174346a980e59cc0ede44c6acdc3b2e2e;p=rhonda%2Fimpostor.hazel.git Flush outgoing packets before shutting closing socket This ensures we at least attempt to send out Disconnect packets to connected clients when shutting down. Prior to this the send thread would race the Dispose thread causing indeterminate behavior. Sometimes disconnect packets would be sent out, sometimes they wouldn't. --- diff --git a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs index 46a723a..84ac5a4 100644 --- a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs +++ b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs @@ -366,20 +366,25 @@ namespace Hazel.Udp.FewerThreads kvp.Value.Dispose(); } + bool wasActive = this.isActive; + this.isActive = false; + + // Flush outgoing packets + this.sendQueue?.CompleteAdding(); + if (wasActive) + { + this.sendThread.Join(); + } + try { this.socket.Shutdown(SocketShutdown.Both); } catch { } try { this.socket.Close(); } catch { } try { this.socket.Dispose(); } catch { } - bool wasActive = this.isActive; - this.isActive = false; - this.receiveQueue?.CompleteAdding(); - this.sendQueue?.CompleteAdding(); if (wasActive) { this.reliablePacketThread.Join(); - this.sendThread.Join(); this.receiveThread.Join(); this.processThreads.Join(); } @@ -388,7 +393,6 @@ namespace Hazel.Udp.FewerThreads this.receiveQueue = null; this.sendQueue?.Dispose(); this.sendQueue = null; - } public void Dispose()