Class TCPServer
Defined in File tcp_server.h
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 int)> func)
This callback will be triggered on clients connecting to the server.
- 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 int)> func)
This callback will be triggered on clients disconnecting from the server.
- 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 int, 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()
Shut down the event listener thread. After calling this, no events will be handled anymore, but the socket will remain open and bound to the port. Call start() in order to restart event handling.
-
bool write(const int 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
-
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.