Exemplo n.º 1
0
    def readTick(self):
        if self.mode == constants.MODE_SERVER:
            loadingTimeout = self.connectionTimeout * 2
            for client in list(self.activeConnections.values()):
                if time.time() - client.lastPacketTime > (
                        self.connectionTimeout
                        if client.ready else loadingTimeout):
                    del self.activeConnections[client.address]
                    if self.disconnectCallback is not None:
                        self.disconnectCallback(client.address)
        elif self.mode == MODE_CLIENT:
            if self.clientConnected:
                if self.disconnectCallback is not None and time.time(
                ) - self.hostConnection.lastPacketTime > self.connectionTimeout:
                    self.disconnectCallback(self.hostConnection.address)
                    self.clientConnected = False
            else:
                if self.connectionAttempts < 10:
                    if time.time() - self.lastConnectionAttempt > 0.5:
                        self.clientConnect(self.clientUsername)
                        self.connectionAttempts += 1
                        self.lastConnectionAttempt = time.time()
                else:
                    self.connectionAttempts = 0
                    self.disconnectCallback(self.hostConnection.address)

        readQueue = []
        while True:
            try:
                message, address = self.socket.recvfrom(1024)
            except socket.error:
                return readQueue

            if not message:
                continue

            if address in self.activeConnections:
                self.activeConnections[address].lastPacketTime = time.time()

            try:
                message = zlib.decompress(message)
            except zlib.error:
                continue

            iterator = PyDatagram(message)
            if not iterator.getRemainingSize():
                continue

            code = Uint8.getFrom(iterator)
            if code == constants.PACKET_HOSTLIST:
                numHosts = Uint16.getFrom(iterator)
                hosts = []
                for _ in range(numHosts):
                    ip = String.getFrom(iterator)
                    port = Uint16.getFrom(iterator)
                    user = String.getFrom(iterator)
                    map = String.getFrom(iterator)
                    activePlayers = Uint8.getFrom(iterator)
                    playerSlots = Uint8.getFrom(iterator)
                    hosts.append((user, map, ip + ":" + str(port),
                                  activePlayers, playerSlots))
                #engine.log.debug("Received " + str(numHosts) + " hosts from lobby server.")
                if self.hostListCallback is not None:
                    self.hostListCallback(hosts)
            if self.mode == constants.MODE_SERVER:
                if code == constants.PACKET_NEWCLIENTNOTIFICATION:
                    ip = String.getFrom(iterator)
                    port = Uint16.getFrom(iterator)
                    clientAddress = (ip, port)
                    self.connectionAttempts = 0
                    #engine.log.info("Received notification from lobby server of new client " + ip + ":" + str(port))
                    self.serverConnect(clientAddress)
                elif code == constants.PACKET_DISCONNECT:
                    if address in self.activeConnections:
                        del self.activeConnections[address]
                elif code == constants.PACKET_CLIENTREADY:
                    if address in self.activeConnections:
                        self.activeConnections[address].ready = True
            elif self.mode == MODE_CLIENT and address == self.hostConnection.address:
                self.hostConnection.lastPacketTime = time.time()
            readQueue.append((message, address))
        return readQueue