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. tcp_acceptor 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; tcp_acceptor 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 io_handler { public: MyServerHandler(io_event_loop& loop, tcpsocket sock, MyDispatcher *disp) : io_handler(loop, sock) { ... } }; typedef tcp_acceptor<MyServerHandler, MyDispatcher*> MyAcceptor; int main() { static const unsigned int PORT = 8002; MyDispatcher *dispatcher = new MyDispatcher; io_event_loop loop; MyAcceptor acceptor(loop, PORT, dispatcher); loop.run(); return 0; }
Public Methods | |
tcp_acceptor (io_event_loop &_loop, in_port_t localport, Arg arg) | |
Creates the service,. | |
virtual | ~tcp_acceptor () |
|
Creates the service,.
|
|
|