diff --git a/src/Ether.Network/Common/NetConnection.cs b/src/Ether.Network/Common/NetConnection.cs index d3dc226..f5cc7b2 100644 --- a/src/Ether.Network/Common/NetConnection.cs +++ b/src/Ether.Network/Common/NetConnection.cs @@ -54,12 +54,16 @@ protected virtual void Dispose(bool disposing) { if (this.Socket != null) { +#if !NETSTANDARD1_3 + this.Socket.Close(); +#endif this.Socket.Dispose(); this.Socket = null; } + + this._disposedValue = true; } - this._disposedValue = true; } } } diff --git a/src/Ether.Network/Server/NetServer.cs b/src/Ether.Network/Server/NetServer.cs index ea25365..ff2adcf 100644 --- a/src/Ether.Network/Server/NetServer.cs +++ b/src/Ether.Network/Server/NetServer.cs @@ -87,9 +87,9 @@ public void Start() this._writePool.Push(NetUtils.CreateSocketAsync(null, this.IO_Completed, this.Configuration.BufferSize)); } - this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); this.Initialize(); + this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + this.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); this.Socket.Bind(new IPEndPoint(address, this.Configuration.Port)); this.Socket.Listen(this.Configuration.Backlog); this.IsRunning = true; @@ -105,22 +105,21 @@ public void Stop() if (!this.IsRunning) return; - this.IsRunning = false; - - this.ClearClients(); - this._readPool.Clear(); - this._writePool.Clear(); - - if (this.Configuration.Blocking) - this._manualResetEvent.Set(); + this.ClearResources(); if (this.Socket != null) { +#if !NETSTANDARD1_3 + this.Socket.Close(); +#endif this.Socket.Dispose(); this.Socket = null; } - this._messageQueue.Clear(); + if (this.Configuration.Blocking) + this._manualResetEvent.Set(); + + this.IsRunning = false; } /// @@ -346,14 +345,17 @@ private void HandleIncomingMessages(T user, byte[] messageData) } /// - /// Clear client's list. + /// Clear NetServer's resources. /// - private void ClearClients() + private void ClearResources() { foreach (T client in this.Clients) client.Dispose(); this._clients.Clear(); + this._readPool.Clear(); + this._writePool.Clear(); + this._messageQueue.Clear(); } /// @@ -393,18 +395,11 @@ protected override void Dispose(bool disposing) if (disposing) { this._sendQueueTaskCancelTokenSource.Cancel(false); - this._readPool?.Dispose(); - this._writePool?.Dispose(); - this.ClearClients(); - this._messageQueue.Clear(); + this.ClearResources(); + this._readPool.Dispose(); + this._writePool.Dispose(); this._messageQueue.Dispose(); - - if (this.Socket != null) - { - this.Socket.Dispose(); - this.Socket = null; - } - + this._isDisposed = true; } } diff --git a/test/Ether.Network.Tests/NetServerConfigurationTest.cs b/test/Ether.Network.Tests/NetServerConfigurationTest.cs index 6756d00..d3378b9 100644 --- a/test/Ether.Network.Tests/NetServerConfigurationTest.cs +++ b/test/Ether.Network.Tests/NetServerConfigurationTest.cs @@ -15,8 +15,6 @@ public void StartServerWithoutConfiguration() Exception ex = Assert.Throws(() => server.Start()); Assert.IsType(ex); - - server.Stop(); } } @@ -27,7 +25,6 @@ public void SetupServerConfigurationBeforeStart() { server.SetupConfiguration(); server.Start(); - server.Stop(); } } @@ -42,8 +39,6 @@ public void SetupServerConfigurationAfterStart() Exception ex = Assert.Throws(() => server.SetupConfiguration()); Assert.IsType(ex); - - server.Stop(); } }