LNAcceptor.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "StdAfx.h"
  2. #include "LNAcceptor.h"
  3. #include "LNBuffer.h"
  4. #include "AppService.h"
  5. string g_strLocalIp;
  6. CLNAcceptor::CLNAcceptor(CProtocolHandler * pHandler)
  7. {
  8. m_pHandler = pHandler;
  9. }
  10. CLNAcceptor::~CLNAcceptor(void)
  11. {
  12. }
  13. CLNContext* CLNAcceptor::make_handler()
  14. {
  15. CLNContext* pContext = new CLNContext();
  16. if(pContext)
  17. {
  18. CLNBuffer* pBuffer = new CLNBuffer();
  19. pContext->SetBuffer(pBuffer);
  20. pContext->SetProtocolHandler(m_pHandler);
  21. cs.Lock();
  22. m_lstClientContext.push_back(pContext);
  23. cs.Unlock();
  24. }
  25. return pContext;
  26. }
  27. BOOL CLNAcceptor::Start()
  28. {
  29. Stop();
  30. m_bWork = true;
  31. m_pThread = new std::thread(ThreadProc, (DWORD_PTR)this);
  32. return TRUE;
  33. }
  34. void CLNAcceptor::Stop()
  35. {
  36. m_bWork = false;
  37. if (m_pThread)
  38. {
  39. m_pThread->join();
  40. delete m_pThread;
  41. m_pThread = nullptr;
  42. }
  43. }
  44. void CLNAcceptor::OnClearContext()
  45. {
  46. Lock();
  47. list<CLNContext*>::iterator it = m_lstClientContext.begin();
  48. for(; it != m_lstClientContext.end(); )
  49. {
  50. CLNContext* pContext = *it;
  51. if(pContext == NULL)
  52. {
  53. it = m_lstClientContext.erase(it);
  54. continue;
  55. }
  56. UINT nIdleTime = pContext->GetIdleTime2();
  57. if((nIdleTime > 180) || pContext->IsInactive(INFINITE))
  58. {
  59. auto strAddr = pContext->GetIPAdressNew();
  60. SPDLOG_ERROR("心跳超时.{} ", strAddr);
  61. //删除会话
  62. if(pContext->Delete())
  63. {
  64. SPDLOG_ERROR("删除链接.{} ", strAddr);
  65. it = m_lstClientContext.erase(it);
  66. continue;
  67. }
  68. }
  69. it++;
  70. }
  71. Unlock();
  72. }
  73. void CLNAcceptor::ClearAllContexts()
  74. {
  75. int nRepeatNum = 20;
  76. while(true)
  77. {
  78. TRACE("nRepeatNum = %d\n", nRepeatNum);
  79. if(nRepeatNum < 0)
  80. break;
  81. BOOL bForced = (nRepeatNum == 0) ? TRUE : FALSE;
  82. Lock();
  83. if(m_lstClientContext.empty())
  84. {
  85. Unlock();
  86. return;
  87. }
  88. list<CLNContext*>::iterator it = m_lstClientContext.begin();
  89. for(; it != m_lstClientContext.end(); )
  90. {
  91. CLNContext* pContext = *it;
  92. if((pContext == NULL) || pContext->Delete(bForced))
  93. it = m_lstClientContext.erase(it);
  94. else
  95. it++;
  96. }
  97. Unlock();
  98. Sleep(100);
  99. nRepeatNum--;
  100. }
  101. Lock();
  102. m_lstClientContext.clear();
  103. Unlock();
  104. }
  105. int CLNAcceptor::GetContextNum()
  106. {
  107. Lock();
  108. int nNum = (int)m_lstClientContext.size();
  109. Unlock();
  110. return nNum;
  111. }
  112. CLNContext* CLNAcceptor::FindContext(DWORD_PTR pContext)
  113. {
  114. CLNContext* pFind = nullptr;
  115. Lock();
  116. for (auto it : m_lstClientContext)
  117. {
  118. if (pContext == (DWORD_PTR)it)
  119. {
  120. pFind = (CLNContext*)it->GetSharedPointer();
  121. break;
  122. }
  123. }
  124. Unlock();
  125. return pFind;
  126. }
  127. void CLNAcceptor::SendDataAllContexts(uint8_t* data, size_t len)
  128. {
  129. Lock();
  130. for (auto it : m_lstClientContext)
  131. {
  132. auto pContext = (CLNContext*)it;
  133. pContext->Send(data, len);
  134. }
  135. Unlock();
  136. }
  137. void CLNAcceptor::ThreadProc(DWORD_PTR wparam)
  138. {
  139. CLNAcceptor* pThis = (CLNAcceptor*)wparam;
  140. SPDLOG_WARN("10090 心跳线程启动");
  141. Sleep(2000);
  142. uint8_t bSend[100] = { 0 };
  143. int len = CHjDataConver::conver_sendpack(bSend, nullptr, 0, 0, 0, E_ZL_PROTOCAL::ZL_KEEP, OR_DATA_INFO(0, 0, 1, 3, OPT_TYPE::OPT_SYNC));
  144. time_t tmNow;
  145. while (pThis->m_bWork)
  146. {
  147. for (size_t i = 0; i < 10; i++)
  148. {
  149. Sleep(500);
  150. if (pThis->m_bWork == false) break;
  151. }
  152. //发送心跳
  153. time(&tmNow);
  154. if (tmNow - pThis->m_tmLastKeep > 30)
  155. {
  156. pThis->SendDataAllContexts(bSend, len);
  157. pThis->m_tmLastKeep = tmNow;
  158. }
  159. if (pThis->m_lstClientContext.size() == 0)
  160. {
  161. if (pThis->m_tmLastZero == 0)
  162. pThis->m_tmLastZero = tmNow;
  163. else if (tmNow - pThis->m_tmLastZero > 60)
  164. {
  165. bool bRet = false;
  166. if (bRet)
  167. {
  168. SPDLOG_ERROR("关闭{}端口", 10090);
  169. pThis->close();
  170. SPDLOG_ERROR("监听{}端口", 10090);
  171. if (pThis->listen(10090) == -1)
  172. SPDLOG_CRITICAL("端口{} tcp 监听失败", 10090);
  173. pThis->m_tmLastZero = tmNow;
  174. }
  175. }
  176. }
  177. else
  178. if (!pThis->m_tmLastZero) pThis->m_tmLastZero = 0;
  179. //清除链接
  180. pThis->OnClearContext();
  181. }
  182. SPDLOG_WARN("10090 心跳线程退出");
  183. }