// Write msg to output stream public override byte[] ToBytes() { MBinaryWriter mbw = new MBinaryWriter(); mbw.Write(msg_len); mbw.Write(msg_id); byte[] data_bytes = data.ToBytes(); mbw.Write(data_bytes); byte[] CG_Echo_Request_bytes = mbw.ToArray(); mbw.Close(); mbw = null; // Verify the length of msg if (CG_Echo_Request_bytes.Length != 274) { Console.WriteLine("Invalid msg len, expect:274, real:CG_Echo_Request_bytes.Length"); return(null); } return(CG_Echo_Request_bytes); }
// Write msg to output stream public override byte[] ToBytes() { MBinaryWriter mbw = new MBinaryWriter(); mbw.Write(msg_len); mbw.Write(msg_id); byte[] data_bytes = data.ToBytes(); mbw.Write(data_bytes); mbw.Write((Int16)response_code); byte[] GC_Echo_Response_bytes = mbw.ToArray(); mbw.Close(); mbw = null; // Verify the length of msg if (GC_Echo_Response_bytes.Length != 276) { Console.WriteLine("Invalid msg len, expect:276, real:GC_Echo_Response_bytes.Length"); return(null); } return(GC_Echo_Response_bytes); }
// Verify msg type: EchoMsg private static void Verify_EchoMsg() { EchoMsg stSrc = new EchoMsg(); // Make object rand stSrc.protocol_version = "test"; stSrc.int_param = 76; stSrc.char_array = "test"; byte[] src_bytes = stSrc.ToBytes(); MBinaryReader mbr = new MBinaryReader(src_bytes); EchoMsg stDst = new EchoMsg(); stDst.FromBytes(mbr); // Verify object content if (stSrc.int_param != stDst.int_param) { Console.WriteLine("Failed to verify field: int_param"); } if (stSrc.enum_value != stDst.enum_value) { Console.WriteLine("Failed to verify field: enum_value"); } // Compare object by bytes byte[] dst_bytes = stDst.ToBytes(); if (dst_bytes.Length != src_bytes.Length) { Console.WriteLine("Failed to verify field: EchoMsg by bytes length"); } for (int byte_index = 0; byte_index < dst_bytes.Length; ++byte_index) { if (src_bytes[byte_index] != dst_bytes[byte_index]) { Console.WriteLine("Failed to verify field: EchoMsg by bytes length"); } } }