Ejemplo n.º 1
0
    def get_lock(self, uid):
        uid = cppsutil.to_str(uid)
        if len(uid) > 0:
            if uid not in self.lock:
                self.lock[uid] = Semaphore()
        else:
            return Semaphore()

        return self.lock[uid]
Ejemplo n.º 2
0
    def login(self, client_sock, uid, rid, data):
        if isinstance(data, str):
            data = json.loads(data.decode('utf-8'), 'utf-8')

        if not isinstance(data, dict) or not "uid" in data or not "timestamp" in data:
            logging.error("error login message `%s` from `%s`", data, client_sock)
            response_message = {
                "id"   : rid,
                "data" : {"err_id":"error login message format"}
            }
            self.ser_to_cli(client_sock, uid, json.dumps(response_message))
            return (False, "error login message format")

        hm = hashlib.md5();
        hm.update(self.secret_key + str(data["uid"]) + str(data["timestamp"]) + str(data["random"]))
        if uid != str(data['uid']) or data["sign"] != hm.hexdigest():
            logging.error("error login message `%s` from `%s`", data, client_sock)
            response_message = {
                "id"   : rid,
                "data" : {"err_id":"error login message sign"}
            }
            self.ser_to_cli(client_sock, uid, json.dumps(response_message))
            return (False, "error login message sign")

        client_player_uid = cppsutil.to_str(uid)
        if client_player_uid in self.rel_mapping:
            self.dis_connect(self.rel_mapping[client_player_uid], "relogin")

        logging.info("login message `%s` from `%s`", data, client_sock)
        response_message = {
            "id"   : rid,
            "data" : {"err_id":""}
        }
        fd = self.get_sock_fd(client_sock)
        self.cli_conns[fd]['uid']       = client_player_uid
        self.rel_mapping[client_player_uid] = client_sock
        self.clients.login(data);
        self.ser_to_cli(client_sock, uid, json.dumps(response_message))

        return (True, "")