Google Protocol buffers in client/server when have different packet type [closed]

三世轮回 提交于 2019-12-06 12:00:45

Pangea noted self-describing messages, but actually I'd suggest union messages, if you don't know which type may be involved, but you know the complete spectrum of types available. (It depends on what exactly you mean by "I may not know the type in advance.")

Basically both techniques are "metadata, data" approaches - it just depends on whether the metadata needs to include the message descriptor, or just a discriminator to say which message type comes next.

You can combine the two techniques, of course - create a union type where one of the type involved is a self-describing type. This would be useful if you knew what you'd normally expect, but you'd occasionally get something "different" without knowing about the message type in advance.

Look at Self-describing Messages section of the documentation.

NOTE: Jon Skeet's answer is a complete one as always.

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