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 |
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.
|
delete |
urcl::comm::TCPServer::TCPServer | ( | const int | port | ) |
Definition at line 44 of file tcp_server.cpp.
|
virtual |
Definition at line 51 of file tcp_server.cpp.
|
private |
Definition at line 126 of file tcp_server.cpp.
|
inline |
Get the maximum number of clients allowed to connect to this server.
Definition at line 132 of file tcp_server.h.
|
private |
Handles connection events.
Definition at line 159 of file tcp_server.cpp.
|
private |
Definition at line 247 of file tcp_server.cpp.
|
private |
Definition at line 58 of file tcp_server.cpp.
|
private |
read data from socket
Definition at line 267 of file tcp_server.cpp.
|
inline |
This callback will be triggered on clients connecting to the server.
func | Function 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.
|
inline |
This callback will be triggered on clients disconnecting from the server.
func | Function 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.
|
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.
|
inline |
This callback will be triggered on messages received on the socket.
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. |
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.
|
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.
|
private |
Definition at line 147 of file tcp_server.cpp.
|
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.
[in] | fd | File descriptor belonging to the client the data should be sent to. The file descriptor will be given from the connection callback. |
[in] | buf | Buffer of bytes to write |
[in] | buf_len | Number of bytes in the buffer |
[out] | written | Number of bytes actually written |
Definition at line 314 of file tcp_server.cpp.
|
private |
Definition at line 178 of file tcp_server.h.
|
private |
Definition at line 187 of file tcp_server.h.
|
private |
Definition at line 184 of file tcp_server.h.
|
staticprivate |
Definition at line 183 of file tcp_server.h.
|
private |
Definition at line 167 of file tcp_server.h.
|
private |
Definition at line 170 of file tcp_server.h.
|
private |
Definition at line 174 of file tcp_server.h.
|
private |
Definition at line 177 of file tcp_server.h.
|
private |
Definition at line 173 of file tcp_server.h.
|
private |
Definition at line 188 of file tcp_server.h.
|
private |
Definition at line 186 of file tcp_server.h.
|
private |
Definition at line 171 of file tcp_server.h.
|
private |
Definition at line 181 of file tcp_server.h.
|
private |
Definition at line 175 of file tcp_server.h.
|
private |
Definition at line 168 of file tcp_server.h.