예제 #1
0
        // Verify msg type: GC_Chat_Msg
        private static void Verify_GC_Chat_Msg()
        {
            GC_Chat_Msg stSrc = new GC_Chat_Msg();

            // Make object rand
            byte[]        src_bytes = stSrc.ToBytes();
            MBinaryReader mbr       = new MBinaryReader(src_bytes);
            GC_Chat_Msg   stDst     = new GC_Chat_Msg();

            stDst.msg_len = mbr.ReadUInt16();
            stDst.msg_id  = mbr.ReadUInt16();
            stDst.FromBytes(mbr);
            // Verify object content
            if (stDst.msg_len != GC_Chat_Msg.MSG_LEN)
            {
                Console.WriteLine("Failed to verify field: msg_len");
            }
            if (stDst.msg_id != GC_Chat_Msg.MSG_ID)
            {
                Console.WriteLine("Failed to verify field: msg_id");
            }
            // Compare object by bytes
            byte[] dst_bytes = stDst.ToBytes();
            if (dst_bytes.Length != src_bytes.Length)
            {
                Console.WriteLine("Failed to verify field: GC_Chat_Msg 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: GC_Chat_Msg by bytes length");
                }
            }
        }
예제 #2
0
        // Parse a protocol.
        private void ParseProcotol()
        {
            // Check recv len
            int socket_recv_len = m_socket.Receive(m_recv_buff, SocketFlags.None);

            if (socket_recv_len <= 0)
            {
                return;
            }
            // Check pos
            m_recv_stream.Position = m_recv_stream.Length;
            m_recv_stream.Write(m_recv_buff, 0, socket_recv_len);
            m_recv_stream.Position = m_recv_stream_pos;
            while (m_recv_stream.Length - m_recv_stream.Position >= Message.MSG_BASIC_LEN)
            {
                m_recv_stream_pos = m_recv_stream.Position;
                m_recv_stream.Read(m_recv_buff, 0, Message.MSG_BASIC_LEN);
                MBinaryReader mbr = new MBinaryReader(m_recv_buff);

                int msg_len = mbr.ReadUInt16();
                if (m_recv_stream.Length - m_recv_stream.Position >= msg_len - Message.MSG_BASIC_LEN)
                {
                    m_recv_stream_pos = 0;
                    m_recv_stream.Read(m_recv_buff, 0, msg_len - Message.MSG_BASIC_LEN);
                    mbr = new MBinaryReader(m_recv_buff);
                    ushort  msg_id = mbr.ReadUInt16();
                    Message msg    = MessageFactory.CreateMessageById(msg_id);
                    if (null == msg)
                    {
                        CSLogger.LogNotice("Invalid msg id: " + msg_id);
                    }
                    if (!msg.FromBytes(mbr))
                    {
                        CSLogger.LogNotice("ParseProcotol: Fail to parse msg: " + msg.ToString());
                    }
                    if (m_recv_queue != null)
                    {
                        lock (m_recv_queue)
                        {
                            m_recv_queue.Enqueue(msg);
                        }
                    }
                }
                else
                {
                    m_recv_stream.Position = m_recv_stream_pos;
                    break;
                }
                mbr.Close();
                mbr = null;
            }
            if (m_recv_stream.Position == m_recv_stream.Length)
            {
                m_recv_stream.SetLength(0);
            }
        }
예제 #3
0
 // Read msg to input stream
 public override bool FromBytes(MBinaryReader mbr)
 {
     // Skip msg_id and msg_len as it has been read by outside.
     heartbeat_index = mbr.ReadUInt16();
     if (msg_id != MSG_ID)
     {
         Console.WriteLine("Invalid msg id when parse msg type [CG_HeartBeat_Msg], expect:[16340], real:[" + msg_id + "]");
         return(false);
     }
     if (msg_len != MSG_LEN)
     {
         Console.WriteLine("Invalid msg len when parse msg type [CG_HeartBeat_Msg], expect:[6], real:[" + msg_len + "]");
         return(false);
     }
     return(true);
 }