public ClientModule(System.Net.IPAddress host, int port, ClientGame parentGame) { netSession = new Network.ClientSession(0, new System.Net.IPEndPoint(host, port), parentGame.Main.ScriptConsole.Write); netSession.onDatagramReceived += (data) => { var gram = new Network.ReadOnlyDatagram(data); while (gram.More) { uint messageCode = 0; gram.ReadUInt(out messageCode, 8); switch (messageCode) { case 0: if (simulation != null) throw new InvalidProgramException(); { String episodeName; uint version; gram.ReadString(out episodeName); gram.ReadUInt(out version, 32); parentGame.StartSimulation(episodeName, version); } break; case 1: { UInt32 entityID; UInt32 syncID; UInt32 dataLength; gram.ReadUInt(out entityID, 32); gram.ReadUInt(out syncID, 8); gram.ReadUInt(out dataLength, 32); var bytes = new byte[dataLength]; gram.ReadBytes(bytes, dataLength); if (syncables.ContainsKey(entityID)) foreach (var syncable in syncables[entityID]) //if (syncable.SyncID == syncID) syncable.ReadFullSync(new ReadOnlyDatagram(bytes)); } break; case 2: { UInt32 length; gram.ReadUInt(out length, 32); byte[] message = new byte[length]; gram.ReadBytes(message, length); string messageID = null; MISP.ScriptList messageData = null; ScriptMessage.DecodeMessage(message, out messageID, out messageData); simulation.EnqueueEvent(messageID, messageData); } break; } } }; parentGame.Main.Write("Connected to server " + host + " on port " + port + "\n"); }
public static void DecodeMessage(byte[] bytes, out String ID, out MISP.ScriptList Data) { Initialize(); var datagram = new ReadOnlyDatagram(bytes); datagram.ReadString(out ID); Data = DecodeList(datagram); }
public static void DecodeMessage(byte[] bytes, out String ID, out Common.ObjectList Data) { Initialize(); var datagram = new ReadOnlyDatagram(bytes); datagram.ReadString(out ID); Data = DecodeList(datagram); }
private static MISP.ScriptList DecodeList(ReadOnlyDatagram datagram) { var r = new MISP.ScriptList(); uint count = 0; datagram.ReadUInt(out count, 8); byte[] temp = new byte[4]; for (int i = 0; i < count; ++i) { uint typeCode = 0; datagram.ReadUInt(out typeCode, 8); switch ((ScriptTypes)typeCode) { case ScriptTypes.List: r.Add(DecodeList(datagram)); break; case ScriptTypes.String: { String s; datagram.ReadString(out s); r.Add(s); } break; case ScriptTypes.Int32: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToInt32(temp, 0)); break; case ScriptTypes.UInt32: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToUInt32(temp, 0)); break; case ScriptTypes.Single: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToSingle(temp, 0)); break; case ScriptTypes.Bool: { uint b = 0; datagram.ReadUInt(out b, 8); if (b == 0) r.Add(null); else r.Add(true); } break; default: throw new MISP.ScriptError("Error decoding message", null); } } return r; }
private static Common.ObjectList DecodeList(ReadOnlyDatagram datagram) { var r = new Common.ObjectList(); uint count = 0; datagram.ReadUInt(out count, 8); byte[] temp = new byte[4]; for (int i = 0; i < count; ++i) { uint typeCode = 0; datagram.ReadUInt(out typeCode, 8); switch ((ScriptTypes)typeCode) { case ScriptTypes.List: r.Add(DecodeList(datagram)); break; case ScriptTypes.String: { String s; datagram.ReadString(out s); r.Add(s); } break; case ScriptTypes.Int32: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToInt32(temp, 0)); break; case ScriptTypes.UInt32: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToUInt32(temp, 0)); break; case ScriptTypes.Single: datagram.ReadBytes(temp, 4); r.Add(BitConverter.ToSingle(temp, 0)); break; case ScriptTypes.Bool: { uint b = 0; datagram.ReadUInt(out b, 8); if (b == 0) { r.Add(null); } else { r.Add(true); } } break; default: throw new InvalidProgramException("Error decoding message", null); } } return(r); }