本文整理汇总了Python中krpc.decoder.Decoder.decode_size_and_position方法的典型用法代码示例。如果您正苦于以下问题:Python Decoder.decode_size_and_position方法的具体用法?Python Decoder.decode_size_and_position怎么用?Python Decoder.decode_size_and_position使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类krpc.decoder.Decoder
的用法示例。
在下文中一共展示了Decoder.decode_size_and_position方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _receive_response
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode_size_and_position [as 别名]
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)
示例2: update_thread
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode_size_and_position [as 别名]
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)
示例3: update_thread
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode_size_and_position [as 别名]
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)
示例4: test_decode_size_and_position
# 需要导入模块: from krpc.decoder import Decoder [as 别名]
# 或者: from krpc.decoder.Decoder import decode_size_and_position [as 别名]
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)