// Read msg to input stream public override bool FromBytes(MBinaryReader mbr) { // Skip msg_id and msg_len as it has been read by outside. data.FromBytes(mbr); if (msg_id != MSG_ID) { Console.WriteLine("Invalid msg id when parse msg type [GC_Chat_Msg], expect:[17881], real:[" + msg_id + "]"); return(false); } if (msg_len != MSG_LEN) { Console.WriteLine("Invalid msg len when parse msg type [GC_Chat_Msg], expect:[268], real:[" + msg_len + "]"); return(false); } return(true); }
// Verify msg type: ChatMsg private static void Verify_ChatMsg() { ChatMsg stSrc = new ChatMsg(); // Make object rand stSrc.content = "test"; stSrc.speaker_id = 78; stSrc.time = 51; byte[] src_bytes = stSrc.ToBytes(); MBinaryReader mbr = new MBinaryReader(src_bytes); ChatMsg stDst = new ChatMsg(); stDst.FromBytes(mbr); // Verify object content if (stSrc.speaker_id != stDst.speaker_id) { Console.WriteLine("Failed to verify field: speaker_id"); } if (stSrc.time != stDst.time) { Console.WriteLine("Failed to verify field: time"); } // Compare object by bytes byte[] dst_bytes = stDst.ToBytes(); if (dst_bytes.Length != src_bytes.Length) { Console.WriteLine("Failed to verify field: ChatMsg 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: ChatMsg by bytes length"); } } }