Beispiel #1
0
    def _receive_response(self):
        """ Receive data from the server and decode it into a KRPC.Response object """

        # Read the size and position of the response message
        data = b''
        while True:
            try:
                data += self._rpc_connection.partial_receive(1)
                size, _ = Decoder.decode_size_and_position(data)
                break
            except IndexError:
                pass

        # Read and decode the response message
        data = self._rpc_connection.receive(size)
        return Decoder.decode(data, self._response_type)
Beispiel #2
0
    def _receive_response(self):
        """ Receive data from the server and decode it into a KRPC.Response object """

        # Read the size and position of the response message
        data = b""
        while True:
            try:
                data += self._rpc_connection.partial_receive(1)
                size, position = Decoder.decode_size_and_position(data)
                break
            except IndexError:
                pass

        # Read and decode the response message
        data = self._rpc_connection.receive(size)
        return Decoder.decode(data, self._response_type)
Beispiel #3
0
def update_thread(connection, stop, cache, cache_lock):
    stream_message_type = Types().as_type('KRPC.StreamMessage')

    while True:

        # Read the size and position of the response message
        data = b''
        while True:
            try:
                data += connection.partial_receive(1)
                size, _ = Decoder.decode_size_and_position(data)
                break
            except IndexError:
                pass
            except:  # pylint: disable=bare-except
                # TODO: is there a better way to catch exceptions when the
                #      thread is forcibly stopped (e.g. by CTRL+c)?
                return
            if stop.is_set():
                connection.close()
                return

        # Read and decode the response message
        data = connection.receive(size)
        response = Decoder.decode(data, stream_message_type)

        # Add the data to the cache
        with cache_lock:
            for response in response.responses:
                if response.id not in cache:
                    continue

                # Check for an error response
                if response.response.has_error:
                    cache[response.id].value = RPCError(
                        response.response.error)
                    continue

                # Decode the return value and store it in the cache
                typ = cache[response.id].return_type
                value = Decoder.decode(response.response.return_value, typ)
                cache[response.id].update(value)
Beispiel #4
0
def update_thread(connection, stop):
    stream_message_type = Types().as_type('KRPC.StreamMessage')
    response_type = Types().as_type('KRPC.Response')

    while True:

        # Read the size and position of the response message
        data = b''
        while True:
            try:
                data += connection.partial_receive(1)
                size,position = Decoder.decode_size_and_position(data)
                break
            except IndexError:
                pass
            except:
                #TODO: is there a better way to catch exceptions when the thread is forcibly stopped (e.g. by CTRL+c)?
                return
            if stop.is_set():
                connection.close()
                return

        # Read and decode the response message
        data = connection.receive(size)
        response = Decoder.decode(data, stream_message_type)

        # Add the data to the cache
        with _stream_cache_lock:
            for response in response.responses:
                id = response.id
                if id not in _stream_cache:
                    continue

                # Check for an error response
                if response.response.has_error:
                    _stream_cache[id].value = RPCError(response.response.error)
                    continue

                # Decode the return value and store it in the cache
                typ = _stream_cache[id].return_type
                value = Decoder.decode(response.response.return_value, typ)
                _stream_cache[id].update(value)
Beispiel #5
0
def update_thread(connection):
    stream_message_type = Types().as_type('KRPC.StreamMessage')
    response_type = Types().as_type('KRPC.Response')

    while True:

        # Read the size and position of the response message
        data = b''
        while True:
            try:
                data += connection.partial_receive(1)
                size,position = Decoder.decode_size_and_position(data)
                break
            except IndexError:
                pass
            except socket.error:
                return

        # Read and decode the response message
        try:
            data = connection.receive(size)
        except socket.error:
            return
        response = Decoder.decode(data, stream_message_type)

        # Add the data to the cache
        with _stream_cache_lock:
            for response in response.responses:
                id = response.id
                if id not in _stream_cache:
                    continue

                # Check for an error response
                if response.response.HasField('error'):
                    _stream_cache[id].value = RPCError(response.response.error)
                    continue

                # Decode the return value and store it in the cache
                typ = _stream_cache[id].return_type
                value = Decoder.decode(response.response.return_value, typ)
                _stream_cache[id].update(value)
Beispiel #6
0
 def test_decode_size_and_position(self):
     message = '1c'
     size,position = Decoder.decode_size_and_position(unhexlify(message))
     self.assertEqual(28, size)
     self.assertEqual(1, position)
Beispiel #7
0
 def test_decode_size_and_position(self):
     message = '1c'
     size, position = Decoder.decode_size_and_position(unhexlify(message))
     self.assertEqual(28, size)
     self.assertEqual(1, position)