From: Forest Date: Thu, 25 Jul 2019 23:42:40 +0000 (-0700) Subject: Revert some bad code that caused OnNewConnection to be called multiple times for... X-Git-Tag: 1.0.0~38 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=9625ce21c080faa56423f72c6b7ed8c37d8b8289;p=rhonda%2Fimpostor.hazel.git Revert some bad code that caused OnNewConnection to be called multiple times for some connections --- diff --git a/Hazel/MessageReader.cs b/Hazel/MessageReader.cs index 1914437..b5f5384 100644 --- a/Hazel/MessageReader.cs +++ b/Hazel/MessageReader.cs @@ -23,7 +23,7 @@ namespace Hazel this.readHead = value + Offset; } } - + private int _position; private int readHead; @@ -123,7 +123,6 @@ namespace Hazel return output; } - /// public void Recycle() { ReaderPool.PutObject(this); diff --git a/Hazel/ObjectPool.cs b/Hazel/ObjectPool.cs index 8899699..725353e 100644 --- a/Hazel/ObjectPool.cs +++ b/Hazel/ObjectPool.cs @@ -72,10 +72,5 @@ namespace Hazel throw new Exception("Duplicate add " + typeof(T).Name); } } - - public bool IsObjectInUse(T item) - { - return inuse.ContainsKey(item); - } } } diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 09ee2ac..32a3e06 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -31,7 +31,7 @@ namespace Hazel.Udp /// /// The connections we currently hold /// - ConcurrentDictionary allConnections = new ConcurrentDictionary(); + private ConcurrentDictionary allConnections = new ConcurrentDictionary(); public int ConnectionCount { get { return this.allConnections.Count; } } @@ -194,30 +194,37 @@ namespace Hazel.Udp UdpServerConnection connection; if (!this.allConnections.TryGetValue(remoteEndPoint, out connection)) { - //Check for malformed connection attempts - if (!isHello) + lock (this.allConnections) { - message.Recycle(); - Interlocked.Decrement(ref this.ActiveCallbacks); - return; - } - - if (AcceptConnection != null) - { - if (!AcceptConnection(out var response)) + if (!this.allConnections.TryGetValue(remoteEndPoint, out connection)) { - message.Recycle(); - SendData(response, response.Length, remoteEndPoint); - Interlocked.Decrement(ref this.ActiveCallbacks); - return; + //Check for malformed connection attempts + if (!isHello) + { + message.Recycle(); + Interlocked.Decrement(ref this.ActiveCallbacks); + return; + } + + if (AcceptConnection != null) + { + if (!AcceptConnection(out var response)) + { + message.Recycle(); + SendData(response, response.Length, remoteEndPoint); + Interlocked.Decrement(ref this.ActiveCallbacks); + return; + } + } + + aware = false; + connection = new UdpServerConnection(this, (IPEndPoint)remoteEndPoint, this.IPMode); + if (!this.allConnections.TryAdd(remoteEndPoint, connection)) + { + throw new HazelException("Failed to add a connection. This should never happen."); + } } } - - connection = this.allConnections.GetOrAdd(remoteEndPoint, (ep) => - { - aware = false; - return new UdpServerConnection(this, (IPEndPoint)ep, this.IPMode); - }); } //Inform the connection of the buffer (new connections need to send an ack back to client) @@ -272,14 +279,7 @@ namespace Hazel.Udp length, SocketFlags.None, endPoint, - delegate (IAsyncResult result) - { - try - { - socket.EndSendTo(result); - } - catch { } - }, + SendCallback, null ); } @@ -294,6 +294,15 @@ namespace Hazel.Udp } } + private void SendCallback(IAsyncResult result) + { + try + { + socket.EndSendTo(result); + } + catch { } + } + /// /// Sends data from the listener socket. /// diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index 530920d..5641ca3 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -7,7 +7,7 @@ namespace Hazel.Udp /// Represents a servers's connection to a client that uses the UDP protocol. /// /// - sealed class UdpServerConnection : UdpConnection + internal sealed class UdpServerConnection : UdpConnection { /// /// The connection listener that we use the socket of. @@ -17,7 +17,7 @@ namespace Hazel.Udp /// created this connection and is hence the listener this conenction sends and receives via. /// public UdpConnectionListener Listener { get; private set; } - + /// /// Creates a UdpConnection for the virtual connection to the endpoint. ///