本文介绍了Python Twisted TCP套接字如何设置TCP_NODELAY(禁用Nagle的算法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个正在进行中的实时多人游戏(目前在应用商店中:),并且我同时使用TCP和UDP发送实时消息(TCP用于需要可靠性的消息,例如玩家死亡),并且我的服务器端是用python编写的,服务器使用Twisted。我经常不得不快速发送小数据包,并且禁用数据包排队(Nagle的算法)应该有助于使数据包更快到达。

I have a real-time multiplayer game in continued development (It's currently live on the app store: https://itunes.apple.com/us/app/blewp!-eat-or-be-eaten-mmo/id996122625?mt=8), and I use both TCP and UDP for sending real-time messages (TCP for messages that need reliability, like players dieing), and my server-side is written in python, using Twisted for the server. I often have to send small packets quickly, and disabling packet queuing (Nagle's algo.) should help get packs to arrive faster.

我对twisted进行了一些研究,发现此设置在abstract.FileDescriptor下处于twisted(文档链接:)

I did some research on twisted, and found that this setting is under abstract.FileDescriptor in twisted (documentation link: http://twistedmatrix.com/documents/8.1.0/api/twisted.internet.tcp.Connection.html)

我是Twisted的新手,所以有人可以告诉我如何为TCP协议服务器实际调用此设置吗?我找不到在哪里可以访问FileDescriptor。 :(

I'm quite new to Twisted, so could someone tell me how to actually call this setting, for a TCP Protocol server? I couldn't find where the FileDescriptor is accessable. :(

示例代码行会很不错,建议总是很受欢迎;)

A line of example code would be nice, advice is always appreciated ;)

这是一个我做一个简单的例子来帮助解释这个问题:

Here is a simple example I made to help explain the question:

class TCPProtocol(Protocol):

    def connectionMade(self):
        #add to list of connected clients
        factory.clients.append(self)
        '''HELP HERE PLEASE!!!'''
        '''EG.  self.NO_DELAY=True HOW DO I SET THIS?'''


    def dataReceived(self, data):
        pass

    def sendData(self, data):
        self.transport.write(data)



#setup factory and TCP protocol class
factory = Factory()
factory.protocol = TCPProtocol
factory.clients = []
reactor.listenTCP(1959, factory)


推荐答案

协议对象的 self.transport 就是您要查找的 FileDescriptor 。这段代码应该执行您想要的操作:

A Protocol object has a self.transport that is the FileDescriptor you're looking for. This code should do what you want:

class TCPProtocol(Protocol):

    def connectionMade(self):
        # add to list of connected clients
        factory.clients.append(self)
        self.transport.setTcpNoDelay(True)

这篇关于Python Twisted TCP套接字如何设置TCP_NODELAY(禁用Nagle的算法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 16:27