From e8ca70ec5ebbc4bc4d73ab19d7f64945293a7ec4 Mon Sep 17 00:00:00 2001 From: Forest Date: Mon, 25 Mar 2019 14:41:04 -0700 Subject: [PATCH] Resolve an iOS bug due to setting buffers too large, standardize some socket shutdown, relax some reliable message disconnect defaults/tolerances --- Hazel/Udp/UdpBroadcastListener.cs | 18 +++--------------- Hazel/Udp/UdpBroadcaster.cs | 18 +++--------------- Hazel/Udp/UdpClientConnection.cs | 6 ++++-- Hazel/Udp/UdpConnection.KeepAlive.cs | 2 +- Hazel/Udp/UdpConnection.Reliable.cs | 15 +++++++++++---- Hazel/Udp/UdpConnectionListener.cs | 14 +++++--------- 6 files changed, 27 insertions(+), 46 deletions(-) diff --git a/Hazel/Udp/UdpBroadcastListener.cs b/Hazel/Udp/UdpBroadcastListener.cs index fb8c52a..5ac1a17 100644 --- a/Hazel/Udp/UdpBroadcastListener.cs +++ b/Hazel/Udp/UdpBroadcastListener.cs @@ -139,21 +139,9 @@ namespace Hazel.Udp { if (this.socket != null) { - try - { - this.socket.Shutdown(SocketShutdown.Both); - } - catch { } - try - { - this.socket.Close(); - } - catch { } - try - { - this.socket.Dispose(); - } - catch { } + try { this.socket.Shutdown(SocketShutdown.Both); } catch { } + try { this.socket.Close(); } catch { } + try { this.socket.Dispose(); } catch { } this.socket = null; } } diff --git a/Hazel/Udp/UdpBroadcaster.cs b/Hazel/Udp/UdpBroadcaster.cs index d0a9f84..50c3c68 100644 --- a/Hazel/Udp/UdpBroadcaster.cs +++ b/Hazel/Udp/UdpBroadcaster.cs @@ -52,21 +52,9 @@ namespace Hazel.Udp { if (this.socket != null) { - try - { - this.socket.Shutdown(SocketShutdown.Both); - } - catch { } - try - { - this.socket.Close(); - } - catch { } - try - { - this.socket.Dispose(); - } - catch { } + try { this.socket.Shutdown(SocketShutdown.Both); } catch { } + try { this.socket.Close(); } catch { } + try { this.socket.Dispose(); } catch { } this.socket = null; } } diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 58a16be..7ce5dbc 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -306,8 +306,10 @@ namespace Hazel.Udp if (this.socket != null) { - this.socket.Close(); - this.socket.Dispose(); + try { this.socket.Shutdown(SocketShutdown.Both); } catch { } + try { this.socket.Close(); } catch { } + try { this.socket.Dispose(); } catch { } + this.socket = null; } diff --git a/Hazel/Udp/UdpConnection.KeepAlive.cs b/Hazel/Udp/UdpConnection.KeepAlive.cs index 67abe78..eccfc98 100644 --- a/Hazel/Udp/UdpConnection.KeepAlive.cs +++ b/Hazel/Udp/UdpConnection.KeepAlive.cs @@ -38,7 +38,7 @@ namespace Hazel.Udp ResetKeepAliveTimer(); } } - int keepAliveInterval = 3000; + int keepAliveInterval = 2000; /// /// The timer creating keepalive pulses. diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index acac1bb..af8251e 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -36,7 +36,7 @@ namespace Hazel.Udp /// A compounding multiplier to back off resend timeout. /// Applied to ping before first timeout when ResendTimeout == 0. /// - public volatile float ResendPingMultiplier = 3; + public volatile float ResendPingMultiplier = 2; /// /// Holds the last ID allocated. @@ -72,7 +72,7 @@ namespace Hazel.Udp /// This returns the average ping for a one-way trip as calculated from the reliable packets that have been sent /// and acknowledged by the endpoint. /// - public float AveragePingMs = 500; + public float AveragePingMs = 200; /// /// The maximum times a message should be resent before marking the endpoint as disconnected. @@ -171,7 +171,7 @@ namespace Hazel.Udp return 0; } - this.NextTimeout = (int)Math.Min(this.NextTimeout * connection.ResendPingMultiplier, connection.DisconnectTimeout); + this.NextTimeout = (int)Math.Min(this.NextTimeout * connection.ResendPingMultiplier, 1500); try { connection.WriteBytesToConnection(this.Data, this.Length); @@ -243,7 +243,7 @@ namespace Hazel.Udp this, buffer, sendLength, - ResendTimeout > 0 ? ResendTimeout : (int)Math.Max(300, Math.Min(AveragePingMs * this.ResendPingMultiplier, 2000)), + ResendTimeout > 0 ? ResendTimeout : ClampToInt(AveragePingMs * this.ResendPingMultiplier, 300, 1000), ackCallback); if (!reliableDataPacketsSent.TryAdd(id, packet)) @@ -252,6 +252,13 @@ namespace Hazel.Udp } } + public static int ClampToInt(float value, int min, int max) + { + if (value < min) return min; + if (value > max) return max; + return (int)value; + } + /// /// Sends the bytes reliably and stores the send. /// diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index b38530d..c137776 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -57,8 +57,8 @@ namespace Hazel.Udp this.socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); } - socket.ReceiveBufferSize = 4194304; - socket.SendBufferSize = 1048576; + socket.ReceiveBufferSize = BufferSize; + socket.SendBufferSize = BufferSize; reliablePacketTimer = new Timer(ManageReliablePackets, null, 100, Timeout.Infinite); } @@ -330,13 +330,9 @@ namespace Hazel.Udp if (this.socket != null) { - try - { - this.socket.Shutdown(SocketShutdown.Both); - } - catch { } - this.socket.Close(); - this.socket.Dispose(); + try { this.socket.Shutdown(SocketShutdown.Both); } catch { } + try { this.socket.Close(); } catch { } + try { this.socket.Dispose(); } catch { } this.socket = null; } -- 2.39.5