From 21c400dc402d8e62111ba29007841277dae50398 Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Sun, 24 Jan 2021 09:24:04 -0800 Subject: [PATCH] Allow subclasses to mark a connection as stale These connections will be disconnected at the next call to DisconnectOldConnections --- .../ThreadLimitedUdpConnectionListener.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs index dcb4268..7e2da60 100644 --- a/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs +++ b/Hazel/FewerThreads/ThreadLimitedUdpConnectionListener.cs @@ -75,6 +75,7 @@ namespace Hazel.Udp.FewerThreads } private ConcurrentDictionary allConnections = new ConcurrentDictionary(); + private ConcurrentStack staleConnections = new ConcurrentStack(); private BlockingCollection receiveQueue; private BlockingCollection sendQueue = new BlockingCollection(); @@ -127,6 +128,14 @@ namespace Hazel.Udp.FewerThreads this.Dispose(false); } + protected void MarkConnectionAsStale(ConnectionId connectionId) + { + if (this.allConnections.ContainsKey(connectionId.Id)) + { + this.staleConnections.Push(connectionId); + } + } + public void DisconnectOldConnections(TimeSpan maxAge, MessageWriter disconnectMessage) { var now = DateTime.UtcNow; @@ -137,6 +146,16 @@ namespace Hazel.Udp.FewerThreads conn.Disconnect("Stale Connection", disconnectMessage); } } + + ConnectionId connectionId; + while (this.staleConnections.TryPop(out connectionId)) + { + ThreadLimitedUdpServerConnection connection; + if (this.allConnections.TryGetValue(connectionId.Id, out connection)) + { + connection.Disconnect("Stale Connection", disconnectMessage); + } + } } private void ManageReliablePackets() -- 2.39.5