コード例 #1
0
ファイル: LoginBusiness.cs プロジェクト: junfff/mygame
        private void OnSendButton(object param)
        {
            if (param is string)
            {
                string str = (string)param;
                if (string.IsNullOrEmpty(str))
                {
                    Debug.LogErrorFormat("OnSendButton str IsNullOrEmpty");
                    return;
                }

                MsgPerson p = new MsgPerson();
                p.Name  = "huangqiaoping_hahaha";
                p.Email = "*****@*****.**";
                p.Id    = 222;

                IBaseMessage msg = ReceiverHelper.PopMessage();
                msg.WriteIn(p, DefineProtobuf.MSG_PERSON);
                CoreModules.netMDL.SendMessage(msg);
                Debug.LogErrorFormat("OnSendButton str = {0}", str);
            }
        }
コード例 #2
0
    public List <IBaseMessage> Decode(byte[] buff, int len)
    {
        //拷贝本次的有效字节
        byte[] _b = new byte[len];
        Array.Copy(buff, 0, _b, 0, _b.Length);
        buff = _b;
        if (this._LBuff.Count > 0)
        {
            //拷贝之前遗留的字节
            this._LBuff.AddRange(_b);
            buff = this._LBuff.ToArray();
            this._LBuff.Clear();
        }

        List <IBaseMessage> list = new List <IBaseMessage>();

        using (MemoryStream ms = new MemoryStream(buff))
        {
            using (BinaryReader buffers = new BinaryReader(ms, this.utf8))
            {
                try
                {
                    byte[] _buff;

                    while (true)
                    {
                        #region 包头读取
                        //循环读取包头
                        //判断本次解析的字节是否满足常量字节数
                        if ((buffers.BaseStream.Length - buffers.BaseStream.Position) < ConstLenght)
                        {
                            _buff = buffers.ReadBytes((int)(buffers.BaseStream.Length - buffers.BaseStream.Position));
                            this._LBuff.AddRange(_buff);
                            return(list);
                        }
                        byte tt1 = buffers.ReadByte();
                        byte tt2 = buffers.ReadByte();
                        byte tt3 = buffers.ReadByte();
                        byte tt4 = buffers.ReadByte();
                        if (!(tt1 == t1 && tt3 == t2))
                        {
                            Debug.LogErrorFormat("tt1 = {0},tt2 = {1}", tt1.ToString("x8"), tt2.ToString("x8"));
                            Debug.LogErrorFormat("tt3 = {0},tt4 = {1}", tt3.ToString("x8"), tt4.ToString("x8"));
                            long ttttt = buffers.BaseStream.Seek(-3, SeekOrigin.Current);
                            continue;
                        }
                        else
                        {
                            //Debug.LogErrorFormat("成功 包头!!!");
                        }
                        #endregion

                        #region 包协议
                        int offset = buffers.ReadInt16();
                        //Debug.LogErrorFormat("包协议,长度是 offset={0}",offset);
                        #endregion

                        #region 包解析
                        //剩余字节数大于本次需要读取的字节数
                        if (offset <= (buffers.BaseStream.Length - buffers.BaseStream.Position))
                        {
                            int msgID = buffers.ReadInt32();
                            //Debug.LogErrorFormat("msgID= {0}",msgID);
                            _buff = buffers.ReadBytes(offset - 4);

                            IBaseMessage msg = ReceiverHelper.PopMessage();
                            msg.WriteIn(msgID, _buff, 0, _buff.Length);
                            list.Add(msg);
                            if ((buffers.BaseStream.Length - buffers.BaseStream.Position) > 0)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            //剩余字节数刚好小于本次读取的字节数 存起来,等待接受剩余字节数一起解析
                            _buff = buffers.ReadBytes((int)(buffers.BaseStream.Length - buffers.BaseStream.Position + ConstLenght));
                            this._LBuff.AddRange(_buff);
                        }
                        #endregion

                        break;
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogErrorFormat("MarshalEndian Decode error: {0}", ex);
                }
            }
        }
        return(list);
    }