public async Task WriteAsync(ITurtleApiMessage msg) { writerReset.WaitOne(); try { int type, len; MessageConverter.Write(msg, ref writerBuffer, out type, out len); var payload = len - 8; writerBuffer[0] = (byte)(payload >> 0); writerBuffer[1] = (byte)(payload >> 8); writerBuffer[2] = (byte)(payload >> 16); writerBuffer[3] = (byte)(payload >> 24); writerBuffer[4] = (byte)(type >> 0); writerBuffer[5] = (byte)(type >> 8); writerBuffer[6] = (byte)(type >> 16); writerBuffer[7] = (byte)(type >> 24); await stream.WriteAsync(writerBuffer, 0, len); } finally { writerReset.Set(); } }
protected override void Dispatch(ITurtleApiMessage msg) { var cm = msg as ClientMessage; if (cm != null) { Program.Verbose($"ClientMessage: {cm.Label} -> {cm.Command}"); ClientServer.Execute(cm.Label, cm.Command); } }
protected override void Dispatch(ITurtleApiMessage msg) { if (msg.GetType() == typeof(TurtleMessage)) { var tm = (TurtleMessage)msg; Console.WriteLine($"TurtleMessage: {tm.Label}"); if (!turtles.ContainsKey(tm.Label)) { turtles[tm.Label] = new Turtle(tm.Label, this); } } else if (msg.GetType() == typeof(Response)) { var rs = (Response)msg; Console.WriteLine($"Response: {rs.Label} -> {rs.Content}"); turtles[rs.Label].OnMessage(rs.Content); } }
protected abstract void Dispatch(ITurtleApiMessage msg);
public static void Write(ITurtleApiMessage msg, ref byte[] buffer, out int messageType, out int len) { messageType = TypeMap[msg.GetType()]; len = Writers[messageType](ref buffer, msg); }