Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 20 additions & 2 deletions AsyncTcpClient/AsyncTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,16 @@ public async Task RunAsync()
Message?.Invoke(this, new AsyncTcpEventArgs("Connection reset remotely", ex));
readLength = -2;
}
catch(Exception ex)
{
Message?.Invoke(this, new AsyncTcpEventArgs("Read from stream failed", ex));
readLength = -3;
}
if (readLength <= 0)
{
if (readLength == 0)
{
Message?.Invoke(this, new AsyncTcpEventArgs("Connection closed remotely"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message will be invoked even if connection was disconnected locally because readAsync always return 0

Message?.Invoke(this, new AsyncTcpEventArgs("Connection closed gracefully"));
}
closedTcs.TrySetResult(true);
OnClosed(readLength != -1);
Expand All @@ -263,14 +268,27 @@ public async Task RunAsync()
}

/// <summary>
/// Closes the socket connection normally. This does not release the resources used by the
/// Closes the socket connection normally. Switch off AutoReconnect. This does not release the resources used by the
/// <see cref="AsyncTcpClient"/>.
/// </summary>
public void Disconnect()
{
AutoReconnect = false;
tcpClient.Client.Disconnect(false);
}


/// <summary>
/// Closes the socket connection normally and try to connect again. This does not release the resources used by the
/// <see cref="AsyncTcpClient"/>.
/// </summary>
public void DisconnectAndReconnect()
{
AutoReconnect = true;
tcpClient.Client.Disconnect(false);
}


/// <summary>
/// Releases the managed and unmanaged resources used by the <see cref="AsyncTcpClient"/>.
/// Closes the connection to the remote host and disabled automatic reconnecting.
Expand Down
24 changes: 17 additions & 7 deletions AsyncTcpClientDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ private async Task RunAsync2()

var client = new AsyncTcpClient
{
IPAddress = IPAddress.IPv6Loopback,
IPAddress = IPAddress.Loopback,
Port = port,
//AutoReconnect = true,
AutoReconnect = true,
ConnectedCallback = async (c, isReconnected) =>
{
await c.WaitAsync(); // Wait for server banner
Expand Down Expand Up @@ -143,19 +143,29 @@ private async Task RunAsync2()
byte[] bytes = c.ByteBuffer.Dequeue(count);
string message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
Console.WriteLine("Client: received: " + message);

if (message == "close")
c.Disconnect();

return Task.CompletedTask;
}
};
client.Message += (s, a) => Console.WriteLine("Client: " + a.Message);
var clientTask = client.RunAsync();

await Task.Delay(10 * 1000);
Console.WriteLine("Program: stopping server");
server.Stop(true);
await serverTask;

client.Dispose();
// Setup 1
//await Task.Delay(10 * 1000);
//Console.WriteLine("Program: stopping server");
//server.Stop(true);
//await serverTask;
//client.Disconnect();
//await clientTask;


// Setup 2
await clientTask;
client.Dispose();
}
}
}