Example #1
0
    def handle(self, channel, request):
        recData = common.decodePacket(request)
        if not recData: #不合法的消息
            print '收到一个不合法的请求'
            print request
            return False # + 断开连接

        if recData[0] in self.channels[channel]['process']: #进程列表中已有该进程
            res = {'opt_key':recData[1], 'opt_id':recData[0],'flag':2}
            return common.encodePacket(res)
        
        self.channels[channel]['process'].add(recData[0]) #添加进程
        
        print '所有的channel信息', self.channels
        print '收到来自%s的请求' % str(channel.peername)
        print recData
        
        res = self.checkOpt(channel, recData[0], recData[1], recData[2])
        
        print '对该请求的处理结果是'
        print res
        
        self.channels[channel]['process'].remove(recData[0]) #移除进程
        
        if res in [False, None]: #False 断开连接  None 不回复客户端消息
            return res
        else:
            return common.encodePacket(res)
Example #2
0
    def multicast(users, content=None):
        '''组播'''
        #users - (userid, serverid) or [(userid1, serverid1), ...]
        try:
            if isinstance(users, tuple):
                users = [users]

            print '要发送的用户', users
            snd_cnt = 0 #发送的客户端个数
            content = common.encodePacket(content) #对封包发送前的处理,在此处。
            assert isinstance(content, basestring), '发送的内容应是字符串'
            for user in users:
                if user in Gcore.clients:
                    Gcore.clients[user]['channel']._send(content)
                    print 'send to', user, 'content', content
                    print '-' * 20
                    snd_cnt += 1
            return snd_cnt #返回发送的客户端数量
        except Exception:
            return False #组播失败,返回False。
Example #3
0
    def multicast(users, content=None):
        '''组播'''
        #users - (userid, serverid) or [(userid1, serverid1), ...]
        try:
            if isinstance(users, tuple):
                users = [users]

            print '要发送的用户', users
            snd_cnt = 0  #发送的客户端个数
            content = common.encodePacket(content)  #对封包发送前的处理,在此处。
            assert isinstance(content, basestring), '发送的内容应是字符串'
            for user in users:
                if user in Gcore.clients:
                    Gcore.clients[user]['channel']._send(content)
                    print 'send to', user, 'content', content
                    print '-' * 20
                    snd_cnt += 1
            return snd_cnt  #返回发送的客户端数量
        except Exception:
            return False  #组播失败,返回False。