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 *)> 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)> 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

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

Definition at line 44 of file tcp_server.cpp.

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

Definition at line 51 of file tcp_server.cpp.

Member Function Documentation

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

Definition at line 126 of file tcp_server.cpp.

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.

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

Handles connection events.

Definition at line 159 of file tcp_server.cpp.

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

Definition at line 247 of file tcp_server.cpp.

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

Definition at line 58 of file tcp_server.cpp.

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

read data from socket

Definition at line 267 of file tcp_server.cpp.

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.

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.

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.

void urcl::comm::TCPServer::setMessageCallback ( std::function< void(const int, char *)>  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.

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 107 of file tcp_server.cpp.

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

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

Definition at line 193 of file tcp_server.cpp.

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 307 of file tcp_server.cpp.

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

Definition at line 147 of file tcp_server.cpp.

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

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

Definition at line 298 of file tcp_server.cpp.

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 314 of file tcp_server.cpp.

Member Data Documentation

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

Definition at line 178 of file tcp_server.h.

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

Definition at line 187 of file tcp_server.h.

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

Definition at line 184 of file tcp_server.h.

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

Definition at line 183 of file tcp_server.h.

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

Definition at line 167 of file tcp_server.h.

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

Definition at line 170 of file tcp_server.h.

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

Definition at line 174 of file tcp_server.h.

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

Definition at line 177 of file tcp_server.h.

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

Definition at line 173 of file tcp_server.h.

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

Definition at line 188 of file tcp_server.h.

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

Definition at line 186 of file tcp_server.h.

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

Definition at line 171 of file tcp_server.h.

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

Definition at line 181 of file tcp_server.h.

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

Definition at line 175 of file tcp_server.h.

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 Sun May 9 2021 02:16:26