-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
Milestone
Description
/// <summary>
/// Exchange a message with the host.
/// </summary>
/// <param name="message">Message to send.</param>
/// <returns>Message sent by the host.</returns>
internal void Send(string message)
{
if (!this.isContinue)
{
Console.WriteLine($"isContinue {this.isContinue}");
return;
}
if (this.connected)
{
var nioBuffers = new List<ArraySegment<byte>>();
nioBuffers.Add(new ArraySegment<byte>(Encoding.UTF8.GetBytes(message)));
SocketError errorCode;
long localWrittenBytes = this.clientSocket.Send(nioBuffers, SocketFlags.None, out errorCode);
if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock)
{
throw new SocketException((int)errorCode);
}
if (errorCode == SocketError.WouldBlock)
{
this.isContinue = false;
Console.WriteLine(message);
var sendArgs = new SocketAsyncEventArgs();
sendArgs.BufferList = nioBuffers;
sendArgs.Completed += this.SendArgs_Completed;
bool pending = this.clientSocket.SendAsync(sendArgs);
if (!pending)
{
Console.WriteLine($"pending false BytesTransferred {sendArgs.BytesTransferred}");
}
else
{
Console.WriteLine($"pending true");
}
}
else
{
Console.WriteLine($"{nioBuffers.Sum(n => n.Array.Length)} {localWrittenBytes}");
}
}
else
{
throw new SocketException((int)SocketError.NotConnected);
}
}https://github.com/caozhiyuan/DotNetty/tree/dev/examples/SocketAsyncClient
https://github.com/caozhiyuan/DotNetty/tree/dev/examples/SocketAsyncServer
[EDIT] Add C# syntax highlighting by @karelz
