EUDAQ
 All Classes Namespaces Files Functions Variables Pages
TransportTCP.hh
1 #ifndef EUDAQ_INCLUDED_TransportTCP
2 #define EUDAQ_INCLUDED_TransportTCP
3 
4 #include "eudaq/TransportFactory.hh"
5 #include "eudaq/TransportBase.hh"
6 #include "eudaq/Platform.hh"
7 
8 #if EUDAQ_PLATFORM_IS(WIN32) || EUDAQ_PLATFORM_IS(MINGW)
9 #ifndef __CINT__
10 #include <winsock.h> // using winsock2.h here would cause conflicts when including Windows4Root.h
11  // required e.g. by the ROOT online monitor
12 #endif
13 #else
14 #include <sys/select.h>
15 typedef int SOCKET;
16 #endif
17 
18 #include <vector>
19 #include <string>
20 #include <map>
21 
22 namespace eudaq {
23 
25  public:
26  ConnectionInfoTCP(SOCKET fd, const std::string &host = "")
27  : m_fd(fd), m_len(0), m_buf(""){ m_host=host;}
28  void append(size_t length, const char *data);
29  bool havepacket() const;
30  std::string getpacket();
31  SOCKET GetFd() const { return m_fd; }
32  void Disable() {
33  m_state = -1;
34  m_len = 0;
35  m_buf = "";
36  }
37  virtual bool Matches(const ConnectionInfo &other) const;
38  virtual void Print(std::ostream &) const;
39  virtual std::string GetRemote() const { return m_host; }
40  virtual ConnectionInfo *Clone() const {
41  return new ConnectionInfoTCP(*this);
42  }
43 
44  private:
45  void update_length(bool = false);
46  SOCKET m_fd;
47  size_t m_len;
48  std::string m_buf;
49  };
50 
51  class TCPServer : public TransportServer {
52  public:
53  TCPServer(const std::string &param);
54  virtual ~TCPServer();
55 
56  virtual void Close(const ConnectionInfo &id);
57  virtual void SendPacket(const unsigned char *data, size_t len,
58  const ConnectionInfo &id = ConnectionInfo::ALL,
59  bool duringconnect = false);
60  virtual void ProcessEvents(int timeout);
61 
62  virtual std::string ConnectionString() const;
63  static const std::string name;
64 
65  private:
66  int m_port;
67  SOCKET m_srvsock;
68  SOCKET m_maxfd;
69  fd_set m_fdset;
70 
71  ConnectionInfoTCP &GetInfo(SOCKET fd) const;
72  // typedef std::map<int, TCPConnection> map_t;
73  // map_t m_map;
74  };
75 
76  class TCPClient : public TransportClient {
77  public:
78  TCPClient(const std::string &param);
79  virtual ~TCPClient();
80 
81  virtual void SendPacket(const unsigned char *data, size_t len,
82  const ConnectionInfo &id = ConnectionInfo::ALL,
83  bool = false);
84  virtual void ProcessEvents(int timeout = -1);
85 
86  private:
87  void OpenConnection();
88  std::string m_server;
89  int m_port;
90  SOCKET m_sock;
91  ConnectionInfoTCP m_buf;
92  };
93 }
94 
95 #endif // EUDAQ_INCLUDED_TransportTCP
virtual void ProcessEvents(int timeout)
Definition: TransportTCP.cc:421
virtual void ProcessEvents(int timeout=-1)
Definition: TransportTCP.cc:590
Definition: TransportTCP.hh:76
virtual void SendPacket(const unsigned char *data, size_t len, const ConnectionInfo &id=ConnectionInfo::ALL, bool=false)
Definition: TransportTCP.cc:580
virtual void Close(const ConnectionInfo &id)
Definition: TransportTCP.cc:390
Definition: TransportTCP.hh:51
Definition: TransportServer.hh:10
Definition: TransportBase.hh:22
Definition: TransportClient.hh:9
virtual void SendPacket(const unsigned char *data, size_t len, const ConnectionInfo &id=ConnectionInfo::ALL, bool duringconnect=false)
Definition: TransportTCP.cc:405
Definition: TransportTCP.hh:24