开发者

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

开发者 https://www.devze.com 2023-04-09 05:51 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help cla开
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help cla开发者_开发问答rifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I want to use protocol buffers in tcp/ip communication, but i may have different packets to send/receive, how can i differentiate the package types over protocol buffers, it seems I have to know the next packet type to use protocol buffer, but in my case, i may don't know the type in advance.

any idea? is protocol buffers not good for this kind of communication?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号