From 2c41e377ab7257f879840abca9305fd25c274100 Mon Sep 17 00:00:00 2001 From: Diego Roversi Date: Sat, 23 May 2020 10:05:51 +0200 Subject: more string to bytearray conversion --- common/gamesrv.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'common') diff --git a/common/gamesrv.py b/common/gamesrv.py index eaa9a87..44a3e9c 100644 --- a/common/gamesrv.py +++ b/common/gamesrv.py @@ -59,19 +59,28 @@ class DataChunk: def read(self, slice=None): f = open(self.filename, "rb") data = f.read() + print(f"read: data={type(data)}",file=sys.stderr) f.close() if slice: start, length = slice data = data[start:start+length] + print(f"read: data={type(data)}",file=sys.stderr) return data def defall(self, client): if client.proto == 1 or not self.filename: # protocol 1 - try: + if hasattr(self,'msgdef'): msgdef = self.msgdef - except AttributeError: - data = zlib.compress(self.read()) + else: + print(f"self={type(self)}", file=sys.stderr) + data = self.read() + if type(data)==type(''): + raise Exception('data should be bytes, not str') +# data=data.encode() + print(f"defall: data={type(data)}", file=sys.stderr) + print(f"defall: data={data}", file=sys.stderr) + data = zlib.compress(data) msgdef = self.msgdef = self.getmsgdef(data) else: # protocol >= 2 @@ -130,6 +139,8 @@ class Bitmap(DataChunk): class MemoryBitmap(Bitmap): def __init__(self, code, data, colorkey=None): + if type(data)==type(''): + raise Exception('data should be bytes, not str') self.data = data Bitmap.__init__(self, code, None, colorkey) @@ -477,13 +488,16 @@ class Client: del self.sounds[key] if broadcast_extras is None or self not in broadcast_clients: if self.dyncompress is not None: - # DEBUG: print(f"self.dyncompress={self.dyncompress}", file=sys.stderr) + # DEBUG: + print(f"(before) self.dyncompress={self.dyncompress}", file=sys.stderr) udpdatas = self.dynamic_compress(udpdata) - # DEBUG: print(f"udpdata={udpdata}",file=sys.stderr) + # DEBUG: + print(f"(after) udpdata={udpdata} udpdatas={udpdatas}",file=sys.stderr) else: udpdatas = [udpdata] - # DEBUG: print(f"udpdatas={udpdatas}",file=sys.stderr) - for udpdata in udpdatas or []: + # DEBUG: + print(f"udpdatas={udpdatas}",file=sys.stderr) + for udpdata in udpdatas: # or []: try: self.udpsockcounter += self.udpsocket.send(udpdata) except error as e: @@ -547,16 +561,17 @@ class Client: yield None, None frame = (frame + 1) & 0xFF - self.dyncompress = dyncompress() + self.dyncompress = None # dyncompress() disable compression TODO def dynamic_compress(self, framedata): result = [] + print(f"head={head} co={co}", file=sys.stderr) for head, co in self.dyncompress: if not co: return result data = [head, co.compress(framedata), co.flush(zlib.Z_SYNC_FLUSH)] if head: - result.append(''.join(data)) + result.append(b''.join(data)) def send_can_mix(self): return not self.msgl and self.socket is not None @@ -825,6 +840,7 @@ class SimpleClient(Client): def finishinit(self, game): num = 0 for keyname, icolist, fn in game.FnKeys: + print(f"finishinit: fn={fn} keyname={keyname} icolist={icolist}", file=sys.stderr) self.msgl.append(message(MSG_DEF_KEY, keyname.encode(), num, *[ico.code for ico in icolist])) num += 1 @@ -834,9 +850,11 @@ class SimpleClient(Client): try: player = self.players[pid] fn = game.FnKeys[keynum][2] + print(f"cmsg_key: fn={fn} keynum={keynum}", file=sys.stderr) except (KeyError, IndexError): game.FnUnknown() else: + print(f"cmsg_key: player={player}", file=sys.stderr) getattr(player, fn) () MESSAGES = Client.MESSAGES.copy() -- cgit v1.2.3