MsgMgr.cpp
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include "stdafx.h"
- #include "MsgMgr.h"
- CMsgMgr::CMsgMgr(void)
- : m_uMsgCounter(0)
- {
- }
- CMsgMgr::~CMsgMgr(void)
- {
- }
- MSGID CMsgMgr::CreateMsgID(UINT MsgFlags)
- {
- CAutoLock Lock(&m_CritSec);
- m_uMsgCounter++;
- //MSGDATA *pMSGDATA = new MSGDATA;
- //pMSGDATA->MsgType = MsgType;
- //pMSGDATA->strMsg = _TEXT("");
- MSGDATA msg;
- msg.MsgFlags = MsgFlags;
- msg.strMsg = _TEXT("");
- msg.b_visited = FALSE;
- msg.m_updated_tickcount = 0;
- m_MsgMap.insert(MsgMap::value_type(m_uMsgCounter, msg));
- return m_uMsgCounter;
- }
- void CMsgMgr::DeleteMsgID(MSGID MsgID)
- {
- CAutoLock Lock(&m_CritSec);
- m_MsgMap.erase(MsgID);
- }
- void CMsgMgr::UpdateMessage(MSGID MsgID, const CString &strNewMsg, UINT msgFlags, MESSAGELEVEL msglv)
- {
- CAutoLock Lock(&m_CritSec);
- MsgMap::iterator itr = m_MsgMap.find(MsgID);
- if(itr != m_MsgMap.end())
- {
- //不能更新为较低等级的消息
- if(!IsMsgExpired(itr->second) && itr->second.m_msgLevel > msglv)
- return;
- itr->second.strMsg = strNewMsg;
- if(msgFlags != NOUPDATE)
- {
- itr->second.MsgFlags = msgFlags;
- itr->second.m_msgLevel = msglv;
- }
- itr->second.m_updated_tickcount = GetTickCount();
- }
- }
- void CMsgMgr::ClearMessage(MSGID MsgID)
- {
- CAutoLock Lock(&m_CritSec);
- MsgMap::iterator itr = m_MsgMap.find(MsgID);
- if(itr != m_MsgMap.end())
- {
- itr->second.strMsg.Empty();
- }
- }
- const CString CMsgMgr::PeerMessage(MSGID MsgID)
- {
- CString tmpstr;
- if(DispatchMessage(MsgID, tmpstr, GMF_NOVISITEDFLAG | GMF_NOCLEANFORCEFLAG))
- return tmpstr;
- else
- return _T("");
- }
- BOOL CMsgMgr::IsMsgExpired(MSGDATA& msg)
- {
- if((msg.MsgFlags & TIMELESS) != TIMELESS)
- {
- int lifetime = msg.MsgFlags & (TIMELESS - 1);
- if(msg.m_updated_tickcount + lifetime*1000 < GetTickCount())
- return TRUE;
- }
- return FALSE;
- }
- BOOL CMsgMgr::DispatchMessage(const MSGID MsgID, CString& strMsg,
- const UINT getflags, UINT* p_bMsgFlags)
- {
- CAutoLock Lock(&m_CritSec);
- MsgMap::iterator itr = m_MsgMap.find(MsgID);
- if(itr != m_MsgMap.end())
- {
- if(IsMsgExpired(itr->second))
- {
- return FALSE;
- }
- if((getflags & GMF_NOOLDMSG) && (itr->second.b_visited == TRUE))
- return FALSE;
- //获得消息信息,包括消息的文本串和消息类型
- strMsg = itr->second.strMsg;
- if(p_bMsgFlags != NULL)
- *p_bMsgFlags = itr->second.MsgFlags;
- if(!(getflags & GMF_NOVISITEDFLAG))
- itr->second.b_visited = TRUE;
- if(!(getflags & GMF_NOCLEANFORCEFLAG))
- itr->second.MsgFlags &= (~(FORCESHOW | FORCEHIDE));
- return TRUE;
- }
- return FALSE;
- }
- UINT CMsgMgr::GetMsgNum()
- {
- CAutoLock Lock(&m_CritSec);
- UINT uMsgNum = (UINT) m_MsgMap.size();
- return uMsgNum;
- }
- void CMsgMgr::GetMsgIDArray(MSGID *pMsgIDArray)
- {
- CAutoLock Lock(&m_CritSec);
- int i = 0;
- MsgMap::iterator itr = NULL;
- for(itr=m_MsgMap.begin(); itr!=m_MsgMap.end(); itr++)
- {
- *(pMsgIDArray+i) = itr->first;
- i++;
- }
- }