Skip to content

Commit

Permalink
Centralize resource cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe GOMES PEIXOTO committed Feb 21, 2018
1 parent 2e920e6 commit 9afb7db
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
6 changes: 5 additions & 1 deletion src/Ether.Network/Common/NetConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
43 changes: 19 additions & 24 deletions src/Ether.Network/Server/NetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/// <inheritdoc />
Expand Down Expand Up @@ -346,14 +345,17 @@ private void HandleIncomingMessages(T user, byte[] messageData)
}

/// <summary>
/// Clear client's list.
/// Clear NetServer's resources.
/// </summary>
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();
}

/// <summary>
Expand Down Expand Up @@ -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;
}
}
Expand Down
5 changes: 0 additions & 5 deletions test/Ether.Network.Tests/NetServerConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public void StartServerWithoutConfiguration()
Exception ex = Assert.Throws<EtherConfigurationException>(() => server.Start());

Assert.IsType<EtherConfigurationException>(ex);

server.Stop();
}
}

Expand All @@ -27,7 +25,6 @@ public void SetupServerConfigurationBeforeStart()
{
server.SetupConfiguration();
server.Start();
server.Stop();
}
}

Expand All @@ -42,8 +39,6 @@ public void SetupServerConfigurationAfterStart()
Exception ex = Assert.Throws<EtherConfigurationException>(() => server.SetupConfiguration());

Assert.IsType<EtherConfigurationException>(ex);

server.Stop();
}
}

Expand Down

0 comments on commit 9afb7db

Please sign in to comment.