BOOL MessageModule_Impl::getHistoryMessage(IN const std::string& sId, IN UInt32 nMsgCount , IN BOOL scrollBottom, OUT std::vector<MessageEntity>& msgList) { //step 0 get message id from cache module::SessionEntity sessEntity; if (!module::getSessionModule()->getSessionEntityBySId(sId, sessEntity)) { LOGA__(ERR,"getSessionEntityBySId failed,sessionId:%s",util::stringToCString(sId)); return FALSE; } UInt32 topMsgId = _getSessionTopMsgId(sId); if (0 == topMsgId) { topMsgId = sessEntity.latestmsgId; } //step 1 query history message from local db std::vector<MessageEntity> msgListTemp; module::getDatabaseModule()->sqlGetHistoryMessage(sId, topMsgId, nMsgCount, msgListTemp); //step 2 compare the topMsgId to latest local db msgId and check which is sequential if (!msgListTemp.empty()) { //todo 还有优化的余地,取出的比msgCount+1的数量,看是否连续的。 MessageEntity& latestMsgEntity = msgListTemp.front(); MessageEntity& firstMsgEntity = msgListTemp.back(); if ((topMsgId == latestMsgEntity.msgId) && ((firstMsgEntity.msgId + nMsgCount - 1) == latestMsgEntity.msgId)) { msgList = msgListTemp; setSessionTopMsgId(sId, firstMsgEntity.msgId-1); return TRUE; } } //final fetch history message from server db imcore::IMLibCoreStartOperationWithLambda( [=]()mutable { LOG__(APP, _T("IMGetMsgListReq history message ,sessionId = %s,sessionName=%s,last msgId = %d,Count = %d") , util::stringToCString(sessEntity.sessionID) ,sessEntity.getName() ,topMsgId, nMsgCount); IM::Message::IMGetMsgListReq imGetMsgListReq; imGetMsgListReq.set_user_id(module::getSysConfigModule()->userId()); imGetMsgListReq.set_session_type((IM::BaseDefine::SessionType)sessEntity.sessionType); imGetMsgListReq.set_session_id(sessEntity.getOriginIntegerSessionId()); imGetMsgListReq.set_msg_id_begin(topMsgId);//服务端是包含当前的那一条的 imGetMsgListReq.set_msg_cnt(nMsgCount); UInt16 reserved = (TRUE == scrollBottom) ? imcore::RESERVED_TYPE_HISTORY_SCROLLBOTTOM_MESSAGE : imcore::RESERVED_TYPE_HISTORY_MESSAGE; module::getTcpClientModule()->sendPacket(IM::BaseDefine::SID_MSG , IM::BaseDefine::CID_MSG_LIST_REQUEST, reserved , &imGetMsgListReq); } ); return FALSE; }
uint32_t ClientConn::getMsgList(uint32_t nUserId, IM::BaseDefine::SessionType nType, uint32_t nPeerId, uint32_t nMsgId, uint32_t nMsgCnt) { CImPdu cPdu; IM::Message::IMGetMsgListReq msg; msg.set_user_id(nUserId); msg.set_session_type(nType); msg.set_session_id(nPeerId); msg.set_msg_id_begin(nMsgId); msg.set_msg_cnt(nMsgCnt); cPdu.SetPBMsg(&msg); cPdu.SetServiceId(IM::BaseDefine::SID_MSG); cPdu.SetCommandId(IM::BaseDefine::CID_MSG_LIST_REQUEST); uint32_t nSeqNo = m_pSeqAlloctor->getSeq(ALLOCTOR_PACKET); cPdu.SetSeqNum(nSeqNo); SendPdu(&cPdu); return nSeqNo; }