ASL
|
This is a reusable TCP or Unix socket server that listens to incoming connections and answers them concurrently (default) or sequentially.
To make an actual server, derive a class from it and implement the virtual function serve()
. That function will be called whenever a new connection arrives.
The server can be bound to more than one local address, incuding a TCP port and a Local Unix socket path.
To add a TLS serving end point, use the bindTLS
function, and set the certificate and key.
For long-running connections, the recommended way of dealing with possible disconnections is this:
Add && !_requestStop
to the while condition to let the service be stoppable by SocketServer::stop().
#include <SocketServer.h>
Public Member Functions | |
void | start (bool nonblocking=false) |
Makes the server start listening. More... | |
bool | bindPath (const String &sname) |
Assigns a Unix local socket to listen to (where supported). | |
bool | bind (int port) |
Assigns a TCP port to listen to. | |
bool | bind (const String &ip, int port) |
Assigns an interface IP address and TCP port to listen to. | |
bool | bindTLS (const String &ip, int port) |
Assigns an interface IP address and TCP port to listen for TLS connections. | |
bool | bindTLS (int port) |
Assigns a TCP port for TLS connections. | |
bool | useCert (const String &cert, const String &key) |
Sets the certificate and private key for the TLS server in PEM format. | |
virtual void | serve (Socket client) |
This function is called in a new thread with each new incoming client connection. More... | |
void | stop (bool sync=false) |
Requests the server to stop receiving connections, and waits for all clients to exit if sync is true. | |
void | setSequential (bool on) |
Sets the mode of operation: sequential (connections will be handled in sequence) or concurrent ( connections will be handled in parallel by starting a new thread each time); this must be called before calling start() to start the server. | |
bool | running () const |
Returns true if this server started and has not yet stopped or still has clients running. | |
String | socketError () const |
Returns a string representation of the last socket error (after a failed bind or use cert...) | |
|
inlinevirtual |
This function is called in a new thread with each new incoming client connection.
Implement it in a subclass to make a specific server.
void start | ( | bool | nonblocking = false | ) |
Makes the server start listening.
This function blocks by default. If the nonblocking
argument is true
the server will listen in its own thread and the call will not block.