Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
urcl::comm::TCPServer Class Reference

Wrapper class for a TCP socket server. More...

#include <tcp_server.h>

Public Member Functions

uint32_t getMaxClientsAllowed () const
 Get the maximum number of clients allowed to connect to this server. More...
 
void setConnectCallback (std::function< void(const int)> func)
 This callback will be triggered on clients connecting to the server. More...
 
void setDisconnectCallback (std::function< void(const int)> func)
 This callback will be triggered on clients disconnecting from the server. More...
 
void setMaxClientsAllowed (const uint32_t &max_clients_allowed)
 Set the maximum number of clients allowed to connect to this server. More...
 
void setMessageCallback (std::function< void(const int, char *, int)> func)
 This callback will be triggered on messages received on the socket. More...
 
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. More...
 
void start ()
 Start event handling. More...
 
 TCPServer ()=delete
 
 TCPServer (const int port)
 
bool write (const int fd, const uint8_t *buf, const size_t buf_len, size_t &written)
 Writes to a client. More...
 
virtual ~TCPServer ()
 

Private Member Functions

void bind ()
 
void handleConnect ()
 Handles connection events. More...
 
void handleDisconnect (const int fd)
 
void init ()
 
void readData (const int fd)
 read data from socket More...
 
void spin ()
 Event handler. Blocks until activity on any client or connection attempt. More...
 
void startListen ()
 
void worker ()
 Runs spin() as long as keep_running_ is set to true. More...
 

Private Attributes

std::vector< int > client_fds_
 
std::function< void(const int)> disconnect_callback_
 
char input_buffer_ [INPUT_BUFFER_SIZE]
 
std::atomic< bool > keep_running_
 
std::atomic< int > listen_fd_
 
fd_set masterfds_
 
uint32_t max_clients_allowed_
 
int maxfd_
 
std::function< void(const int, char *buffer, int nbytesrecv)> message_callback_
 
std::function< void(const int)> new_connection_callback_
 
int port_
 
int self_pipe_ [2]
 
fd_set tempfds_
 
std::thread worker_thread_
 

Static Private Attributes

static const int INPUT_BUFFER_SIZE = 100
 

Detailed Description

Wrapper class for a TCP socket server.

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

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.

Definition at line 59 of file tcp_server.h.

Constructor & Destructor Documentation

◆ TCPServer() [1/2]

urcl::comm::TCPServer::TCPServer ( )
delete

◆ TCPServer() [2/2]

urcl::comm::TCPServer::TCPServer ( const int  port)

Definition at line 45 of file tcp_server.cpp.

◆ ~TCPServer()

urcl::comm::TCPServer::~TCPServer ( )
virtual

Definition at line 52 of file tcp_server.cpp.

Member Function Documentation

◆ bind()

void urcl::comm::TCPServer::bind ( )
private

Definition at line 127 of file tcp_server.cpp.

◆ getMaxClientsAllowed()

uint32_t urcl::comm::TCPServer::getMaxClientsAllowed ( ) const
inline

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.

Definition at line 132 of file tcp_server.h.

◆ handleConnect()

void urcl::comm::TCPServer::handleConnect ( )
private

Handles connection events.

Definition at line 160 of file tcp_server.cpp.

◆ handleDisconnect()

void urcl::comm::TCPServer::handleDisconnect ( const int  fd)
private

Definition at line 248 of file tcp_server.cpp.

◆ init()

void urcl::comm::TCPServer::init ( )
private

Definition at line 59 of file tcp_server.cpp.

◆ readData()

void urcl::comm::TCPServer::readData ( const int  fd)
private

read data from socket

Definition at line 268 of file tcp_server.cpp.

◆ setConnectCallback()

void urcl::comm::TCPServer::setConnectCallback ( std::function< void(const int)>  func)
inline

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

Parameters
funcFunction handling the event information. The file descriptor created by the connection event will be passed to the function.

Definition at line 72 of file tcp_server.h.

◆ setDisconnectCallback()

void urcl::comm::TCPServer::setDisconnectCallback ( std::function< void(const int)>  func)
inline

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

Parameters
funcFunction handling the event information. The file descriptor created by the connection event will be passed to the function.

Definition at line 83 of file tcp_server.h.

◆ setMaxClientsAllowed()

void urcl::comm::TCPServer::setMaxClientsAllowed ( const uint32_t &  max_clients_allowed)
inline

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

0 means unlimited number of clients allowed.

Definition at line 143 of file tcp_server.h.

◆ setMessageCallback()

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

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

Parameters
funcFunction 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.

Definition at line 94 of file tcp_server.h.

◆ shutdown()

void urcl::comm::TCPServer::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.

Definition at line 108 of file tcp_server.cpp.

◆ spin()

void urcl::comm::TCPServer::spin ( )
private

Event handler. Blocks until activity on any client or connection attempt.

Definition at line 194 of file tcp_server.cpp.

◆ start()

void urcl::comm::TCPServer::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.

Definition at line 308 of file tcp_server.cpp.

◆ startListen()

void urcl::comm::TCPServer::startListen ( )
private

Definition at line 148 of file tcp_server.cpp.

◆ worker()

void urcl::comm::TCPServer::worker ( )
private

Runs spin() as long as keep_running_ is set to true.

Definition at line 299 of file tcp_server.cpp.

◆ write()

bool urcl::comm::TCPServer::write ( const int  fd,
const uint8_t *  buf,
const size_t  buf_len,
size_t &  written 
)

Writes to a client.

Parameters
[in]fdFile descriptor belonging to the client the data should be sent to. The file descriptor will be given from the connection callback.
[in]bufBuffer of bytes to write
[in]buf_lenNumber of bytes in the buffer
[out]writtenNumber of bytes actually written
Returns
True on success, false otherwise

Definition at line 315 of file tcp_server.cpp.

Member Data Documentation

◆ client_fds_

std::vector<int> urcl::comm::TCPServer::client_fds_
private

Definition at line 178 of file tcp_server.h.

◆ disconnect_callback_

std::function<void(const int)> urcl::comm::TCPServer::disconnect_callback_
private

Definition at line 187 of file tcp_server.h.

◆ input_buffer_

char urcl::comm::TCPServer::input_buffer_[INPUT_BUFFER_SIZE]
private

Definition at line 184 of file tcp_server.h.

◆ INPUT_BUFFER_SIZE

const int urcl::comm::TCPServer::INPUT_BUFFER_SIZE = 100
staticprivate

Definition at line 183 of file tcp_server.h.

◆ keep_running_

std::atomic<bool> urcl::comm::TCPServer::keep_running_
private

Definition at line 167 of file tcp_server.h.

◆ listen_fd_

std::atomic<int> urcl::comm::TCPServer::listen_fd_
private

Definition at line 170 of file tcp_server.h.

◆ masterfds_

fd_set urcl::comm::TCPServer::masterfds_
private

Definition at line 174 of file tcp_server.h.

◆ max_clients_allowed_

uint32_t urcl::comm::TCPServer::max_clients_allowed_
private

Definition at line 177 of file tcp_server.h.

◆ maxfd_

int urcl::comm::TCPServer::maxfd_
private

Definition at line 173 of file tcp_server.h.

◆ message_callback_

std::function<void(const int, char* buffer, int nbytesrecv)> urcl::comm::TCPServer::message_callback_
private

Definition at line 188 of file tcp_server.h.

◆ new_connection_callback_

std::function<void(const int)> urcl::comm::TCPServer::new_connection_callback_
private

Definition at line 186 of file tcp_server.h.

◆ port_

int urcl::comm::TCPServer::port_
private

Definition at line 171 of file tcp_server.h.

◆ self_pipe_

int urcl::comm::TCPServer::self_pipe_[2]
private

Definition at line 181 of file tcp_server.h.

◆ tempfds_

fd_set urcl::comm::TCPServer::tempfds_
private

Definition at line 175 of file tcp_server.h.

◆ worker_thread_

std::thread urcl::comm::TCPServer::worker_thread_
private

Definition at line 168 of file tcp_server.h.


The documentation for this class was generated from the following files:


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47