Skip to content

Commit

Permalink
Merge pull request #65 from befelix/fix_py3
Browse files Browse the repository at this point in the history
Fix python client for bytestring data
  • Loading branch information
qiuwch committed Jun 18, 2017
2 parents fecfaed + 68b194e commit 29227e6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions client/python/unrealcv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def ReceivePayload(cls, socket):

rfile.close()

return payload.decode('UTF-8')
return payload

@classmethod
def WrapAndSendPayload(cls, socket, payload):
Expand Down Expand Up @@ -116,6 +116,7 @@ def WrapAndSendPayload(cls, socket, payload):
_L.error('Fail to send message %s', e)
return False


class BaseClient(object):
'''
BaseClient send message out and receiving message in a seperate thread.
Expand Down Expand Up @@ -171,7 +172,7 @@ def connect(self, timeout = 1):
self.socket = None

def isconnected(self):
return self.socket != None
return self.socket is not None

def disconnect(self):
if self.isconnected():
Expand All @@ -191,7 +192,7 @@ def __receiving(self):
Also check whether client is still connected
'''
_L.debug('BaseClient start receiving in %s', threading.current_thread().name)
while (1):
while True:
if self.isconnected():
# Only this thread is allowed to read from socket, otherwise need lock to avoid competing
message = SocketMessage.ReceivePayload(self.socket)
Expand All @@ -201,8 +202,8 @@ def __receiving(self):
self.socket = None
continue

if message.startswith('connected'):
_L.info('Got connection confirm: %s', repr(message))
if message.startswith(b'connected'):
_L.info('Got connection confirm: %s', repr(message.decode('utf-8')))
self.wait_connected.set()
# self.wait_connected.clear()
continue
Expand All @@ -224,6 +225,7 @@ def send(self, message):
_L.error('Fail to send message, client is not connected')
return False


class Client(object):
'''
Client can be used to send request to a game and get response
Expand All @@ -233,9 +235,15 @@ class Client(object):
def __raw_message_handler(self, raw_message):
# print 'Waiting for message id %d' % self.message_id
match = self.raw_message_regexp.match(raw_message)

if match:
[message_id, message_body] = (int(match.group(1)), match.group(2)) # TODO: handle multiline response
message_body = raw_message[len(match.group(1))+1:]
# Convert to utf-8 if it's not a byte array (as is the case for images)
try:
message_body = message_body.decode('utf-8')
except UnicodeDecodeError:
pass
# print 'Received message id %s' % message_id
if message_id == self.message_id:
self.response = message_body
Expand All @@ -252,7 +260,7 @@ def do_callback():
_L.error('No message handler to handle message %s', raw_message)

def __init__(self, endpoint, message_handler=None):
self.raw_message_regexp = re.compile('(\d{1,8}):(.*)')
self.raw_message_regexp = re.compile(b'(\d{1,8}):(.*)')
self.message_client = BaseClient(endpoint, self.__raw_message_handler)
self.message_handler = message_handler
self.message_id = 0
Expand Down

0 comments on commit 29227e6

Please sign in to comment.