Skip to content

Commit

Permalink
expose the shutdown method to close the connection gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
gaochundong committed May 31, 2017
1 parent e62d8d4 commit 7362c08
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ private async Task Process()
}
catch (ObjectDisposedException)
{
// looking forward to a graceful quit from the ReadAsync, but the EndRead doesn't make it happen
// looking forward to a graceful quit from the ReadAsync but the inside EndRead will raise the ObjectDisposedException,
// so a gracefully close for the socket should be a Shutdown, but we cannot avoid the Close triggers this happen.
}
catch (Exception ex)
{
Expand Down
13 changes: 6 additions & 7 deletions Cowboy/Cowboy.Sockets/Tcp/Client/APM/TcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,16 @@ private void Close(bool shallNotifyUserSide)
Clean();
}

private void Shutdown()
public void Shutdown()
{
try
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
if (_tcpClient != null && _tcpClient.Connected)
{
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
_tcpClient.Client.Shutdown(SocketShutdown.Send);
}
catch { }
}

private void Clean()
Expand Down
13 changes: 6 additions & 7 deletions Cowboy/Cowboy.Sockets/Tcp/Client/SAEA/TcpSocketSaeaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,16 @@ private async Task Close(bool shallNotifyUserSide)
Clean();
}

private void Shutdown()
public void Shutdown()
{
try
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
if (_socket != null && _socket.Connected)
{
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
_socket.Shutdown(SocketShutdown.Send);
}
catch { }
}

private void Clean()
Expand Down
16 changes: 8 additions & 8 deletions Cowboy/Cowboy.Sockets/Tcp/Client/TAP/AsyncTcpSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ private async Task Process()
}
catch (ObjectDisposedException)
{
// looking forward to a graceful quit from the ReadAsync, but the EndRead doesn't make it happen
// looking forward to a graceful quit from the ReadAsync but the inside EndRead will raise the ObjectDisposedException,
// so a gracefully close for the socket should be a Shutdown, but we cannot avoid the Close triggers this happen.
}
catch (Exception ex)
{
Expand Down Expand Up @@ -444,17 +445,16 @@ private async Task Close(bool shallNotifyUserSide)
Clean();
}

private void Shutdown()
public void Shutdown()
{
try
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
if (_tcpClient != null && _tcpClient.Connected)
{
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
_tcpClient.Client.Shutdown(SocketShutdown.Send);
}
catch { }
}

private void Clean()
Expand Down
13 changes: 6 additions & 7 deletions Cowboy/Cowboy.Sockets/Tcp/Server/APM/TcpSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,16 @@ private void Close(bool shallNotifyUserSide)
Clean();
}

private void Shutdown()
public void Shutdown()
{
try
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
if (_tcpClient != null && _tcpClient.Connected)
{
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
_tcpClient.Client.Shutdown(SocketShutdown.Send);
}
catch { }
}

private void Clean()
Expand Down
13 changes: 6 additions & 7 deletions Cowboy/Cowboy.Sockets/Tcp/Server/SAEA/TcpSocketSaeaSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,16 @@ private async Task Close(bool shallNotifyUserSide)
Clean();
}

private void Shutdown()
public void Shutdown()
{
try
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
if (_socket != null && _socket.Connected)
{
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
_socket.Shutdown(SocketShutdown.Send);
}
catch { }
}

private void Clean()
Expand Down
16 changes: 8 additions & 8 deletions Cowboy/Cowboy.Sockets/Tcp/Server/TAP/AsyncTcpSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ private async Task Process()
}
catch (ObjectDisposedException)
{
// looking forward to a graceful quit from the ReadAsync, but the EndRead doesn't make it happen
// looking forward to a graceful quit from the ReadAsync but the inside EndRead will raise the ObjectDisposedException,
// so a gracefully close for the socket should be a Shutdown, but we cannot avoid the Close triggers this happen.
}
catch (Exception ex)
{
Expand Down Expand Up @@ -382,17 +383,16 @@ private async Task Close(bool shallNotifyUserSide)
Clean();
}

private void Shutdown()
public void Shutdown()
{
try
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
if (_tcpClient != null && _tcpClient.Connected)
{
// The correct way to shut down the connection (especially if you are in a full-duplex conversation)
// is to call socket.Shutdown(SocketShutdown.Send) and give the remote party some time to close
// their send channel. This ensures that you receive any pending data instead of slamming the
// connection shut. ObjectDisposedException should never be part of the normal application flow.
_tcpClient.Client.Shutdown(SocketShutdown.Send);
}
catch { }
}

private void Clean()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static void Main(string[] args)
}
}

_client.Close().Wait();
_client.Shutdown();
Console.WriteLine("TCP client has disconnected from server [{0}].", remoteEP);
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion Cowboy/Tests/Cowboy.Sockets.TestTcpSocketClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static void Main(string[] args)
}
}

_client.Close();
_client.Shutdown();
Console.WriteLine("TCP client has disconnected from server.");

Console.ReadKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static void Main(string[] args)
}
}

_client.Close().Wait();
_client.Shutdown();
Console.WriteLine("TCP client has disconnected from server [{0}].", remoteEP);
}
catch (Exception ex)
Expand Down

0 comments on commit 7362c08

Please sign in to comment.