protected void ReceiveCompleted(IAsyncResult result) { int count = 0; try { count = _socket.EndReceive(result); if (count > 0) { long sid = _server.NextId(this); long cid; RCValue message = _buffer.CompleteReceive(_listenState.Runner, count, _server.Handle, sid, out cid); // Console.Out.WriteLine ("Server receiving {0}", cid); if (message != null) { _inbox.Add(_listenState.Runner, message); } _socket.BeginReceive(_buffer.RecvBuffer, _buffer.Read, _buffer.RecvBuffer.Length - _buffer.Read, SocketFlags.None, new AsyncCallback(ReceiveCompleted), null); } } catch (Exception ex) { _listenState.Runner.Report(_listenState.Closure, ex); // Make sure we close the socket. count = 0; } finally { // What is this timeout garbage, shouldn't there be a BeginClose. // Do I need to create one? if (count == 0) { // We need to tell the server to remove this connection from the list. // No need to try and close the connection. That is already done. _socket.Close(1000); RCSystem.Log.Record(_listenState.Closure, "socket", _handle, "closed", ""); } } }
protected void ReceiveCompleted(IAsyncResult result) { int count = 0; try { count = _socket.EndReceive(result); if (count > 0) { long sid = -1; long ignore; RCBlock message = _buffer.CompleteReceive(_openState.Runner, count, _handle, sid, out ignore); RCSymbol id = (RCSymbol)message.Get("id"); // Console.Out.WriteLine ("Client receiving {0}, message:{1}", id, message); if (message != null) { _inbox.Add(id[0], message); } _socket.BeginReceive( _buffer.RecvBuffer, _buffer.Read, _buffer.RecvBuffer.Length - _buffer.Read, SocketFlags.None, new AsyncCallback(ReceiveCompleted), null); } } catch (Exception ex) { _openState.Runner.Report(_openState.Closure, ex); } finally { if (count == 0) { _socket.Close(1000); RCSystem.Log.Record(_openState.Closure, "socket", _handle, "closed", ""); } } }