使用 protobuf-net 反序列化由 google 的 protobuf 序列化的数据时出现问题
我目前正在开发在一个应用程序 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
倒回流 - 它位于末尾,因此
Read
将不返回任何数据:或者,使用重载的构造函数来准备流:
Rewind your stream - it is at the end, so
Read
will return no data:alternatively, use the overloaded constructor to prepare the stream: