修改三方通话功能,在发起三方通话时,先保持住主叫,然后再拉回主叫到会议

MsgCenter.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "StdAfx.h"
  2. #include "MsgCenter.h"
  3. #include "IMsgObserver.h"
  4. SINGLETON_IMPLEMENT(CMsgCenter)
  5. CMsgCenter::CMsgCenter(void) : m_Stop(false), m_pThreadObj(NULL)
  6. {
  7. }
  8. CMsgCenter::~CMsgCenter(void)
  9. {
  10. m_Stop = true;
  11. if(m_pThreadObj != NULL)
  12. {
  13. DWORD ExitCode;
  14. GetExitCodeThread(m_pThreadObj->m_hThread, &ExitCode);
  15. if(ExitCode == STILL_ACTIVE)
  16. {
  17. m_ThreadSleepFlag.SetEvent();
  18. WaitForSingleObject(m_pThreadObj->m_hThread, INFINITE);
  19. }
  20. m_pThreadObj = NULL;
  21. }
  22. }
  23. /*****************************************************************
  24. **【函数名称】 __wait
  25. **【函数功能】 置线程于等待信号状态
  26. **【参数】
  27. **【返回值】
  28. ****************************************************************/
  29. void CMsgCenter::__wait( void )
  30. {
  31. m_ThreadSleepFlag.Lock();
  32. }
  33. /*****************************************************************
  34. **【函数名称】 __dispatchMsg
  35. **【函数功能】 分派消息
  36. **【参数】
  37. **【返回值】
  38. ****************************************************************/
  39. int CMsgCenter::__dispatchMsg( void )
  40. {
  41. CSingleLock Locker(&m_RWLock, TRUE); // 互斥加锁
  42. // 获取事件队列信息
  43. if(m_MsgList.IsEmpty())
  44. return 0;
  45. MSG_INNER* pMsg = m_MsgList.RemoveHead();
  46. Locker.Unlock();
  47. // 根据实际类型分发消息
  48. __sendMsgToObserver(pMsg->MsgType, pMsg->Content);
  49. __releaseMsg(pMsg->MsgType, pMsg->Content);
  50. // 删除节点,释放内存
  51. delete pMsg;
  52. // 返回
  53. return m_MsgList.GetCount();
  54. }
  55. /*****************************************************************
  56. **【函数名称】 __sendMsgToObserver
  57. **【函数功能】 消息发送给订阅者
  58. **【参数】
  59. **【返回值】
  60. ****************************************************************/
  61. void CMsgCenter::__sendMsgToObserver( UINT MsgType, PARAM lpContent )
  62. {
  63. ObsvrMap::iterator itr;
  64. ObsvrMap::iterator itrEnd;
  65. itr = m_ObsvrMap.find(MsgType);
  66. if(itr == m_ObsvrMap.end())
  67. return;
  68. itrEnd = m_ObsvrMap.upper_bound(MsgType);
  69. for(; itr != itrEnd; ++itr)
  70. {
  71. itr->second->onMessage(MsgType, lpContent);
  72. }
  73. }
  74. /*****************************************************************
  75. **【函数名称】 __releaseMsg
  76. **【函数功能】 释放消息内容
  77. **【参数】
  78. **【返回值】
  79. ****************************************************************/
  80. void CMsgCenter::__releaseMsg( UINT MsgType, PARAM lpContent )
  81. {
  82. if(lpContent == NULL)
  83. return;
  84. switch(MsgType)
  85. {
  86. case SYS_MSG_RES_NEW:
  87. case SYS_MSG_RES_DEL:
  88. case SYS_MSG_RES_STATE:
  89. delete (DEV_RES_ID*)lpContent;
  90. break;
  91. }
  92. }
  93. /*****************************************************************
  94. **【函数名称】 __dipatchThreadFunc
  95. **【函数功能】 消息分发线程函数
  96. **【参数】
  97. **【返回值】
  98. ****************************************************************/
  99. UINT CMsgCenter::__dipatchThreadFunc( LPVOID pParam )
  100. {
  101. CMsgCenter* pSelf = (CMsgCenter*)pParam;
  102. while(!pSelf->m_Stop)
  103. {
  104. if(pSelf->__dispatchMsg() == 0)
  105. pSelf->__wait();
  106. } // end while
  107. return 0;
  108. }
  109. /*****************************************************************
  110. **【函数名称】 regist
  111. **【函数功能】 订阅消息
  112. **【参数】
  113. **【返回值】
  114. ****************************************************************/
  115. void CMsgCenter::regist( UINT MsgType, IMsgObserver* pObserver )
  116. {
  117. m_ObsvrMap.insert(pair<UINT, IMsgObserver*>(MsgType, pObserver));
  118. }
  119. /*****************************************************************
  120. **【函数名称】 unregist
  121. **【函数功能】 取消订阅
  122. **【参数】
  123. **【返回值】
  124. ****************************************************************/
  125. void CMsgCenter::unregist( UINT MsgType, IMsgObserver* pObserver )
  126. {
  127. ObsvrMap::iterator itr;
  128. ObsvrMap::iterator itrEnd;
  129. itr = m_ObsvrMap.find(MsgType);
  130. if(itr == m_ObsvrMap.end())
  131. return;
  132. itrEnd = m_ObsvrMap.upper_bound(MsgType);
  133. while(itr != itrEnd)
  134. {
  135. if(itr->second == pObserver)
  136. {
  137. m_ObsvrMap.erase(itr);
  138. break;
  139. }
  140. itr++;
  141. }
  142. }
  143. /*****************************************************************
  144. **【函数名称】 pushMsg
  145. **【函数功能】 压入消息
  146. **【参数】
  147. **【返回值】
  148. ****************************************************************/
  149. void CMsgCenter::pushMsg( UINT MsgType, PARAM lpContent )
  150. {
  151. if(m_Stop)
  152. return;
  153. // 生成消息实体
  154. MSG_INNER* pMsg = new MSG_INNER;
  155. memset(pMsg, 0, sizeof(pMsg));
  156. pMsg->MsgType = MsgType; // 消息类型
  157. // 填充事件内容
  158. switch(MsgType)
  159. {
  160. case SYS_MSG_RES_NEW:
  161. case SYS_MSG_RES_DEL:
  162. case SYS_MSG_RES_STATE:
  163. {
  164. DEV_RES_ID* pResId = new DEV_RES_ID(*(DEV_RES_ID*)lpContent);
  165. pMsg->Content = (PARAM)pResId;
  166. }
  167. break;
  168. default:
  169. pMsg->Content = lpContent;
  170. } // end switch
  171. m_RWLock.Lock(); // 互斥加锁
  172. // 将消息插入缓冲队列
  173. m_MsgList.AddTail(pMsg);
  174. m_RWLock.Unlock();
  175. // 需要唤醒分发线程
  176. m_ThreadSleepFlag.SetEvent();
  177. }
  178. /*****************************************************************
  179. **【函数名称】 startInThreadMode
  180. **【函数功能】 以线程方式启动消息循环
  181. **【参数】
  182. **【返回值】
  183. ****************************************************************/
  184. void CMsgCenter::startInThreadMode( void )
  185. {
  186. m_pThreadObj = AfxBeginThread(__dipatchThreadFunc, this);
  187. }
  188. /*****************************************************************
  189. **【函数名称】 startMsgPump
  190. **【函数功能】 直接启动消息泵
  191. **【参数】
  192. **【返回值】
  193. ****************************************************************/
  194. void CMsgCenter::startMsgPump( void )
  195. {
  196. while(!m_Stop)
  197. {
  198. if(__dispatchMsg() == 0)
  199. __wait();
  200. }
  201. }
  202. /*****************************************************************
  203. **【函数名称】 stopMsgPump
  204. **【函数功能】 停止消息泵
  205. **【参数】
  206. **【返回值】
  207. ****************************************************************/
  208. void CMsgCenter::stopMsgPump( void )
  209. {
  210. m_Stop = true;
  211. m_ThreadSleepFlag.SetEvent();
  212. }