00001 #ifndef TCPSocketTransfer_hh
00002 #define TCPSocketTransfer_hh
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdint.h>
00012 #include <sys/uio.h>
00013
00014
00015 #include <condition_variable>
00016
00017
00018 #include "fhiclcpp/fwd.h"
00019
00020
00021 #include "artdaq/TransferPlugins/TransferInterface.hh"
00022 #include "artdaq/TransferPlugins/detail/SRSockets.hh"
00023 #include "artdaq/TransferPlugins/detail/Timeout.hh"
00024 #include "artdaq-core/Data/Fragment.hh"
00025
00026 namespace artdaq
00027 {
00028 class TCPSocketTransfer;
00029 }
00030
00034 class artdaq::TCPSocketTransfer : public TransferInterface
00035 {
00036 public:
00054 TCPSocketTransfer(fhicl::ParameterSet const& ps, Role role);
00055
00056 virtual ~TCPSocketTransfer();
00057
00064 int receiveFragmentHeader(detail::RawFragmentHeader& header, size_t receiveTimeout) override;
00065
00072 int receiveFragmentData(RawDataType* destination, size_t wordCount) override;
00073
00080 CopyStatus copyFragment(Fragment& frag, size_t timeout_usec) override { return sendFragment_(std::move(frag), timeout_usec); }
00081
00088 CopyStatus moveFragment(Fragment&& frag, size_t timeout_usec) override { return sendFragment_(std::move(frag), timeout_usec); }
00089
00090 private:
00091
00092 int fd_;
00093 int listen_fd_;
00094
00095 union
00096 {
00097 MessHead mh;
00098 uint8_t mha[sizeof(MessHead)];
00099 };
00100
00101 enum class SocketState
00102 {
00103 Metadata,
00104 Data
00105 };
00106
00107 SocketState state_;
00108
00109 size_t offset;
00110 int target_bytes;
00111 size_t rcvbuf_;
00112 size_t sndbuf_;
00113
00114 struct DestinationInfo
00115 {
00116 std::string hostname;
00117 int portOffset;
00118 };
00119
00120 std::unordered_map<size_t, DestinationInfo> hostMap_;
00121
00122 volatile unsigned connect_state : 1;
00123 unsigned blocking : 1;
00124
00125
00126 Timeout tmo_;
00127 bool stats_connect_stop_;
00128 boost::thread stats_connect_thread_;
00129 std::condition_variable stopstatscv_;
00130 std::mutex stopstatscvm_;
00131
00132 bool timeoutMessageArmed_;
00133
00134 private:
00135 CopyStatus sendFragment_(Fragment&& frag, size_t timeout_usec);
00136
00137 CopyStatus sendData_(const void* buf, size_t bytes, size_t tmo);
00138
00139 CopyStatus sendData_(const struct iovec* iov, int iovcnt, size_t tmo);
00140
00141
00142 void stats_connect_();
00143
00144
00145 void connect_();
00146
00147 void reconnect_();
00148
00149
00150 void listen_();
00151
00152 int calculate_port_() const { return (hostMap_.at(destination_rank())).portOffset + source_rank(); }
00153 };
00154
00155 #endif // TCPSocketTransfer_hh