private void OnReadFromClient(IAsyncResult result)
{
// find the connection.
Connection connection = result.AsyncState as Connection;
if (connection == null || !connection.IncomingSocket.Connected)
{
return;
}
try
{
// complete read.
int bytesReceived = connection.IncomingSocket.EndReceive(result);
if (bytesReceived == 0)
{
Close(connection);
return;
}
connection.OutgoingSocket.Send(connection.ClientToServerBuffer, 0, bytesReceived, SocketFlags.None);
// start read from client.
connection.IncomingSocket.BeginReceive(
connection.ClientToServerBuffer,
0,
connection.ClientToServerBuffer.Length,
SocketFlags.None,
OnReadFromClient,
connection);
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected error reading from client.");
Close(connection);
}
}