Epoll.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // Copyright @ 2015 Hangzhou Topzen Ltd.
  3. // Author: Dong Xiaohua (dongxh@hztopzen.com) @ 2015-08
  4. //
  5. #ifndef __EPOLL_H
  6. #define __EPOLL_H
  7. #if defined(LINUX)
  8. #include "BaseTypes.h"
  9. #include "BaseHeaders.h"
  10. #include "SockAddress.h"
  11. #include "Locks.h"
  12. #include <sys/epoll.h>
  13. namespace tzc {
  14. class IOEventHandler
  15. {
  16. public:
  17. IOEventHandler() {}
  18. virtual ~IOEventHandler() {}
  19. virtual void Recvice() = 0;
  20. virtual void Send() = 0;
  21. virtual void Exception(TZ_Int32 iEvent) = 0;
  22. };
  23. class Epoll {
  24. public:
  25. #define DEFAILT_MAX_CONN 1024
  26. typedef enum __EventMode
  27. {
  28. Mode_LTRead = EPOLLIN,
  29. Mode_LTWrite = EPOLLOUT,
  30. Mode_ETRead = EPOLLIN | EPOLLET,
  31. Mode_ETWrite = EPOLLOUT | EPOLLET,
  32. Mode_PRI = EPOLLPRI,
  33. Mode_Err = EPOLLERR,
  34. Mode_Hup = EPOLLHUP
  35. } EventMode_E;
  36. public:
  37. Epoll(TZ_Int32 iTimeout = 1, TZ_Int32 iMaxConn = DEFAILT_MAX_CONN);
  38. ~Epoll();
  39. TZ_Int32 EpollInitialize();
  40. TZ_Int32 EpollAdd(TZ_Int32 iSockFd, TZ_Int32 iEventMode, IOEventHandler *pHandler);
  41. TZ_Int32 EpollModify(TZ_Int32 iSockFd, TZ_Int32 iEventMode, IOEventHandler *pHandler);
  42. TZ_Int32 EpollDelete(TZ_Int32 iSockFd, TZ_Int32 iEventMode, IOEventHandler *pHandler);
  43. void EpollWait();
  44. void EpollWait(TZ_Int32 iTimeout);
  45. private:
  46. typedef struct epoll_event EpollEvent_S;
  47. const TZ_Int32 m_iMaxConn;
  48. TZ_Int32 m_iEpollFd;
  49. TZ_Int32 m_iTimeout;
  50. EpollEvent_S * m_pEvents;
  51. Mutex m_mtxEvent;
  52. std::map<TZ_SOCKET, IOEventHandler *> m_mapEventHandler;
  53. };
  54. }; // namespace tzc
  55. #endif
  56. #endif /* ----- #ifndef __EPOLL_H ----- */