SocketAcceptor.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // SocketAcceptor.h
  3. //
  4. // Library: Net
  5. // Package: Reactor
  6. // Module: SocketAcceptor
  7. //
  8. // Definition of the SocketAcceptor class.
  9. //
  10. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Net_SocketAcceptor_INCLUDED
  16. #define Net_SocketAcceptor_INCLUDED
  17. #include "Poco/Net/Net.h"
  18. #include "Poco/Net/SocketNotification.h"
  19. #include "Poco/Net/SocketReactor.h"
  20. #include "Poco/Net/ServerSocket.h"
  21. #include "Poco/Net/StreamSocket.h"
  22. #include "Poco/Observer.h"
  23. namespace Poco {
  24. namespace Net {
  25. template <class ServiceHandler>
  26. class SocketAcceptor
  27. /// This class implements the Acceptor part of the
  28. /// Acceptor-Connector design pattern.
  29. ///
  30. /// The Acceptor-Connector pattern has been described in the book
  31. /// "Pattern Languages of Program Design 3", edited by Robert Martin,
  32. /// Frank Buschmann and Dirk Riehle (Addison Wesley, 1997).
  33. ///
  34. /// The Acceptor-Connector design pattern decouples connection
  35. /// establishment and service initialization in a distributed system
  36. /// from the processing performed once a service is initialized.
  37. /// This decoupling is achieved with three components: Acceptors,
  38. /// Connectors and Service Handlers.
  39. /// The SocketAcceptor passively waits for connection requests (usually
  40. /// from a remote Connector) and establishes a connection upon
  41. /// arrival of a connection requests. Also, a Service Handler is
  42. /// initialized to process the data arriving via the connection in
  43. /// an application-specific way.
  44. ///
  45. /// The SocketAcceptor sets up a ServerSocket and registers itself
  46. /// for a ReadableNotification, denoting an incoming connection request.
  47. ///
  48. /// When the ServerSocket becomes readable the SocketAcceptor accepts
  49. /// the connection request and creates a ServiceHandler to
  50. /// service the connection.
  51. ///
  52. /// The ServiceHandler class must provide a constructor that
  53. /// takes a StreamSocket and a SocketReactor as arguments,
  54. /// e.g.:
  55. /// MyServiceHandler(const StreamSocket& socket, ServiceReactor& reactor)
  56. ///
  57. /// When the ServiceHandler is done, it must destroy itself.
  58. ///
  59. /// Subclasses can override the createServiceHandler() factory method
  60. /// if special steps are necessary to create a ServiceHandler object.
  61. {
  62. public:
  63. explicit SocketAcceptor(ServerSocket& socket):
  64. _socket(socket),
  65. _pReactor(0)
  66. /// Creates a SocketAcceptor, using the given ServerSocket.
  67. {
  68. }
  69. SocketAcceptor(ServerSocket& socket, SocketReactor& reactor):
  70. _socket(socket),
  71. _pReactor(&reactor)
  72. /// Creates a SocketAcceptor, using the given ServerSocket.
  73. /// The SocketAcceptor registers itself with the given SocketReactor.
  74. {
  75. _pReactor->addEventHandler(_socket, Poco::Observer<SocketAcceptor,
  76. ReadableNotification>(*this, &SocketAcceptor::onAccept));
  77. }
  78. virtual ~SocketAcceptor()
  79. /// Destroys the SocketAcceptor.
  80. {
  81. try
  82. {
  83. if (_pReactor)
  84. {
  85. _pReactor->removeEventHandler(_socket, Poco::Observer<SocketAcceptor,
  86. ReadableNotification>(*this, &SocketAcceptor::onAccept));
  87. }
  88. }
  89. catch (...)
  90. {
  91. poco_unexpected();
  92. }
  93. }
  94. void setReactor(SocketReactor& reactor)
  95. /// Sets the reactor for this acceptor.
  96. {
  97. _pReactor = &reactor;
  98. if (!_pReactor->hasEventHandler(_socket, Poco::Observer<SocketAcceptor,
  99. ReadableNotification>(*this, &SocketAcceptor::onAccept)))
  100. {
  101. registerAcceptor(reactor);
  102. }
  103. }
  104. virtual void registerAcceptor(SocketReactor& reactor)
  105. /// Registers the SocketAcceptor with a SocketReactor.
  106. ///
  107. /// A subclass can override this function to e.g.
  108. /// register an event handler for timeout event.
  109. ///
  110. /// If acceptor was constructed without providing reactor to it,
  111. /// the override of this method must either call the base class
  112. /// implementation or directly register the accept handler with
  113. /// the reactor.
  114. {
  115. if (_pReactor)
  116. throw Poco::InvalidAccessException("Acceptor already registered.");
  117. _pReactor = &reactor;
  118. _pReactor->addEventHandler(_socket, Poco::Observer<SocketAcceptor, ReadableNotification>(*this, &SocketAcceptor::onAccept));
  119. }
  120. virtual void unregisterAcceptor()
  121. /// Unregisters the SocketAcceptor.
  122. ///
  123. /// A subclass can override this function to e.g.
  124. /// unregister its event handler for a timeout event.
  125. ///
  126. /// If the accept handler was registered with the reactor,
  127. /// the overriding method must either call the base class
  128. /// implementation or directly unregister the accept handler.
  129. {
  130. if (_pReactor)
  131. {
  132. _pReactor->removeEventHandler(_socket, Poco::Observer<SocketAcceptor, ReadableNotification>(*this, &SocketAcceptor::onAccept));
  133. }
  134. }
  135. void onAccept(ReadableNotification* pNotification)
  136. /// Accepts connection and creates event handler.
  137. {
  138. pNotification->release();
  139. StreamSocket sock = _socket.acceptConnection();
  140. _pReactor->wakeUp();
  141. createServiceHandler(sock);
  142. }
  143. protected:
  144. virtual ServiceHandler* createServiceHandler(StreamSocket& socket)
  145. /// Create and initialize a new ServiceHandler instance.
  146. ///
  147. /// Subclasses can override this method.
  148. {
  149. return new ServiceHandler(socket, *_pReactor);
  150. }
  151. SocketReactor* reactor()
  152. /// Returns a pointer to the SocketReactor where
  153. /// this SocketAcceptor is registered.
  154. ///
  155. /// The pointer may be null.
  156. {
  157. return _pReactor;
  158. }
  159. Socket& socket()
  160. /// Returns a reference to the SocketAcceptor's socket.
  161. {
  162. return _socket;
  163. }
  164. private:
  165. SocketAcceptor();
  166. SocketAcceptor(const SocketAcceptor&);
  167. SocketAcceptor& operator = (const SocketAcceptor&);
  168. ServerSocket _socket;
  169. SocketReactor* _pReactor;
  170. };
  171. } } // namespace Poco::Net
  172. #endif // Net_SocketAcceptor_INCLUDED