otsdaq  3.11.00
TCPListenServer.h
1 #ifndef _TCPListenServer_h_
2 #define _TCPListenServer_h_
3 
4 #include "otsdaq/NetworkUtilities/TCPReceiverSocket.h"
5 #include "otsdaq/NetworkUtilities/TCPServerBase.h"
6 
7 #include "TRACE/trace.h"
8 
9 #include <chrono>
10 
11 namespace ots
12 {
19 {
20  public:
21  TCPListenServer(unsigned int serverPort, unsigned int maxNumberOfClients = -1);
22  virtual ~TCPListenServer(void);
23 
27  template<class T>
28  T receive();
30  std::string receivePacket();
31 
32  protected:
33  void acceptConnections() override;
34 
39  TCPReceiverSocket* waitForReadableClient(std::chrono::milliseconds timeout);
40 
41  static constexpr std::chrono::milliseconds kPollTimeout{5};
42 
43  int lastReceived = -1;
44 };
45 
46 template<class T>
48 {
49  TCPReceiverSocket* client = waitForReadableClient(kPollTimeout);
50  if(client == nullptr)
51  return T();
52  const int socketId = client->getSocketId();
53  try
54  {
55  return client->receive<T>();
56  }
57  catch(...)
58  {
59  removeClient(socketId);
60  throw;
61  }
62 }
63 } // namespace ots
64 #endif
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