root / trunk / tbeta / OSX / libs / poco / include / Poco / Net / SocketAcceptor.h @ 151
View | Annotate | Download (5.9 KB)
| 1 | 151 | ss | //
|
|---|---|---|---|
| 2 | 151 | ss | // SocketAcceptor.h
|
| 3 | 151 | ss | //
|
| 4 | 151 | ss | // $Id: //poco/1.3/Net/include/Poco/Net/SocketAcceptor.h#1 $
|
| 5 | 151 | ss | //
|
| 6 | 151 | ss | // Library: Net
|
| 7 | 151 | ss | // Package: Reactor
|
| 8 | 151 | ss | // Module: SocketAcceptor
|
| 9 | 151 | ss | //
|
| 10 | 151 | ss | // Definition of the SocketAcceptor class.
|
| 11 | 151 | ss | //
|
| 12 | 151 | ss | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
|
| 13 | 151 | ss | // and Contributors.
|
| 14 | 151 | ss | //
|
| 15 | 151 | ss | // Permission is hereby granted, free of charge, to any person or organization
|
| 16 | 151 | ss | // obtaining a copy of the software and accompanying documentation covered by
|
| 17 | 151 | ss | // this license (the "Software") to use, reproduce, display, distribute,
|
| 18 | 151 | ss | // execute, and transmit the Software, and to prepare derivative works of the
|
| 19 | 151 | ss | // Software, and to permit third-parties to whom the Software is furnished to
|
| 20 | 151 | ss | // do so, all subject to the following:
|
| 21 | 151 | ss | //
|
| 22 | 151 | ss | // The copyright notices in the Software and this entire statement, including
|
| 23 | 151 | ss | // the above license grant, this restriction and the following disclaimer,
|
| 24 | 151 | ss | // must be included in all copies of the Software, in whole or in part, and
|
| 25 | 151 | ss | // all derivative works of the Software, unless such copies or derivative
|
| 26 | 151 | ss | // works are solely in the form of machine-executable object code generated by
|
| 27 | 151 | ss | // a source language processor.
|
| 28 | 151 | ss | //
|
| 29 | 151 | ss | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 30 | 151 | ss | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 31 | 151 | ss | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
| 32 | 151 | ss | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
| 33 | 151 | ss | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
| 34 | 151 | ss | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
| 35 | 151 | ss | // DEALINGS IN THE SOFTWARE.
|
| 36 | 151 | ss | //
|
| 37 | 151 | ss | |
| 38 | 151 | ss | |
| 39 | 151 | ss | #ifndef Net_SocketAcceptor_INCLUDED
|
| 40 | 151 | ss | #define Net_SocketAcceptor_INCLUDED
|
| 41 | 151 | ss | |
| 42 | 151 | ss | |
| 43 | 151 | ss | #include "Poco/Net/Net.h" |
| 44 | 151 | ss | #include "Poco/Net/SocketNotification.h" |
| 45 | 151 | ss | #include "Poco/Net/ServerSocket.h" |
| 46 | 151 | ss | #include "Poco/Net/StreamSocket.h" |
| 47 | 151 | ss | #include "Poco/Observer.h" |
| 48 | 151 | ss | |
| 49 | 151 | ss | |
| 50 | 151 | ss | namespace Poco {
|
| 51 | 151 | ss | namespace Net {
|
| 52 | 151 | ss | |
| 53 | 151 | ss | |
| 54 | 151 | ss | template <class ServiceHandler> |
| 55 | 151 | ss | class SocketAcceptor |
| 56 | 151 | ss | /// This class implements the Acceptor part of the
|
| 57 | 151 | ss | /// Acceptor-Connector design pattern.
|
| 58 | 151 | ss | ///
|
| 59 | 151 | ss | /// The Acceptor-Connector pattern has been described in the book
|
| 60 | 151 | ss | /// "Pattern Languages of Program Design 3", edited by Robert Martin,
|
| 61 | 151 | ss | /// Frank Buschmann and Dirk Riehle (Addison Wesley, 1997).
|
| 62 | 151 | ss | ///
|
| 63 | 151 | ss | /// The Acceptor-Connector design pattern decouples connection
|
| 64 | 151 | ss | /// establishment and service initialization in a distributed system
|
| 65 | 151 | ss | /// from the processing performed once a service is initialized.
|
| 66 | 151 | ss | /// This decoupling is achieved with three components: Acceptors,
|
| 67 | 151 | ss | /// Connectors and Service Handlers.
|
| 68 | 151 | ss | /// The SocketAcceptor passively waits for connection requests (usually
|
| 69 | 151 | ss | /// from a remote Connector) and establishes a connection upon
|
| 70 | 151 | ss | /// arrival of a connection requests. Also, a Service Handler is
|
| 71 | 151 | ss | /// initialized to process the data arriving via the connection in
|
| 72 | 151 | ss | /// an application-specific way.
|
| 73 | 151 | ss | ///
|
| 74 | 151 | ss | /// The SocketAcceptor sets up a ServerSocket and registers itself
|
| 75 | 151 | ss | /// for a ReadableNotification, denoting an incoming connection request.
|
| 76 | 151 | ss | ///
|
| 77 | 151 | ss | /// When the ServerSocket becomes readable the SocketAcceptor accepts
|
| 78 | 151 | ss | /// the connection request and creates a ServiceHandler to
|
| 79 | 151 | ss | /// service the connection.
|
| 80 | 151 | ss | ///
|
| 81 | 151 | ss | /// The ServiceHandler class must provide a constructor that
|
| 82 | 151 | ss | /// takes a StreamSocket and a SocketReactor as arguments,
|
| 83 | 151 | ss | /// e.g.:
|
| 84 | 151 | ss | /// MyServiceHandler(const StreamSocket& socket, ServiceReactor& reactor)
|
| 85 | 151 | ss | ///
|
| 86 | 151 | ss | /// When the ServiceHandler is done, it must destroy itself.
|
| 87 | 151 | ss | ///
|
| 88 | 151 | ss | /// Subclasses can override the createServiceHandler() factory method
|
| 89 | 151 | ss | /// if special steps are necessary to create a ServiceHandler object.
|
| 90 | 151 | ss | {
|
| 91 | 151 | ss | public:
|
| 92 | 151 | ss | SocketAcceptor(ServerSocket& socket): |
| 93 | 151 | ss | _socket(socket), |
| 94 | 151 | ss | _pReactor(0)
|
| 95 | 151 | ss | /// Creates an SocketAcceptor, using the given ServerSocket.
|
| 96 | 151 | ss | {
|
| 97 | 151 | ss | } |
| 98 | 151 | ss | |
| 99 | 151 | ss | SocketAcceptor(ServerSocket& socket, SocketReactor& reactor): |
| 100 | 151 | ss | _socket(socket), |
| 101 | 151 | ss | _pReactor(0)
|
| 102 | 151 | ss | /// Creates an SocketAcceptor, using the given ServerSocket.
|
| 103 | 151 | ss | /// The SocketAcceptor registers itself with the given SocketReactor.
|
| 104 | 151 | ss | {
|
| 105 | 151 | ss | registerAcceptor(reactor); |
| 106 | 151 | ss | } |
| 107 | 151 | ss | |
| 108 | 151 | ss | virtual ~SocketAcceptor() |
| 109 | 151 | ss | /// Destroys the SocketAcceptor.
|
| 110 | 151 | ss | {
|
| 111 | 151 | ss | unregisterAcceptor(); |
| 112 | 151 | ss | } |
| 113 | 151 | ss | |
| 114 | 151 | ss | virtual void registerAcceptor(SocketReactor& reactor)
|
| 115 | 151 | ss | /// Registers the SocketAcceptor with a SocketReactor.
|
| 116 | 151 | ss | ///
|
| 117 | 151 | ss | /// A subclass can override this and, for example, also register
|
| 118 | 151 | ss | /// an event handler for a timeout event.
|
| 119 | 151 | ss | ///
|
| 120 | 151 | ss | /// The overriding method must call the baseclass implementation first.
|
| 121 | 151 | ss | {
|
| 122 | 151 | ss | _pReactor = &reactor; |
| 123 | 151 | ss | _pReactor->addEventHandler(_socket, Poco::Observer<SocketAcceptor, ReadableNotification>(*this, &SocketAcceptor::onAccept)); |
| 124 | 151 | ss | } |
| 125 | 151 | ss | |
| 126 | 151 | ss | virtual void unregisterAcceptor()
|
| 127 | 151 | ss | /// Unregisters the SocketAcceptor.
|
| 128 | 151 | ss | ///
|
| 129 | 151 | ss | /// A subclass can override this and, for example, also unregister
|
| 130 | 151 | ss | /// its event handler for a timeout event.
|
| 131 | 151 | ss | ///
|
| 132 | 151 | ss | /// The overriding method must call the baseclass implementation first.
|
| 133 | 151 | ss | {
|
| 134 | 151 | ss | if (_pReactor)
|
| 135 | 151 | ss | {
|
| 136 | 151 | ss | _pReactor->removeEventHandler(_socket, Poco::Observer<SocketAcceptor, ReadableNotification>(*this, &SocketAcceptor::onAccept)); |
| 137 | 151 | ss | } |
| 138 | 151 | ss | } |
| 139 | 151 | ss | |
| 140 | 151 | ss | void onAccept(ReadableNotification* pNotification)
|
| 141 | 151 | ss | {
|
| 142 | 151 | ss | pNotification->release(); |
| 143 | 151 | ss | StreamSocket sock = _socket.acceptConnection(); |
| 144 | 151 | ss | createServiceHandler(sock); |
| 145 | 151 | ss | } |
| 146 | 151 | ss | |
| 147 | 151 | ss | protected:
|
| 148 | 151 | ss | virtual ServiceHandler* createServiceHandler(StreamSocket& socket) |
| 149 | 151 | ss | /// Create and initialize a new ServiceHandler instance.
|
| 150 | 151 | ss | ///
|
| 151 | 151 | ss | /// Subclasses can override this method.
|
| 152 | 151 | ss | {
|
| 153 | 151 | ss | return new ServiceHandler(socket, *_pReactor);
|
| 154 | 151 | ss | } |
| 155 | 151 | ss | |
| 156 | 151 | ss | SocketReactor* reactor() |
| 157 | 151 | ss | /// Returns a pointer to the SocketReactor where
|
| 158 | 151 | ss | /// this SocketAcceptor is registered.
|
| 159 | 151 | ss | ///
|
| 160 | 151 | ss | /// The pointer may be null.
|
| 161 | 151 | ss | {
|
| 162 | 151 | ss | return _pReactor;
|
| 163 | 151 | ss | } |
| 164 | 151 | ss | |
| 165 | 151 | ss | Socket& socket() |
| 166 | 151 | ss | /// Returns a reference to the SocketAcceptor's socket.
|
| 167 | 151 | ss | {
|
| 168 | 151 | ss | return _socket;
|
| 169 | 151 | ss | } |
| 170 | 151 | ss | |
| 171 | 151 | ss | private:
|
| 172 | 151 | ss | SocketAcceptor(); |
| 173 | 151 | ss | SocketAcceptor(const SocketAcceptor&);
|
| 174 | 151 | ss | SocketAcceptor& operator = (const SocketAcceptor&);
|
| 175 | 151 | ss | |
| 176 | 151 | ss | ServerSocket _socket; |
| 177 | 151 | ss | SocketReactor* _pReactor; |
| 178 | 151 | ss | }; |
| 179 | 151 | ss | |
| 180 | 151 | ss | |
| 181 | 151 | ss | } } // namespace Poco::Net
|
| 182 | 151 | ss | |
| 183 | 151 | ss | |
| 184 | 151 | ss | #endif // Net_SocketAcceptor_INCLUDED |
