def receive_and_send(s, player): while True: data = s.recv(BUFFER_SIZE) data = json.loads(data.decode('utf-8')) if data == -1: break try: output = execute_input(player, data) s.send(json.dumps(output).encode('utf-8')) except: output = GO_CRAZY s.send(json.dumps(output).encode('utf-8')) break
def receive_and_send(s, player): while True: data = s.recv(BUFFER_SIZE) # instead of checking for a -1 if data == b'': break data = json.loads(data.decode('utf-8')) try: output = execute_input(player, data) s.send(json.dumps(output).encode('utf-8')) except GoCrazyException: output = GO_CRAZY s.send(json.dumps(output).encode('utf-8')) break except CloseConnectionException: break
config = read_config('go.config') TCP_IP = config["IP"] TCP_PORT = config["port"] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TCP_IP, TCP_PORT)) s.settimeout(60) s.listen(1) conn, addr = s.accept() player_inner = GoPlayerNetwork(conn) player = GoPlayerContract(player_inner) output = [] for obj in objs: ret = execute_input(player, obj) if ret: output.append(ret) if ret == GO_CRAZY: break ## Close the connection conn.send(json.dumps(-1).encode('utf-8')) try_shutdown(conn, socket.SHUT_RDWR) conn.close() try_shutdown(s, socket.SHUT_RDWR) s.close() ## Update the Port config["port"] = new_port(TCP_PORT) write_config(config, 'go.config')
def call_exec(obj): return execute_input(player, obj)