Twisted中的多个响应

时间:2013-01-29 17:00:15

标签: python networking twisted

我正在尝试使用Twisted和Pygame开发简单的TCP,clinet / server游戏,但是我在向客户端发送数据时遇到了困难。 Twisted不允许我连续发送多个响应。这就是我想要做的事情:

我有方法处理玩家状态更改并将其重新发送给其他客户:

def handle_stateChanged(self, data):
    #get playerState from client and override local copy of player
    #check if all players are ready
    #if needed, change gameState form 'inLOBBY' to 'inGAME'
    #if gameState == 'inGAME', start 'proceed' method (see below)
    #send message about player and game state to others

proceed方法(使用LoopingCall称为每1s / 30)只需计算所有游戏内容并将其发送给玩家。这两个功能相互阻塞,它们分别工作得很好,但是当它们一起工作时,只有来自其中一个的数据到达目的地。这样的事情也行不通:

def dataRecived(self, data):
    ...
    sendData(data1) #only data1 is delivered
    sendData(data2)
    ...  

我不知道这是TCP的工作方式还是我对Twisted缺乏了解。 我应该如何向客户端和后台处理用户输入发送更新?

编辑:

class Server(Protocol):
    def __init__(self, factory):
        self.factory = factory
        self.player = None #when connection is made here goes my player class
        self.world = factory.world
        ...
        self.factory.loop = LoopingCall(self.proceed)

    def dataReceived(self, data):
        data = pickle.loads(data)
        #this calls 'handle_stateChanged'
        getattr(self, 'handle_{}'.format(data['header']))(data) #data[header] = 'stateChanged'

    def handle_stateChanged(self, data):      
        isReady = data['isReady']
        self.player.isReady = isReady

        if isReady:
            count = 0
            for connection in self.factory.connections.values():
                if connection.player.isReady:
                    count += 1

            if count == len(self.factory.connections) and count > 1 and self.world.state == 'inLOBBY':
                self.world.state = 'inGAME'
                self.world.playersDistribution(self.factory.connections)
                self.factory.loop.start(1 / 30)

        data['state'] = self.world.state
        data['players'] = self.getPlayers()        
        self.sendToOthers(data)

    def sendToOthers(self, data, omitId = None):
        connections = self.factory.connections

        for key in connections.keys():
            if key != omitId:
                connections[key].sendData(data)

    def proceed(self):
        #It's only a prototype of my method. 
        #The point is that the client keep getting
        #'test' and data from self.sendToOthers(data) in handle_stateChanged
        #is not being delivered even if the method (handle_stateChanged) is called

        if self.world.state != 'inGAME':
            return

        data = {'header' : 'message', 'body' : 'test'}
        #When i comment this out, handle_stateChanged works fine and sends data
        self.sendToOthers(data)

class ServerFactory(Factory):

    def __init__(self, world):
        self.world = world
        self.connections = {}

    def buildProtocol(self, addr):
        return Server(self)

1 个答案:

答案 0 :(得分:4)

您的服务器中存在远程任意代码执行漏洞。

在极少数情况下 - 如果有的话 - 你应该取消从网络收到的数据。这样做允许任何对等方劫持您的服务器以实现任意,可能是恶意的目的。 Note the big red box in the pickle documentation

除了这个严重的安全问题之外,你所解决的第一个数据被解释的问题可能是由于两个数据在遍历网络时连接在一起。您的接收代码没有适当的框架支持,因此它无法分辨出两条消息。碰巧的是,pickle会从第一条消息中加载数据,然后忽略代表第二条消息的额外数据,从而有效地将数据丢弃在地板上。

如果你切换到一个更具表现力的协议(比传输非成帧泡菜字符串的TCP更具表现力),安全问题和框架问题都可以解决,例如AMP