tdaq-develop-2025-02-12
ReceiverSocket.h
1 #ifndef _ots_ReceiverSocket_h_
2 #define _ots_ReceiverSocket_h_
3 
4 #include "otsdaq/NetworkUtilities/Socket.h"
5 
6 #include <mutex> //for std::mutex
7 #include <string>
8 #include <vector>
9 
10 namespace ots
11 {
12 class ReceiverSocket : public virtual Socket
13 {
16  friend class TransceiverSocket;
17 
18  public:
19  ReceiverSocket(std::string IPAddress, unsigned int port = 0);
20  virtual ~ReceiverSocket(void);
21 
22  int receive(std::string& buffer,
23  unsigned int timeoutSeconds = 1,
24  unsigned int timeoutUSeconds = 0,
25  bool verbose = false);
26  int receive(std::vector<uint32_t>& buffer,
27  unsigned int timeoutSeconds = 1,
28  unsigned int timeoutUSeconds = 0,
29  bool verbose = false);
30  int receive(std::string& buffer,
31  unsigned long& fromIPAddress,
32  unsigned short& fromPort,
33  unsigned int timeoutSeconds = 1,
34  unsigned int timeoutUSeconds = 0,
35  bool verbose = false);
36  int receive(std::vector<uint32_t>& buffer,
37  unsigned long& fromIPAddress,
38  unsigned short& fromPort,
39  unsigned int timeoutSeconds = 1,
40  unsigned int timeoutUSeconds = 0,
41  bool verbose = false);
42 
43  std::string getLastIncomingIPAddress(void);
44  unsigned short getLastIncomingPort(void);
45 
46  protected:
47  ReceiverSocket(void);
48 
49  private:
50  fd_set fileDescriptor_;
51  struct timeval timeout_;
52  struct sockaddr_in fromAddress_;
53  socklen_t addressLength_;
54  int numberOfBytes_;
55 
56  unsigned long lastIncomingIPAddress_ = 0;
57  unsigned short lastIncomingPort_ = 0;
58  unsigned int readCounter_;
59 
60  std::mutex receiveMutex_;
61  // i.e. multiple threads can share a socket and call receive()
62 };
63 
64 } // namespace ots
65 
66 #endif
ReceiverSocket(void)
protected constructor