具有不同数据包类型时客户端/服务器中的 Google 协议缓冲区 [英] Google Protocol buffers in client/server when have different packet type

查看:30
本文介绍了具有不同数据包类型时客户端/服务器中的 Google 协议缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 tcp/ip 通信中使用协议缓冲区,但我可能有不同的数据包要发送/接收,我如何通过协议缓冲区区分包类型,看来我必须知道下一个使用协议的数据包类型缓冲区,但就我而言,我可能事先不知道类型.

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 注意到自描述消息,但实际上我建议 联合消息,如果您不知道可能涉及哪种类型,但您知道可用类型的完整范围.(这取决于我可能事先不知道类型"的确切含义.)

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.

这篇关于具有不同数据包类型时客户端/服务器中的 Google 协议缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆