Whenever a connection is accepted, a handler of type T, the first template argument, is constructed. Each acceptor may have an arbitrarily-typed argument (of type Arg, the second template argument) which is passed to all the handlers on construction. TCPAcceptor constructs handlers like this:
new T(loop, sock, arg);
where loop is the event loop that this acceptor is bound to, sock is a TCP Socket object, and arg is the arbitrary argument passed to the constructor.
If no such argument is required, then omit the second template argument; TCPAcceptor will then construct handlers like this:
new T(loop, sock);
A typical usage pattern, in which all the server handlers need access to some dispatcher object that manages global state, is as follows:
class MyDispatcher { ... }; class MyServerHandler : public IOHandler { public: MyServerHandler(IOEventLoop& loop, TCPSocket sock, MyDispatcher *disp) : IOHandler(loop, sock) { ... } }; typedef TCPAcceptor<MyServerHandler, MyDispatcher*> MyAcceptor; int main() { static const unsigned int PORT = 8002; MyDispatcher *dispatcher = new MyDispatcher; IOEventLoop loop; MyAcceptor acceptor(loop, PORT, dispatcher); loop.run(); return 0; }
Public Methods | |
TCPAcceptor (IOEventLoop &_loop, in_port_t localport, Arg arg) | |
Creates the service,. | |
virtual | ~TCPAcceptor () |
|
Creates the service,.
|
|
|