otsdaq  v2_05_02_indev
TCPReceiverSocket.h
1 #ifndef _TCPReceiverSocket_h_
2 #define _TCPReceiverSocket_h_
3 
4 #include <string>
5 #include "otsdaq/NetworkUtilities/TCPPacket.h"
6 #include "otsdaq/NetworkUtilities/TCPSocket.h"
7 
8 namespace ots
9 {
10 class TCPReceiverSocket : public virtual TCPSocket
11 {
12  public:
13  TCPReceiverSocket(int socketId = invalidSocketId);
14  virtual ~TCPReceiverSocket();
15  // TCPReceiverSocket(TCPReceiverSocket const&) = delete ;
16  TCPReceiverSocket(TCPReceiverSocket&& theTCPReceiverSocket) = default;
17 
18  template<class T>
19  T receive(void)
20  {
21  T buffer;
22  buffer.resize(maxSocketSize);
23  int length = receive(static_cast<char*>(&buffer.at(0)), maxSocketSize);
24  if(length == -1)
25  length = 0;
26  buffer.resize(length);
27  // std::cout << __PRETTY_FUNCTION__ << "Message received-" << fBuffer << "-" <<
28  // std::endl;
29  return buffer; // c++11 doesn't make a copy anymore when returned
30  }
31  std::string receivePacket();
32  void setReceiveTimeout(unsigned int timeoutSeconds, unsigned int timeoutMicroSeconds);
33 
34  private:
35  std::size_t receive(char* buffer, std::size_t bufferSize = maxSocketSize, int timeoutMicroSeconds = -1);
36  static constexpr unsigned int maxSocketSize = 65536;
37  TCPPacket fPacket;
38 };
39 }
40 #endif