NetThreadManager.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "NetThreadManager.h"
  2. #include "AllObjectManagerUtil.h"
  3. #include "ErrorCode.h"
  4. #include "Poco/Exception.h"
  5. #include "Util.h"
  6. #include "PhmsLogger.h"
  7. using Poco::FileNotFoundException;
  8. map<string, CExternalNetInterface*> CNetThreadManager::m_map;
  9. map<Thread::TID, string> CNetThreadManager::m_mapTidAndHandle;
  10. map<string, Thread::TID> CNetThreadManager::m_mapHandleAndTid;
  11. int CNetThreadManager::InsertNetThread(string stringUuid)
  12. {
  13. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  14. Thread::TID nThreadId = Thread::currentTid();
  15. if(m_mapTidAndHandle.find(nThreadId) != m_mapTidAndHandle.end())
  16. {
  17. //说明该线程已经初始化过了
  18. return PHMS_SUCCESSFUL_RESULT;
  19. }
  20. CExternalNetInterface* pNetInterface = NULL;
  21. try
  22. {
  23. pNetInterface = new CExternalNetInterface();
  24. }
  25. catch(FileNotFoundException& e)
  26. {
  27. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  28. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_NO_NET_CONFIG_FILE), __FUNCTION__, __FILE__, __LINE__);
  29. return COMMON_NO_NET_CONFIG_FILE;
  30. }
  31. catch(Exception& e)
  32. {
  33. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  34. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_NET_CONFIG_READ_ERROR), __FUNCTION__, __FILE__, __LINE__);
  35. return COMMON_NET_CONFIG_READ_ERROR;
  36. }
  37. m_map.insert(map<string, CExternalNetInterface*>::value_type(stringUuid, pNetInterface));
  38. m_mapTidAndHandle.insert(map<Thread::TID, string>::value_type(nThreadId, stringUuid));
  39. m_mapHandleAndTid.insert(map<string, Thread::TID>::value_type(stringUuid, nThreadId));
  40. string stringSessionId;
  41. if(CNetThreadManager::m_map.size() != 0)
  42. {
  43. map<string, CExternalNetInterface*>::const_iterator iter;
  44. for(iter=CNetThreadManager::m_map.begin(); iter!=CNetThreadManager::m_map.end(); iter++)
  45. {
  46. if(iter->second->GetSessionId().size()!=0 && iter->second->GetSessionId()!="00000000000000000000000000000000")
  47. {
  48. stringSessionId = iter->second->GetSessionId();
  49. pNetInterface->SetSessionId(stringSessionId);
  50. break;
  51. }
  52. }
  53. //设置公卫系统的SessionId
  54. for(iter=CNetThreadManager::m_map.begin(); iter!=CNetThreadManager::m_map.end(); iter++)
  55. {
  56. if(iter->second->GetGWSessionId().size()!=0 && iter->second->GetGWSessionId()!="00000000000000000000000000000000")
  57. {
  58. stringSessionId = iter->second->GetGWSessionId();
  59. pNetInterface->SetGWSessionId(stringSessionId);
  60. break;
  61. }
  62. }
  63. }
  64. else
  65. {
  66. map<string, CPushBase*>::const_iterator iter;
  67. for(iter=CPushObjectManager::m_map.begin(); iter!=CPushObjectManager::m_map.end(); iter++)
  68. {
  69. if(iter->second->GetSessionId().size()!=0 && iter->second->GetSessionId()!="00000000000000000000000000000000")
  70. {
  71. stringSessionId = iter->second->GetSessionId();
  72. pNetInterface->SetSessionId(stringSessionId);
  73. break;
  74. }
  75. }
  76. }
  77. return PHMS_SUCCESSFUL_RESULT;
  78. }
  79. int CNetThreadManager::DeleteNetThread(string stringUuid)
  80. {
  81. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  82. CExternalNetInterface* pNetInterface = NULL;
  83. if(m_map.find(stringUuid) != m_map.end())
  84. {
  85. pNetInterface = m_map[stringUuid];
  86. }
  87. if(pNetInterface != NULL)
  88. {
  89. m_map.erase(stringUuid);
  90. Thread::TID nThreadId = m_mapHandleAndTid[stringUuid];
  91. m_mapTidAndHandle.erase(nThreadId);
  92. m_mapHandleAndTid.erase(stringUuid);
  93. delete pNetInterface;
  94. //CommonNetInterface中执行ReleasePhmsLogger
  95. return PHMS_SUCCESSFUL_RESULT;
  96. }
  97. else
  98. {
  99. return COMMON_INVALID_HANDLE;
  100. }
  101. }
  102. string CNetThreadManager::GetHandleByThreadId(Thread::TID nThreadId)
  103. {
  104. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  105. string stringUuid;
  106. if(m_mapTidAndHandle.find(nThreadId) != m_mapTidAndHandle.end())
  107. {
  108. stringUuid = m_mapTidAndHandle[nThreadId];
  109. }
  110. return stringUuid;
  111. }
  112. CExternalNetInterface* CNetThreadManager::GetNetThread(string stringUuid, bool bCrossingThread)
  113. {
  114. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  115. CExternalNetInterface* pNetInterface = NULL;
  116. if(m_map.find(stringUuid) != m_map.end())
  117. {
  118. if(!bCrossingThread)
  119. {
  120. Thread::TID nThreadId = m_mapHandleAndTid[stringUuid];
  121. if(nThreadId != Thread::currentTid())
  122. {
  123. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_HANDLE_THREAD_MISMATCH), __FUNCTION__, __FILE__, __LINE__);
  124. return NULL;
  125. }
  126. }
  127. pNetInterface = m_map[stringUuid];
  128. }
  129. return pNetInterface;
  130. }
  131. int CNetThreadManager::GetSessionId(string& stringSessionId)
  132. {
  133. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  134. map<string, CExternalNetInterface*>::const_iterator iter;
  135. for(iter=m_map.begin(); iter!=m_map.end(); iter++)
  136. {
  137. if(iter->second->GetSessionId().size()!=0 && iter->second->GetSessionId()!="00000000000000000000000000000000")
  138. {
  139. stringSessionId = iter->second->GetSessionId();
  140. break;
  141. }
  142. }
  143. if(iter == m_map.end())
  144. {
  145. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_NO_VALID_SESSIONID), __FUNCTION__, __FILE__, __LINE__);
  146. return COMMON_NO_VALID_SESSIONID;
  147. }
  148. return PHMS_SUCCESSFUL_RESULT;
  149. }
  150. int CNetThreadManager::SetSessionId(string stringSessionId)
  151. {
  152. HandleSessionSync(stringSessionId);
  153. return PHMS_SUCCESSFUL_RESULT;
  154. }
  155. void CNetThreadManager::HandleSessionSync(string stringSessionId)
  156. {
  157. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  158. map<string, CExternalNetInterface*>::iterator iter;
  159. for(iter=m_map.begin(); iter!=m_map.end(); iter++)
  160. {
  161. iter->second->SetSessionId(stringSessionId);
  162. }
  163. return;
  164. }
  165. int CNetThreadManager::SetGWSessionId(string stringSessionId)
  166. {
  167. HandleGWSessionSync(stringSessionId);
  168. return PHMS_SUCCESSFUL_RESULT;
  169. }
  170. int CNetThreadManager::GetGWSessionId(string& stringSessionId)
  171. {
  172. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  173. map<string, CExternalNetInterface*>::const_iterator iter;
  174. for(iter=m_map.begin(); iter!=m_map.end(); iter++)
  175. {
  176. if(iter->second->GetGWSessionId().size()!=0 && iter->second->GetGWSessionId()!="00000000000000000000000000000000")
  177. {
  178. stringSessionId = iter->second->GetGWSessionId();
  179. break;
  180. }
  181. }
  182. if(iter == m_map.end())
  183. {
  184. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_NO_VALID_SESSIONID), __FUNCTION__, __FILE__, __LINE__);
  185. return COMMON_NO_VALID_SESSIONID;
  186. }
  187. return PHMS_SUCCESSFUL_RESULT;
  188. }
  189. void CNetThreadManager::HandleGWSessionSync(string stringSessionId)
  190. {
  191. Mutex::ScopedLock lock(CAllObjectManagerUtil::m_mutex);
  192. map<string, CExternalNetInterface*>::iterator iter;
  193. for(iter=m_map.begin(); iter!=m_map.end(); iter++)
  194. {
  195. iter->second->SetGWSessionId(stringSessionId);
  196. }
  197. return;
  198. }