示例#1
0
    public byte[] Encode(IBaseMessage msg)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (BinaryWriter bw = new BinaryWriter(ms, new UTF8Encoding()))
            {
                byte[] msgBuffer = msg.GetByte();

                #region 封装包头
                bw.Write(t1);
                bw.Write(t2);
                #endregion

                int len = 0;
                #region 包协议
                if (msgBuffer != null)
                {
                    len = msgBuffer.Length + 4;
                    bw.Write((Int16)(len));

                    bw.Write(msg.MsgID); //数据域
                    bw.Write(msgBuffer); //数据域
                }
                else
                {
                    bw.Write((Int16)len);
                }
                #endregion

                return(ms.ToArray());
            }
        }
    }
示例#2
0
        public static IMessage ParseFrom(IBaseMessage msg)
        {
            ////反序列化操作
            //Person p = Person.Parser.ParseFrom(msg.GetByte());
            switch (msg.MsgID)
            {
            case (int)DefineProtobuf.MSG_PERSON:
                return(MsgPerson.Parser.ParseFrom(msg.GetByte()));

            case (int)DefineProtobuf.MSG_REGISTER:
                return(MsgRegister_SC.Parser.ParseFrom(msg.GetByte()));

            case (int)DefineProtobuf.MSG_HEARTBEAT:
                return(MsgHeartBeat.Parser.ParseFrom(msg.GetByte()));
            }
            Debug.LogErrorFormat("ReceiverHelper ParseFrom not find parser  !!!! msgid = {0}", msg.MsgID);
            return(null);
        }