Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(207评价)
优惠后 ¥1899.00优惠前 ¥2499.00 双十一全年最大力度限时优惠
1对1 答疑即时回复7天无理由退款30天价保提问必答

服务保障

1对1 答疑

提供专属的一对一在线答疑,快速答疑解惑。

即时回复

及时响应学习咨询,帮助解决问题。

7天无理由退款

购买后7天内可申请无理由退款。

30天价保

购买后30天内如遇降价,可申请价保。

提问必答

对于提问做到有问必答,帮您扫清学习过程中的种种障碍

protobuf 序列化的问题
Lemonsuan发起了问答2022-03-17
1
回复
382
浏览
[ProtoContract]
public class LoginMsg : MsgBase
{
    [ProtoMember(1)]
    public string User { get; set; }
    [ProtoMember(2)]
    public string Pwd { get; set; }
    [ProtoMember(3)]
    public override ProtoCode Proto { get; set; }

    public LoginMsg()
    {
        Proto = ProtoCode.LoginMsg;
    }
}


 public void SendData(MsgBase msg)
    {


        try
        {
            byte[] sendArr = PackData(msg);
            ByteArray ba = new ByteArray(sendArr);
            int count = 0;
            lock (que_Write)
            {
                que_Write.Enqueue(ba);
                count = que_Write.Count;
            }
            if (count == 1)
            {
                m_Socket.BeginSend(sendArr, 0, sendArr.Length, 0, SendCallBack, m_Socket);

            }
        }
        catch (Exception e)
        {
            Debug.LogError("send msg is fail : " + e);
            Close();
        }
    }

    public byte[] PackData(MsgBase msg)
    {
        //len+prto+data
        byte[] protoByte = BitConverter.GetBytes((int)msg.Proto);
        using (var ms = new MemoryStream())
        {
            Serializer.Serialize(ms, msg);
            Debug.Log("serializer msg");
            byte[] data = ms.ToArray();
            byte[] len = BitConverter.GetBytes(data.Length + protoByte.Length);
            return len.Concat(protoByte).Concat(data).ToArray();
        }
    }

出现了send msg is fail : System.InvalidOperationException: Unexpected sub-type: LoginMsg

打了序列化标签 但是不支持LoginMsg的序列化  运行到序列化的代码时 直接跳过

 

所有回复
  • 老师_Trigger • 2022-03-18

    同学你好,可以参考一下:

    https://blog.csdn.net/weixin_30791095/article/details/97466097

    https://www.789zhao.com/blog/J8LOHZ9DORW7.html

    https://www.5axxw.com/questions/content/lrkbsi

    具体问题的话同学需要花时间再找找测试一下

    还有-5条回复,点击查看
    你还没有登录,请先登录或注册!
发表回复
你还没有登录,请先 登录或 注册!