otsdaq  3.11.00
ots::TCPListenServer Class Reference

#include <otsdaq/NetworkUtilities/TCPListenServer.h>

+ Inheritance diagram for ots::TCPListenServer:
+ Collaboration diagram for ots::TCPListenServer:

Public Member Functions

 TCPListenServer (unsigned int serverPort, unsigned int maxNumberOfClients=-1)
 
template<class T >
T receive ()
 
std::string receivePacket ()
 Length-prefixed packet from a ready client. Same timeout/throw semantics.
 
- Public Member Functions inherited from ots::TCPServerBase
 TCPServerBase (unsigned int serverPort, unsigned int maxNumberOfClients=0)
 Means as many unsigned allows.
 
void startAccept (void)
 
void shutdownAccept (void)
 
void broadcastPacket (const char *message, std::size_t length)
 
void broadcastPacket (const std::string &message)
 
void broadcast (const char *message, std::size_t length)
 
void broadcast (const std::string &message)
 
void broadcast (const std::vector< char > &message)
 
void broadcast (const std::vector< uint16_t > &message)
 
- Public Member Functions inherited from ots::TCPSocket
 TCPSocket (int socketId=invalidSocketId)
 Designed to be a base class not used used directly.
 
 TCPSocket (TCPSocket &&move)
 Moveable but not Copyable.
 
TCPSocket & operator= (TCPSocket &&move)
 
void swap (TCPSocket &other)
 
 TCPSocket (TCPSocket const &)=delete
 Explicitly deleting copy constructor.
 
TCPSocket & operator= (TCPSocket const &)=delete
 
int getSocketId (void) const
 
void open (void)
 
void close (void)
 
void sendClose (void)
 

Protected Member Functions

void acceptConnections () override
 
TCPReceiverSocket * waitForReadableClient (std::chrono::milliseconds timeout)
 
- Protected Member Functions inherited from ots::TCPServerBase
void closeClientSocket (int socket)
 
template<class T >
T * acceptClient (bool blocking=true)
 
void removeClient (int socketId)
 Drop a client whose peer has gone away (no shutdown handshake, just close).
 
std::vector< int > getClientSocketIds (void) const
 Snapshot of connected client socket ids, safe to call from any thread.
 
void pingActiveClients (void)
 
bool getAccept ()
 

Protected Attributes

int lastReceived = -1
 
- Protected Attributes inherited from ots::TCPServerBase
std::mutex fClientsMutex
 
std::map< int, TCPSocket * > fConnectedClients
 
std::map< int, std::future< void > > fConnectedClientsFuture
 
const int E_SHUTDOWN = 0
 

Static Protected Attributes

static constexpr std::chrono::milliseconds kPollTimeout {5}
 
- Static Protected Attributes inherited from ots::TCPSocket
static constexpr int invalidSocketId = -1
 

Detailed Description

TCPListenServer Accepts any number of clients and lets one reader thread pull data from whichever client has something to say. Reads never block on an idle client: the reader polls all clients, picks a ready one round-robin, and reads from it. A client whose peer closed the connection is dropped from the list on the spot.

Definition at line 18 of file TCPListenServer.h.

Member Function Documentation

◆ receive()

template<class T >
T ots::TCPListenServer::receive
inline

Raw read from a ready client. Returns an empty T if no client had data within the poll timeout. Throws if the chosen client's connection closed (after removing it).

Definition at line 47 of file TCPListenServer.h.

◆ waitForReadableClient()

TCPReceiverSocket * TCPListenServer::waitForReadableClient ( std::chrono::milliseconds  timeout)
protected

Wait up to timeout for any client to become readable and return it, rotating through clients so a chatty one cannot starve the others. nullptr on timeout or when no clients are connected (the timeout is still honoured, so callers do not spin).

Definition at line 27 of file TCPListenServer.cc.


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