#include "StdAfx.h" #include "MsgCenter.h" #include "IMsgObserver.h" SINGLETON_IMPLEMENT(CMsgCenter) CMsgCenter::CMsgCenter(void) : m_Stop(false) { m_pThreadObj = AfxBeginThread(__dipatchThreadFunc, this); } CMsgCenter::~CMsgCenter(void) { m_Stop = true; if (m_pThreadObj != NULL) { DWORD ExitCode; GetExitCodeThread(m_pThreadObj->m_hThread, &ExitCode); if (ExitCode == STILL_ACTIVE) { m_ThreadSleepFlag.SetEvent(); WaitForSingleObject(m_pThreadObj->m_hThread, INFINITE); } m_pThreadObj = NULL; } } /***************************************************************** **【函数名称】 __wait **【函数功能】 置线程于等待信号状态 **【参数】 **【返回值】 ****************************************************************/ void CMsgCenter::__wait(void) { m_ThreadSleepFlag.Lock(); } /***************************************************************** **【函数名称】 __releaseMsg **【函数功能】 释放消息 **【参数】 **【返回值】 ****************************************************************/ void CMsgCenter::__releaseMsg(UINT MsgType, PARAM lpContent) { // 填充事件内容 switch (MsgType) { case DEV_EVENT_LOG: // 设备日志 { //delete (EventLog*)lpContent; auto pEvtLog = (EventLog*)lpContent; if (pEvtLog != nullptr) { delete pEvtLog; pEvtLog = nullptr; } } break; case DEV_EVENT_RES_TYPE: // 设备资源类型 { //delete (EventResType*)lpContent; auto pEvtResType = (EventResType*)lpContent; if (pEvtResType != nullptr) { delete pEvtResType; pEvtResType = nullptr; } } break; case DEV_EVENT_RES_DETAIL: // 设备资源明细 { //delete (EventResDetail*)lpContent; auto pEvtResDetail = (EventResDetail*)lpContent; if (pEvtResDetail != nullptr) { delete pEvtResDetail; pEvtResDetail = nullptr; } } break; case DEV_EVENT_RES_STATUS: // 设备资源状态变化 { //delete (EventResStatus*)lpContent; auto pEvtResStatus = (EventResStatus*)lpContent; if (pEvtResStatus != nullptr) { delete pEvtResStatus; pEvtResStatus = nullptr; } } break; case DEV_EVENT_LINE_OP_PROCESS: // 线路操作进展 { // delete (EventOpProcess*)lpContent; auto pEvtOpProcess = (EventOpProcess*)lpContent; if (pEvtOpProcess != nullptr) { delete pEvtOpProcess; pEvtOpProcess = nullptr; } } break; case DEV_EVENT_LINE_OP_RESULT: // 线路操作执行结果 { //delete (EventOpResult*)lpContent; // 2021-02-04 EventOpResult* pEvtOpResult = (EventOpResult*)lpContent; if (pEvtOpResult != nullptr) { delete pEvtOpResult; pEvtOpResult = nullptr; } } break; case DEV_EVENT_DEV_OPERATOR: // 设备主动操作事件 { //delete (EventDevOperation*)lpContent; auto pEvtDevOperation = (EventDevOperation*)lpContent; if (pEvtDevOperation != nullptr) { delete pEvtDevOperation; pEvtDevOperation = nullptr; } } break; } // end switch } /***************************************************************** **【函数名称】 __dispatchMsg **【函数功能】 分派消息 **【参数】 **【返回值】 ****************************************************************/ int CMsgCenter::__dispatchMsg(void) { CSingleLock Locker(&m_RWLock, TRUE); // 获取事件队列信息 if (m_MsgList.IsEmpty()) return 0; MSG_INNER* pMsg = m_MsgList.RemoveHead(); Locker.Unlock(); // 根据实际类型分发消息 __sendMsgToObserver(pMsg->MsgType, pMsg->Content); __releaseMsg(pMsg->MsgType, pMsg->Content); // 删除节点,释放内存 delete pMsg; pMsg = NULL; // 返回 return m_MsgList.GetCount(); } /***************************************************************** **【函数名称】 __sendMsgToObserver **【函数功能】 消息发送给订阅者 **【参数】 **【返回值】 ****************************************************************/ void CMsgCenter::__sendMsgToObserver(UINT MsgType, const PARAM lpContent) { ObsvrMap::iterator itr; ObsvrMap::iterator itrEnd; itr = m_ObsvrMap.find(MsgType); if (itr == m_ObsvrMap.end()) return; itrEnd = m_ObsvrMap.upper_bound(MsgType); for (; itr != itrEnd; ++itr) { if (itr->second != nullptr) // 2021-02-02 增加判断是否是空,防止null造成异常 { itr->second->onMessage(MsgType, lpContent); } else { ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_WARNING, _T("{CMsgCenter}: 消息中心任务分发失败,MsgType[%u],[%u]"), MsgType, itr->first); } } } /***************************************************************** **【函数名称】 __dipatchThreadFunc **【函数功能】 消息分发线程函数 **【参数】 **【返回值】 ****************************************************************/ UINT CMsgCenter::__dipatchThreadFunc(LPVOID pParam) { CMsgCenter* pSelf = (CMsgCenter*)pParam; while (!pSelf->m_Stop) { if (pSelf->__dispatchMsg() == 0) pSelf->__wait(); } // end while return 0; } /***************************************************************** **【函数名称】 regist **【函数功能】 订阅消息 **【参数】 **【返回值】 ****************************************************************/ void CMsgCenter::regist(UINT MsgType, IMsgObserver* pObserver) { m_ObsvrMap.insert(pair(MsgType, pObserver)); } /***************************************************************** **【函数名称】 unregist **【函数功能】 取消订阅 **【参数】 **【返回值】 ****************************************************************/ void CMsgCenter::unregist(UINT MsgType, IMsgObserver* pObserver) { ObsvrMap::iterator itr; ObsvrMap::iterator itrEnd; itr = m_ObsvrMap.find(MsgType); if (itr == m_ObsvrMap.end()) return; itrEnd = m_ObsvrMap.upper_bound(MsgType); while (itr != itrEnd) { if (itr->second == pObserver) { m_ObsvrMap.erase(itr); break; } itr++; } } /***************************************************************** **【函数名称】 pushMsg **【函数功能】 压入消息 **【参数】 **【返回值】 ****************************************************************/ void CMsgCenter::pushMsg(UINT MsgType, const PARAM lpContent) { // 生成消息实体 MSG_INNER* pMsg = new(std::nothrow) MSG_INNER; memset(pMsg, 0, sizeof(pMsg)); pMsg->MsgType = MsgType; // 消息类型 // 填充事件内容 switch (MsgType) { case DEV_EVENT_LOG: // 设备日志 { pMsg->Content = new(std::nothrow) EventLog; memcpy(pMsg->Content, lpContent, sizeof(EventLog)); } break; case DEV_EVENT_RES_TYPE: // 设备资源类型 { pMsg->Content = new(std::nothrow) EventResType; memcpy(pMsg->Content, lpContent, sizeof(EventResType)); } break; case DEV_EVENT_RES_DETAIL: // 设备资源明细 { pMsg->Content = new(std::nothrow) EventResDetail; memcpy(pMsg->Content, lpContent, sizeof(EventResDetail)); } break; case DEV_EVENT_INIT_END: // 设备初始化结束 case DEV_EVENT_DEV_CLOSED: // 设备已关闭 { pMsg->Content = NULL; } break; case DEV_EVENT_RES_STATUS: // 设备资源状态变化 { pMsg->Content = new(std::nothrow) EventResStatus; memcpy(pMsg->Content, lpContent, sizeof(EventResStatus)); } break; case DEV_EVENT_LINE_OP_PROCESS: // 线路操作进展 { pMsg->Content = new(std::nothrow) EventOpProcess; memcpy(pMsg->Content, lpContent, sizeof(EventOpProcess)); } break; case DEV_EVENT_LINE_OP_RESULT: // 线路操作执行结果 { pMsg->Content = new(std::nothrow) EventOpResult; memcpy(pMsg->Content, lpContent, sizeof(EventOpResult)); } break; case DEV_EVENT_DEV_OPERATOR: // 设备主动操作事件 { pMsg->Content = new(std::nothrow) EventDevOperation; memcpy(pMsg->Content, lpContent, sizeof(EventDevOperation)); } break; } // end switch m_RWLock.Lock(); // 互斥加锁 // 将消息插入缓冲队列 m_MsgList.AddTail(pMsg); m_RWLock.Unlock(); // 需要唤醒分发线程 m_ThreadSleepFlag.SetEvent(); }