Exemple #1
0
    int32	login(const std::string& cid, const std::string& pwd,
                  int enc, const std::string& version) {
        int32 ret = 0;
        LoginRequest lgr;
        lgr.set_cid(cid);
        lgr.set_enc(enc);
        lgr.set_version(version);

        std::string s = cid + pwd + version + ef::itostr(time(NULL));
        std::string tk;
        ef::MD5Hex(tk, (const ef::uint8*)s.data(), s.size());
        lgr.set_token(tk);

        m_cid = cid;
        m_pwd = pwd;
        LoginResponse lgresp;
        std::string req;
        lgr.SerializeToString(&req);

        std::string rsp;
        ret = send_req(100, req);

        if(ret < 0) {
            LOG_OUT << "test_login send fail, cid:" << m_cid
                    << std::endl;
            return  ret;
        }

        int32 cmd = 0;
        ret = recv_resp(cmd, rsp);
        if(ret <= 0) {
            LOG_OUT << "login recv_resp fail, cid:" << m_cid
                    << std::endl;
            return ret;
        }
        //redirect
        if(cmd == 1001) {
            RedirectResponse rdresp;
            rdresp.ParseFromString(rsp);
            disconnect();
            if(rdresp.status() != 0 || !rdresp.addrs_size()) {
                return -1;
            }
            int i = 0;
            for(; i < rdresp.addrs_size(); ++i) {
                const Address& a = rdresp.addrs(i);
                LOG_OUT << "test_login redirect, cid:"
                        << m_cid << ", addr:" << a.ip()
                        << ", port:" << a.port() << std::endl;
                ret = bind_connect(a.ip(), a.port(),
                                   m_local_ip, m_local_port);
                if(ret < 0) {
                    LOG_OUT << "test_login redirect fail, cid:"
                            << m_cid << std::endl;
                } else {
                    break;
                }
            }
            if(i < rdresp.addrs_size())
                return	login(m_cid, pwd, enc, version);
        }

        lgresp.ParseFromString(rsp);
        if(lgresp.status() == 0) {
            LOG_OUT << "test_login success: cid:" << m_cid
                    << ", sessid:" << lgresp.sessid() << std::endl;
        } else {
            LOG_OUT << "test_login fail: cid:" << m_cid
                    << ", status:"
                    << lgresp.status() << std::endl;
            return -1;
        }
        m_sessid = lgresp.sessid();
        ++success_count;
        return	ret;
    }
Exemple #2
0
	int32	login(const std::string& cid, const std::string& version, 
		const std::string& token){
		int32 ret = 0;
		LoginRequest lgr;

		string enctk;

		if(token.size()){
			ef::encrypt(token, enctk);	
		}	

		lgr.set_id(cid);		
		lgr.set_type(-1);
		lgr.set_version(version);	
		lgr.set_token(enctk);
	
		m_cid = cid;
		m_token = token;
		LoginResponse lgresp;
		std::string req;
		lgr.SerializeToString(&req);	
	
		std::string rsp;
		ret = send_req(100, req);

		if(ret < 0){
			LOG_OUT << "test_login send fail, cid:" << m_cid 
				<< std::endl;
			return  ret;
		}
		
		int32 cmd = 0;
		ret = recv_resp(cmd, rsp);	
		if(ret <= 0){
			LOG_OUT << "login recv_resp fail, cid:" << m_cid
				<< std::endl;
			return ret;
		}
		//redirect
		if(cmd == 1001){
			RedirectResponse rdresp;
			rdresp.ParseFromString(rsp);
			disconnect();
			if(rdresp.status() != 0 || !rdresp.addrs_size()){
				return -1;
			}
			int i = 0;
			for(; i < rdresp.addrs_size(); ++i){
				const Address& a = rdresp.addrs(i);
				LOG_OUT << "test_login redirect, cid:"
					<< m_cid << ", addr:" << a.ip()
					<< ", port:" << a.port() << std::endl;
				ret = bind_connect(a.ip(), a.port(), 
					m_local_ip, m_local_port);
				if(ret < 0){
					LOG_OUT << "test_login redirect fail, cid:"
						<< m_cid << std::endl;
				}else{
					break;
				}
			}
			if(i < rdresp.addrs_size())
				return	login(m_cid, version, token);
		}

		lgresp.ParseFromString(rsp);
		if(lgresp.status() == 0){
			LOG_OUT << "test_login success: cid:" << m_cid 
				<< ", sessid:" << lgresp.sessid() << std::endl;	
		}else{
			LOG_OUT << "test_login fail: cid:" << m_cid 
				<< ", status:" 
				<< lgresp.status() << std::endl;	
			return -1;
		}
		m_sessid = lgresp.sessid();
		++success_count;
		if(token.size()){
			m_key = token + m_sessid;
		}
		return	ret;
	}