使用 UDP 时出现巨大延迟。为什么?
我开发了一款 MMO 游戏,当很多人连接时,我遇到了很多延迟。我正在使用 UDP 和 TCP 数据包。 TCP 似乎工作得很好,UDP 有时会延迟 5 秒。可能是什么原因造成的?我正在使用 MongoDB,每个客户端每秒发送数据包 30 次。谢谢。
I have build an MMO game and I'm having a lot of lags when lot of people are connected. I'm using UDP and TCP packets. the TCP seeme to work really good, throgh the UDP comes after a delay of 5 seconds sometimes. What may cause this? I'm using MongoDB and sending packets 30 times per second per client. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UDP 协议不为包裹传送提供保证。所以你的问题可能就在于此。 http://en.wikipedia.org/wiki/User_Datagram_Protocol
UDP protocol does not provide guarantie for the package delivery. So your problem may be in this. http://en.wikipedia.org/wiki/User_Datagram_Protocol
疯狂猜测:您的 UDP 数据包接收路径涉及分配内存,并且您泄漏了该内存。接收到的数据包越多,分配内存所需的时间就越长。
Wild guess: your UDP packet receive path involves allocating memory, and you leak that memory. The more packets are received, the longer it takes to allocate memory.
我也在这里猜测,但情况可能是您的应用程序每 n 毫秒轮询一次 UDP 数据包(例如,如果您的 FPS 是 20,那么 n 将是 50 毫秒)。
现在的猜测是,在此期间,您的 PC 收到了大量 UDP 数据包,并且它们被您的操作系统堆积起来。然后,当您轮询这些数据包时,您或操作系统都不会立即清除堆栈。更大的堆栈可能意味着更大的延迟。
我想我在某处读过,甚至有些路由器也试图变得智能并堆叠 UDP 数据包。但我无法找到我看到它的页面。
我在这里发布了类似的问题:
Router gets unresponsive while using UDP
和以上是我目前的理论:),不幸的是我还不知道如何解决它。我想到的一种可能性是使用一个单独的线程,它可以立即对网络请求做出反应,而不是每 n 毫秒轮询一次网络。
I'm also guessing here, but the situation could be that your application is polling for UDP packets every n milliseconds (like, if your FPS is 20 then n would be 50ms).
Now the guess is that during this time your PC receives a lot of UDP packets and they get stacked up by your operating system. Then, when you get to poll for these packets, either you or the operating system don't clear the stack at once. A bigger stack might then mean bigger lag.
I think I've read somewhere that even some routers try to be smart and stack up UDP packets. But I'm unable to find the page where I saw it.
I've posted a similar question here:
Router gets unresponsive when using UDP
and the above is my current theory :), unfortunately I don't yet know how to work around it. One possibility that comes to my mind is to use a separate thread that would react instantly to network requests instead of polling the network every n ms.