otsdaq  v2_05_02_indev
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 {
14  // TransceiverSocket is a "Friend" class of ReceiverSocket so has access to private
15  // members.
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, unsigned int timeoutSeconds = 1, unsigned int timeoutUSeconds = 0, bool verbose = false);
23  int receive(std::vector<uint32_t>& buffer, unsigned int timeoutSeconds = 1, unsigned int timeoutUSeconds = 0, bool verbose = false);
24  int receive(std::string& buffer,
25  unsigned long& fromIPAddress,
26  unsigned short& fromPort,
27  unsigned int timeoutSeconds = 1,
28  unsigned int timeoutUSeconds = 0,
29  bool verbose = false);
30  int receive(std::vector<uint32_t>& buffer,
31  unsigned long& fromIPAddress,
32  unsigned short& fromPort,
33  unsigned int timeoutSeconds = 1,
34  unsigned int timeoutUSeconds = 0,
35  bool verbose = false);
36 
37  protected:
38  ReceiverSocket(void);
39 
40  private:
41  fd_set fileDescriptor_;
42  struct timeval timeout_;
43  struct sockaddr_in fromAddress_;
44  socklen_t addressLength_;
45  int numberOfBytes_;
46 
47  unsigned long dummyIPAddress_;
48  unsigned short dummyPort_;
49  unsigned int readCounter_;
50 
51  std::mutex receiveMutex_; // to make receiver socket thread safe
52  // i.e. multiple threads can share a socket and call receive()
53 };
54 
55 } // namespace ots
56 
57 #endif