def send_message(self, broadcast, sock, message): # Check to see if this is a broadcast message if broadcast == True: for client_socket in self._socket_list: if client_socket != self._server_socket: MessageProtocol.send_msg(client_socket, message) # Send the message to the specified socket (sock) else: MessageProtocol.send_msg(sock, message)
def run(self): while True: fd_list = [self._client_socket] # Get the list of sockets that are readable ready_to_read, ready_to_write, input_error = select.select(fd_list, [], [], self.__select_timeout) for sock in ready_to_read: # Received message from server if sock == self._client_socket: # This should ensure all the data from the socket is received message_list = [] while 1: message, bytes_read = MessageProtocol.recv_msg(sock) if bytes_read > 0: message_list.append(message) else: break # Check to see if data is available message_list_length = len(message_list) if message_list_length > 0: for message in message_list: # Place the server message into the output queue and notify the client that data has been received self._output_queue.put(message) self.notify_client_message() # Disconnected from server else: self._logger.error('Disconnected from the server.') sys.exit() # Check to see if data is available on the input queue # Note: Normally the queue would be in the select call, but I don't think # Queue is implemented as a file descriptor in Python (or Windows sucks) if self._input_queue.qsize() > 0: self._logger.debug('Retrieving message from input queue.') try: message = self._input_queue.get_nowait() except: break # Send message to the server self._logger.debug('Sending message to server.') MessageProtocol.send_msg(self._client_socket, message)
def run(self): while True: ready_to_read, ready_to_write, input_error = select.select(self._socket_list, [], [], self.__select_timeout) # Loop through list of sockets that have data for sock in ready_to_read: # Received new connection request if sock == self._server_socket: # Accept the connection and append it to the socket list self._logger.debug('Received connection request. Establishing connection with client.') new_sock, address = self._server_socket.accept() new_sock.settimeout(0.5) #TODO: Remove this and while loop below if we start running into issues (workaround for while loop timeout-blocking issue) # Check the number of players currently connected to the server if len(self._socket_list) < 7: self._logger.debug('Client connected from %s on port %s' % address) # Add the socket to the socket list self._socket_list.append(new_sock) # Add a new player to the server model self._server_message.add_player(address) # Send all messages in the output queue self.send_all_messages(new_sock) # Game is full else: self._logger.debug('Closed connection with %s on port %s' % address) new_sock.close() # Received message from client else: # This should ensure all the data from the socket is received message_list = [] while 1: message, bytes_read = MessageProtocol.recv_msg(sock) if bytes_read > 0: message_list.append(message) else: break # Check to see if data is available message_list_length = len(message_list) if message_list_length > 0: # Retrieve the player associated with this socket address = sock._sock.getpeername() player = self._server_message.get_player(address) # Handle the request self._server_message.handle_message(message_list, player) # Send response to the client(s) self._logger.debug('Sending message(s) to client(s).') # Send all messages in the output queue self.send_all_messages(sock) # Client disconnected else: self._logger.error('Client disconnected.') self.remove_client(sock) self._server_socket.close()