summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorDiego Roversi <diegor@tiscali.it>2019-09-20 11:04:45 +0200
committerDiego Roversi <diegor@tiscali.it>2019-09-20 11:04:45 +0200
commit55c5d83e6ef3586db477c26f93539d8182956b0e (patch)
tree10b30c5131b672c954259e709217f21a4d519f7d /common
parenta78183e29504b9c1915b9e3d254db6ab185f7b33 (diff)
convert string to bytearrays
Diffstat (limited to 'common')
-rw-r--r--common/msgstruct.py30
1 files changed, 23 insertions, 7 deletions
diff --git a/common/msgstruct.py b/common/msgstruct.py
index 93b6bea..7a26142 100644
--- a/common/msgstruct.py
+++ b/common/msgstruct.py
@@ -1,5 +1,7 @@
from struct import pack, unpack, calcsize
+import sys
+
try:
from localmsg import PORTS
except ImportError:
@@ -11,7 +13,7 @@ except ImportError:
HOSTNAME = gethostname()
-MSG_WELCOME = "Welcome to gamesrv.py(3) !\n"
+MSG_WELCOME = b"Welcome to gamesrv.py(3) !\n"
MSG_BROADCAST_PORT= b"*"
MSG_DEF_PLAYFIELD = b"p"
MSG_DEF_KEY = b"k"
@@ -44,14 +46,17 @@ CMSG_PONG = b"G"
CMSG_DATA_REQUEST = b"M"
CMSG_PLAYER_NAME = b"n"
-BROADCAST_MESSAGE = "game!" # less than 6 bytes
+BROADCAST_MESSAGE = b"game!" # less than 6 bytes
def message(tp, *values):
+ print(f"tp={tp} values={values}")
strtype = type('')
+ bytestype = type(b'')
typecodes = ['']
for v in values:
- if type(v) is strtype:
+ print( f"message: type(v) = {type(v)} , value = {v}", file=sys.stderr )
+ if type(v) is strtype or type(v) is bytestype:
typecodes.append('%ds' % len(v))
elif 0 <= v < 256:
typecodes.append('B')
@@ -59,14 +64,25 @@ def message(tp, *values):
typecodes.append('l')
typecodes = ''.join(typecodes)
assert len(typecodes) < 256
- return pack(("!B%dsc" % len(typecodes)) + typecodes,
- len(typecodes), typecodes.encode(), tp, *values)
+ print(f"typecodes={typecodes}",file=sys.stderr)
+ fmt= ("!B%dsc" % len(typecodes)) + typecodes
+# print(f"fmt={fmt} typecodes.encode() = {typecodes.encode()}",file=sys.stderr )
+# print(f"type(typecodes.encode()) = {type(typecodes.encode())}", file=sys.stderr )
+ try:
+ ret = pack(fmt, len(typecodes), typecodes.encode(), tp, *values)
+ except Exception as e:
+ print("Exception!",file=sys.stderr)
+ print(f"fmt={fmt} typecodes.encode() = {typecodes.encode()}",file=sys.stderr )
+ print(f"type(typecodes.encode()) = {type(typecodes.encode())}", file=sys.stderr )
+
+ raise e
+ return ret
def decodemessage(data):
if data:
- limit = ord(data[0]) + 1
+ limit = data[0] + 1
if len(data) >= limit:
- typecodes = "!c" + data[1:limit]
+ typecodes = b"!c" + data[1:limit]
end = limit + calcsize(typecodes)
if len(data) >= end:
return unpack(typecodes, data[limit:end]), data[end:]