artdaq  v3_09_01
TCPSocketTransfer.hh
1 #ifndef TCPSocketTransfer_hh
2 #define TCPSocketTransfer_hh
3 // This file (TCPSocketTransfer.hh) was created by Ron Rechenmacher <ron@fnal.gov> on
4 // Sep 14, 2016. "TERMS AND CONDITIONS" governing this file are in the README
5 // or COPYING file. If you do not have such a file, one can be obtained by
6 // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
7 // $RCSfile: .emacs.gnu,v $
8 // rev="$Revision: 1.30 $$Date: 2016/03/01 14:27:27 $";
9 
10 // C Includes
11 #include <sys/uio.h> // iovec
12 #include <cstdint> // uint64_t
13 
14 // C++ Includes
15 #include <boost/thread.hpp>
16 #include <condition_variable>
17 
18 // Products includes
19 #include "fhiclcpp/fwd.h"
20 
21 // artdaq Includes
22 #include "artdaq-core/Data/Fragment.hh"
23 #include "artdaq/TransferPlugins/TransferInterface.hh"
24 #include "artdaq/TransferPlugins/detail/HostMap.hh"
25 #include "artdaq/TransferPlugins/detail/SRSockets.hh"
26 #include "artdaq/TransferPlugins/detail/Timeout.hh" // Timeout
27 
28 #ifndef USE_ACKS
29 #define USE_ACKS 0
30 #endif
31 
32 namespace artdaq {
33 class TCPSocketTransfer;
34 }
35 
40 {
41 public:
60  TCPSocketTransfer(fhicl::ParameterSet const& ps, Role role);
61 
62  virtual ~TCPSocketTransfer() noexcept;
63 
70  int receiveFragmentHeader(detail::RawFragmentHeader& header, size_t timeout_usec) override;
71 
78  int receiveFragmentData(RawDataType* destination, size_t wordCount) override;
79 
86  CopyStatus transfer_fragment_min_blocking_mode(Fragment const& frag, size_t timeout_usec) override { return sendFragment_(Fragment(frag), timeout_usec); }
87 
93  CopyStatus transfer_fragment_reliable_mode(Fragment&& frag) override { return sendFragment_(std::move(frag), 0); }
94 
99  bool isRunning() override;
100 
105  void flush_buffers() override;
106 
107 private:
108  static std::atomic<int> listen_thread_refcount_;
109  static std::mutex listen_thread_mutex_;
110  static std::unique_ptr<boost::thread> listen_thread_;
111  static std::map<int, std::set<int>> connected_fds_;
112  static std::mutex fd_mutex_;
113  int send_fd_;
114  std::map<int, int> active_receive_fds_;
115  std::map<int, int> last_active_receive_fds_;
116 
117  union
118  {
119  MessHead mh;
120  uint8_t mha[sizeof(MessHead)];
121  };
122 
123  enum class SocketState
124  {
125  Metadata,
126  Data
127  };
128 
129  size_t rcvbuf_;
130  size_t sndbuf_;
131  size_t send_retry_timeout_us_;
132 
133  hostMap_t hostMap_;
134 
135  volatile unsigned connect_state : 1; // 0=not "connected" (initial msg not sent)
136  unsigned blocking : 1; // compatible with bool (true/false)
137 
138  bool connection_was_lost_;
139 
140  bool timeoutMessageArmed_; // don't repeatedly print about the send fd not being open...
141  std::chrono::steady_clock::time_point last_recv_time_; // Time of last successful receive
142  double receive_disconnected_wait_s_; // How long to wait between messages before returning DATA_END
143  size_t receive_err_wait_us_; // Amount of time to wait if there are no connected receive sockets
144  std::atomic<bool> receive_socket_has_been_connected_; // Whether the receiver has ever been connected to a sender
145  std::atomic<int> send_ack_diff_; // Number of sends - number of acks received. Not allowed to exceed buffer_count.
146  std::unique_ptr<boost::thread> ack_listen_thread_; // Thread to listen for ack messages on the sender
147 
148 private: // methods
149  TCPSocketTransfer(TCPSocketTransfer const&) = delete;
151  TCPSocketTransfer& operator=(TCPSocketTransfer const&) = delete;
152  TCPSocketTransfer& operator=(TCPSocketTransfer&&) = delete;
153 
154  CopyStatus sendFragment_(Fragment&& frag, size_t timeout_usec);
155 
156  CopyStatus sendData_(const void* buf, size_t bytes, size_t send_timeout_usec, bool isHeader = false);
157 
158  CopyStatus sendData_(const struct iovec* iov, int iovcnt, size_t send_timeout_usec, bool isHeader = false);
159 
160 #if USE_ACKS
161  void receive_acks_();
162  void send_ack_(int fd);
163 #endif
164 
165  // Sender is responsible for connecting to receiver
166  void connect_();
167 
168  void reconnect_();
169 
170  void disconnect_receive_socket_(const std::string& msg = "");
171 
172  // Receiver should listen for connections
173  void start_listen_thread_();
174  static void listen_(int port, size_t rcvbuf);
175 
176  size_t getConnectedFDCount_(int source_rank);
177  int getActiveFD_(int source_rank);
178  void setActiveFD_(int source_rank, int fd);
179  int getLastActiveFD_(int source_rank);
180  void setLastActiveFD_(int source_rank, int fd);
181 };
182 
183 #endif // TCPSocketTransfer_hh
bool isRunning() override
Determine whether the TransferInterface plugin is able to send/receive data.
virtual int source_rank() const
Get the source rank for this TransferInterface instance.
Role role() const
Get the TransferInterface::Role of this TransferInterface.
int receiveFragmentHeader(detail::RawFragmentHeader &header, size_t timeout_usec) override
Receive a Fragment Header from the transport mechanism.
CopyStatus transfer_fragment_reliable_mode(Fragment &&frag) override
Transfer a Fragment to the destination. This should be reliable, if the underlying transport mechanis...
int receiveFragmentData(RawDataType *destination, size_t wordCount) override
Receive the body of a Fragment to the given destination pointer.
TCPSocketTransfer(fhicl::ParameterSet const &ps, Role role)
TCPSocketTransfer Constructor.
void flush_buffers() override
Flush any in-flight data. This should be used by the receiver after the receive loop has ended...
Role
Used to determine if a TransferInterface is a Sender or Receiver.
This interface defines the functions used to transfer data between artdaq applications.
TransferInterface implementation plugin that sends data using TCP sockets.
This header is sent by the TCPSocket_transfer to allow for more efficient writev calls.
Definition: SRSockets.hh:15
std::map< int, std::string > hostMap_t
The host_map is a map associating ranks with artdaq::DestinationInfo objects.
Definition: HostMap.hh:39
CopyStatus transfer_fragment_min_blocking_mode(Fragment const &frag, size_t timeout_usec) override
Transfer a Fragment to the destination. May not necessarily be reliable, but will not block longer th...
CopyStatus
Returned from the send functions, this enumeration describes the possible return codes. If an exception occurs, it will be thrown and should be handled normally.