LNAcceptor.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "StdAfx.h"
  2. #include "LNAcceptor.h"
  3. #include "LNBuffer.h"
  4. #include "AppService.h"
  5. CLNAcceptor::CLNAcceptor(CProtocolHandler * pHandler)
  6. {
  7. m_pHandler = pHandler;
  8. }
  9. CLNAcceptor::~CLNAcceptor(void)
  10. {
  11. }
  12. CLNContext* CLNAcceptor::make_handler()
  13. {
  14. CLNContext* pContext = new CLNContext();
  15. if(pContext)
  16. {
  17. CLNBuffer* pBuffer = new CLNBuffer();
  18. pBuffer->pContext = pContext; //赋值一下父指针
  19. pContext->SetBuffer(pBuffer);
  20. //set client protocol handler
  21. //pContext->SetProtocolHandler(CAppService::Instance()->GetHandle());
  22. pContext->SetProtocolHandler(m_pHandler);
  23. cs.Lock();
  24. m_lstClientContext.push_back(pContext);
  25. cs.Unlock();
  26. }
  27. return pContext;
  28. }
  29. void CLNAcceptor::OnClearContext()
  30. {
  31. Lock();
  32. list<CLNContext*>::iterator it = m_lstClientContext.begin();
  33. for(; it != m_lstClientContext.end(); )
  34. {
  35. CLNContext* pContext = *it;
  36. if(pContext == NULL)
  37. {
  38. it = m_lstClientContext.erase(it);
  39. continue;
  40. }
  41. UINT nIdleTime = pContext->GetIdleTime2();
  42. if((nIdleTime > 300) || pContext->IsInactive(INFINITE))
  43. {
  44. auto strAddr = pContext->GetIPAdressNew();
  45. CSimpleLog::Info(fmt::format("心跳超时.{} imei:{} {}", strAddr,
  46. pContext->m_mapImei.size() ? (LPCSTR)pContext->m_mapImei.begin()->first : "", (LPCSTR)pContext->ivuname).c_str());
  47. //删除会话
  48. if(pContext->Delete())
  49. {
  50. CSimpleLog::Info(fmt::format("删除链接.{} ", strAddr).c_str());
  51. it = m_lstClientContext.erase(it);
  52. continue;
  53. }
  54. }
  55. it++;
  56. }
  57. Unlock();
  58. }
  59. void CLNAcceptor::ClearAllContexts()
  60. {
  61. int nRepeatNum = 20;
  62. while(true)
  63. {
  64. TRACE("nRepeatNum = %d\n", nRepeatNum);
  65. if(nRepeatNum < 0)
  66. break;
  67. BOOL bForced = (nRepeatNum == 0) ? TRUE : FALSE;
  68. Lock();
  69. if(m_lstClientContext.empty())
  70. {
  71. Unlock();
  72. return;
  73. }
  74. list<CLNContext*>::iterator it = m_lstClientContext.begin();
  75. for(; it != m_lstClientContext.end(); )
  76. {
  77. CLNContext* pContext = *it;
  78. if((pContext == NULL) || pContext->Delete(bForced))
  79. it = m_lstClientContext.erase(it);
  80. else
  81. it++;
  82. }
  83. Unlock();
  84. Sleep(100);
  85. nRepeatNum--;
  86. }
  87. Lock();
  88. m_lstClientContext.clear();
  89. Unlock();
  90. }
  91. int CLNAcceptor::GetContextNum()
  92. {
  93. Lock();
  94. int nNum = (int)m_lstClientContext.size();
  95. Unlock();
  96. return nNum;
  97. }
  98. CLNContext* CLNAcceptor::FindContext(DWORD_PTR pContext)
  99. {
  100. CLNContext* pFind = nullptr;
  101. Lock();
  102. for (auto it : m_lstClientContext)
  103. {
  104. if (pContext == (DWORD_PTR)it)
  105. {
  106. pFind = (CLNContext*)it->GetSharedPointer();
  107. break;
  108. }
  109. }
  110. Unlock();
  111. return pFind;
  112. }
  113. CLNContext* CLNAcceptor::FindContextByIMEI(const char *imei)
  114. {
  115. CLNContext* pFind = nullptr;
  116. Lock();
  117. for (auto it : m_lstClientContext)
  118. {
  119. if (it->m_mapImei.find(imei) != it->m_mapImei.end())
  120. {
  121. pFind = (CLNContext*)it->GetSharedPointer();
  122. break;
  123. }
  124. }
  125. Unlock();
  126. return pFind;
  127. }
  128. void CLNAcceptor::SendDataAllContexts(uint8_t* data, size_t len)
  129. {
  130. Lock();
  131. for (auto it : m_lstClientContext)
  132. {
  133. auto pContext = (CLNContext*)it;
  134. pContext->Send(data, len);
  135. }
  136. Unlock();
  137. }
  138. CLNContext* CLNAcceptor::FindContextByIVU(const char *imei)
  139. {
  140. CLNContext* pFind = nullptr;
  141. Lock();
  142. for (auto it : m_lstClientContext)
  143. {
  144. if (it->ivuname.CompareNoCase(imei) == 0)
  145. {
  146. pFind = (CLNContext*)it->GetSharedPointer();
  147. break;
  148. }
  149. }
  150. Unlock();
  151. return pFind;
  152. }
  153. void CLNAcceptor::GetAllContextImei(std::list<CString>& lst)
  154. {
  155. Lock();
  156. for (const auto& it : m_lstClientContext)
  157. {
  158. for (const auto& ik : it->m_mapImei)
  159. lst.push_back(ik.first);
  160. }
  161. Unlock();
  162. }
  163. int CLNAcceptor::validate_connection(const ACE_Asynch_Accept::Result& result, const ACE_INET_Addr& remote, const ACE_INET_Addr& local)
  164. {
  165. auto ip_addr = local.get_ip_address();
  166. auto strIp = local.get_host_addr();;
  167. //if (ip_addr != 0x7F000001)
  168. // g_strLocalIp = strIp;
  169. SPDLOG_ERROR("new connection: {}:{} local:{}:{}", remote.get_host_addr(), remote.get_port_number(), strIp, local.get_port_number());
  170. return 0;
  171. }