为什么我的 protobuf-net 流不起作用? [英] Why does my protobuf-net stream not work?

查看:45
本文介绍了为什么我的 protobuf-net 流不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以序列化和反序列化的对象,但是在反序列化时它会抛出一个错误:

I have an object that can be serialized and deserialized but upon deserialization it throws me an error:

源数据中的字段无效:0

我不知道为什么会这样

反序列化和接收代码:

public void listenUDP()
{
        EndPoint ep = (EndPoint)groupEP;
        //BinaryFormatter bf = new BinaryFormatter();
        recieving_socket.Bind(ep);
        while (true)
        {

            byte[] objData = new byte[65535];
            recieving_socket.ReceiveFrom(objData, ref ep);
            MemoryStream ms = new MemoryStream();
            ms.Write(objData, 0, objData.Length);
            ms.Seek(0, SeekOrigin.Begin);

            messageHandle(ProtoBuf.Serializer.Deserialize<SimplePacket>(ms));
            ms.Dispose();


        }
    }

序列化代码:

public void sendDataUDP(Vec2f[] data)
    {

            SimplePacket packet = new SimplePacket(DateTime.UtcNow, data);
            //IFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            System.Diagnostics.Stopwatch st = System.Diagnostics.Stopwatch.StartNew();
            //formatter.Serialize(stream, data);
            ProtoBuf.Serializer.Serialize<SimplePacket>(stream, packet);
            //Console.WriteLine(st.ElapsedTicks);
            stream.Close();
            st.Restart();
            sending_socket.SendTo(stream.ToArray(), sending_end_point);
            //Console.WriteLine(st.ElapsedTicks);
            st.Stop();

    }

推荐答案

谷歌规范定义的 protobuf 消息中的根对象不包括消息结束的任何概念.这是有意为之,因此串联等同于合并两个片段.因此,消费代码需要将自身限制为单个消息.这在所有 protobuf 实现中都是相同的,并且不特定于 protobuf-net.

The root object in a protobuf message, as defined by the google specification, does not include any notion of the end of the message. This is intentional, so that concatenation is identical to merging two fragments. Consequently, the consuming code needs to restrict itself to a single message. This is identical between all protobuf implementations, and is not specific for protobuf-net.

发生的情况是您的缓冲区当前过大,最后是垃圾.目前(因为您正在阅读一条消息)垃圾很可能全为零,并且零不是字段的有效标记.但是,当重新使用缓冲区时,垃圾可能是……任何东西.

What is happening is that your buffer is currently oversized, with garbage at the end. Currently (because you are reading one message) that garbage is most likely all zeros, and a zero is not a valid marker for a field. However, when re-using the buffer the garbage could be... anything.

在您的情况下,可能最简单的方法是使用 SerializeWithLengthPrefix/DeserializeWithLengthPrefix 方法,它们通过在消息开始,只处理那么多数据.

In your case, probably the easiest way to do this is to use the SerializeWithLengthPrefix / DeserializeWithLengthPrefix methods, which handle all this for you by prepending the payload length at the start of the message, and only processing that much data.

最后一个想法:我不清楚您的代码是否能保证读取整个消息;单个接收可以(至少在 TCP 上)返回消息的一部分 - 或 2 条消息,等等:TCP 是基于流的,而不是基于消息的.

As a final thought: it is not clear to me that your code will guarantee that is has read an entire message; a single receive could (on TCP, at least) return part of a message - or 2 and a bit messages, etc: TCP is stream-based, not message-based.

这篇关于为什么我的 protobuf-net 流不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆