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

(197评价)
价格: 4431.00元
protobuf 序列化的问题
Lemonsuan发起了问答2022-03-17
1
回复
261
浏览
[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条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!