Пример #1
0
    async def handle_payload(self,
                             handle_nr,
                             method=None,
                             data=None,
                             fault=None):
        """
		Handle a callback/response payload or fault.
		
		:param handle_nr: Handler ID
		:param method: Method name
		:param data: Parsed payload data.
		:param fault: Fault object.
		"""
        if handle_nr in self.handlers:
            await self.handle_response(handle_nr, method, data, fault)
        elif method and data is not None:
            if method == 'ManiaPlanet.ModeScriptCallbackArray':
                await self.handle_scripted(handle_nr, method, data)
            else:
                await self.handle_callback(handle_nr, method, data)
        elif fault is not None:
            raise TransportException(
                'Handle payload got invalid parameters, see fault exception! {}'
                .format(fault)) from fault
        else:
            print(method, handle_nr, data)
            logging.warning(
                'Received gbx data, but handle wasn\'t known or payload invalid: handle_nr: {}, method: {}'
                .format(
                    handle_nr,
                    method,
                ))
Пример #2
0
	def prepare(self):
		"""
		Prepare the query, marshall the payload, create binary data and calculate length (size).
		"""
		self.packet = dumps(self.args, methodname=self.method, allow_none=True).encode()
		self.length = len(self.packet)

		if (self.length + 8) > self._client.MAX_REQUEST_SIZE:
			raise TransportException('The prepared query is larger than the maximum request size, we will not send this query!')
Пример #3
0
    async def connect(self):
        """
		Make connection to the server. This will first check the protocol version and after successful connection
		also authenticate, set the API version and enable callbacks.
		"""
        logger.debug('Trying to connect to the dedicated server...')

        # Create socket (produces coroutine).
        self.reader, self.writer = await asyncio.open_connection(
            host=self.host,
            port=self.port,
            loop=self.event_loop,
        )
        _, header = struct.unpack_from('<L11s', await
                                       self.reader.readexactly(15))
        if header.decode() != 'GBXRemote 2':
            raise TransportException(
                'Server is not a valid GBXRemote 2 server.')
        logger.debug('Dedicated connection established!')

        # From now we need to start listening.
        self.loop_task = self.event_loop.create_task(self.listen())

        # Startup tasks.
        await self.execute('Authenticate', self.user, self.password)
        await asyncio.gather(
            self.execute('SetApiVersion', self.api_version),
            self.execute('EnableCallbacks', True),
        )

        # Fetch gbx methods.
        self.gbx_methods = await self.execute('system.listMethods')

        # Check for scripted mode.
        mode = await self.execute('GetGameMode')
        settings = await self.execute('GetModeScriptSettings')
        if mode == 0:
            if 'S_UseScriptCallbacks' in settings:
                settings['S_UseScriptCallbacks'] = True
            if 'S_UseLegacyCallback' in settings:
                settings['S_UseLegacyCallback'] = False
            if 'S_UseLegacyXmlRpcCallbacks' in settings:
                settings['S_UseLegacyXmlRpcCallbacks'] = False
            await asyncio.gather(
                self.execute('SetModeScriptSettings', settings),
                self.execute('TriggerModeScriptEventArray',
                             'XmlRpc.EnableCallbacks', ['true']))

        logger.debug(
            'Dedicated authenticated, API version set and callbacks enabled!')
Пример #4
0
    async def connect(self):
        """
		Make connection to the server. This will first check the protocol version and after successful connection
		also authenticate, set the API version and enable callbacks.
		"""
        logger.debug('Trying to connect to the dedicated server...')

        # Create socket (+ retry few times if not successful.
        retries = 0
        max_retries = 10
        while True:
            try:
                self.reader, self.writer = await asyncio.open_connection(
                    host=self.host,
                    port=self.port,
                    loop=self.event_loop,
                )
                break
            except Exception as exc:
                if retries >= max_retries:
                    raise
                retries += 1

                logger.info(
                    'Couldn\'t connect to Dedicated Server. Retry {} of {} (Error: {}'
                    .format(retries, max_retries, str(exc)))
                await asyncio.sleep(2)

        _, header = struct.unpack_from('<L11s', await
                                       self.reader.readexactly(15))
        if header.decode() != 'GBXRemote 2':
            raise TransportException(
                'Server is not a valid GBXRemote 2 server.')
        logger.debug('Dedicated connection established!')

        # From now we need to start listening.
        self.loop_task = self.event_loop.create_task(self.listen())

        # Startup tasks.
        await self.execute('Authenticate', self.user, self.password)
        await asyncio.gather(
            self.execute('SetApiVersion', self.api_version),
            self.execute('EnableCallbacks', True),
        )

        # Fetch gbx methods and dedicated server version.
        self.gbx_methods, versions = await asyncio.gather(
            self.execute('system.listMethods'), self.execute('GetVersion'))
        self.dedicated_version = versions['Version']
        self.dedicated_build = versions['Build']

        # Check for scripted mode.
        mode = await self.execute('GetGameMode')
        settings = await self.execute('GetModeScriptSettings')
        if mode == 0:
            if 'S_UseScriptCallbacks' in settings:
                settings['S_UseScriptCallbacks'] = True
            if 'S_UseLegacyCallback' in settings:
                settings['S_UseLegacyCallback'] = False
            if 'S_UseLegacyXmlRpcCallbacks' in settings:
                settings['S_UseLegacyXmlRpcCallbacks'] = False
            await asyncio.gather(
                self.execute('SetModeScriptSettings', settings),
                self.execute('TriggerModeScriptEventArray',
                             'XmlRpc.EnableCallbacks', ['true']))

        logger.debug(
            'Dedicated authenticated, API version set and callbacks enabled!')