コード例 #1
0
        // 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[] GC_Chat_Msg_bytes = mbw.ToArray();
            mbw.Close();
            mbw = null;
            // Verify the length of msg
            if (GC_Chat_Msg_bytes.Length != 268)
            {
                Console.WriteLine("Invalid msg len, expect:268, real:GC_Chat_Msg_bytes.Length");
                return(null);
            }
            return(GC_Chat_Msg_bytes);
        }
コード例 #2
0
        // 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");
                }
            }
        }