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