Google Protocol buffers in client/server when have different packet type [closed]
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.
精彩评论