Python/Twisted - TCP 数据包碎片?

发布于 2024-07-11 10:27:36 字数 157 浏览 8 评论 0原文

在Twisted中实现dataReceived方法时,似乎没有任何涉及数据包被分段的示例。 在所有其他语言中,这是您手动实现的东西,所以我只是想知道这是否已经为您完成了扭曲还是什么? 如果是这样,我是否需要为数据包添加长度标头前缀? 或者我必须手动执行此操作? 如果是的话,那会是什么方式呢?

In Twisted when implementing the dataReceived method, there doesn't seem to be any examples which refer to packets being fragmented. In every other language this is something you manually implement, so I was just wondering if this is done for you in twisted already or what? If so, do I need to prefix my packets with a length header? Or do I have to do this manually? If so, what way would that be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-07-18 10:27:36

在 dataReceived 方法中,您以不确定长度的字符串形式返回数据,这意味着它可能是协议中的整个消息,也可能只是某些“客户端”发送给您的消息的一部分。 您必须检查数据以查看它是否包含协议中的完整消息。

我目前正在我的一个项目中使用 Twisted 来实现协议,并决定使用 struct 模块来打包/解包我的数据。 我正在实现的协议具有固定的标头大小,因此在读取至少 HEADER_SIZE 字节量之前我不会构造任何消息。 总消息大小在此标头数据部分中声明。

我想您实际上并不需要将消息长度定义为协议的一部分,但它会有所帮助。 如果您没有定义,则必须有一个特殊的分隔符来确定消息何时开始/结束。 FIX 协议如何使用 SOH 字节来分隔字段的排序。 尽管它确实有一个必填字段告诉您消息有多长(只是不告诉您消息中有多少个字段)。

In the dataReceived method you get back the data as a string of indeterminate length meaning that it may be a whole message in your protocol or it may only be part of the message that some 'client' sent to you. You will have to inspect the data to see if it comprises a whole message in your protocol.

I'm currently using Twisted on one of my projects to implement a protocol and decided to use the struct module to pack/unpack my data. The protocol I am implementing has a fixed header size so I don't construct any messages until I've read at least HEADER_SIZE amount of bytes. The total message size is declared in this header data portion.

I guess you don't really need to define a message length as part of your protocol but it helps. If you didn't define one you would have to have a special delimiter that determines when a message begins/ends. Sort of how the FIX protocol uses the SOH byte to delimit fields. Though it does have a required field that tells you how long a message is (just not how many fields are in a message).

兔小萌 2024-07-18 10:27:36

在处理 TCP 时,您真的应该忘记所有“数据包”的概念。 TCP 是一种流协议——数据从另一端流入,数据从另一端流出。 一旦数据被发送,只要数据全部以正确的顺序到达,就可以根据需要以任意数量的块到达。 您必须像其他语言一样,使用长度字段、消息类型字段或特殊分隔符等手动进行定界。

When dealing with TCP, you should really forget all notion of 'packets'. TCP is a stream protocol - you stream data in and data streams out the other side. Once the data is sent, it is allowed to arrive in as many or as few blocks as it wants, as long as the data all arrives in the right order. You'll have to manually do the delimitation as with other languages, with a length field, or a message type field, or a special delimiter character, etc.

我的鱼塘能养鲲 2024-07-18 10:27:36

您还可以使用 LineReceiver 协议

You can also use a LineReceiver protocol

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文