otsdaq  3.04.02
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 flush()
23  {
24  int n = 0;
25  while(recvfrom(socketNumber_,
26  flushBuffer_,
27  sizeof(flushBuffer_),
28  MSG_DONTWAIT,
29  nullptr,
30  nullptr) >= 0)
31  {
32  ++n;
33  }
34  return n;
35  };
36 
37  int receive(std::string& buffer,
38  unsigned int timeoutSeconds = 1,
39  unsigned int timeoutUSeconds = 0,
40  bool verbose = false);
41  int receive(std::vector<uint32_t>& buffer,
42  unsigned int timeoutSeconds = 1,
43  unsigned int timeoutUSeconds = 0,
44  bool verbose = false);
45  int receive(std::string& buffer,
46  unsigned long& fromIPAddress,
47  unsigned short& fromPort,
48  unsigned int timeoutSeconds = 1,
49  unsigned int timeoutUSeconds = 0,
50  bool verbose = false);
51  int receive(std::vector<uint32_t>& buffer,
52  unsigned long& fromIPAddress,
53  unsigned short& fromPort,
54  unsigned int timeoutSeconds = 1,
55  unsigned int timeoutUSeconds = 0,
56  bool verbose = false);
57 
58  std::string getLastIncomingIPAddress(void);
59  unsigned short getLastIncomingPort(void);
60 
61  protected:
62  ReceiverSocket(void);
63 
64  private:
65  fd_set fileDescriptor_;
66  struct timeval timeout_;
67  struct sockaddr_in fromAddress_;
68  socklen_t addressLength_;
69  int numberOfBytes_;
70 
71  unsigned long lastIncomingIPAddress_ = 0;
72  unsigned short lastIncomingPort_ = 0;
73  unsigned int readCounter_;
74 
75  std::mutex receiveMutex_;
76  // i.e. multiple threads can share a socket and call receive()
77 
78  char flushBuffer_
79  [1500]{};
80 };
81 
82 } // namespace ots
83 
84 #endif
int receive(std::string &buffer, unsigned int timeoutSeconds=1, unsigned int timeoutUSeconds=0, bool verbose=false)
returns count of dropped packets
ReceiverSocket(void)
protected constructor
defines used also by OtsConfigurationWizardSupervisor