最近联系人相关信令和协议设计
enum BuddyListCmdID {
CID_BUDDY_LIST_RECENT_CONTACT_SESSION_REQUEST = 513,
CID_BUDDY_LIST_RECENT_CONTACT_SESSION_RESPONSE = 514,
};
流程图
根据最新的时间戳查找最新的会话 更新客户端的会话时间
/**
* 获取最近会话接口
*
* @param pPdu 收到的packet包指针
* @param conn_uuid 该包过来的socket 描述符
*/
void getRecentSession(CImPdu* pPdu, uint32_t conn_uuid)
{
IM::Buddy::IMRecentContactSessionReq msg;
IM::Buddy::IMRecentContactSessionRsp msgResp;
if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength()))
{
CImPdu* pPduResp = new CImPdu;
uint32_t nUserId = msg.user_id();
uint32_t nLastTime = msg.latest_update_time();
//获取最近联系人列表
list<IM::BaseDefine::ContactSessionInfo> lsContactList;
CSessionModel::getInstance()->getRecentSession(nUserId, nLastTime, lsContactList);
msgResp.set_user_id(nUserId);
for(auto it=lsContactList.begin(); it!=lsContactList.end(); ++it)
{
IM::BaseDefine::ContactSessionInfo* pContact = msgResp.add_contact_session_list();
// *pContact = *it;
pContact->set_session_id(it->session_id());
pContact->set_session_type(it->session_type());
pContact->set_session_status(it->session_status());
pContact->set_updated_time(it->updated_time());
pContact->set_latest_msg_id(it->latest_msg_id());
pContact->set_latest_msg_data(it->latest_msg_data());
pContact->set_latest_msg_type(it->latest_msg_type());
pContact->set_latest_msg_from_user_id(it->latest_msg_from_user_id());
}
log("userId=%u, last_time=%u, count=%u", nUserId, nLastTime, msgResp.contact_session_list_size());
msgResp.set_attach_data(msg.attach_data());
pPduResp->SetPBMsg(&msgResp);
pPduResp->SetSeqNum(pPdu->GetSeqNum());
pPduResp->SetServiceId(IM::BaseDefine::SID_BUDDY_LIST);
pPduResp->SetCommandId(IM::BaseDefine::CID_BUDDY_LIST_RECENT_CONTACT_SESSION_RESPONSE);
CProxyConn::AddResponsePdu(conn_uuid, pPduResp);
}
else
{
log("parse pb failed");
}
}
void CSessionModel::getRecentSession(uint32_t nUserId, uint32_t lastTime, list<IM::BaseDefine::ContactSessionInfo>& lsContact)
{
CDBManager* pDBManager = CDBManager::getInstance();
CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_slave");
if (pDBConn)
{
string strSql = "select * from IMRecentSession where userId = " + int2string(nUserId) + " and status = 0 and updated >" + int2string(lastTime) + " order by updated desc limit 100";
CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str());
if (pResultSet)
{
while (pResultSet->Next())
{
IM::BaseDefine::ContactSessionInfo cRelate;
uint32_t nPeerId = pResultSet->GetInt("peerId");
cRelate.set_session_id(nPeerId);
cRelate.set_session_status(::IM::BaseDefine::SessionStatusType(pResultSet->GetInt("status")));
IM::BaseDefine::SessionType nSessionType = IM::BaseDefine::SessionType(pResultSet->GetInt("type"));
if(IM::BaseDefine::SessionType_IsValid(nSessionType))
{
cRelate.set_session_type(IM::BaseDefine::SessionType(nSessionType));
cRelate.set_updated_time(pResultSet->GetInt("updated"));
lsContact.push_back(cRelate);
}
else
{
log("invalid sessionType. userId=%u, peerId=%u, sessionType=%u", nUserId, nPeerId, nSessionType);
}
}
delete pResultSet;
}
else
{
log("no result set for sql: %s", strSql.c_str());
}
pDBManager->RelDBConn(pDBConn);
if(!lsContact.empty())
{
fillSessionMsg(nUserId, lsContact);
}
}
else
{
log("no db connection for teamtalk_slave");
}
}