#include <Pt/Net/TcpServer.h>
Listens for TCP connections. More...
Inherits Selectable.
Public Member Functions | |
| TcpServer () | |
| Creates a closed server. | |
| TcpServer (System::EventLoop &loop) | |
| Creates a server and attaches it to loop. | |
| TcpServer (const Endpoint &ep) | |
| Creates a server and listens on ep. | |
| ~TcpServer () | |
| Destroys the server. | |
| void | listen (const Endpoint &ep) |
| Listens on local endpoint ep. | |
| void | listen (const Endpoint &ep, const TcpServerOptions &options) |
| Listens on ep with options. | |
| void | beginAccept () |
| Begins accepting a connection. | |
| void | close () |
| Closes the server and stops listening. | |
| Signal< TcpServer & > & | connectionPending () |
| Returns the signal that a connection is pending. | |
| System::EventLoop * | loop () const |
| Returns the parent event loop. | |
| void | setActive (EventLoop &parent) |
| Sets the parent loop, so that operations can be run. | |
| void | detach () |
| Remove from event loop and cancels outstanding operations. | |
| void | cancel () |
| Cancels all operations. | |
| bool | run () |
| Run operation if it is ready. | |
| EventLoop * | parent () const |
| Returns the used event loop. | |
Protected Member Functions | |
| virtual void | onAttach (System::EventLoop &loop) |
| Attached to loop. | |
| virtual void | onDetach (System::EventLoop &loop) |
| Detached from loop. | |
| virtual void | onCancel () |
| Blocks until operation has cancelled. | |
| virtual bool | onRun () |
| Check if ready and run. | |
| void | post () |
| Posts this selectable to its event loop from any thread. | |
TcpServer is the listening Selectable in the TCP model: it binds a local endpoint and reports pending peers, but it is not an IODevice and it does not read or write bytes. A TcpSocket takes each connection.
listen() binds the local endpoint and waits for peers, and if that address is already occupied the call throws AddressInUse. A second listen() closes the previous listen first, so the server holds at most one local binding. The constructors create a closed server, attach one to an EventLoop, or listen immediately on an endpoint.
TcpServerOptions configure the listen; they do not open a server. The accept backlog is the number of pending connections the server will queue, and deferred accept, when set to a positive number of seconds, may wait until the peer sends data before reporting the connection. A negative deferred-accept value leaves that setting unset.
Asynchronous accept requires an attached event loop. beginAccept() waits for the next pending connection, and connectionPending() is emitted when a peer is ready. There is no matching end-accept: a TcpSocket takes the connection with accept(). The server must be attached before beginAccept(), and after the slot takes the connection, beginAccept() is called again to wait for the next peer.
close() stops listening and accepting. The caller owns the server, and attaching it to a loop does not transfer ownership.
The example is the pending-connection slot. The acceptor is Connectable so it can bind connectionPending() to onPending(), and when the signal fires, accept() takes the stream into a TcpSocket that the acceptor owns.
|
explicit |
| %AddressInUse | if the local address is already occupied. |
| void listen | ( | const Endpoint & | ep | ) |
| %AddressInUse | if the local address is already occupied. |
| void listen | ( | const Endpoint & | ep, |
| const TcpServerOptions & | options ) |
| %AddressInUse | if the local address is already occupied. |
| void beginAccept | ( | ) |
The server must be attached to an event loop.