Nagle-Like问题 [英] Nagle-Like Problem

查看:138
本文介绍了Nagle-Like问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个实时游戏,使用 SFML库,带有禁用nagle的C ++服务器,并且使用 asyncsocket 的客户端也会禁用nagle.我每1秒发送30个数据包.从客户端发送到服务器没有问题,但是从服务器发送到客户端时,某些数据包正在迁移.例如,如果我以完全不同的数据包发送"a"和"b",则客户端将其读取为"ab".一次只发生一次,但在游戏中却是一个真正的问题.

so I have this real-time game, with a C++ sever with disabled nagle using SFML library , and client using asyncsocket, also disables nagle. I'm sending 30 packets every 1 second. There is no problem sending from the client to the server, but when sending from the server to the clients, some of the packets are migrating. For example, if I'm sending "a" and "b" in completly different packets, the client reads it as "ab". It's happens just once a time, but it makes a real problem in the game.

那我该怎么办?我该如何解决?也许是服务器上的东西?也许是操作系统设置?

So what should I do? How can I solve that? Maybe it's something in the server? Maybe OS settings?

要清楚:我不使用nagle,但仍然存在此问题.我在客户端和服务器上都禁用了.

To be clear: I AM NOT using nagle but I still have this problem. I disabled in both client and server.

推荐答案

您必须在两个同级中都禁用Nagle .您可能想要找到其他基于记录的协议,例如SCTP.

You have to disable Nagle in both peers. You might want to find a different protocol that's record-based such as SCTP.

既然您要的是协议,这就是我的操作方式:

Since you are asking for a protocol here's how I would do it:

  • 定义消息的标题.假设我会选择一个32位的标头.

  • Define a header for the message. Let's say I would pick a 32 bits header.

Header:
    MSG Length: 16b
    Version: 8b
    Type: 8b

  • 然后真正的消息进入了,具有MSG Length个字节.

  • Then the real message comes in, having MSG Length bytes.

    现在我有了格式,我将如何处理事情?

    So now that I have a format, how would I handle things ?

    当我写一条消息时,我会先添加控制信息(长度是最重要的),然后将整个内容发送出去.启用或不启用NODELAY都没关系.

    When I write a message, I prepend the control information (the length is the most important, really) and send the whole thing. Having NODELAY enabled or not makes no difference.

    我不断从服务器接收东西,对吧?所以我必须做某种read.

    I continuously receive stuff from the server, right ? So I have to do some sort of read.

    • 从服务器读取字节.任何数量都可以到达.继续阅读,直到至少有4个字节为止.
    • 一旦有了这4个字节,就将它们解释为标题并提取MSG Length
    • 继续读取,直到至少有MSG Length个字节为止.现在您已经收到您的消息并可以对其进行处理
    • Read bytes from the server. Any amount can arrive. Keep reading until you've got at least 4 bytes.
    • Once you have these 4 bytes, interpret them as the header and extract the MSG Length
    • Keep reading until you've got at least MSG Length bytes. Now you've got your message and can process it

    无论TCP选项(例如NODELAY),MTU限制等如何,该方法均有效.

    This works regardless of TCP options (such as NODELAY), MTU restrictions, etc.

    这篇关于Nagle-Like问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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