Google Protocol Buffers, how to handle multiple message-Types?

爷,独闯天下 提交于 2019-12-05 19:11:32

We cannot determine if the file contains a Address or a User. Because there is not type information encoded in the data.

To handle multiple Message-Types, you can use meta data like:

  • Extension of the filename
  • Headers in HTTP
  • Specific frame header in frame base stream protocol
  • ...

Either you:

  1. Include the information whether this is an address or a name in the file-ending (if files only contain one type at a time)
  2. Explicitly specify the type of the packet in the serialized form, i.e. add a field required int32 type and deduce the type from that field. (if both types are included in one file at a time)
  3. Devise a specific outer message format that explicitly includes this information and wraps the protocol buffer.

Whichever is appropriate, if you multiplex them onto one channel - which you do by choosing the same file-ending - you have to demultiplex them again when you receive them.

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