public TcpSendState Send(RCRunner runner, RCClosure closure, long cid, RCBlock message) { TcpSendState correlation = new TcpSendState(_handle, cid, message); RCAsyncState state = new RCAsyncState(runner, closure, correlation); byte[] payload = Encoding.ASCII.GetBytes(message.ToString()); // If other items are queued in the outbox Add will return false. // This message should be sent after the others. if (_outbox.Add(state)) { // Send will add the header to the payload. // This is something I will probably want to change by adding some kind of // serializer/formatter abstraction. int size = _buffer.PrepareSend(cid, payload); _socket.BeginSend( _buffer.SendBuffer, 0, size, SocketFlags.None, new AsyncCallback(SendCompleted), state); // Console.Out.WriteLine ("Server sending {0}", correlation.Id); } return(correlation); }
protected void SendCompleted(IAsyncResult result) { RCAsyncState state = (RCAsyncState)result.AsyncState; try { // Don't know what to do with the count returned... Anything? _socket.EndSend(result); // Dequeue the next item for sending on this channel. // if (next != null) RCAsyncState next = _outbox.Remove(); if (next != null) { TcpSendState correlation = (TcpSendState)next.Other; byte[] payload = Encoding.ASCII.GetBytes(correlation.Message.ToString()); int size = _buffer.PrepareSend(correlation.Id, payload); _socket.BeginSend(_buffer.SendBuffer, 0, size, SocketFlags.None, new AsyncCallback(SendCompleted), state); // Console.Out.WriteLine ("Server sending {0}", correlation.Id); // Send (next.Runner, next.Closure, other.Id, other.Message); } } catch (Exception ex) { state.Runner.Report(state.Closure, ex); } }
protected void SendCompleted(IAsyncResult result) { RCAsyncState state = (RCAsyncState)result.AsyncState; try { // Don't know what to do with the count returned... Anything? _socket.EndSend(result); // Dequeue the next item for sending on this channel. RCAsyncState next = _outbox.Remove(); if (next != null) { // Send will add the header to the payload. // This is something I will probably want to change by adding some kind of // serializer/formatter abstraction. TcpSendState correlation = (TcpSendState)next.Other; byte[] payload = _protocol.Serialize(this, correlation.Message); int size = _buffer.PrepareSend(correlation.Id, payload); _socket.BeginSend(_buffer.SendBuffer, 0, size, SocketFlags.None, new AsyncCallback(SendCompleted), state); // Console.Out.WriteLine ("Client sending {0}", correlation.Id); } } catch (Exception ex) { state.Runner.Report(state.Closure, ex); } }
public override TcpSendState Send(RCRunner runner, RCClosure closure, RCBlock message) { long cid = Interlocked.Increment(ref _cid); // Console.Out.WriteLine ("New Cid:{0}", cid); byte[] payload = _protocol.Serialize(this, message); TcpSendState correlation = new TcpSendState(_handle, cid, message); RCAsyncState state = new RCAsyncState(runner, closure, correlation); if (_outbox.Add(state)) { // Send will add the header to the payload. // This is something I will probably want to change by adding some kind of // serializer/formatter abstraction. int size = _buffer.PrepareSend(correlation.Id, payload); _socket.BeginSend( _buffer.SendBuffer, 0, size, SocketFlags.None, new AsyncCallback(SendCompleted), state); // Console.Out.WriteLine ("Client sending {0}", correlation.Id); } else { // Console.Out.WriteLine ("Another message has to go first"); } return(correlation); }
protected void DoSend(RCRunner runner, RCClosure closure, RCLong left, RCBlock right) { RCSymbolScalar[] result = new RCSymbolScalar[left.Count]; RCBot bot = runner.GetBot(closure.Bot); for (int i = 0; i < left.Count; ++i) { Client client = (Client)bot.Get(left[i]); TcpSendState state = client.Send(runner, closure, right); RCSymbolScalar handle = new RCSymbolScalar(null, state.Handle); result[i] = new RCSymbolScalar(handle, state.Id); } runner.Yield(closure, new RCSymbol(result)); }
protected void DoReply(RCRunner runner, RCClosure closure, RCSymbol left, RCBlock right) { RCBot bot = runner.GetBot(closure.Bot); RCSymbolScalar[] result = new RCSymbolScalar[left.Count]; for (int i = 0; i < left.Count; ++i) { long channel = (long)left[i].Part(0); long sid = (long)left[i].Part(1); long cid = (long)left[i].Part(2); Server server = (Server)bot.Get(channel); TcpSendState state = server.Reply(runner, closure, sid, cid, right); RCSymbolScalar handle = new RCSymbolScalar(null, state.Handle); result[i] = new RCSymbolScalar(handle, state.Id); } runner.Yield(closure, new RCSymbol(result)); }