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

欢迎加入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<byte> protobufData = DataCenter.ProtobufUtility.Serialize(protobufObject).ToList();// 消息对象序列化为二进制字节

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

            //List<byte> 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<byte>();
            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扫描二维码加群

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幻世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值