使用 protobuf-net 反序列化由 google 的 protobuf 序列化的数据时出现问题

发布于 2024-10-14 09:27:52 字数 1307 浏览 4 评论 0原文

我目前正在开发在一个应用程序 (C++) 中序列化并需要在另一个应用程序 (C#) 中反序列化的代码。我正在尝试使用 google proto + protobuf-net 但出现问题。

.cc 和 .cs 消息定义文件都是使用各自的编译器从同一个 .proto 文件生成的。

数据通过 UDP 发送,消息 (~40B) 可以轻松放入单个数据报中。

在 C++ 大小上, boost::asio 用于传输数据,相关代码是:

ProtocolBufferdata data;
...
boost::asio::streambuf b;
std::ostream os(&b);
data.SerializeToOstream(&os);
m_Socket.send_to(b.data(), m_Endpoint);

我相当确定它工作正常,因为使用wireshark我至少可以看到数据报中我期望的所有字符串。在 C# 方面,使用 Begin/End 接收,我们在回调中具有以下内容:

byte[] buffer ....        

public void ReceiveData(IAsyncResult iar)
{
    try
    {
        Socket remote = (Socket)iar.AsyncState;
        int recv = remote.EndReceive(iar);
        using (MemoryStream memStream = new MemoryStream())
        {
            memStream.Write(buffer, 0, recv);
            ProtoData data = ProtoBuf.Serializer.Deserialize<ProtoData >(memStream);
            onReceive(data);
        }
    }
    catch (Exception ex)
    {
        ...
    }
    finally
    {
        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), socket);
    }
}

缓冲区中确实具有预期的字节数,并且具有指示性字符串。 protobuf-net 容器具有所有默认值。

我对这里发生的事情有点困惑,几乎不可能将调试器附加到客户端应用程序,因为它被部署为另一个不能与远程调试器很好地配合的应用程序的插件。我很感激任何建议,这让我很困惑。

I am currently working on code that serializes in one app (C++) and needs to deserialize it in another (C#). I am trying to use google proto + protobuf-net but something is failing.

Both the .cc and the .cs message definition files were generated with their respective compilers, from the same .proto file.

The data is being sent via UDP, and the messages (~40B) easily fit into a single datagram.

On the C++ size, boost::asio is being used to transmit the data, the relevant code being:

ProtocolBufferdata data;
...
boost::asio::streambuf b;
std::ostream os(&b);
data.SerializeToOstream(&os);
m_Socket.send_to(b.data(), m_Endpoint);

I am fairly sure this is working correctly, since using wireshark I can at least see all the strings I expect in the datagram. On the C# side, using Begin/End recieve, we have the following in the callback:

byte[] buffer ....        

public void ReceiveData(IAsyncResult iar)
{
    try
    {
        Socket remote = (Socket)iar.AsyncState;
        int recv = remote.EndReceive(iar);
        using (MemoryStream memStream = new MemoryStream())
        {
            memStream.Write(buffer, 0, recv);
            ProtoData data = ProtoBuf.Serializer.Deserialize<ProtoData >(memStream);
            onReceive(data);
        }
    }
    catch (Exception ex)
    {
        ...
    }
    finally
    {
        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), socket);
    }
}

The buffer does have the expected number of bytes in it, and has the tell-tale strings. The protobuf-net container has all default values.

I am a bit puzzled about what is going on here, and it is almost impossible to attach a debugger to the client application since it is deployed as a plug-in to another application that does not play well with a remote debugger. I would appreciate any advice, this has me stumped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-10-21 09:27:52

倒回流 - 它位于末尾,因此 Read 将不返回任何数据:

memStream.Write(buffer, 0, recv);
memStream.Position = 0; // <===========here
ProtoData data = ProtoBuf.Serializer.Deserialize<ProtoData>(memStream);

或者,使用重载的构造函数来准备流:

using (MemoryStream memStream = new MemoryStream(buffer, 0, recv))
{
    ProtoData data = ProtoBuf.Serializer.Deserialize<ProtoData>(memStream);

Rewind your stream - it is at the end, so Read will return no data:

memStream.Write(buffer, 0, recv);
memStream.Position = 0; // <===========here
ProtoData data = ProtoBuf.Serializer.Deserialize<ProtoData>(memStream);

alternatively, use the overloaded constructor to prepare the stream:

using (MemoryStream memStream = new MemoryStream(buffer, 0, recv))
{
    ProtoData data = ProtoBuf.Serializer.Deserialize<ProtoData>(memStream);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文