// Write msg to output stream public byte[] ToBytes() { MBinaryWriter mbw = new MBinaryWriter(); mbw.WriteString(protocol_version, protocol_version_size); mbw.Write(int_param); for (Int32 nElementIndex = 0; nElementIndex < int_array_size; ++nElementIndex) { mbw.Write(int_array[nElementIndex]); } mbw.WriteString(char_array, char_array_size); byte[] rand_string_field_bytes = rand_string_field.ToBytes(); mbw.Write(rand_string_field_bytes); for (Int32 nElementIndex = 0; nElementIndex < rand_string_array_size; ++nElementIndex) { byte[] rand_string_array_bytes = rand_string_array[nElementIndex].ToBytes(); mbw.Write(rand_string_array_bytes); } mbw.Write((Int16)enum_value); byte[] EchoMsg_bytes = mbw.ToArray(); mbw.Close(); mbw = null; // Verify the length of msg if (EchoMsg_bytes.Length != 270) { Console.WriteLine("Invalid msg len, expect:270, real:EchoMsg_bytes.Length"); return(null); } return(EchoMsg_bytes); }
// Verify msg type: RandString private static void Verify_RandString() { RandString stSrc = new RandString(); // Make object rand stSrc.data = "test"; byte[] src_bytes = stSrc.ToBytes(); MBinaryReader mbr = new MBinaryReader(src_bytes); RandString stDst = new RandString(); stDst.FromBytes(mbr); // Verify object content // Compare object by bytes byte[] dst_bytes = stDst.ToBytes(); if (dst_bytes.Length != src_bytes.Length) { Console.WriteLine("Failed to verify field: RandString 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: RandString by bytes length"); } } }