#!/usr/bin/python3

import json
import socket
import sys

MSG_SIZE = 1024

# int, int
def getGlobalReqId(clientId, clientReqId):
    return str(clientId) + "_" + str(clientReqId)

def splitGlobalReqId(gReqId):
    clientId, clientReqId = gReqId.split('_')
    return int(clientId), int(clientReqId)

def prepare(msg):
    msg = json.dumps(msg)
    #print(msg)
    size = len(msg)
    if size > 1024:
        print("Message size too big", file=sys.stderr)
    
    # make all msgs 1024 bytes
    msg = str.encode(msg.ljust(MSG_SIZE, '\0'))
    return msg

def extractUDPPair(address):
    ip, udp, tcp = address
    return (ip, udp)

def extractTCPPair(address):
    ip, udp, tcp = address
    return (ip, tcp)

# address (ip: str, udpport: int, tcpport: int)
# msg should be a dictionary that will be converted to json, <= 1024 bytes
def send(address, msg):
    msg = prepare(msg)
    address = extractUDPPair(address)

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(msg, address)

def sendTCP(address, msg):
    msg = prepare(msg)
    address = extractTCPPair(address)

    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(1.0)
        sock.connect(address)
        sock.send(msg)
        sock.close()
    except socket.error:
        return

def recv(sock):
    data = sock.recv(MSG_SIZE)
    json_str = data.decode().strip('\0')
    try:
        msg = json.loads(json_str)
        return msg
    except json.decoder.JSONDecodeError:
        return
@@ -51,16 +51,20 @@
 rt sys%0A%0A
+MAX_
 MSG_SIZE
@@ -71,16 +71,16 @@
  = 1024%0A
-
 %0A# int, 
@@ -345,24 +345,8 @@
 sg)%0A
-    #print(msg)%0A
     
@@ -375,20 +375,28 @@
  size %3E 
-1024
+MAX_MSG_SIZE
 :%0A      
@@ -452,39 +452,19 @@
     
-%0A
     
-# make all msgs 1024 bytes
+return%0A
 %0A   
@@ -488,30 +488,8 @@
 (msg
-.ljust(MSG_SIZE, '%5C0')
 )%0A  
@@ -820,32 +820,67 @@
  = prepare(msg)%0A
+    if msg is None:%0A        return%0A
     address = ex
@@ -1039,24 +1039,59 @@
 repare(msg)%0A
+    if msg is None:%0A        return%0A
     address 
@@ -1372,16 +1372,20 @@
 ck.recv(
+MAX_
 MSG_SIZE
@@ -1418,20 +1418,8 @@
 de()
-.strip('%5C0')
 %0A   

Remove unnecessary padding