artdaq_core_demo  v1_07_01
UDPFragment.hh
1 #ifndef artdaq_core_demo_Overlays_UDPFragment_hh
2 #define artdaq_core_demo_Overlays_UDPFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 
6 #include <ostream>
7 
8 // Implementation of "UDPFragment", an artdaq::Fragment overlay class
9 
10 namespace demo {
11 class UDPFragment;
12 
14 std::ostream& operator<<(std::ostream&, UDPFragment const&);
15 } // namespace demo
16 
21 {
22 public:
30  struct Metadata
31  {
32  typedef uint64_t data_t;
33 
34  data_t port : 16;
35  data_t address : 32;
36  data_t unused : 16;
37 
38  static size_t const size_words = 1ull;
39  };
40 
41  static_assert(sizeof(Metadata) == Metadata::size_words * sizeof(Metadata::data_t),
42  "UDPFragment::Metadata size changed");
43 
58  struct Header
59  {
60  typedef uint32_t data_t;
61 
62  typedef uint32_t event_size_t;
63  typedef uint32_t data_type_t;
64 
67 
68  static size_t const size_words = 1ul;
69  };
70 
71  static_assert(sizeof(Header) == Header::size_words * sizeof(Header::data_t), "UDPFragment::Header size changed");
72 
79  explicit UDPFragment(artdaq::Fragment const& f)
80  : artdaq_Fragment_(f) {}
81 
96  static constexpr size_t hdr_size_words() { return Header::size_words; }
97 
102  size_t udp_data_words() const { return hdr_event_size() - hdr_size_words(); }
103 
108  uint8_t const* dataBegin() const
109  {
110  return reinterpret_cast<uint8_t const*>(header_() + 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic)
111  }
112 
117  uint8_t const* dataEnd() const
118  {
119  return dataBegin() + udp_data_words() * bytes_per_word_(); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
120  }
121 
122 protected:
127  static constexpr size_t bytes_per_word_() { return sizeof(Header::data_t) / sizeof(uint8_t); }
128 
137  Header const* header_() const
138  {
139  return reinterpret_cast<UDPFragment::Header const*>(artdaq_Fragment_.dataBeginBytes()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
140  }
141 
142 private:
143  artdaq::Fragment const& artdaq_Fragment_;
144 };
145 
146 #endif /* artdaq_core_ots_Overlays_UDPFragment_hh */
Header::event_size_t hdr_event_size() const
Get the current value of the Header::event_size field.
Definition: UDPFragment.hh:86
uint32_t data_type_t
Type of the type field.
Definition: UDPFragment.hh:63
Metadata describing the contents of the UDPFragment.
Definition: UDPFragment.hh:30
The UDPFragment::Header contains information about the payload size and the &quot;data type&quot; of the UDP da...
Definition: UDPFragment.hh:58
std::ostream & operator<<(std::ostream &, AsciiFragment const &)
Dumps the AsciiFragment&#39;s data (text) to given stream.
static size_t const size_words
Size of the UDPFragment::Header, in units of Header::data_t.
Definition: UDPFragment.hh:68
Header const * header_() const
Get a const pointer to the UDPFragment::Header.
Definition: UDPFragment.hh:137
A Fragment designed to contain data received from the network in UDP datagrams.
Definition: UDPFragment.hh:20
static constexpr size_t bytes_per_word_()
Translates between data words (type Header::data_t) and bytes.
Definition: UDPFragment.hh:127
size_t udp_data_words() const
Get the number of words in the UDPFragment payload.
Definition: UDPFragment.hh:102
event_size_t event_size
The size of the payload, in words.
Definition: UDPFragment.hh:65
UDPFragment(artdaq::Fragment const &f)
The UDPFragment constructor.
Definition: UDPFragment.hh:79
Header::data_type_t hdr_data_type() const
Get the current value of the Header::type field.
Definition: UDPFragment.hh:91
static size_t const size_words
Size of the UDPFragment::Metadata object, in units of Metadata::data_t.
Definition: UDPFragment.hh:38
data_t port
The port on which the data was received.
Definition: UDPFragment.hh:34
data_t address
The IPv4 address the data was received on.
Definition: UDPFragment.hh:35
uint32_t event_size_t
Type of the event_size field.
Definition: UDPFragment.hh:62
static constexpr size_t hdr_size_words()
Gets the size_words variable from the artdaq::Header.
Definition: UDPFragment.hh:96
uint8_t const * dataBegin() const
Returns a const pointer to the start of the UDP payload.
Definition: UDPFragment.hh:108
uint8_t const * dataEnd() const
Returns a const pointer to the end of the UDP payload.
Definition: UDPFragment.hh:117
uint32_t data_t
The fundamental unit of Header data.
Definition: UDPFragment.hh:60
uint64_t data_t
The fundamental unit of Metadata data.
Definition: UDPFragment.hh:32
data_t unused
Unused bits of the data_t field.
Definition: UDPFragment.hh:36
event_size_t type
The type of the payload data, 0: Raw, 1: JSON, 2: String.
Definition: UDPFragment.hh:66