1 #include "otsdaq/NetworkUtilities/TCPListenServer.h"
2 #include "otsdaq/NetworkUtilities/TCPReceiverSocket.h"
12 constexpr std::chrono::milliseconds TCPListenServer::kPollTimeout;
15 TCPListenServer::TCPListenServer(
unsigned int serverPort,
unsigned int maxNumberOfClients)
21 TCPListenServer::~TCPListenServer(
void)
28 std::chrono::milliseconds timeout)
31 std::vector<std::pair<int, TCPSocket*>> clients;
33 std::lock_guard<std::mutex> lock(fClientsMutex);
34 clients.assign(fConnectedClients.begin(), fConnectedClients.end());
39 std::this_thread::sleep_for(timeout);
43 std::vector<struct pollfd> fds(clients.size());
44 for(
size_t i = 0; i < clients.size(); ++i)
46 fds[i].fd = clients[i].first;
47 fds[i].events = POLLIN;
51 int ready = ::poll(fds.data(), fds.size(),
static_cast<int>(timeout.count()));
57 for(
size_t i = 0; i < clients.size(); ++i)
58 if(clients[i].first == lastReceived)
63 for(
size_t k = 0; k < clients.size(); ++k)
65 size_t i = (start + k) % clients.size();
68 if(fds[i].revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL))
70 lastReceived = clients[i].first;
71 std::lock_guard<std::mutex> lock(fClientsMutex);
72 auto it = fConnectedClients.find(clients[i].first);
73 if(it == fConnectedClients.end())
75 TLOG(25,
"TCPListenServer")
76 <<
"Reading from socket " << lastReceived <<
", there are "
77 << fConnectedClients.size() <<
" clients connected.";
90 const int socketId = client->getSocketId();
95 return client->receivePacket(std::chrono::milliseconds(100));
105 void TCPListenServer::acceptConnections()
112 acceptClient<TCPReceiverSocket>();
TCPReceiverSocket * waitForReadableClient(std::chrono::milliseconds timeout)
std::string receivePacket()
Length-prefixed packet from a ready client. Same timeout/throw semantics.
void removeClient(int socketId)
Drop a client whose peer has gone away (no shutdown handshake, just close).
defines used also by OtsConfigurationWizardSupervisor