Ejemplo n.º 1
0
    def mainloop(self, socket_, address):
        """mainloop方法对应每个客户端都是一个协程
        """
        jb_sock = JBSocket(socket_, comm.tuple_as_md5(socket_.getpeername()))
        if jb_sock.sid not in JBServer.client_dict:
            self.client_dict[jb_sock.sid] = jb_sock

        while 1:
            try:
                header_data = jb_sock.recv(4)
                if header_data:
                    tid, len_ = comm.struct_unpack(header_data)
                    content_data = jb_sock.recv(len_)
                    if content_data:
                        content_data = comm.unpack_data(content_data)
                        self.on_message(jb_sock, tid, content_data)
                else:
                    break
            except Exception, e:
                comm.print_exception(e)
                break
Ejemplo n.º 2
0
def you_recv(s):
    while 1:
        if int(time.time()) % 10 == 0:
            protocol.C2S_HEARTBEAT.data = int(time.time())
            p = comm.copy_protocol('C2S_HEARTBEAT')
            p.set_data(int(time.time()))
            s.sendall(comm.pack_data(p.type_, p.data))
        print '------------'

        r, w, x = select.select([s, sys.stdin], [], [])
        for rr in r:
            if rr is s:
                header_reply = rr.recv(4)
                if header_reply:
                    type_, len_ = comm.struct_unpack(header_reply)
                    content_reply = rr.recv(len_)
                    content_reply = comm.unpack_data(content_reply)
                    print type_
                    print protocol.S2C_VERSION.type_
                    if protocol.S2C_VERSION.eq(type_):
                        print 'Version right!!'
                    elif protocol.ERR_VERSION.eq(type_):
                        print 'Version wrong!!'
                    elif protocol.S2C_HEARTBEAT.eq(type_):
                        print 'heartbeating ...'
                    elif protocol.S2C_LOGIN.eq(type_):
                        print 'S2C_LOGIN ...'


                    # print "type = %s, content=%s" % (type_, content_reply)
                
            else:
                msg = rr.readline()
                protocol.C2S_VERSION.data = '0.0.1'
                p = comm.copy_protocol('C2S_LOGIN')
                p.set_data(json.dumps({'name': 'daimin2', 'passwd': '123'}))
                s.sendall(comm.pack_data(p.type_, p.data))
        gevent.sleep(0)
Ejemplo n.º 3
0
def you_recv(s):
    while 1:
        if int(time.time()) % 10 == 0:
            protocol.C2S_HEARTBEAT.data = int(time.time())
            p = comm.copy_protocol('C2S_HEARTBEAT')
            p.set_data(int(time.time()))
            s.sendall(comm.pack_data(p.type_, p.data))
        print '------------'

        r, w, x = select.select([s, sys.stdin], [], [])
        for rr in r:
            if rr is s:
                header_reply = rr.recv(4)
                if header_reply:
                    type_, len_ = comm.struct_unpack(header_reply)
                    content_reply = rr.recv(len_)
                    content_reply = comm.unpack_data(content_reply)
                    print type_
                    print protocol.S2C_VERSION.type_
                    if protocol.S2C_VERSION.eq(type_):
                        print 'Version right!!'
                    elif protocol.ERR_VERSION.eq(type_):
                        print 'Version wrong!!'
                    elif protocol.S2C_HEARTBEAT.eq(type_):
                        print 'heartbeating ...'
                    elif protocol.S2C_LOGIN.eq(type_):
                        print 'S2C_LOGIN ...'

                    # print "type = %s, content=%s" % (type_, content_reply)

            else:
                msg = rr.readline()
                protocol.C2S_VERSION.data = '0.0.1'
                p = comm.copy_protocol('C2S_LOGIN')
                p.set_data(json.dumps({'name': 'daimin2', 'passwd': '123'}))
                s.sendall(comm.pack_data(p.type_, p.data))
        gevent.sleep(0)