예제 #1
0
    def datagramReceived(self, server_response, server_info):
        try:
            if self.logger.isEnabledFor(logging.DEBUG):
                self.logger.debug("Packet received: \n" + server_response)

            packet = Packet(server_response)

            first_long = packet.get_long()
            first_byte = packet.get_byte()

            if first_long == -1 and (first_byte == ord('m') or first_byte == ord('I')):
                self.recv_a2s_info(server_response, server_info)
                return

            if first_byte == self.a2s_challenge_reply_byte:
                self.recv_a2s_challenge(server_response, server_info)
                return

            if first_byte == self.a2s_player_reply_byte:
                self.recv_a2s_player(server_response, server_info)
                return
        except struct.error:
            if self.logger.isEnabledFor(logging.DEBUG):
                self.logger.debug("malformed packet received: \n" + server_response)
예제 #2
0
    def send_a2s_player(self, host, port):
        if self.server_dict[(str(host) + ":" + str(port))]['challenge'] is None:
            self.send_a2s_challenge(host, port)
            return

        packet = Packet()
        packet.put_long(self.whole_packet)
        packet.put_byte(self.a2s_player_query_byte)
        packet.put_long(self.server_dict[(str(host) + ":" + str(port))]['challenge'])
        self.transport.write(packet.getvalue(), (host, port))

    def recv_a2s_player(self, server_response, (host, port)):
        packet = Packet(server_response)
        packet.get_long()
        packet.get_byte()
        num_players = packet.get_byte()

        self.server_dict[(str(host) + ":" + str(port))]['players'] = []

        try:
            for _ in xrange(num_players):
                player = dict()
                player['index'] = packet.get_byte()
                player['name'] = packet.get_string()
                player['kills'] = packet.get_long()
                player['time'] = packet.get_float()
                self.server_dict[(str(host) + ":" + str(port))]['players'].append(player)
        except struct.error:
            if self.logger.isEnabledFor(logging.DEBUG):
                self.logger.debug("malformed packet received: \n" + server_response)