Beispiel #1
0
 def handle(self, *args, **kwargs):
     if Logger.is_debug_network():
         peer = kwargs.get('peer', 'INNER')
         mode = kwargs.get('proto', 'TCP')
         Logger.debug('==== RECV %s FROM %s: %08X %s' %
                      (mode, peer, self.cmd, repr(self.raw)))
     if Message.is_inner(self.cmd):
         cmd = Message.to_outer(self.cmd)
         mi = MsgLine.unpack(self.raw)
         self.onInnerMessage(cmd, mi, *args, **kwargs)
     else:
         self.onOuterMessage(self.cmd, self.raw, *args, **kwargs)
Beispiel #2
0
    def onOuterMessage(self, cmd, raw, *args, **kwargs):
        if not self.connection.has_login():
            with self.connection.locker:
                if cmd == Message.MSG_SYS_USER_INFO | Message.ID_REQ:
                    mi = MsgPack.unpack(cmd, raw)
                    self.process_login(mi)
                    if self.connection.has_login():
                        self.send_to_entity(cmd, self.connection.userId, raw)
                    return True

        if self.connection.has_login():
            uid = self.connection.userId
            if Message.is_game_server(cmd):
                return self.send_to_game(cmd, uid, raw)
            elif cmd == Message.MSG_SYS_HOLD | Message.ID_REQ:
                return self.on_hold()
            elif cmd == Message.MSG_SYS_QUICK_START | Message.ID_REQ:
                return self.send_to_quick(cmd, uid, raw)
            elif cmd == Message.MSG_SYS_BIND_GAME | Message.ID_REQ:
                return self.on_bind_game(cmd, raw)
            elif cmd == Message.MSG_INNER_BROKEN:
                return self.process_broken(raw)
            else:
                return self.send_to_entity(cmd, uid, raw)

        return False
Beispiel #3
0
 def forward_to_system(self, cmd, msg):
     if not isinstance(msg, str):
         msg = msg.pack()
     ids = self.map_server_type.get(FlagType.flag_type_connect, [])
     inner_cmd = Message.to_inner(cmd)
     for _id in ids:
         conn = self.map_server_connect.get(_id, None)
         if conn:
             conn.sendMsg(inner_cmd, msg)
Beispiel #4
0
    def broadcast_to_system(self, msg, gid=None, room=None, target=None):
        ids = self.map_server_type.get(FlagType.flag_type_connect, [])
        inner_cmd = Message.to_inner(msg.get_cmd())
        data = MsgLine(msg.pack(), gid, room, target).pack()

        for _id in ids:
            conn = self.map_server_connect.get(_id, None)
            if conn:
                conn.sendMsg(inner_cmd, data)
Beispiel #5
0
 def setTimeout(self, second, param, *args, **kwargs):
     self.type = self.TIMER_ONCE
     self.second = second
     msg = MsgPack(Message.MSG_INNER_TIMER, param)
     uid = msg.get_param('userId')
     gid = msg.get_param('gameId')
     msg = MsgLine(msg.pack(), gid, target=uid).pack()
     task = self.tasklet(Message.to_inner(Message.MSG_INNER_TIMER), msg,
                         None).run
     self.timer = TaskManager.call_later(self.__timeout, second, msg, task,
                                         *args, **kwargs)
     return True
Beispiel #6
0
 def setInterval(self, interval, param, delay=None, *args, **kwargs):
     self.type = self.TIMER_LOOP
     self.second = interval
     msg = MsgPack(Message.MSG_INNER_TIMER, param)
     uid = msg.get_param('userId')
     gid = msg.get_param('gameId')
     msg = MsgLine(msg.pack(), gid, target=uid).pack()
     task = self.tasklet(Message.to_inner(Message.MSG_INNER_TIMER), msg,
                         None).run
     if delay is None:
         self.timer = TaskManager.call_later(self.__timeout, self.second,
                                             msg, task, *args, **kwargs)
     else:
         self.timer = TaskManager.call_later(self.__timeout, delay, msg,
                                             task, *args, **kwargs)
     return True
Beispiel #7
0
    def __send_to_server(self, uid, msg, dst, sid, cmd, gid):
        if sid is None:
            ids = self.map_server_type.get(dst, [])
            if not ids:
                return False
            sid = ids[uid % len(ids)]
        conn = self.map_server_connect.get(sid, None)
        if not conn:
            return False

        if cmd is None:
            if isinstance(msg, str):
                return False
            cmd = msg.get_cmd()

        cmd = Message.to_inner(cmd)
        if not isinstance(msg, str):
            msg = msg.pack()

        mo = MsgLine(msg, gid, target=uid)
        return conn.sendMsg(cmd, mo.pack())