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