示例#1
0
    def _test_connect(self, client_version=None, server_version=None):
        server = FakeServer(minecraft_version=server_version)
        addr, port = server.listen_socket.getsockname()

        cond = threading.Condition()

        def handle_client_exception(exc, exc_info):
            with cond:
                cond.exc_info = exc_info
                cond.notify_all()

        def client_write(packet, *args, **kwds):
            def packet_write(*args, **kwds):
                logging.debug('[C-> ] %s' % packet)
                return real_packet_write(*args, **kwds)
            real_packet_write = packet.write
            packet.write = packet_write
            return real_client_write(packet, *args, **kwds)

        def client_react(packet, *args, **kwds):
            logging.debug('[ ->C] %s' % packet)
            return real_client_react(packet, *args, **kwds)

        client = connection.Connection(
            addr, port, username='******', initial_version=client_version,
            handle_exception=handle_client_exception)
        real_client_react = client._react
        real_client_write = client.write_packet
        client.write_packet = client_write
        client._react = client_react

        try:
            with cond:
                server_thread = threading.Thread(
                    name='_ConnectTest server',
                    target=self._test_connect_server,
                    args=(server, cond))
                server_thread.daemon = True
                server_thread.start()

                self._test_connect_client(client, cond)

                cond.exc_info = Ellipsis
                cond.wait(THREAD_TIMEOUT_S)
                if cond.exc_info is Ellipsis:
                    self.fail('Timed out.')
                elif cond.exc_info is not None:
                    raise_(*cond.exc_info)
        finally:
            # Wait for all threads to exit.
            for thread in server_thread, client.networking_thread:
                if thread is not None and thread.is_alive():
                    thread.join(THREAD_TIMEOUT_S)
                if thread is not None and thread.is_alive():
                    if cond.exc_info is None:
                        self.fail('Thread "%s" timed out.' % thread.name)
                    else:
                        # Keep the earlier exception, if there is one.
                        break
示例#2
0
    def run_connection(self):
        try:
            auth = self.authenticate()
        except Exception as e:
            if not isinstance(e, NO_TRACE_ERRORS):
                traceback.print_exc()
            return self.disconnect(e)

        with self.rlock:
            if auth is None:
                kwds = {'username': self.uname}
                self.profile_name = self.uname
            else:
                kwds = {'auth_token': auth}
                self.profile_name = auth.profile.name

            def conn_exception(exc, exc_info):
                with self.rlock:
                    if conn is self.connection:
                        if not isinstance(exc, NO_TRACE_ERRORS):
                            traceback.print_exception(*exc_info)
                        self.disconnect(exc)

            allowed_versions = None if self.version is None else {self.version}
            conn = connection.Connection(self.host,
                                         self.port,
                                         allowed_versions=allowed_versions,
                                         handle_exception=conn_exception,
                                         **kwds)
            for plugin in (self, ) + tuple(self.plugins or ()):
                plugin.install(conn)
            if self.show_packets:
                conn.write_packet = partial(self.show_send_packet,
                                            cont=conn.write_packet)
            self.connection = conn
            self.keep_alive = True

        try:
            conn.connect()
        except BaseException as e:
            if not isinstance(e, NO_TRACE_ERRORS):
                traceback.print_exc()
            return self.disconnect(e)

        if self.prevent_timeout:
            thread = Thread(target=self.run_prevent_timeout,
                            name='Connection.run_prevent_timeout')
            thread.daemon = True
            thread.start()

        if self.no_timeout: return
        while conn is self.connection:
            with self.rlock:
                if not self.keep_alive:
                    self.disconnect('Timed out (%ss).' % KEEPALIVE_TIMEOUT_S)
                    return
                self.keep_alive = False
                self.disconnect_cond.wait(KEEPALIVE_TIMEOUT_S)
示例#3
0
    def _test_connect(self, client_version, server_version):
        server = FakeServer(minecraft_version=server_version)
        addr, port = server.listen_socket.getsockname()
        client = connection.Connection(addr,
                                       port,
                                       username='******',
                                       initial_version=client_version)

        cond = threading.Condition()
        try:
            with cond:
                server_thread = threading.Thread(
                    name='test_connection server',
                    target=self._test_connect_server,
                    args=(server, cond))
                server_thread.daemon = True
                server_thread.start()

                client_thread = threading.Thread(
                    name='test_connection client',
                    target=self._test_connect_client,
                    args=(client, cond))
                client_thread.daemon = True
                client_thread.start()

                cond.wait()
                if cond.exc_info is not None:
                    raise_(*cond.exc_info)
        finally:
            # Wait for all threads to exit.
            for thread in server_thread, client_thread:
                if thread.is_alive():
                    thread.join(THREAD_TIMEOUT_S)
                if thread.is_alive():
                    if cond.exc_info is None:
                        self.fail('Thread "%s" timed out.' % thread.name)
                    else:
                        # Keep the earlier exception, if there is one.
                        break
示例#4
0
    def _test_connect(self,
                      client_versions=None,
                      server_version=None,
                      client_handler_type=None,
                      compression_threshold=None):
        if client_versions is None:
            client_versions = self.client_versions
        if server_version is None:
            server_version = self.server_version
        if compression_threshold is None:
            compression_threshold = self.compression_threshold
        if client_handler_type is None:
            client_handler_type = self.client_handler_type

        server = FakeServer(minecraft_version=server_version,
                            compression_threshold=compression_threshold,
                            client_handler_type=client_handler_type)
        addr = "localhost"
        port = server.listen_socket.getsockname()[1]

        cond = threading.Condition()
        server_lock = threading.Lock()
        server_exc_info = [None]
        client_lock = threading.Lock()
        client_exc_info = [None]

        def handle_client_exception(exc, exc_info):
            with client_lock:
                client_exc_info[0] = exc_info
            with cond:
                cond.notify_all()

        client = connection.Connection(
            addr,
            port,
            username='******',
            allowed_versions=client_versions,
            handle_exception=handle_client_exception)
        client.register_packet_listener(
            lambda packet: logging.debug('[ ->C] %s' % packet),
            packets.Packet,
            early=True)
        client.register_packet_listener(
            lambda packet: logging.debug('[C-> ] %s' % packet),
            packets.Packet,
            early=True,
            outgoing=True)

        server_thread = threading.Thread(name='FakeServer',
                                         target=self._test_connect_server,
                                         args=(server, cond, server_lock,
                                               server_exc_info))
        server_thread.daemon = True

        errors = []
        try:
            try:
                with cond:
                    server_thread.start()
                    self._start_client(client)
                    cond.wait(THREAD_TIMEOUT_S)
            finally:
                # Wait for all threads to exit.
                server.stop()
                for thread in server_thread, client.networking_thread:
                    if thread is not None and thread.is_alive():
                        thread.join(THREAD_TIMEOUT_S)
                    if thread is not None and thread.is_alive():
                        errors.append(
                            {'msg': 'Thread "%s" timed out.' % thread.name})
        except Exception:
            errors.insert(0, {
                'msg': 'Exception in main thread',
                'exc_info': sys.exc_info()
            })
        else:
            timeout = True
            for lock, [exc_info
                       ], thread_name in ((client_lock, client_exc_info,
                                           'client thread'),
                                          (server_lock, server_exc_info,
                                           'server thread')):
                with lock:
                    if exc_info is None:
                        continue
                    if not issubclass(exc_info[0], FakeServerTestSuccess):
                        errors.insert(
                            0, {
                                'msg': 'Exception in %s:' % thread_name,
                                'exc_info': exc_info
                            })
                    timeout = False
            if timeout:
                errors.insert(0, {'msg': 'Test timed out.'})

        if len(errors) > 1:
            for error in errors:
                logging.error(**error)
            self.fail('Multiple errors: see logging output.')
        elif errors and 'exc_info' in errors[0]:
            raise_(*errors[0]['exc_info'])
        elif errors:
            self.fail(errors[0]['msg'])
示例#5
0
 def test_connection(self):
     connection = conn.Connection("localhost", 25565, None)
     self.assertFalse(connection.options.compression_enabled)