Пример #1
0
 def event_receiver(self, computer):
     """" Events / Messages Receiver """
     try:
         logging.debug("Starting event receiving thread...")
         while self.server.connected:
             events = self.server.socket_.recv(1024)
             print(events)
             if len(events) < 1:
                 continue
             events = events.decode(ENCODE_FORMAT).split(EVENT_SEPARATOR)
             logging.debug(events)
             for event in events:
                 if len(event) <= 1:
                     continue
                 logging.debug(event)
                 event = EventEncoder.decode(event)
                 if event is not None:
                     event.computer = computer
                     event.client = self
                     EventHandler.handle(event)
     except (ConnectionResetError, ConnectionAbortedError):
         self.disconnect()
     except Exception as e:
         logging.error("Exception on client event receiver.")
         logging.exception(e)
Пример #2
0
 def shutdown(self):
     try:
         event = Event(Event.SERVER_SHUT)
         for address in self.clients:
             self.clients[address].connection.sendall(
                 EventEncoder.encode(event))
         self.socket.close()
     except Exception as e:
         logging.error("Exception occurred on server shutdown.")
         logging.exception(e)
Пример #3
0
 def send_event(self, client, event):
     """" Send event to client """
     try:
         if client.address in self.clients:
             client.connection.sendall(EventEncoder.encode(event))
         else:
             logging.warning("Client {} is disconnected or kicked.".format(
                 client.address))
     except Exception as e:
         logging.error("Exception occurred on server send event.")
         logging.exception(e)
Пример #4
0
 def send_event(self, event):
     """" Send event to server """
     try:
         if not isinstance(event, Event):
             raise Exception("Invalid event passed.")
         if self.server is not None and self.server.socket_ is not None:
             self.server.socket_.sendall(EventEncoder.encode(event))
             logging.debug("Event {} sent to server".format(event.name))
         else:
             logging.warning("You are disconnected from server.")
     except Exception as e:
         logging.error("Exception occurred on client send event.")
         logging.exception(e)
Пример #5
0
 def connect(self, host, port, computer):
     """" Connects on a server"""
     try:
         logging.info("Connecting to the server {}:{}".format(host, port))
         self.server.socket_.connect((host, port))
         self.server.connected = True
         event = Event(Event.TEST)
         self.server.socket_.send(EventEncoder.encode(event))
         logging.info("Connected to the server.")
         self.event_receiver_thread(computer)
     except Exception as e:
         logging.error("Unable to connect to server.")
         logging.exception(e)
Пример #6
0
 def __is_server(host):
     """" Returns true if given host is a server """
     try:
         socket.setdefaulttimeout(SERVER_CONNECT_TIMEOUT)
         socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         socket_.connect((host, PORT))
         Interface.__active_server.append(host)
         event = Event(Event.CONNECTION_TEST)
         socket_.send(EventEncoder.encode(event))
         socket_.close()
         logging.debug("Test connection to {}:{} closed.".format(
             host, PORT))
     except socket.timeout:
         pass
     except ConnectionRefusedError:
         pass
     finally:
         socket.setdefaulttimeout(None)
Пример #7
0
    def event_receiver(self, client, computer):
        """" Events receiver """
        try:
            if client.address in self.clients:
                while client.connection and computer.is_running:
                    events = client.connection.recv(1024)
                    if len(events) < 1:
                        continue

                    events = events.decode(ENCODE_FORMAT).split(
                        EVENT_SEPARATOR)
                    logging.debug(events)
                    for event in events:
                        if len(event) <= 1:
                            continue
                        logging.debug(event)
                        event = EventEncoder.decode(event)
                        if event is not None:
                            event.computer = computer
                            event.client = client
                            EventHandler.handle(event)

                            if event.name == Event.CONNECTION_TEST:
                                self.kick(client)
                                break  # Stop receiving
            else:
                logging.warning("Client {} is disconnected or kicked.".format(
                    client.address))
                logging.info("Closing event receive thread for {}".format(
                    client.address))
        except ConnectionResetError:
            self.on_client_disconnect(client)
            logging.warning("Client {} is disconnected".format(client.address))
            logging.info("Closing event receive thread for {}".format(
                client.address))
        except Exception as e:
            logging.error("Exception occurred on server event receive.")
            logging.exception(e)