TcpServer Class Reference

#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::EventLooploop () 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.

Detailed Description

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.

Listen options

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.

Accepting connections

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.

class Acceptor : public Pt::Connectable
{
public:
explicit Acceptor(Pt::Net::TcpServer& server)
: _server(&server)
{
_server->connectionPending() += Pt::slot(*this, &Acceptor::onPending);
}
void onPending(Pt::Net::TcpServer& server)
{
_peer.accept(server);
}
private:
};
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Listens for TCP connections.
Definition TcpServer.h:159
Connected TCP byte stream.
Definition TcpSocket.h:141
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition ConstMethod.h:172

Constructor & Destructor Documentation

◆ TcpServer()

TcpServer ( const Endpoint & ep)
explicit
Exceptions
%AddressInUseif the local address is already occupied.

Member Function Documentation

◆ listen() [1/2]

void listen ( const Endpoint & ep)
Exceptions
%AddressInUseif the local address is already occupied.

◆ listen() [2/2]

void listen ( const Endpoint & ep,
const TcpServerOptions & options )
Exceptions
%AddressInUseif the local address is already occupied.

◆ beginAccept()

void beginAccept ( )

The server must be attached to an event loop.