private ProtocolPacket StringToPacket(string line) { TextDataReader reader = new TextDataReader(line.Trim()); ProtocolCommandId cmd = (ProtocolCommandId)Enum.Parse(typeof(ProtocolCommandId), reader.ReadString()); return(ProtocolPacket.FromData(cmd, reader)); }
private static byte[] PacketToBytes(ProtocolPacket packet) { BinaryDataWriter writer = new BinaryDataWriter(); packet.GetData(writer); return(writer.ToArray()); }
static string PacketToString(ProtocolPacket packet) { TextDataWriter writer = new TextDataWriter(); writer.Write(packet.CommandId.ToString()); packet.GetData(writer); return(writer.ToString()); }
public static ProtocolPacket ReadPacket(BinaryReader reader, int total_len) { int chksum = reader.ReadInt32NetworkOrder(); byte cmd = reader.ReadByte(); byte[] data = reader.ReadAllBytes(total_len - 1); if (CalculateChecksum(cmd, data) != chksum) { throw new InvalidDataException("Checksum does not match"); } return(ProtocolPacket.FromData((ProtocolCommandId)cmd, new BinaryDataReader(data))); }
public static void WritePacket(ProtocolPacket packet, BinaryWriter writer, bool write_length) { byte[] data = PacketToBytes(packet); int total_len = 1 + data.Length; byte cmd = (byte)(int)packet.CommandId; int chksum = CalculateChecksum(cmd, data); if (write_length) { writer.WriteNetworkOrder(total_len); } writer.WriteNetworkOrder(chksum); writer.Write(cmd); writer.Write(data); writer.Flush(); }
public void WritePacket(ProtocolPacket packet) { _writer.WriteLine(PacketToString(packet)); }
public void WritePacket(ProtocolPacket packet) { WritePacket(packet, _writer, true); }