def handleClientInfo(client: socket, addr: int): print("I am a thread handling client with address %s" % str(addr)) # We need to let the client know we established a connection. client.settimeout(100) sendIDToClient(client, addr) client_lock.acquire() global client_sockets client_sockets[clients[addr[0]]] = client client_lock.release() components = getClientComponents(client, addr) global game_in_progress while not game_in_progress: checkClientReadyState(client, addr)
def receive_from(connection: socket)->bytes: buffer = b'' connection.settimeout(1) try: while True: data = connection.recv(4096) if not data: break buffer += data except Exception as e: pass return buffer
def recv_fromWorker(self, conn: socket): """Receive string from Worker via the client module.""" msg = [] conn.settimeout(0.5) # is there a better way to do this??? while True: # receive data 1024 bytes at a time try: data = conn.recv(1024) print(data) except Exception as e: print('the bed hath been shat') print(e) break msg.append(data.decode(encoding='UTF-8')) # append decoded string message = ''.join(msg) # concatenate string return message
def target_on_listening(s: socket, target: tuple): host = target[0] if target[0] != 'localhost' else '127.0.0.1' port = target[1] s.settimeout(1) try: SYN = struct.pack('iii', 1, 1, 0) s.sendto(SYN, target) s.recvfrom(1024) s.settimeout(None) return True except timeout: s.settimeout(None) return False
def handle_client(self, client_socket: socket): username = '' client_socket.settimeout(0.2) client_state = ClientState.NOT_LOGGED_IN try: while True: try: self.acquire() if client_state == ClientState.NOT_LOGGED_IN: msg = MessageBetweenNodeAndClient() client_socket.settimeout(SOCKET_CLIENT_RECEIVE_SHORT_TIMEOUT) t = time.time() msg.recv(client_socket) client_socket.settimeout(SOCKET_CLIENT_RECEIVE_LONG_TIMEOUT) if msg.message_type == MessageType.SIGN_UP: list_of_data = json.loads(msg.content) username, pk_as_str = list_of_data pk = Key.create_from_str(pk_as_str) # check if pk and username are taken and send result to client { is_user_taken = self.server_database.users_table.is_user_exist(username) is_pk_taken = self.server_database.users_table.is_public_key_exist(pk) content = str(int(is_user_taken)) + str(int(is_pk_taken)) msg_to_send = MessageBetweenNodeAndClient(MessageType.SIGN_UP_ANSWER, content) msg_to_send.send(client_socket) # } if not is_user_taken and not is_pk_taken: pass # add user to upload user = User(username, pk) self.list_of_new_users_to_upload.append(user) self.dict_of_clients_and_usernames_waiting_for_confirmation[user] = client_socket client_state = ClientState.WAITING_FOR_CONFIRMATION else: pass elif msg.message_type == MessageType.LOG_IN_REQUEST: username = msg.content if not self.server_database.users_table.is_user_exist(username): print(f'not user named {username}') msg = MessageBetweenNodeAndClient(MessageType.LOG_IN_FAILED) msg.send(client_socket) random = str(randint(0, 10000)) msg = MessageBetweenNodeAndClient(MessageType.LOG_IN_RAND, random) msg.send(client_socket) msg.recv(client_socket) if msg.message_type != MessageType.LOG_IN_RAND_ANSWER: raise Exception('unxpexted msg') client_pk = self.server_database.users_table.get_public_key(username) if not client_pk.verify(msg.content, random): msg = MessageBetweenNodeAndClient(MessageType.LOG_IN_FAILED) else: msg = MessageBetweenNodeAndClient(MessageType.LOG_IN_ACCEPTED) msg.send(client_socket) client_socket.settimeout(SOCKET_CLIENT_RECEIVE_SHORT_TIMEOUT) self.dict_of_clients_and_usernames[username] = client_socket client_state = ClientState.LOGGED_IN else: raise Exception('unxpexted msg') elif client_state == ClientState.LOGGED_IN: msg = MessageBetweenNodeAndClient() msg.recv(client_socket) if msg.message_type == MessageType.GET_ALL_TRANSACTIONS: # serch the database for all transactions revolving the user { tuples, current_amount_of_money = self.server_database.get_all_transactions_of(username) print(f'current_amount_of_money {current_amount_of_money} (190)') content = json.dumps( [[(t[0].as_str(), t[1]) for t in tuples], str(current_amount_of_money)]) msg_to_send = MessageBetweenNodeAndClient( MessageType.RECEIVE_ALL_TRANSACTIONS, content) msg_to_send.send(client_socket) # } elif msg.message_type == MessageType.TRANSACTION_OFFERED: transaction = Transaction.create_from_str(msg.content) self.list_of_transactions_to_make.append(transaction) elif msg.message_type == MessageType.TRANSACTION_COMPLETED: transaction = Transaction.create_from_str(msg.content) self.list_of_transactions_to_make.append(transaction) elif client_state == ClientState.WAITING_FOR_CONFIRMATION: if self.dict_of_clients_and_usernames_waiting_for_confirmation.get(user) is None: client_socket.close() self.release() return self.release() except socket.timeout: self.release() except ConnectionError or json.decoder.JSONDecodeError as e: print(e) print('nlafsdfdsfdsfds') self.release() return