artdaq_core_demo  v1_07_01
UDPFragmentWriter.hh
1 #ifndef artdaq_core_demo_Overlays_UDPFragmentWriter_hh
2 #define artdaq_core_demo_Overlays_UDPFragmentWriter_hh
3 
4 #include "artdaq-core-demo/Overlays/UDPFragment.hh"
5 #include "artdaq-core/Data/Fragment.hh"
6 
7 namespace demo {
8 class UDPFragmentWriter;
9 }
10 
24 {
25 public:
31  explicit UDPFragmentWriter(artdaq::Fragment& f);
32 
37  uint8_t* dataBegin();
38 
43  uint8_t* dataEnd();
44 
50  {
51  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header));
52  return reinterpret_cast<Header*>(artdaq_Fragment_.dataBeginBytes()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
53  }
54 
59  void set_hdr_type(Header::data_type_t dataType) { header_()->type = dataType & 0xF; }
60 
65  void resize(size_t nBytes);
66 
67 private:
73  static size_t calc_event_size_words_(size_t nBytes);
74 
80  static size_t bytes_to_words_(size_t nBytes);
81 
82  // Note that this non-const reference hides the const reference in the base class
83  artdaq::Fragment& artdaq_Fragment_;
84 };
85 
86 inline demo::UDPFragmentWriter::UDPFragmentWriter(artdaq::Fragment& f)
87  : UDPFragment(f), artdaq_Fragment_(f)
88 {
89  if (!f.hasMetadata() || f.dataSizeBytes() > 0)
90  {
91  throw cet::exception(
92  "Error in UDPFragmentWriter: Raw artdaq::Fragment object does not appear to consist of (and only of) its own "
93  "header + the UDPFragment::Metadata object");
94  }
95 
96  // Allocate space for the header
97  artdaq_Fragment_.resizeBytes(sizeof(Header));
98 }
99 
101 {
102  // Make sure there's data past the UDPFragment header
103  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header) + sizeof(artdaq::Fragment::value_type));
104  return reinterpret_cast<uint8_t*>(header_() + 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic)
105 }
106 
108 {
109  return dataBegin() + udp_data_words() * bytes_per_word_(); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
110 }
111 
112 inline void demo::UDPFragmentWriter::resize(size_t nBytes)
113 {
114  artdaq_Fragment_.resizeBytes(sizeof(Header::data_t) * calc_event_size_words_(nBytes));
115  header_()->event_size = calc_event_size_words_(nBytes);
116 }
117 
118 inline size_t demo::UDPFragmentWriter::calc_event_size_words_(size_t nBytes)
119 {
120  return bytes_to_words_(nBytes) + hdr_size_words();
121 }
122 
123 inline size_t demo::UDPFragmentWriter::bytes_to_words_(size_t nBytes)
124 {
125  auto mod(nBytes % bytes_per_word_());
126  return (mod == 0) ? nBytes / bytes_per_word_() : nBytes / bytes_per_word_() + 1;
127 }
128 
129 #endif /* artdaq_demo_Overlays_UDPFragmentWriter_hh */
uint32_t data_type_t
Type of the type field.
Definition: UDPFragment.hh:63
Class derived from UDPFragment which allows writes to the data.
Header * header_()
Get a pointer to the UDPFragment::Header object for writing.
The UDPFragment::Header contains information about the payload size and the &quot;data type&quot; of the UDP da...
Definition: UDPFragment.hh:58
A Fragment designed to contain data received from the network in UDP datagrams.
Definition: UDPFragment.hh:20
uint8_t * dataBegin()
Get a pointer to the start of the UDP payload.
void resize(size_t nBytes)
Resize the UDP payload to the given number of bytes.
uint8_t * dataEnd()
Get a pointer to the end of the UDP payload.
event_size_t type
The type of the payload data, 0: Raw, 1: JSON, 2: String.
Definition: UDPFragment.hh:66
UDPFragmentWriter(artdaq::Fragment &f)
UDPFragmentWriter constructor.
void set_hdr_type(Header::data_type_t dataType)
Setter for the Header::type field.