LNAcceptor.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <Acceptor.h>
  3. #include "LNContext.h"
  4. #include <list>
  5. #include <mutex>
  6. #include <thread>
  7. extern string g_strLocalIp;
  8. class CLNAcceptor
  9. : public CAcceptor<CLNContext>
  10. {
  11. public:
  12. CLNAcceptor(CProtocolHandler *);
  13. virtual ~CLNAcceptor(void);
  14. friend class CLNHandle;
  15. virtual CLNContext* make_handler() override;
  16. BOOL Start();
  17. void Stop();
  18. void Lock() { cs.Lock(); }
  19. void Unlock() { cs.Unlock(); }
  20. virtual void OnClearContext(); //定时清除链接
  21. void ClearAllContexts(); //清除所有链接
  22. int GetContextNum();
  23. //查找链接
  24. CLNContext* FindContext(DWORD_PTR pContext);
  25. void SendDataAllContexts(uint8_t* data, size_t len);
  26. private:
  27. static void ThreadProc(DWORD_PTR);
  28. std::thread* m_pThread = nullptr;
  29. bool m_bWork = false;
  30. time_t m_tmLastKeep = 0;
  31. time_t m_tmLastZero = 0;
  32. protected:
  33. virtual int validate_connection(const ACE_Asynch_Accept::Result& result,
  34. const ACE_INET_Addr& remote,
  35. const ACE_INET_Addr& local) override
  36. {
  37. auto ip_addr = local.get_ip_address();
  38. auto strIp =local.get_host_addr();;
  39. if (ip_addr != 0x7F000001)
  40. g_strLocalIp = strIp;
  41. SPDLOG_ERROR("new connection: {}:{} local:{}:{}", remote.get_host_addr(), remote.get_port_number(), strIp, local.get_port_number());
  42. return 0;
  43. }
  44. CCriticalSection cs;
  45. std::list<CLNContext*> m_lstClientContext;
  46. CProtocolHandler *m_pHandler = nullptr;
  47. };