From d5bca1e9147e66e92614a821ed8a289c396b74ac Mon Sep 17 00:00:00 2001 From: Forest Date: Fri, 17 Jan 2020 15:04:20 -0800 Subject: [PATCH] Remove unused timeout param from ConnectAsync --- Hazel/Connection.cs | 3 +-- Hazel/ConnectionListener.cs | 18 ++---------------- Hazel/FewerThreads/UdpConnectionListener2.cs | 1 - Hazel/FewerThreads/UdpServerConnection2.cs | 2 +- Hazel/Udp/UdpClientConnection.cs | 4 ++-- Hazel/Udp/UdpConnection.KeepAlive.cs | 2 ++ Hazel/Udp/UdpConnectionListener.cs | 13 ++++++++----- Hazel/Udp/UdpServerConnection.cs | 2 +- Hazel/Udp/UnityUdpClientConnection.cs | 5 +++-- 9 files changed, 20 insertions(+), 30 deletions(-) diff --git a/Hazel/Connection.cs b/Hazel/Connection.cs index b5bfe33..d1194cd 100644 --- a/Hazel/Connection.cs +++ b/Hazel/Connection.cs @@ -169,8 +169,7 @@ namespace Hazel /// This method does not block. /// /// The bytes of data to send in the handshake. - /// The number of milliseconds to wait before giving up on the connect attempt. - public abstract void ConnectAsync(byte[] bytes = null, int timeout = 5000); + public abstract void ConnectAsync(byte[] bytes = null); /// /// Invokes the DataReceived event. diff --git a/Hazel/ConnectionListener.cs b/Hazel/ConnectionListener.cs index b4b852f..3facfe2 100644 --- a/Hazel/ConnectionListener.cs +++ b/Hazel/ConnectionListener.cs @@ -29,10 +29,8 @@ namespace Hazel /// client. /// /// - /// Hazel doesn't store connections so it is your responsibility to keep track of the connections to your - /// server. Note that as implements if you are not storing - /// a connection then as a bare minimum you should call here in order to - /// release the connection correctly. + /// Hazel may or may not store connections so it is your responsibility to keep track and properly Dispose of + /// connections to your server. /// /// /// @@ -81,18 +79,6 @@ namespace Hazel } } - /// - /// Closes the connection listener safely. - /// - /// - /// Internally this simply calls Dispose therefore trying to reuse the ConnectionListener after calling Close will - /// cause ObjectDisposedExceptions. - /// - public virtual void Close() - { - Dispose(); - } - /// /// Call to dispose of the connection listener. /// diff --git a/Hazel/FewerThreads/UdpConnectionListener2.cs b/Hazel/FewerThreads/UdpConnectionListener2.cs index 52d65da..d4e8876 100644 --- a/Hazel/FewerThreads/UdpConnectionListener2.cs +++ b/Hazel/FewerThreads/UdpConnectionListener2.cs @@ -289,7 +289,6 @@ namespace Hazel.Udp.FewerThreads return this.allConnections.TryRemove(endPoint, out var conn); } - /// protected virtual void Dispose(bool disposing) { foreach (var kvp in this.allConnections) diff --git a/Hazel/FewerThreads/UdpServerConnection2.cs b/Hazel/FewerThreads/UdpServerConnection2.cs index fd5ca4f..c5e6795 100644 --- a/Hazel/FewerThreads/UdpServerConnection2.cs +++ b/Hazel/FewerThreads/UdpServerConnection2.cs @@ -57,7 +57,7 @@ namespace Hazel.Udp.FewerThreads /// /// This will always throw a HazelException. /// - public override void ConnectAsync(byte[] bytes = null, int timeout = 5000) + public override void ConnectAsync(byte[] bytes = null) { throw new InvalidOperationException("Cannot manually connect a UdpServerConnection, did you mean to use UdpClientConnection?"); } diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index de40a39..4500fe5 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -123,7 +123,7 @@ namespace Hazel.Udp /// public override void Connect(byte[] bytes = null, int timeout = 5000) { - this.ConnectAsync(bytes, timeout); + this.ConnectAsync(bytes); //Wait till hello packet is acknowledged and the state is set to Connected bool timedOut = !WaitOnConnect(timeout); @@ -137,7 +137,7 @@ namespace Hazel.Udp } /// - public override void ConnectAsync(byte[] bytes = null, int timeout = 5000) + public override void ConnectAsync(byte[] bytes = null) { this.State = ConnectionState.Connecting; diff --git a/Hazel/Udp/UdpConnection.KeepAlive.cs b/Hazel/Udp/UdpConnection.KeepAlive.cs index 69ceac2..8d55f05 100644 --- a/Hazel/Udp/UdpConnection.KeepAlive.cs +++ b/Hazel/Udp/UdpConnection.KeepAlive.cs @@ -76,6 +76,8 @@ namespace Hazel.Udp keepAliveTimer = new Timer( (o) => { + if (this.State != ConnectionState.Connected) return; + if (this.pingsSinceAck >= this.MissingPingsUntilDisconnect) { this.DisposeKeepAliveTimer(); diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 03bc5df..5570ae6 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -213,10 +213,9 @@ namespace Hazel.Udp } } - //Inform the connection of the buffer (new connections need to send an ack back to client) - connection.HandleReceive(message, bytesReceived); - - //If it's a new connection invoke the NewConnection event. + // If it's a new connection invoke the NewConnection event. + // This needs to happen before handling the message because in localhost scenarios, the ACK and + // subsequent messages can happen before the NewConnection event sets up OnDataRecieved handlers if (!aware) { // Skip header and hello byte; @@ -225,7 +224,11 @@ namespace Hazel.Udp message.Position = 0; InvokeNewConnection(message, connection); } - else if (isHello) + + //Inform the connection of the buffer (new connections need to send an ack back to client) + connection.HandleReceive(message, bytesReceived); + + if (aware && isHello) { message.Recycle(); } diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index 5641ca3..32093c1 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -55,7 +55,7 @@ namespace Hazel.Udp /// /// This will always throw a HazelException. /// - public override void ConnectAsync(byte[] bytes = null, int timeout = 5000) + public override void ConnectAsync(byte[] bytes = null) { throw new InvalidOperationException("Cannot manually connect a UdpServerConnection, did you mean to use UdpClientConnection?"); } diff --git a/Hazel/Udp/UnityUdpClientConnection.cs b/Hazel/Udp/UnityUdpClientConnection.cs index ff92ea5..4f4640f 100644 --- a/Hazel/Udp/UnityUdpClientConnection.cs +++ b/Hazel/Udp/UnityUdpClientConnection.cs @@ -83,7 +83,7 @@ namespace Hazel.Udp } /// - public override void ConnectAsync(byte[] bytes = null, int timeout = 5000) + public override void ConnectAsync(byte[] bytes = null) { this.State = ConnectionState.Connecting; @@ -122,8 +122,9 @@ namespace Hazel.Udp SendHello(bytes, () => { this.State = ConnectionState.Connected; - this.InitializeKeepAliveTimer(); }); + + this.InitializeKeepAliveTimer(); } /// -- 2.39.5