Class TCPServer

Class Documentation

class TCPServer

Wrapper class for a TCP socket server.

The server can be created given a port and it can register callbacks for 3 events:

  • connect: A new client connected

  • disconnect: A client disconnected

  • message: Data sent from one of the clients

Please note that start() has to be called manually after initialization and callback registering in order to start handling socket events.

While this server implementation supports multiple (number limited by system’s socket implementation) clients by default, a maximum number of allowed clients can be configured.

Public Functions

TCPServer() = delete
explicit TCPServer(const int port, const size_t max_num_tries = 0, const std::chrono::milliseconds reconnection_time = std::chrono::seconds(1))

Create a TCPServer object.

Parameters:
  • port – Port on which to operate. The port will be bound to the process creating the object.

  • max_num_tries – If binding the socket fails, it will be retried this many times. If 0 is specified, binding the socket will be tried indefinitely.

  • reconnection_time – Wait time in between binding attempts.

virtual ~TCPServer()
inline void setConnectCallback(std::function<void(const socket_t)> func)

This callback will be triggered on clients connecting to the server.

Note

: The connection callback will be triggered with the socket being accepted. Hence, it is possible to send data from the connection callback directly.

Parameters:

func – Function handling the event information. The file descriptor created by the connection event will be passed to the function.

inline void setDisconnectCallback(std::function<void(const socket_t)> func)

This callback will be triggered on clients disconnecting from the server.

Note

: The socket will already be closed when the disconnect callback is triggered, thus trying to interact with the socket from the disconnect callback will fail.

Parameters:

func – Function handling the event information. The file descriptor created by the connection event will be passed to the function.

inline void setMessageCallback(std::function<void(const socket_t, char*, int)> func)

This callback will be triggered on messages received on the socket.

Parameters:

func – Function handling the event information. The file client’s file_descriptor will be passed to the function as well as the actual message received from the client.

void start()

Start event handling.

Without calling this function the socket will be advertised and bound to a tcp port, but no handling of connection requests will be performed.

void shutdown()

Shutdown the server and close all client connections.

Note

: This should not be called from within any of the registered callback functions, as it will cause a deadlock. If you want to shutdown the server from a callback, you can e.g. start a new thread that calls shutdown() from there.

bool write(const socket_t fd, const uint8_t *buf, const size_t buf_len, size_t &written)

Writes to a client.

Parameters:
  • fd[in] File descriptor belonging to the client the data should be sent to. The file descriptor will be given from the connection callback.

  • buf[in] Buffer of bytes to write

  • buf_len[in] Number of bytes in the buffer

  • written[out] Number of bytes actually written

Returns:

True on success, false otherwise

bool writeUnchecked(const socket_t fd, const uint8_t *buf, const size_t buf_len, size_t &written)

Writes to a filedescriptor without verifying that it is a client or even a valid filedescriptor. It is the caller’s responsibility to ensure that the filedescriptor is valid and belongs to a client.

Parameters:
  • fd[in] File descriptor belonging to the client the data should be sent to. The file descriptor will be given from the connection callback.

  • buf[in] Buffer of bytes to write

  • buf_len[in] Number of bytes in the buffer

  • written[out] Number of bytes actually written

Returns:

True on success, false otherwise

inline uint32_t getMaxClientsAllowed() const

Get the maximum number of clients allowed to connect to this server.

Returns:

The currently configured client limit. 0 means unlimited amount of clients allowed.

inline void setMaxClientsAllowed(const uint32_t &max_clients_allowed)

Set the maximum number of clients allowed to connect to this server.

0 means unlimited number of clients allowed.

inline int getPort() const

Get the port this server is bound to.

If port number 0 is passed during initialization, the server will bind to a random free port. In this case, this function can be used to get the actual port number the server is bound to. This should only be called after the server has been initialized, otherwise the returned port number might not be correct.

Returns:

The port number this server is bound to