From 38f3a3807373a19cacc0171dd7d5fd11f44dbd35 Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 31 Dec 2017 00:10:14 +0100 Subject: merged roomedge into portal, moved tcommunicate and utils to common folder and fixed package when starting main directory --- asciifarm/client/connection.py | 2 +- asciifarm/client/tcommunicate.py | 32 -------------------------------- asciifarm/common/tcommunicate.py | 32 ++++++++++++++++++++++++++++++++ asciifarm/common/utils.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 33 deletions(-) delete mode 100644 asciifarm/client/tcommunicate.py create mode 100644 asciifarm/common/tcommunicate.py create mode 100644 asciifarm/common/utils.py diff --git a/asciifarm/client/connection.py b/asciifarm/client/connection.py index 9ca2ccc..4eb0ffe 100644 --- a/asciifarm/client/connection.py +++ b/asciifarm/client/connection.py @@ -1,7 +1,7 @@ import socket -from .tcommunicate import send, receive +from asciifarm.common.tcommunicate import send, receive class Connection: diff --git a/asciifarm/client/tcommunicate.py b/asciifarm/client/tcommunicate.py deleted file mode 100644 index b1fc1b0..0000000 --- a/asciifarm/client/tcommunicate.py +++ /dev/null @@ -1,32 +0,0 @@ - -HEADER_SIZE = 4 - - -# this module is for sending discree messages over TCP -# this is achieved by prefixing all messages with their length -# calls to send and recv will also keep attempting to send all data unless this proves impossible - - -def send(sock, msg): - length = len(msg) - header = length.to_bytes(4, byteorder="big") - totalmsg = header + msg - sock.sendall(totalmsg) - -def receive(sock): - header = recvall(sock, 4) #sock.recv(4) - length = int.from_bytes(header, byteorder="big") - return recvall(sock, length) - -def recvall(sock, length): - chunks = [] - bytes_recd = 0 - while bytes_recd < length: - chunk = sock.recv(min(length - bytes_recd, 4096)) - if chunk == b'': - break - #raise RuntimeError("socket connection broken") - chunks.append(chunk) - bytes_recd = bytes_recd + len(chunk) - return b''.join(chunks) - diff --git a/asciifarm/common/tcommunicate.py b/asciifarm/common/tcommunicate.py new file mode 100644 index 0000000..b1fc1b0 --- /dev/null +++ b/asciifarm/common/tcommunicate.py @@ -0,0 +1,32 @@ + +HEADER_SIZE = 4 + + +# this module is for sending discree messages over TCP +# this is achieved by prefixing all messages with their length +# calls to send and recv will also keep attempting to send all data unless this proves impossible + + +def send(sock, msg): + length = len(msg) + header = length.to_bytes(4, byteorder="big") + totalmsg = header + msg + sock.sendall(totalmsg) + +def receive(sock): + header = recvall(sock, 4) #sock.recv(4) + length = int.from_bytes(header, byteorder="big") + return recvall(sock, length) + +def recvall(sock, length): + chunks = [] + bytes_recd = 0 + while bytes_recd < length: + chunk = sock.recv(min(length - bytes_recd, 4096)) + if chunk == b'': + break + #raise RuntimeError("socket connection broken") + chunks.append(chunk) + bytes_recd = bytes_recd + len(chunk) + return b''.join(chunks) + diff --git a/asciifarm/common/utils.py b/asciifarm/common/utils.py new file mode 100644 index 0000000..3df020c --- /dev/null +++ b/asciifarm/common/utils.py @@ -0,0 +1,35 @@ + +import os + +def clamp(val, lower, upper): + """ val if it's between lower and upper, else the closest of the two""" + return min(max(val, lower), upper) + + +def concat(arr): + """Takes a list of sequences, returns the concatenation of the sequences """ + if isinstance(arr[0], str): + return "".join(arr) + if isinstance(arr[0], bytes): + return b"".join(arr) + if isinstance(arr[0], list): + l = [] + for s in arr: + l += s + return l + if isinstance(arr[0], tuple): + l = [] + for s in arr: + l += s + return tuple(l) + else: + raise ValueError("type {} can't be concatenated".format(type(arr[0]))) + + +def writeFileSafe(filename, data, tempname=None): + if tempname == None: + tempname = filename + ".tempfile" + with open(tempname, 'w') as f: + f.write(data) + os.rename(tempname, filename) + -- cgit