// Verify msg type: KeyValuePair private static void Verify_KeyValuePair() { KeyValuePair stSrc = new KeyValuePair(); // Make object rand stSrc.key = "test"; stSrc.value = "test"; byte[] src_bytes = stSrc.ToBytes(); MBinaryReader mbr = new MBinaryReader(src_bytes); KeyValuePair stDst = new KeyValuePair(); 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: KeyValuePair 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: KeyValuePair by bytes length"); } } }
// Read msg to input stream public bool FromBytes(MBinaryReader mbr) { // Skip msg_id and msg_len as it has been read by outside. key = mbr.ReadString(key_size); value = mbr.ReadString(value_size); return(true); }
// 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"); } } }
// 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"); } } }
// Read msg to input stream public bool FromBytes(MBinaryReader mbr) { // Skip msg_id and msg_len as it has been read by outside. content = mbr.ReadString(content_size); speaker_id = mbr.ReadUInt32(); time = mbr.ReadUInt32(); return(true); }
// 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); } }
// 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); }
// 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); }
// 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); response_code = (EResponseCode)mbr.ReadInt16(); if (msg_id != MSG_ID) { Console.WriteLine("Invalid msg id when parse msg type [GC_Echo_Response], expect:[16542], real:[" + msg_id + "]"); return(false); } if (msg_len != MSG_LEN) { Console.WriteLine("Invalid msg len when parse msg type [GC_Echo_Response], expect:[276], real:[" + msg_len + "]"); return(false); } return(true); }
// Read msg to input stream public bool FromBytes(MBinaryReader mbr) { // Skip msg_id and msg_len as it has been read by outside. protocol_version = mbr.ReadString(protocol_version_size); int_param = mbr.ReadUInt32(); for (Int32 nElementIndex = 0; nElementIndex < int_array_size; ++nElementIndex) { int_array[nElementIndex] = mbr.ReadUInt32(); } char_array = mbr.ReadString(char_array_size); rand_string_field.FromBytes(mbr); for (Int32 nElementIndex = 0; nElementIndex < rand_string_array_size; ++nElementIndex) { rand_string_array[nElementIndex].FromBytes(mbr); } enum_value = (EResponseCode)mbr.ReadInt16(); return(true); }
// 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"); } } }
// 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"); } } }
public abstract bool FromBytes(MBinaryReader mbr);
// Read msg to input stream public bool FromBytes(MBinaryReader mbr) { // Skip msg_id and msg_len as it has been read by outside. data = mbr.ReadString(data_size); return(true); }