| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 | 
							- #include "StdAfx.h"
 
- #include "LNAcceptor.h"
 
- #include "LNBuffer.h"
 
- #include "AppService.h"
 
- CLNAcceptor::CLNAcceptor(CProtocolHandler * pHandler)
 
- {
 
-     m_pHandler = pHandler;
 
- }
 
- CLNAcceptor::~CLNAcceptor(void)
 
- {
 
- }
 
- CLNContext* CLNAcceptor::make_handler()
 
- {
 
- 	CLNContext* pContext = new CLNContext();
 
- 	if(pContext)
 
- 	{
 
- 		CLNBuffer* pBuffer = new CLNBuffer();
 
- 		pBuffer->pContext = pContext; //赋值一下父指针
 
- 		pContext->SetBuffer(pBuffer);
 
- 		//set client protocol handler
 
- 		//pContext->SetProtocolHandler(CAppService::Instance()->GetHandle());
 
-         pContext->SetProtocolHandler(m_pHandler);
 
- 		cs.Lock();
 
- 		m_lstClientContext.push_back(pContext);
 
- 		cs.Unlock();
 
- 	}
 
- 	return pContext;
 
- }
 
- void CLNAcceptor::OnClearContext()
 
- {
 
- 	Lock();
 
- 	list<CLNContext*>::iterator it = m_lstClientContext.begin();
 
- 	for(; it != m_lstClientContext.end(); )
 
- 	{
 
- 		CLNContext* pContext = *it;
 
- 		if(pContext == NULL)
 
- 		{
 
- 			it = m_lstClientContext.erase(it);
 
- 			continue;
 
- 		}
 
- 		UINT nIdleTime = pContext->GetIdleTime2();
 
- 		if((nIdleTime > 300) || pContext->IsInactive(INFINITE))	
 
- 		{
 
- 			auto strAddr = pContext->GetIPAdressNew();
 
-             CSimpleLog::Info(fmt::format("心跳超时.{} imei:{} {}", strAddr, 
 
- 				pContext->m_mapImei.size() ? (LPCSTR)pContext->m_mapImei.begin()->first : "", (LPCSTR)pContext->ivuname).c_str());
 
- 			//删除会话
 
- 			if(pContext->Delete())
 
- 			{
 
- 				CSimpleLog::Info(fmt::format("删除链接.{} ", strAddr).c_str());
 
- 				it = m_lstClientContext.erase(it);
 
- 				continue;
 
- 			}
 
- 		}
 
- 		it++;
 
- 	}
 
- 	Unlock();
 
- }
 
- void CLNAcceptor::ClearAllContexts()
 
- {
 
- 	int nRepeatNum = 20;
 
- 	while(true)
 
- 	{
 
- 		TRACE("nRepeatNum = %d\n", nRepeatNum);
 
- 		if(nRepeatNum < 0)
 
- 			break;
 
- 		BOOL bForced = (nRepeatNum == 0) ? TRUE : FALSE;
 
- 		Lock();
 
- 		if(m_lstClientContext.empty())
 
- 		{
 
- 			Unlock();
 
- 			return;
 
- 		}
 
- 		list<CLNContext*>::iterator it = m_lstClientContext.begin();
 
- 		for(; it != m_lstClientContext.end(); )
 
- 		{
 
- 			CLNContext* pContext = *it;
 
- 			if((pContext == NULL) || pContext->Delete(bForced))
 
- 				it = m_lstClientContext.erase(it);
 
- 			else
 
- 				it++;
 
- 		}
 
- 		Unlock();
 
- 		Sleep(100);
 
- 		nRepeatNum--;
 
- 	}
 
- 	Lock();
 
- 	m_lstClientContext.clear();
 
- 	Unlock();
 
- }
 
- int CLNAcceptor::GetContextNum()
 
- {
 
- 	Lock();
 
- 	int nNum = (int)m_lstClientContext.size();
 
- 	Unlock();
 
- 	return nNum;
 
- }
 
- CLNContext* CLNAcceptor::FindContext(DWORD_PTR pContext)
 
- {
 
- 	CLNContext* pFind = nullptr;
 
- 	Lock();
 
- 	for (auto it : m_lstClientContext)
 
- 	{
 
- 		if (pContext == (DWORD_PTR)it)
 
- 		{
 
- 			pFind = (CLNContext*)it->GetSharedPointer();
 
- 			break;
 
- 		}
 
- 	}
 
- 	Unlock();
 
- 	return pFind;
 
- }
 
- CLNContext* CLNAcceptor::FindContextByIMEI(const char *imei)
 
- {
 
- 	CLNContext* pFind = nullptr;
 
- 	Lock();
 
- 	for (auto it : m_lstClientContext)
 
- 	{
 
- 		if (it->m_mapImei.find(imei) != it->m_mapImei.end())
 
- 		{
 
- 			pFind = (CLNContext*)it->GetSharedPointer();
 
- 			break;
 
- 		}
 
- 	}
 
- 	Unlock();
 
- 	return pFind;
 
- }
 
- void CLNAcceptor::SendDataAllContexts(uint8_t* data, size_t len)
 
- {
 
- 	Lock();
 
- 	for (auto it : m_lstClientContext)
 
- 	{
 
- 		auto pContext = (CLNContext*)it;
 
- 		pContext->Send(data, len);
 
- 	}
 
- 	Unlock();
 
- }
 
- CLNContext* CLNAcceptor::FindContextByIVU(const char *imei)
 
- {
 
- 	CLNContext* pFind = nullptr;
 
- 	Lock();
 
- 	for (auto it : m_lstClientContext)
 
- 	{
 
- 		if (it->ivuname.CompareNoCase(imei) == 0)
 
- 		{
 
- 			pFind = (CLNContext*)it->GetSharedPointer();
 
- 			break;
 
- 		}
 
- 	}
 
- 	Unlock();
 
- 	return pFind;
 
- }
 
- void CLNAcceptor::GetAllContextImei(std::list<CString>& lst)
 
- {
 
- 	Lock();
 
- 	for (const auto& it : m_lstClientContext)
 
- 	{
 
- 		for (const auto& ik : it->m_mapImei)
 
- 			lst.push_back(ik.first);
 
- 	}
 
- 	Unlock();
 
- }
 
- int CLNAcceptor::validate_connection(const ACE_Asynch_Accept::Result& result, const ACE_INET_Addr& remote, const ACE_INET_Addr& local)
 
- {
 
- 	auto ip_addr = local.get_ip_address();
 
- 	auto strIp = local.get_host_addr();;
 
- 	//if (ip_addr != 0x7F000001)
 
- 	//	g_strLocalIp = strIp;
 
- 	SPDLOG_ERROR("new connection: {}:{} local:{}:{}", remote.get_host_addr(), remote.get_port_number(), strIp, local.get_port_number());
 
- 	return 0;
 
- }
 
 
  |