您当前的位置: 首页 >  网络

幻世界

暂无认证

  • 0浏览

    0关注

    237博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity手游网络通讯 Protobuf的逻辑《发送消息》

幻世界 发布时间:2019-02-20 15:40:59 ,浏览量:0

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

上一篇记录了一下接收消息的基本思路,今天再接着记录一下发送消息的思路  仅供仅参考

1.消息对象赋值

 Debug.Log("******发起进攻请求******");
        var r = new CMD_CR_Attack();
        r.userId = PlayerManager.GetInstance.UserID;
        r.friendType = friendType;
        r.friendIndex = friendIndex;
        r.buildingType = id;
        NetworkManager.GetInstance.SendMessage(r, Protocol.S_GAME.MDM_GAME, Protocol.S_GAME.CMD_GAME.SUB_C_ATTACK);

2.将定义打包好的消息对象发给服务器

 public void SendMessage(object messageObject, int MdmNum, int CmdNum)
    {
        byte[] messageBytes = _packetBuilder.Build(messageObject, MdmNum, CmdNum);
      
        _client.SendMessageToServer(messageBytes);

        //Debug.Log("Send:" + MdmNum + "/" + CmdNum);

    }

3.打包消息为二进制数据

 public byte[] Build(object protobufObject, int MdmNum, int CmdNum)
        {
            if (protobufObject == null)
            {
                UnityEngine.Debug.LogError("Error: protobuf class and name can not be null!");
                return null;
            }
            List protobufData = DataCenter.ProtobufUtility.Serialize(protobufObject).ToList();// 消息对象序列化为二进制字节

            List protobufList = new List();//存储所有字节的字节表

            //List versionBuff = BitConverter.GetBytes((byte)102).ToList();//版本号
            protobufList.Add((byte)102);

            protobufList.Add((byte)((protobufData.Count + 12) & 0x00FF));
            protobufList.Add((byte)((protobufData.Count + 12) >> 8 & 0x00FF));
            protobufList.Add((byte)((protobufData.Count + 12) >> 16 & 0x00FF));

            protobufList.Add((byte)(MdmNum & 0x00FF));
            protobufList.Add((byte)(MdmNum >> 8 & 0x00FF));
            protobufList.Add((byte)(CmdNum & 0x00FF));
            protobufList.Add((byte)(CmdNum >> 8 & 0x00FF));       
            for (int i = 0; i < 4; i++)
            {
                protobufList.Add((byte)0);
            }

            protobufList.AddRange(protobufData.GetRange(0, protobufData.Count));       
            var Data = protobufList.ToArray();
            return Data;
        }

4.消息对象序列化

  public byte[] Serialize(object data)
    {
        byte[] buffer = null;
        using (MemoryStream m = new MemoryStream())
        {
            Serializer.Serialize(m, data);
            m.Position = 0;
            int len = (int)m.Length;
            buffer = new byte[len];
            m.Read(buffer, 0, len);
        }
        return buffer;
    }

5.发送消息给服务器

  public void SendMessageToServer(byte[] data)
        {
            if (this.scoket == null)
            {
                if (this.ReconnectHandler != null)
                {
                    this.ReconnectHandler(this, new EventArgs());
                }
                return;
            }
            if (!this.scoket.Connected)
            {
                if (this.ReconnectHandler != null)
                {
                    this.ReconnectHandler(this, new EventArgs());
                }
                return;
            }
            this.scoket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(this.SendMessageToServerComplete), this.scoket);
        }

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

关注
打赏
1660704426
查看更多评论
立即登录/注册

微信扫码登录

0.0387s