IODispatcher.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "../hpsocket/GlobalDef.h"
  25. #include "Singleton.h"
  26. #include "RingBuffer.h"
  27. #include "Thread.h"
  28. #include <sys/epoll.h>
  29. #include <sys/eventfd.h>
  30. #include <sys/timerfd.h>
  31. #include <memory>
  32. using namespace std;
  33. #define _EPOLL_READ_PRI_EVENTS (EPOLLPRI | EPOLLRDHUP)
  34. #define _EPOLL_READ_EVENTS (EPOLLIN | EPOLLRDHUP)
  35. #define _EPOLL_ALL_READ_EVENTS (_EPOLL_READ_EVENTS | _EPOLL_READ_PRI_EVENTS)
  36. #define _EPOLL_WRITE_EVENTS (EPOLLOUT)
  37. #define _EPOLL_NORMAL_RW_EVENTS (_EPOLL_READ_EVENTS | _EPOLL_WRITE_EVENTS)
  38. #define _EPOLL_ALL_RW_EVENTS (_EPOLL_ALL_READ_EVENTS | _EPOLL_WRITE_EVENTS)
  39. #define _EPOLL_ERROR_EVENTS (EPOLLERR)
  40. #define _EPOLL_HUNGUP_EVENTS (EPOLLHUP | EPOLLRDHUP)
  41. #define _EPOLL_ALL_ERROR_EVENTS (_EPOLL_ERROR_EVENTS | _EPOLL_HUNGUP_EVENTS)
  42. #define _EPOLL_ALL_NORMAL_EVENTS (_EPOLL_NORMAL_RW_EVENTS | _EPOLL_ALL_ERROR_EVENTS)
  43. #define _EPOLL_ALL_EVENTS (_EPOLL_ALL_RW_EVENTS | _EPOLL_ALL_ERROR_EVENTS)
  44. #define DISP_EVENT_FLAG_R 1
  45. #define DISP_EVENT_FLAG_W 2
  46. #define DISP_EVENT_FLAG_H 4
  47. #define RETRIVE_EVENT_FLAG_R(evt) ((evt) & (_EPOLL_ALL_READ_EVENTS) ? DISP_EVENT_FLAG_R : 0)
  48. #define RETRIVE_EVENT_FLAG_W(evt) ((evt) & (_EPOLL_WRITE_EVENTS) ? DISP_EVENT_FLAG_W : 0)
  49. #define RETRIVE_EVENT_FLAG_RW(evt) (RETRIVE_EVENT_FLAG_R(evt) | RETRIVE_EVENT_FLAG_W(evt))
  50. #define RETRIVE_EVENT_FLAG_H(evt) ((evt) & (_EPOLL_HUNGUP_EVENTS) ? DISP_EVENT_FLAG_H : 0)
  51. // ------------------------------------------------------------------------------------------------------------------------------------------------------- //
  52. struct TDispCommand
  53. {
  54. USHORT type;
  55. UINT_PTR wParam;
  56. UINT_PTR lParam;
  57. static TDispCommand* Construct(USHORT t, UINT_PTR wp = 0, UINT_PTR lp = 0)
  58. {return new TDispCommand(t, wp, lp);}
  59. static VOID Destruct(TDispCommand* p)
  60. {if(p) delete p;}
  61. private:
  62. TDispCommand(USHORT t, UINT_PTR wp = 0, UINT_PTR lp = 0)
  63. : type(t), wParam(wp), lParam(lp)
  64. {
  65. }
  66. ~TDispCommand() = default;
  67. };
  68. // ------------------------------------------------------------------------------------------------------------------------------------------------------- //
  69. class IIOHandler
  70. {
  71. public:
  72. virtual VOID OnCommand(TDispCommand* pCmd) = 0;
  73. virtual VOID OnTimer(ULLONG llExpirations) = 0;
  74. virtual BOOL OnBeforeProcessIo(PVOID pv, UINT events) = 0;
  75. virtual VOID OnAfterProcessIo(PVOID pv, UINT events, BOOL rs) = 0;
  76. virtual BOOL OnReadyRead(PVOID pv, UINT events) = 0;
  77. virtual BOOL OnReadyWrite(PVOID pv, UINT events) = 0;
  78. virtual BOOL OnHungUp(PVOID pv, UINT events) = 0;
  79. virtual BOOL OnError(PVOID pv, UINT events) = 0;
  80. virtual BOOL OnReadyPrivilege(PVOID pv, UINT events) = 0;
  81. virtual VOID OnDispatchThreadStart(THR_ID tid) = 0;
  82. virtual VOID OnDispatchThreadEnd(THR_ID tid) = 0;
  83. public:
  84. virtual ~IIOHandler() = default;
  85. };
  86. class CIOHandler : public IIOHandler
  87. {
  88. public:
  89. virtual VOID OnCommand(TDispCommand* pCmd) override {}
  90. virtual VOID OnTimer(ULLONG llExpirations) override {}
  91. virtual BOOL OnBeforeProcessIo(PVOID pv, UINT events) override {return TRUE;}
  92. virtual VOID OnAfterProcessIo(PVOID pv, UINT events, BOOL rs) override {}
  93. virtual BOOL OnReadyWrite(PVOID pv, UINT events) override {return TRUE;}
  94. virtual BOOL OnHungUp(PVOID pv, UINT events) override {return TRUE;}
  95. virtual BOOL OnError(PVOID pv, UINT events) override {return TRUE;}
  96. virtual BOOL OnReadyPrivilege(PVOID pv, UINT events) override {return TRUE;}
  97. virtual VOID OnDispatchThreadStart(THR_ID tid) override {}
  98. virtual VOID OnDispatchThreadEnd(THR_ID tid) override {}
  99. };
  100. // ------------------------------------------------------------------------------------------------------------------------------------------------------- //
  101. class CIODispatcher
  102. {
  103. public:
  104. static const int DEF_WORKER_MAX_EVENTS = 64;
  105. using CCommandQueue = CCASQueue<TDispCommand>;
  106. using CWorkerThread = CThread<CIODispatcher, VOID, int>;
  107. public:
  108. BOOL Start(IIOHandler* pHandler, int iWorkerMaxEvents = DEF_WORKER_MAX_EVENTS, int iWorkers = 0, LLONG llTimerInterval = 0);
  109. BOOL Stop(BOOL bCheck = TRUE);
  110. BOOL SendCommand(TDispCommand* pCmd);
  111. BOOL SendCommand(USHORT t, UINT_PTR wp = 0, UINT_PTR lp = 0);
  112. template<class _List, typename = enable_if_t<is_same<remove_reference_t<typename _List::reference>, TDispCommand*>::value>>
  113. BOOL SendCommands(const _List& cmds)
  114. {
  115. size_t size = cmds.size();
  116. if(size == 0) return FALSE;
  117. for(auto it = cmds.begin(), end = cmds.end(); it != end; ++it)
  118. m_queue.PushBack(*it);
  119. return VERIFY_IS_NO_ERROR(eventfd_write(m_evCmd, size));
  120. }
  121. BOOL AddFD(FD fd, UINT mask, PVOID pv) {return CtlFD(fd, EPOLL_CTL_ADD, mask, pv);}
  122. BOOL ModFD(FD fd, UINT mask, PVOID pv) {return CtlFD(fd, EPOLL_CTL_MOD, mask, pv);}
  123. BOOL DelFD(FD fd) {return CtlFD(fd, EPOLL_CTL_DEL, 0, nullptr);}
  124. BOOL CtlFD(FD fd, int op, UINT mask, PVOID pv);
  125. BOOL ProcessIo(PVOID pv, UINT events);
  126. FD AddTimer (LLONG llInterval, PVOID pv);
  127. BOOL DelTimer (FD fdTimer);
  128. private:
  129. int WorkerProc(PVOID pv = nullptr);
  130. BOOL ProcessExit(UINT events);
  131. BOOL ProcessTimer(UINT events);
  132. BOOL ProcessCommand(UINT events);
  133. BOOL DoProcessIo(PVOID pv, UINT events);
  134. VOID Reset();
  135. VOID MakePrefix();
  136. public:
  137. BOOL HasStarted() {return m_pHandler && m_pWorkers;}
  138. const CWorkerThread* GetWorkerThreads() {return m_pWorkers.get();}
  139. CIODispatcher() {MakePrefix(); Reset();}
  140. ~CIODispatcher() {if(HasStarted()) Stop();}
  141. private:
  142. static LPCTSTR WORKER_THREAD_PREFIX;
  143. static volatile UINT sm_uiNum;
  144. volatile UINT m_uiSeq;
  145. CString m_strPrefix;
  146. private:
  147. IIOHandler* m_pHandler;
  148. FD m_epoll;
  149. FD m_evCmd;
  150. FD m_evExit;
  151. FD m_evTimer;
  152. int m_iWorkers;
  153. int m_iMaxEvents;
  154. CCommandQueue m_queue;
  155. unique_ptr<CWorkerThread[]> m_pWorkers;
  156. };