Beispiel #1
0
void CometResponseStat::deliver(const SessionPtr& session, int status_code,
		int cache_time, bool is_gzip, bool keep_alive,
		const string& extra_header, const string& body, bool bin_data,
		const string& content_type, const string& nogzipmsg) {
	ConnectionStat::deliver(session, status_code, cache_time, is_gzip,
			keep_alive, extra_header, body, bin_data, content_type, nogzipmsg);
	if (keep_alive || status_code == 200) {
		ClientPoolManager::instance().getClientPool(session->jid()->index).unbind(session->jid()->index, session->connectionId());
	}
}
Beispiel #2
0
bool ClientPool::bind(Ice::Long connection, const JidPtr& j,
		const string& ticket, bool flush) {
	MCE_DEBUG("ClientPool::bind --> connectionId:" << connection << " jid:" << jidToString(j));
	int isBind = 0;
	Ice::Long oldConId = 0;
	bool res = true;
	{
		IceUtil::Mutex::Lock lock(_mutex);
		map<Ice::Long,ClientBufferPtr>::iterator it = _clients.find(j->index);
		if (it == _clients.end()) {
			ClientBufferPtr c = new ClientBuffer();
			c->bind(j,connection);
			_clients[j->index] = c;
			c->timestamp(time(NULL));
			isBind = 1;
		} else {
			//if(flush && it->second->isFlush()) {
			//	isBind = 2;
			//}
			oldConId = it->second->activeConnection();
			bool res = it->second->bind(j,connection);
			if(res == false) {
				return false;
			}
			it->second->timestamp(time(NULL));

			if(oldConId != connection && oldConId) {
				IdleConnManager::instance().cometConnSwitch();
			}else{
				oldConId = 0;
			}
		}
	}

	if(isBind > 0){
		try {
			MCE_INFO("ClientPool::bind --> invoke WTalkProxy bind " << isBind << ", " << jidToString(j) << ", ac_conn " << connection);
			res = WTalkProxyAdapter::instance().bind(boost::lexical_cast<string>(j->index), ticket, j);
		} catch(Ice::Exception& e) {
			MCE_WARN("ClientPool::bind --> invoke WTalkProxy bind err" << isBind << ", " << jidToString(j) << ", ac_conn " << connection << ", " << e);
		}
	}
	if(oldConId > 0){
		SessionPtr s = Server::instance().getSession(oldConId);
		SessionPtr newSession = Server::instance().getSession(connection);
		if (s) {
			s->needUnbind(false);
			MCE_DEBUG("ClientPool::bind --> jid " << jidToString(j)
				<< " change from " << s->jid()->userId << "_" << oldConId
				<< " to " << newSession->jid()->userId << "_" << connection);
			s->responseClose(211,"code:211, comet old connection close");
		}
	}
	return res;
}
Beispiel #3
0
void CometRecvStat::deliver(const SessionPtr& session, int status_code,
		int cache_time, bool is_gzip, bool keep_alive,
		const string& extra_header, const string& body, bool bin_data,
		const string& content_type) {
	ConnectionStat::deliver(session, status_code, cache_time, is_gzip,
			keep_alive, extra_header, body, bin_data, content_type);
	//MCE_DEBUG("CometRecvStat::deliver --> " << jidToString(session->jid()) << " " << body);
	if (keep_alive || status_code == 200) {
		ClientPoolManager::instance().getClientPool(session->jid()->index).unbind(session->jid()->index, session->connectionId());
	}
}
Beispiel #4
0
void SessionListener::processPresence(const SessionPtr& session, const xml_document_ptr& doc) {
    string to = doc->child("presence").attribute("to").value();
    if (to.empty()) {
        session->presence(doc->xml());
    } else {
        MCE_DEBUG("presence to muc, " << to);
        IdType type = idType(to);
        if (type == ROOMID || type == ROOMJID) {
            MCE_DEBUG("presence to muc, " << to);
            //MucPresenceAdapter::instance().message(session->jid(), doc->xml());
            MucPresenceMsgHandlerAdapter::instance().message(session->jid(), doc->xml());
        } else if (type == JID) {
            session->presence(doc->xml());
        } else {
            MCE_INFO("DefultAction::execute --> invalid id");
        }

    }

    if (session->state() == SessionStateLogin) {
        LoginProcedureStat::instance().incPresence();
        string type = doc->child("presence").attribute("type").value();
        if (type != "subscribe" && type != "subscribed" && type
                != "unsubscribed") {
            string ver = "";
            string subver = "";
            string ext = "";
            string secver = "";
            string show = "";
            xml_node cNode = doc->child("presence").child("c");
            if (cNode) {
                ver = cNode.attribute("ver").value();
                ext = cNode.attribute("ext").value();
                subver = cNode.attribute("subver").value();
                secver = cNode.attribute("secver").value();
            }
            xml_node showNode = doc->child("presence").child("show");
            if (showNode) {
                show = showNode.first_child().value();
            }
            try {
                PresenceMsgHandlerAdapter::instance().presenceWithSecver(session->jid(),show,ver,subver,ext,secver);
            } catch(Ice::Exception& e) {
                MCE_WARN("PresenceAction::handle --> talklogic presence err :"<<e);
            }
            doc->child("presence").remove_child("status");
            session->state(SessionStateChat);
        }
    }

}
Beispiel #5
0
void SessionListener::processPresence(const SessionPtr& session, const xml_document_ptr& doc){
	xml_node hiNode = doc->child("hi");
	string userid = hiNode.child("userid").child_value();
	string ticket = hiNode.child("ticket").child_value();
	string token = hiNode.child("session").child_value();

	MCE_INFO("LoginAction::execute -> userid: " << userid << ", ticket: "
			<< ticket << ", token: " << token);
	JidPtr jid = new Jid();
	try {
		jid->userId = boost::lexical_cast<int>(userid);
	} catch (std::exception& e) {
		MCE_WARN("LoginAction::execute -> invalid userid: " << userid << ", " << e.what());
		session->shutdown();
		return;
	} catch (...) {
		MCE_WARN("LoginAction::execute -> invalid userid: " << userid);
		session->shutdown();
		return;
	}
	jid->endpoint = ServiceI::instance().getName();
	session->jid(jid);
	bool success = false;
	try {
		success = session->bind(token, ticket);//WTalkProxyAdapter::instance().bind(token, ticket, jid);
	} catch (Ice::Exception& e) {
		MCE_WARN("LoginAction::execute -> failure binding, jid: " << jidToString(jid) << ", " << e);
		session->shutdown();
		return;
	}
	if (success) {
		//_session->jid(jid);
		session->token(token);
		session->verify(true);

		//Server::instance().join(_session, jid);
		string
				msg =
						"<?xml version=\"1.0\" encoding=\"utf-8\"?><hi cmd=\"login_res\"><res>0</res></hi>";
		msg.push_back('\0');
		if (session->deliver(msg)) {
			MCE_INFO("LoginAction::execute -> success -> userid:"<<userid
					<<"  ticket:"<<ticket);
		} else {
			//Server::instance().leave(_session);
			session->shutdown();
		}
	} else {
		MCE_WARN("LoginAction::execute -> failure binding, userid:" << userid);
		string
				msg =
						"<?xml version=\"1.0\" encoding=\"utf-8\"?><hi cmd='login_res'><res>1</res></hi>";
		msg.push_back('\0');
		session->deliver(msg);
		session->shutdown();
	}
}
Beispiel #6
0
//-----------------------------------------------------------------
void CometResponseStat::deliverNotify(const SessionPtr& session, Ice::Long msgId) {
	string msglist = ClientPoolManager::instance().getClientPool(session->jid()->index).getMsg(session->jid()->index, msgId);
	string msg = msglist;
	if (msglist.empty()) {
		return;
	}
	//MCE_DEBUG("CometResponseStat::deliverNotify --> " << jidToString(session->jid()) << " " << msglist);
	if (session->isGzip()) {
		msglist = gzip_compress(msglist);
	}
	deliver(session, 200, 0, session->isGzip(), true, "", msglist, false, "text/html;charset=UTF-8", msg);
	changeStat(session, &ConnectingStat::instance());
}
Beispiel #7
0
//-------------------------
void WapCometResponseStat::deliverNotify(const SessionPtr& session,
		Ice::Long msgId) {
	//MCE_INFO("WapCometResponseStat::deliverNotify --> connection:" << session->connectionId());
	string msglist = ClientPoolManager::instance().getClientPool(session->jid()->index).getMsg(session->jid()->index, msgId);
	//MCE_INFO("deliver Notify size=" << msglist.size() << " " << msglist);
	if (msglist.empty()) {
		return;
	}

	if (session->isGzip()) {
		msglist = gzip_compress(msglist);
	}

	deliver(session, 200, 0, session->isGzip(), true, "", msglist);
	changeStat(session, &ConnectingStat::instance());
}
Beispiel #8
0
//-----------------------------------------------------------------
void CometRecvStat::response(const RequestPtr& request,
		const SessionPtr& session) {
	//MCE_DEBUG("CometRecvStat::response --> connection:"
	//		<<session->connectionId());
	string strHostId = request->cookie("id");
	int hostid = 0;
	string ticket = request->cookie("t");
	string sessionid = request->cookie("wpsid");
	string header;
	if(!strHostId.empty()){
		try{
			hostid = boost::lexical_cast<int>(strHostId);
		}catch(...){
			MCE_WARN("CometRecvStat::response --> cast err, " << strHostId);
		}
	}
	/*if (kl != "") {
		size_t p = kl.find("_");
		if (p != string::npos) {
			try {
				if(p+1 < kl.length()) {
					hostid = boost::lexical_cast<int>(kl.substr(p+1));
				}
			} catch(std::exception& e) {
				MCE_WARN("CometResponseStat::response --> hostid  not a int  err:"<<e.what());
			}

		}
	}*/

	if (hostid<=0 || ticket == "") {
		deliver(session, 403, 0, false, false, "",
				"ERROR[403]: please login first");
		changeStat(session, &ConnectingStat::instance());
		return;
	}

	Ice::Long msgId = 0;
	try {
		msgId = boost::lexical_cast<Ice::Long>(request->getProperty("mid"));
	} catch(std::exception& e) {
		MCE_WARN("CometRecvStat::response --> cast mid err:"<<e.what());
	}

	bool success = false;
	//if (!session->verify()) {
		try {
			bool needSetCookie = false;
			string domain = "wpi.renren.com";
			if(request->host().find("kaixin") != string::npos){
				domain = "wpi.kaixin.com";
			}
			if(sessionid == "") {
				ostringstream os;
				os << "Set-Cookie: wpsid=" << session->connectionId() <<"; domain=."<<domain<<"; path=/\r\n";
				header = os.str();

				//用户刷新过程中,同一个sessionid可能连接到不同的接入端,导致用户状态不准,所以sessionid+serverid保证唯一
				session->jid(hostid,session->connectionId()+Server::instance().getServerIndex());
				needSetCookie = true;
				MCE_DEBUG("CometRecvStat::response --> sid absent, set cookie " << jidToString(session->jid()));
			} else {
				////////////
				//added by yuanfei
				//new user, set a new session id
				Ice::Long sid = boost::lexical_cast<Ice::Long>(sessionid);
				sid += Server::instance().getServerIndex();
				JidPtr jid = ClientPoolManager::instance().getClientPool(sid).getJid(sid);
				if(jid && jid->userId!=hostid) {
					ostringstream os;
					os << "Set-Cookie: wpsid=" << session->connectionId() <<"; domain=."
					   << domain <<"; path=/\r\n";
					header = os.str();
					session->jid(hostid,session->connectionId()+Server::instance().getServerIndex());
					needSetCookie = true;
					MCE_DEBUG("CometRecvStat::response --> sid present, userid incorrect, set cookie " << jidToString(session->jid()));
				} else {
					session->jid(hostid,sid);
				}
			}

			session->needUnbind(true);
			//MCE_INFO("ClientPool::bind --> " << jidToString(session->jid()) << ", ac_conn " << session->connectionId());
			success = session->bind(ticket,true,false);
			if (success) {
				session->verify(true);
				session->isGzip(request->encoding());

				//for new connection
				if(needSetCookie) {
					//MCE_DEBUG();
					deliver(session, 200, 0, false, true, header,"<msglist><message type='system'><body>login successfully</body></message></msglist>");
					changeStat(session,&ConnectingStat::instance());
					return;
				}
			}else{
				MCE_DEBUG("CometRecvStat::response --> verify failed, " << jidToString(session->jid()));
				deliver(session, 403, 0, false, false, "",
					"ERROR[403]: verify is false");
				changeStat(session, &ConnectingStat::instance());
				return;
			}
		} catch(Ice::Exception& e) {
			MCE_WARN("CometRecvStat::response -> failure binding, userid: " << hostid << ", " << e);
			deliver(session, 403, 0, false, false, "","ERROR[403]: userid bind err");
			changeStat(session,&ConnectingStat::instance());
			return;
		}


	//}else{
	//	success = session->bind(ticket, msgId==0, false);
	//}

	map<string,string> props = request->getProperties();
	if (props.find("ins") != props.end()) {
		MCE_DEBUG("CometRecvStat::response --> do instant return " << jidToString(session->jid()));
		deliver(
				session,
				200,
				0,
				false,
				true,
				"",
				"<msglist><message type='system'><body>init ajax loop</body></message></msglist>");
		changeStat(session, &ConnectingStat::instance());
		return;
	}

	changeStat(session, &CometResponseStat::instance());
	session->deliverNotify(msgId);
}
Beispiel #9
0
//---------
void WapCometRecvStat::response(const RequestPtr& request,
		const SessionPtr& session) {
	MCE_DEBUG("WapCometRecvStat::response --> connection:"
			<<session->connectionId());
	string sid = request->getProperty("sid");
	string uid = request->getProperty("uid");
	string pw = request->getProperty("pw");

	MCE_INFO("@@WapCometRecvStat::response --> " << uid << " " << sid << " " << pw);
	if (uid.empty() || (sid.empty() && pw.empty())) {
		MCE_INFO("WapCometRecvStat::response --> parameters error");
		deliver(session, 403, 0, false, false, "",
				"Authentication Error");
//		changeStat(session, &ConnectingStat::instance());
		return;
	}

	if (sid.empty()) {
		session->jid(boost::lexical_cast<int>(uid),session->connectionId() + Server::instance().getServerIndex());
	} else {
		session->jid(boost::lexical_cast<int>(uid),boost::lexical_cast<Ice::Long>(sid) + Server::instance().getServerIndex());
	}

	Ice::Long msgId = 0;
	try {
		msgId = boost::lexical_cast<Ice::Long>(request->getProperty("mid"));
	} catch(std::exception& e) {
		MCE_WARN("WapCometRecvStat::response --> cast mid err:"<<e.what());
	}

	//一个比较特殊的处理,mid为-1时,用户立即下线
	//考虑再增加一种请求类型
	if(msgId == -1){
		MCE_INFO("WapCometRecvStat::response --> offline now, " << jidToString(session->jid()));
		ClientPoolManager::instance().getClientPool(session->jid()->index).unbind(session->jid()->index, session->connectionId(), true);
		ConnectionStat::deliver(session, 200, 0, false, false, "", "offline now");
		return;
	}
	//

	session->needUnbind(true);
	bool success = false;
	if(!pw.empty()){
		success = session->bind(pw,msgId == 0,true);
	}else{
		success = session->bind(sid,msgId == 0,true);
	}
	if(!success) {
		MCE_INFO("WapCometRecvStat::response --> bind error");
		deliver(session, 403, 0, false, false, "",
		"Authentication Error or Session Expired");
		changeStat(session, &ConnectingStat::instance());
		return;
	}
	session->isGzip(request->encoding());

	if(sid.empty()) {
		deliver(session, 200, 0, false, true, "",boost::lexical_cast<string>(session->connectionId()));
		changeStat(session,&ConnectingStat::instance());
		return;
	}
	//MCE_DEBUG("WapCometRecvStat::response --> line:"<<__LINE__);
	changeStat(session, &WapCometResponseStat::instance());
	//MCE_DEBUG("WapCometRecvStat::response --> line:"<<__LINE__);

	session->deliverNotify(msgId);
	//MCE_DEBUG("CometRecvStat::response --> line:"<<__LINE__);
}