Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to TCP client/session to send remaining bytes when disconnecting #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions source/NetCoreServer/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ private TcpClient(EndPoint endpoint, string address, int port)
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;

/// <summary>
/// Option: whether to send all remaining bytes when disconnecting
/// </summary>
/// <remarks>
/// This only affects bytes end via SendAsync(...)
/// </remarks>
public bool OptionSendRemainingBytesOnDisconnect { get; set; } = false;

#region Connect/Disconnect client

private SocketAsyncEventArgs _connectEventArg;
Expand Down Expand Up @@ -270,6 +278,12 @@ public virtual bool Disconnect()
if (IsConnecting)
Socket.CancelConnectAsync(_connectEventArg);

// Wait for pending asynchronous send operations if desired
if (OptionSendRemainingBytesOnDisconnect)
{
_sendBuffersEmpty.WaitOne();
}

// Reset event args
_connectEventArg.Completed -= OnAsyncCompleted;
_receiveEventArg.Completed -= OnAsyncCompleted;
Expand Down Expand Up @@ -413,6 +427,7 @@ public virtual bool ReconnectAsync()
private Buffer _sendBufferFlush;
private SocketAsyncEventArgs _sendEventArg;
private long _sendBufferFlushOffset;
private readonly ManualResetEvent _sendBuffersEmpty = new ManualResetEvent(true);

/// <summary>
/// Send data to the server (synchronous)
Expand Down Expand Up @@ -496,6 +511,7 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size)
}

// Fill the main send buffer
_sendBuffersEmpty.Reset();
_sendBufferMain.Append(buffer, offset, size);

// Update statistic
Expand Down Expand Up @@ -659,6 +675,8 @@ private void TrySend()
// Call the empty send buffer handler
if (empty)
{
_sendBuffersEmpty.Set();

OnEmpty();
return;
}
Expand Down Expand Up @@ -867,6 +885,8 @@ private bool ProcessSend(SocketAsyncEventArgs e)
return true;
else
{
_sendBuffersEmpty.Set();

SendError(e.SocketError);
DisconnectAsync();
return false;
Expand Down Expand Up @@ -992,6 +1012,7 @@ protected virtual void Dispose(bool disposingManagedResources)
{
// Dispose managed resources here...
DisconnectAsync();
_sendBuffersEmpty.Dispose();
}

// Dispose unmanaged resources here...
Expand Down
21 changes: 21 additions & 0 deletions source/NetCoreServer/TcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public TcpSession(TcpServer server)
/// </summary>
public int OptionSendBufferSize { get; set; } = 8192;

/// <summary>
/// Option: whether to send all remaining bytes when disconnecting
/// </summary>
/// <remarks>
/// This only affects bytes end via SendAsync(...)
/// </remarks>
public bool OptionSendRemainingBytesOnDisconnect { get; set; } = false;

#region Connect/Disconnect session

/// <summary>
Expand Down Expand Up @@ -154,6 +162,12 @@ public virtual bool Disconnect()
if (!IsConnected)
return false;

// Wait for pending asynchronous send operations if desired
if (OptionSendRemainingBytesOnDisconnect)
{
_sendBuffersEmpty.WaitOne();
}

// Reset event args
_receiveEventArg.Completed -= OnAsyncCompleted;
_sendEventArg.Completed -= OnAsyncCompleted;
Expand Down Expand Up @@ -225,6 +239,7 @@ public virtual bool Disconnect()
private Buffer _sendBufferFlush;
private SocketAsyncEventArgs _sendEventArg;
private long _sendBufferFlushOffset;
private readonly ManualResetEvent _sendBuffersEmpty = new ManualResetEvent(true);

/// <summary>
/// Send data to the client (synchronous)
Expand Down Expand Up @@ -309,6 +324,7 @@ public virtual bool SendAsync(byte[] buffer, long offset, long size)
}

// Fill the main send buffer
_sendBuffersEmpty.Reset();
_sendBufferMain.Append(buffer, offset, size);

// Update statistic
Expand Down Expand Up @@ -473,6 +489,8 @@ private void TrySend()
// Call the empty send buffer handler
if (empty)
{
_sendBuffersEmpty.Set();

OnEmpty();
return;
}
Expand Down Expand Up @@ -628,6 +646,8 @@ private bool ProcessSend(SocketAsyncEventArgs e)
return true;
else
{
_sendBuffersEmpty.Set();

SendError(e.SocketError);
Disconnect();
return false;
Expand Down Expand Up @@ -753,6 +773,7 @@ protected virtual void Dispose(bool disposingManagedResources)
{
// Dispose managed resources here...
Disconnect();
_sendBuffersEmpty.Dispose();
}

// Dispose unmanaged resources here...
Expand Down