コード例 #1
0
ファイル: server.py プロジェクト: vahriin/CoolDDChat
    async def register_client(self, reader, writer):
        client = b""
        try:
            while True:
                # ProtocolException will be raised if client disconnect during this
                client = await self.get_client(reader, writer)
                # check nick
                if client == None:
                    writer.write(
                        protocol.dump(
                            protocol.new_status(
                                410,
                                "Nick should contains only letters or digits")
                        ).encode("utf8"))
                    writer.write(b'\n')
                    continue
                self._clients[client.nick]  # check this nick in clients dict
                writer.write(
                    protocol.dump(protocol.new_status(409)).encode("utf8"))
                writer.write(b'\n')
        except KeyError:
            writer.write(protocol.dump(protocol.new_status()).encode("utf8"))
            writer.write(b'\n')

            self._clients[client.nick] = client
            logging.info("User {} connected.".format(client.nick))
            self.broadcast_clients()
            await self.client_handler(client)
コード例 #2
0
ファイル: server.py プロジェクト: vahriin/CoolDDChat
 async def accept_connection(self, reader, writer):
     try:
         writer.write(protocol.dump(protocol.new_status()).encode("utf8"))
         writer.write(b'\n')
         try:
             await self.register_client(reader, writer)
         except protocol.ProtocolException as pe:
             writer.write(
                 protocol.dump(protocol.new_status(418,
                                                   str(pe))).encode("utf8"))
             writer.write(b'\n')
     except BrokenPipeError:
         return
コード例 #3
0
ファイル: server.py プロジェクト: vahriin/CoolDDChat
 def service_handler(self, message, client):
     self._messages[message["messageId"]]["clients_got"] += 1
     if len(self._clients) <= self._messages[
             message["messageId"]]["clients_got"]:
         author_nick = self._messages.pop(message["messageId"])["author"]
         status = protocol.new_status()
         status["users"] = []
         for user in self._clients.keys():
             if user != client.nick:
                 status["users"].append(user)
         self._clients[author_nick].send_service(status)
コード例 #4
0
ファイル: server.py プロジェクト: vahriin/CoolDDChat
 async def get_client(self, reader, writer):
     user_data = (await reader.readline()).decode("utf8")
     _type, data = protocol.load(user_data)
     try:
         if not re.match(r"^[A-Za-z0-9]+$", data["nick"]):
             return None
         client = user.User(data, reader, writer)
     except KeyError:
         writer.write(
             protocol.dump(
                 protocol.new_status(
                     418,
                     'Registration request should contains "nick" field')).
             encode("utf8"))
     return client
コード例 #5
0
ファイル: client.py プロジェクト: vahriin/CoolDDChat
 def message_handler(self, message):
     print("{}: {}\n".format(message["nick"], message["message"]))
     sys.stdout.flush()
     self._writer.write(protocol.dump(protocol.new_status(message_id=message["messageId"])).encode("utf8"))
     self._writer.write(b'\n')