interleave protobuf-net and file

北慕城南 提交于 2019-12-24 06:44:23

问题


I need to exchange both protobuf-net objects and files between computers and am trying to figure out the best way to do that. Is there a way for A to inform B that the object that follows is a protobuf or a file? Alternately, when a file is transmitted, is there a way to know that the file has ended and the Byte[] that follows is a protobuf?

Using C# 4.0, Visual Studio 2010

Thanks, Manish


回答1:


This has nothing to do with protobuf or files, and everything to do with your comms protocol, specifically "framing". This means simply: how you demark sub-messages in a single stream. For example, if this is a raw socket you might choose to send (all of)

  • a brief message-type, maybe a byte: 01 for file, 02 for a protobuf message of a particular file
  • a length prefix (typically 4 bytes network-byte-order)
  • the payload, consisting of the previous number of bytes

Then rinse and repeat for each message.

You don't state what comms you are asking, so I can be more specific.

Btw, another approach would be to treat a file as a protobuf message with a byte[] member - mainly suitable for small files, though



来源:https://stackoverflow.com/questions/10672895/interleave-protobuf-net-and-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!