From 995728313296ee49c2c7520d35833bfa499a703f Mon Sep 17 00:00:00 2001 From: Forest Date: Tue, 18 Dec 2018 12:00:26 -0800 Subject: [PATCH] I like this idea, but why memmory? --- Hazel/Udp/UdpConnection.Reliable.cs | 2 +- Hazel/Udp/UdpConnectionListener.cs | 35 ++++++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 8145ce1..16554da 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -240,7 +240,7 @@ namespace Hazel.Udp Trace.WriteLine("Resend."); }, - resendTimeout > 0 ? resendTimeout : (int)Math.Max(40, Math.Min(AveragePingMs * 4, 750)), + resendTimeout > 0 ? resendTimeout : (int)Math.Max(100, Math.Min(AveragePingMs * 4, 1500)), ackCallback ); } diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 86a32f9..bcefc86 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -17,6 +17,8 @@ namespace Hazel.Udp { public const int BufferSize = ushort.MaxValue / 4; + public int MinConnectionLength = 0; + /// /// The socket listening for connections. /// @@ -26,7 +28,7 @@ namespace Hazel.Udp /// The connections we currently hold /// ConcurrentDictionary allConnections = new ConcurrentDictionary(); - + public int ConnectionCount { get { return this.allConnections.Count; } } /// /// Creates a new UdpConnectionListener for the given , port and . @@ -165,23 +167,30 @@ namespace Hazel.Udp //Begin receiving again StartListeningForData(); - bool aware = true; - bool isHello = message.Buffer[0] == (byte)UdpSendOption.Hello; + bool aware; + bool isHello = message.Buffer[0] == (byte)UdpSendOption.Hello + && message.Length >= MinConnectionLength; //If we're aware of this connection use the one already //If this is a new client then connect with them! - UdpServerConnection connection = this.allConnections.GetOrAdd( - remoteEndPoint, - key => { aware = false; return new UdpServerConnection(this, key, IPMode); }); - - if (!aware) + UdpServerConnection connection; + if (!(aware = this.allConnections.TryGetValue(remoteEndPoint, out connection))) { - //Check for malformed connection attempts - if (!isHello) + lock (this.allConnections) { - Interlocked.Decrement(ref ActiveCallbacks); - message.Recycle(); - return; + if (!(aware = this.allConnections.TryGetValue(remoteEndPoint, out connection))) + { + //Check for malformed connection attempts + if (!isHello) + { + Interlocked.Decrement(ref ActiveCallbacks); + message.Recycle(); + return; + } + + connection = new UdpServerConnection(this, remoteEndPoint, this.IPMode); + this.allConnections.TryAdd(remoteEndPoint, connection); + } } } -- 2.39.5