private void OnSocketData(IAsyncResult result)
{
try {
int len = _socket.EndReceive(result);
if (!_channelReady.WaitOne(3000, false))
throw new IOException("channel ready timed out");
//Debug.WriteLine(String.Format("OnSocketData ch={0} len={1}", _channel.LocalChannelID, len));
if (len <= 0) {
_socket.ShutdownReceive();
_channel.SendEOF();
_channel.Close();
}
else {
_channel.Transmit(_buffer, 0, len);
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(this.OnSocketData), null);
}
}
catch (Exception ex) {
Debug.WriteLine("OnSocketData catch handler" + _channel.LocalChannelID);
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
try {
_channel.Close();
}
catch (Exception ex2) {
Debug.WriteLine("Channel Close Error");
Debug.WriteLine(ex2.Message);
Debug.WriteLine(ex2.StackTrace);
}
try {
_socket.Close();
}
catch (Exception ex2) {
Debug.WriteLine("Socket Close Error");
Debug.WriteLine(ex2.Message);
Debug.WriteLine(ex2.StackTrace);
}
}
}