From 0f6003763f35de66faec31eb65e1b7a81cb34fd2 Mon Sep 17 00:00:00 2001 From: Ivan Shynkarenka Date: Fri, 19 Feb 2021 18:29:43 +0300 Subject: [PATCH] Socket.RemoteEndPoint exception in OnWsDisconnected #116 --- .appveyor.yml | 2 +- source/NetCoreServer/NetCoreServer.csproj | 2 +- source/NetCoreServer/SslClient.cs | 47 ++++++++++++----------- source/NetCoreServer/SslServer.cs | 32 ++++++++------- source/NetCoreServer/SslSession.cs | 46 +++++++++++----------- source/NetCoreServer/TcpClient.cs | 35 +++++++++-------- source/NetCoreServer/TcpServer.cs | 32 ++++++++------- source/NetCoreServer/TcpSession.cs | 38 +++++++++--------- source/NetCoreServer/UdpClient.cs | 26 ++++++------- source/NetCoreServer/UdpServer.cs | 28 +++++++------- 10 files changed, 151 insertions(+), 137 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 2803dc5..853968d 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -1,5 +1,5 @@ # Specify version format -version: "5.0.7.{build}" +version: "5.0.8.{build}" # Image to use image: Visual Studio 2019 diff --git a/source/NetCoreServer/NetCoreServer.csproj b/source/NetCoreServer/NetCoreServer.csproj index 9b0089f..e1358ce 100644 --- a/source/NetCoreServer/NetCoreServer.csproj +++ b/source/NetCoreServer/NetCoreServer.csproj @@ -2,7 +2,7 @@ net5.0 - 5.0.7.0 + 5.0.8.0 Ivan Shynkarenka Copyright (c) 2019-2021 Ivan Shynkarenka https://github.com/chronoxor/NetCoreServer diff --git a/source/NetCoreServer/SslClient.cs b/source/NetCoreServer/SslClient.cs index b9ecfe5..6b9f10f 100644 --- a/source/NetCoreServer/SslClient.cs +++ b/source/NetCoreServer/SslClient.cs @@ -187,14 +187,17 @@ public virtual bool Connect() { // Close the client socket Socket.Close(); + + // Call the client disconnected handler + SendError(ex.SocketErrorCode); + OnDisconnected(); + // Dispose the client socket Socket.Dispose(); + // Dispose event arguments _connectEventArg.Dispose(); - // Call the client disconnected handler - SendError(ex.SocketErrorCode); - OnDisconnected(); return false; } @@ -303,6 +306,25 @@ public virtual bool Disconnect() // Close the client socket Socket.Close(); + // Update the handshaked flag + IsHandshaked = false; + + // Update the connected flag + IsConnected = false; + + // Update sending/receiving flags + _receiving = false; + _sending = false; + + // Clear send/receive buffers + ClearBuffers(); + + // Call the client disconnected handler + OnDisconnected(); + + // Reset the disconnecting flag + _disconnecting = false; + // Dispose the client socket Socket.Dispose(); @@ -314,25 +336,6 @@ public virtual bool Disconnect() } catch (ObjectDisposedException) {} - // Update the handshaked flag - IsHandshaked = false; - - // Update the connected flag - IsConnected = false; - - // Update sending/receiving flags - _receiving = false; - _sending = false; - - // Clear send/receive buffers - ClearBuffers(); - - // Call the client disconnected handler - OnDisconnected(); - - // Reset the disconnecting flag - _disconnecting = false; - return true; } diff --git a/source/NetCoreServer/SslServer.cs b/source/NetCoreServer/SslServer.cs index 023a79f..6583eec 100644 --- a/source/NetCoreServer/SslServer.cs +++ b/source/NetCoreServer/SslServer.cs @@ -225,26 +225,30 @@ public virtual bool Stop() // Reset acceptor event arg _acceptorEventArg.Completed -= OnAsyncCompleted; - // Close the acceptor socket - _acceptorSocket.Close(); + try + { + // Close the acceptor socket + _acceptorSocket.Close(); - // Dispose the acceptor socket - _acceptorSocket.Dispose(); + // Disconnect all sessions + DisconnectAll(); - // Dispose event arguments - _acceptorEventArg.Dispose(); + // Update the started flag + IsStarted = false; - // Update the acceptor socket disposed flag - IsSocketDisposed = true; + // Call the server stopped handler + OnStopped(); - // Disconnect all sessions - DisconnectAll(); + // Dispose the acceptor socket + _acceptorSocket.Dispose(); - // Update the started flag - IsStarted = false; + // Dispose event arguments + _acceptorEventArg.Dispose(); - // Call the server stopped handler - OnStopped(); + // Update the acceptor socket disposed flag + IsSocketDisposed = true; + } + catch (ObjectDisposedException) {} return true; } diff --git a/source/NetCoreServer/SslSession.cs b/source/NetCoreServer/SslSession.cs index 358b7b0..64c4a9f 100644 --- a/source/NetCoreServer/SslSession.cs +++ b/source/NetCoreServer/SslSession.cs @@ -177,38 +177,38 @@ public virtual bool Disconnect() // Close the session socket Socket.Close(); - // Dispose the session socket - Socket.Dispose(); + // Update the handshaked flag + IsHandshaked = false; - // Update the session socket disposed flag - IsSocketDisposed = true; - } - catch (ObjectDisposedException) {} + // Update the connected flag + IsConnected = false; - // Update the handshaked flag - IsHandshaked = false; + // Update sending/receiving flags + _receiving = false; + _sending = false; - // Update the connected flag - IsConnected = false; + // Clear send/receive buffers + ClearBuffers(); - // Update sending/receiving flags - _receiving = false; - _sending = false; + // Call the session disconnected handler + OnDisconnected(); - // Clear send/receive buffers - ClearBuffers(); + // Call the session disconnected handler in the server + Server.OnDisconnectedInternal(this); - // Call the session disconnected handler - OnDisconnected(); + // Unregister session + Server.UnregisterSession(Id); - // Call the session disconnected handler in the server - Server.OnDisconnectedInternal(this); + // Reset the disconnecting flag + _disconnecting = false; - // Unregister session - Server.UnregisterSession(Id); + // Dispose the session socket + Socket.Dispose(); - // Reset the disconnecting flag - _disconnecting = false; + // Update the session socket disposed flag + IsSocketDisposed = true; + } + catch (ObjectDisposedException) {} return true; } diff --git a/source/NetCoreServer/TcpClient.cs b/source/NetCoreServer/TcpClient.cs index e703701..84e5c13 100644 --- a/source/NetCoreServer/TcpClient.cs +++ b/source/NetCoreServer/TcpClient.cs @@ -165,16 +165,19 @@ public virtual bool Connect() { // Close the client socket Socket.Close(); + + // Call the client disconnected handler + SendError(ex.SocketErrorCode); + OnDisconnected(); + // Dispose the client socket Socket.Dispose(); + // Dispose event arguments _connectEventArg.Dispose(); _receiveEventArg.Dispose(); _sendEventArg.Dispose(); - // Call the client disconnected handler - SendError(ex.SocketErrorCode); - OnDisconnected(); return false; } @@ -242,6 +245,19 @@ public virtual bool Disconnect() // Close the client socket Socket.Close(); + // Update the connected flag + IsConnected = false; + + // Update sending/receiving flags + _receiving = false; + _sending = false; + + // Clear send/receive buffers + ClearBuffers(); + + // Call the client disconnected handler + OnDisconnected(); + // Dispose the client socket Socket.Dispose(); @@ -255,19 +271,6 @@ public virtual bool Disconnect() } catch (ObjectDisposedException) {} - // Update the connected flag - IsConnected = false; - - // Update sending/receiving flags - _receiving = false; - _sending = false; - - // Clear send/receive buffers - ClearBuffers(); - - // Call the client disconnected handler - OnDisconnected(); - return true; } diff --git a/source/NetCoreServer/TcpServer.cs b/source/NetCoreServer/TcpServer.cs index a83c862..527e638 100644 --- a/source/NetCoreServer/TcpServer.cs +++ b/source/NetCoreServer/TcpServer.cs @@ -216,26 +216,30 @@ public virtual bool Stop() // Reset acceptor event arg _acceptorEventArg.Completed -= OnAsyncCompleted; - // Close the acceptor socket - _acceptorSocket.Close(); + try + { + // Close the acceptor socket + _acceptorSocket.Close(); - // Dispose the acceptor socket - _acceptorSocket.Dispose(); + // Disconnect all sessions + DisconnectAll(); - // Dispose event arguments - _acceptorEventArg.Dispose(); + // Update the started flag + IsStarted = false; - // Update the acceptor socket disposed flag - IsSocketDisposed = true; + // Call the server stopped handler + OnStopped(); - // Disconnect all sessions - DisconnectAll(); + // Dispose the acceptor socket + _acceptorSocket.Dispose(); - // Update the started flag - IsStarted = false; + // Dispose event arguments + _acceptorEventArg.Dispose(); - // Call the server stopped handler - OnStopped(); + // Update the acceptor socket disposed flag + IsSocketDisposed = true; + } + catch (ObjectDisposedException) {} return true; } diff --git a/source/NetCoreServer/TcpSession.cs b/source/NetCoreServer/TcpSession.cs index f34af15..0562fa2 100644 --- a/source/NetCoreServer/TcpSession.cs +++ b/source/NetCoreServer/TcpSession.cs @@ -153,6 +153,25 @@ public virtual bool Disconnect() // Close the session socket Socket.Close(); + // Update the connected flag + IsConnected = false; + + // Update sending/receiving flags + _receiving = false; + _sending = false; + + // Clear send/receive buffers + ClearBuffers(); + + // Call the session disconnected handler + OnDisconnected(); + + // Call the session disconnected handler in the server + Server.OnDisconnectedInternal(this); + + // Unregister session + Server.UnregisterSession(Id); + // Dispose the session socket Socket.Dispose(); @@ -165,25 +184,6 @@ public virtual bool Disconnect() } catch (ObjectDisposedException) {} - // Update the connected flag - IsConnected = false; - - // Update sending/receiving flags - _receiving = false; - _sending = false; - - // Clear send/receive buffers - ClearBuffers(); - - // Call the session disconnected handler - OnDisconnected(); - - // Call the session disconnected handler in the server - Server.OnDisconnectedInternal(this); - - // Unregister session - Server.UnregisterSession(Id); - return true; } diff --git a/source/NetCoreServer/UdpClient.cs b/source/NetCoreServer/UdpClient.cs index 4cafac4..655120a 100644 --- a/source/NetCoreServer/UdpClient.cs +++ b/source/NetCoreServer/UdpClient.cs @@ -210,6 +210,19 @@ public virtual bool Disconnect() // Close the client socket Socket.Close(); + // Update the connected flag + IsConnected = false; + + // Update sending/receiving flags + _receiving = false; + _sending = false; + + // Clear send/receive buffers + ClearBuffers(); + + // Call the client disconnected handler + OnDisconnected(); + // Dispose the client socket Socket.Dispose(); @@ -222,19 +235,6 @@ public virtual bool Disconnect() } catch (ObjectDisposedException) {} - // Update the connected flag - IsConnected = false; - - // Update sending/receiving flags - _receiving = false; - _sending = false; - - // Clear send/receive buffers - ClearBuffers(); - - // Call the client disconnected handler - OnDisconnected(); - return true; } diff --git a/source/NetCoreServer/UdpServer.cs b/source/NetCoreServer/UdpServer.cs index 74cf5a7..7c7ddba 100644 --- a/source/NetCoreServer/UdpServer.cs +++ b/source/NetCoreServer/UdpServer.cs @@ -236,6 +236,19 @@ public virtual bool Stop() // Close the server socket Socket.Close(); + // Update the started flag + IsStarted = false; + + // Update sending/receiving flags + _receiving = false; + _sending = false; + + // Clear send/receive buffers + ClearBuffers(); + + // Call the server stopped handler + OnStopped(); + // Dispose the server socket Socket.Dispose(); @@ -244,23 +257,10 @@ public virtual bool Stop() _sendEventArg.Dispose(); // Update the server socket disposed flag - IsSocketDisposed = false; + IsSocketDisposed = true; } catch (ObjectDisposedException) {} - // Update the started flag - IsStarted = false; - - // Update sending/receiving flags - _receiving = false; - _sending = false; - - // Clear send/receive buffers - ClearBuffers(); - - // Call the server stopped handler - OnStopped(); - return true; }