ClientSessionManagerF.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. #include "ClientSessionManagerF.h"
  4. #include "VMSCommLibF.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. //---------------------------------------------------------------------------
  8. TClientSessionManager *ClientSessionManager = NULL;
  9. //---------------------------------------------------------------------------
  10. TClientSessionManager::TClientSessionManager()
  11. {
  12. FLastTick = 0;
  13. }
  14. //---------------------------------------------------------------------------
  15. TClientSessionManager::~TClientSessionManager()
  16. {
  17. }
  18. //---------------------------------------------------------------------------
  19. TClientSession* TClientSessionManager::CreateClientSession(SOCKET ASock, SOCKADDR_IN* ASockAddr)
  20. {
  21. TClientSession *pSession = NULL;
  22. #if USE_POOL
  23. FLists.Lock();
  24. try
  25. {
  26. FOR_STL(TClientSession*, pObj, FLists)
  27. {
  28. if (pObj->Socket == INVALID_SOCKET && pObj->State == eSS_none)
  29. {
  30. pObj->Create(ASock, ASockAddr);
  31. pSession = pObj;
  32. break;
  33. }
  34. }
  35. if (!pSession)
  36. {
  37. pSession = new TClientSession();
  38. if (pSession)
  39. {
  40. pSession->Create(ASock, ASockAddr);
  41. FLists.Push(FLists.Size(), pSession);
  42. }
  43. }
  44. }
  45. __finally
  46. {
  47. FLists.UnLock();
  48. }
  49. #else
  50. pSession = new TClientSession(ASock, ASockAddr);
  51. if (pSession)
  52. {
  53. FClientSessionList.insert(TClientSessionList::value_type(ASock, pSession));
  54. }
  55. #endif
  56. return pSession;
  57. }
  58. //---------------------------------------------------------------------------
  59. void TClientSessionManager::SendFlushAllClient()
  60. {
  61. #if USE_POOL
  62. FLists.Lock();
  63. try
  64. {
  65. FOR_STL(TClientSession*, pSession, FLists)
  66. {
  67. if (pSession->Socket != INVALID_SOCKET && pSession->State >= eSS_OnConnect)
  68. {
  69. if (false == pSession->SendFlush())
  70. {
  71. pSession->Disconnect();
  72. }
  73. }
  74. }
  75. }
  76. __finally
  77. {
  78. FLists.UnLock();
  79. }
  80. #else
  81. for (TClientSessionList::const_iterator it=FClientSessionList.begin(); it!=FClientSessionList.end(); ++it)
  82. {
  83. TClientSession *pSession = it->second;
  84. if (false == pSession->SendFlush())
  85. {
  86. pSession->Disconnect();
  87. }
  88. }
  89. #endif
  90. }
  91. //---------------------------------------------------------------------------
  92. void TClientSessionManager::OnPeriodWork()
  93. {
  94. /// 접속이 끊긴 세션들 주기적으로 정리 (1초 정도 마다 해주자)
  95. DWORD currTick = GetTickCount();
  96. if (currTick - FLastTick >= 1000)
  97. {
  98. CollectGarbageSessions();
  99. FLastTick = currTick;
  100. }
  101. }
  102. //---------------------------------------------------------------------------
  103. void TClientSessionManager::CollectGarbageSessions()
  104. {
  105. std::vector<TClientSession*> disconnectedSessions;
  106. for (TClientSessionList::const_iterator it=FClientSessionList.begin(); it!=FClientSessionList.end(); ++it)
  107. {
  108. TClientSession *pSession = it->second;
  109. if (!pSession->IsConnected)
  110. {
  111. if (pSession->FCloseTick++ > 10)
  112. {
  113. //연결종료후 10초후에 메모리 삭제
  114. disconnectedSessions.push_back(pSession);
  115. }
  116. }
  117. }
  118. // 메모리에서 삭제하지 전까지 Socket은 최초 접속했을때의 값을 유지해야 한다.
  119. for (int ii = 0; ii < (int)disconnectedSessions.size(); ii++)
  120. {
  121. TClientSession *pSession = disconnectedSessions[ii];
  122. FClientSessionList.erase(pSession->Socket);
  123. delete pSession;
  124. }
  125. }
  126. //---------------------------------------------------------------------------
  127. TClientSession* TClientSessionManager::FindClientSession(SOCKET ASock)
  128. {
  129. #if USE_POOL
  130. FLists.Lock();
  131. try
  132. {
  133. FOR_STL(TClientSession*, pSession, FLists)
  134. {
  135. if (pSession->Socket == ASock) return pSession;
  136. }
  137. }
  138. __finally
  139. {
  140. FLists.UnLock();
  141. }
  142. return NULL;
  143. #else
  144. TClientSessionList::const_iterator it = FClientSessionList.find(ASock);
  145. if (it == FClientSessionList.end()) return NULL;
  146. return it->second;
  147. #endif
  148. }
  149. //---------------------------------------------------------------------------
  150. void TClientSessionManager::Init(int APoolSize)
  151. {
  152. #if USE_POOL
  153. for (int ii = 0; ii < APoolSize; ii++)
  154. {
  155. TClientSession *pSession = new TClientSession();
  156. if (pSession)
  157. {
  158. FLists.Push(FLists.Size(), pSession);
  159. }
  160. }
  161. #endif
  162. }
  163. //---------------------------------------------------------------------------
  164. void TClientSessionManager::CloseAll()
  165. {
  166. #if USE_POOL
  167. FLists.Lock();
  168. try
  169. {
  170. FOR_STL(TClientSession*, pSession, FLists)
  171. {
  172. if (pSession->Socket != INVALID_SOCKET && pSession->State >= eSS_OnConnect)
  173. {
  174. pSession->Disconnect();
  175. }
  176. }
  177. }
  178. __finally
  179. {
  180. FLists.UnLock();
  181. }
  182. #else
  183. for (TClientSessionList::const_iterator it=FClientSessionList.begin(); it!=FClientSessionList.end(); ++it)
  184. {
  185. TClientSession *pSession = it->second;
  186. pSession->Disconnect();
  187. }
  188. #endif
  189. }
  190. //---------------------------------------------------------------------------
  191. void TClientSessionManager::CheckCommandTimeoutSessions()
  192. {
  193. int nTimeout;
  194. #if USE_POOL
  195. FLists.Lock();
  196. try
  197. {
  198. FOR_STL(TClientSession*, pSession, FLists)
  199. {
  200. if (pSession->Socket != INVALID_SOCKET && pSession->State >= eSS_OnConnect)
  201. {
  202. if (pSession->CDSCtlr != NULL && pSession->FChkRecvTO)
  203. {
  204. nTimeout = COMM_TimeDiff(pSession->FTmrRecv);
  205. if (nTimeout > 10)
  206. {
  207. MERROR("+CLIM Session Command timeout: %s,%s. will be closed.", pSession->IpAddress.c_str(), pSession->CDSCtlr->CTLR_NMBR.c_str());
  208. pSession->Disconnect();
  209. }
  210. else
  211. if (nTimeout > 5)
  212. {
  213. if (3 <= COMM_TimeDiff(pSession->FTmrSend))
  214. {
  215. MERROR("+CLIM Session Command timeout: %s,%s. request parameter.", pSession->IpAddress.c_str(), pSession->CDSCtlr->CTLR_NMBR.c_str());
  216. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  217. pMsg->Type = eVmsParamReq;
  218. pMsg->ObjPtr = (DWORD)pSession;
  219. g_jobQ.PushBlocking((DWORD)pMsg);
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. __finally
  227. {
  228. FLists.UnLock();
  229. }
  230. #else
  231. for (TClientSessionList::const_iterator it=FClientSessionList.begin(); it!=FClientSessionList.end(); ++it)
  232. {
  233. TClientSession *pSession = it->second;
  234. if (pSession->IsConnected && pSession->CDSCtlr != NULL && pSession->FChkRecvTO)
  235. {
  236. nTimeout = COMM_TimeDiff(pSession->FTmrRecv);
  237. if (nTimeout > 10)
  238. {
  239. MERROR("+CLIM Session Command timeout: %s,%s. will be closed.", pSession->IpAddress.c_str(), pSession->CDSCtlr->CTLR_NMBR.c_str());
  240. pSession->Disconnect();
  241. }
  242. else
  243. if (nTimeout > 5)
  244. {
  245. if (3 <= COMM_TimeDiff(pSession->FTmrSend))
  246. {
  247. MERROR("+CLIM Session Command timeout: %s,%s. request parameter.", pSession->IpAddress.c_str(), pSession->CDSCtlr->CTLR_NMBR.c_str());
  248. IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff();
  249. pMsg->Type = eVmsParamReq;
  250. pMsg->ObjPtr = (DWORD)pSession;
  251. g_jobQ.PushBlocking((DWORD)pMsg);
  252. }
  253. }
  254. }
  255. }
  256. #endif
  257. }
  258. //---------------------------------------------------------------------------