/sparrowhawk/foundation/ESFEpollMultiplexer.h

http://github.com/jtblatt/duderino · C Header · 182 lines · 66 code · 28 blank · 88 comment · 0 complexity · 37b96575cd27feb59a8e4ee1b9f389d7 MD5 · raw file

  1. /** @file ESFEpollMultiplexer.h
  2. * @brief A linux epoll-based implementation of ESFSocketMultiplexer
  3. *
  4. * Copyright (c) 2009 Yahoo! Inc.
  5. * The copyrights embodied in the content of this file are licensed by Yahoo! Inc.
  6. * under the BSD (revised) open source license.
  7. *
  8. * Derived from code that is Copyright (c) 2009 Joshua Blatt and offered under both
  9. * BSD and Apache 2.0 licenses (http://sourceforge.net/projects/sparrowhawk/).
  10. *
  11. * $Author: blattj $
  12. * $Date: 2009/05/25 21:51:08 $
  13. * $Name: $
  14. * $Revision: 1.3 $
  15. */
  16. #ifndef ESF_EPOLL_MULTIPLEXER_H
  17. #define ESF_EPOLL_MULTIPLEXER_H
  18. #ifndef ESF_SOCKET_MULTIPLEXER_H
  19. #include <ESFSocketMultiplexer.h>
  20. #endif
  21. #ifndef ESF_SHARED_COUNTER_H
  22. #include <ESFSharedCounter.h>
  23. #endif
  24. #ifndef ESF_ALLOCATOR_H
  25. #include <ESFAllocator.h>
  26. #endif
  27. #ifndef ESF_EMBEDDED_LIST_H
  28. #include <ESFEmbeddedList.h>
  29. #endif
  30. #ifndef ESF_MUTEX_H
  31. #include <ESFMutex.h>
  32. #endif
  33. #ifndef ESF_SHARED_COUNTER_H
  34. #include <ESFSharedCounter.h>
  35. #endif
  36. #ifdef HAVE_TIME_H
  37. #include <time.h>
  38. #endif
  39. #ifdef HAVE_SYS_EPOLL_H
  40. #include <sys/epoll.h>
  41. #endif
  42. /** A linux epoll-based implementation of ESFSocketMultiplexer.
  43. *
  44. * @ingroup thread
  45. */
  46. class ESFEpollMultiplexer: public ESFSocketMultiplexer {
  47. public:
  48. /** Constructor.
  49. *
  50. * @param name The name of the multiplexer to be used in log messages. Caller is responsible
  51. * for the strings memory - use a string literal if possible.
  52. * @param maxSockets The maximum number of sockets the multiplexer will
  53. * handle. When this limit is hit, the multiplexer will stop accepting
  54. * new connections from any listening sockets and reject any application
  55. * requests to add new sockets.
  56. * @param logger An optional logger. Pass NULL to not log anything.
  57. * @param allocator Internal storage will be allocated using this allocator.
  58. */
  59. ESFEpollMultiplexer(const char *name, int maxSockets, ESFLogger *logger, ESFAllocator *allocator);
  60. /** Destructor.
  61. */
  62. virtual ~ESFEpollMultiplexer();
  63. /** Return an optional handler that can destroy the multiplexer.
  64. *
  65. * @return A handler to destroy the element or NULL if the element should not be destroyed.
  66. */
  67. virtual ESFCleanupHandler *getCleanupHandler();
  68. /** Get the name of the multiplexer. This name can be used in logging messages, etc.
  69. *
  70. * @return The multiplexer's name
  71. */
  72. virtual const char *getName() const;
  73. /** Initialize the multiplexer
  74. *
  75. * @return ESF_SUCCESS if successful, another error code otherwise
  76. */
  77. virtual ESFError initialize();
  78. /** Destroy the multiplexer
  79. *
  80. */
  81. virtual void destroy();
  82. /** Add a new multiplexed socket to the socket multiplexer
  83. *
  84. * @param multiplexedSocket The multiplexed socket
  85. * @return ESF_SUCCESS if successful, ESF_OVERFLOW if the maxSockets limit has
  86. * been reached, another error code otherwise.
  87. */
  88. virtual ESFError addMultiplexedSocket(ESFMultiplexedSocket *multiplexedSocket);
  89. /** Run the multiplexer's event loop until shutdown.
  90. *
  91. * @param isRunning This object will return true as long as the controlling thread
  92. * isRunning, false when the controlling thread wants to shutdown.
  93. * @return If true, caller should destroy the command with the CleanupHandler.
  94. */
  95. virtual bool run(ESFFlag *isRunning);
  96. /** Get the number of sockets this multiplexer is currently handling.
  97. *
  98. * @return the number of sockets this multiplexer is currently handling.
  99. */
  100. virtual int getCurrentSockets();
  101. /** Get the maximum number of sockets this multiplexer can handle.
  102. *
  103. * @return the maximum number of sockets this multiplexer can handle.
  104. */
  105. virtual int getMaximumSockets();
  106. /** Placement new.
  107. *
  108. * @param size The size of the object.
  109. * @param allocator The source of the object's memory.
  110. * @return Memory for the new object or NULL if the memory allocation failed.
  111. */
  112. inline void *operator new(size_t size, ESFAllocator *allocator) {
  113. return allocator->allocate(size);
  114. }
  115. private:
  116. // Disabled
  117. ESFEpollMultiplexer(const ESFEpollMultiplexer &);
  118. ESFEpollMultiplexer &operator=(const ESFEpollMultiplexer &);
  119. /** Keep socket in epoll and socket list, but possibly change the readiness
  120. * events of interest. This does not modify the _currentSocketCount.
  121. *
  122. * @param multiplexedSocket The multiplexedSocket
  123. */
  124. ESFError updateMultiplexedSocket(ESFFlag *isRunning, ESFMultiplexedSocket *multiplexedSocket);
  125. /** Remove a multiplexed socket form the socket multiplexer
  126. *
  127. * @param multiplexedSocket The multiplexed socket to remove
  128. */
  129. ESFError removeMultiplexedSocket(ESFFlag *isRunning, ESFMultiplexedSocket *multiplexedSocket, bool removeFromList =
  130. true);
  131. /** Periodically check for any idle sockets and delete them.
  132. *
  133. * @param isRunning This object will return true as long as the controlling thread
  134. * isRunning, false when the controlling thread wants to shutdown.
  135. */
  136. ESFError checkIdleSockets(ESFFlag *isRunning);
  137. int _epollDescriptor;
  138. int _maxSockets;
  139. #ifdef HAVE_TIME_T
  140. time_t _lastIdleCheckSec;
  141. #else
  142. #error "time_t or equilvalent is required"
  143. #endif
  144. #ifdef HAVE_STRUCT_EPOLL_EVENT
  145. struct epoll_event *_events;
  146. #else
  147. #error "struct epoll_event is required"
  148. #endif
  149. ESFAllocator *_allocator;
  150. ESFSharedCounter _currentSocketCount;
  151. ESFEmbeddedList _currentSocketList;
  152. ESFMutex _lock;
  153. };
  154. #endif /* ! ESF_EPOLL_MULTIPLEXER_H */