DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
SourceModel.hpp
Go to the documentation of this file.
1
8#ifndef ASIOLIBS_SRC_SOURCEMODEL_HPP_
9#define ASIOLIBS_SRC_SOURCEMODEL_HPP_
10
11#include "SourceConcept.hpp"
12
13
15#include "iomanager/Sender.hpp"
16#include "logging/Logging.hpp"
17
19
20// #include "datahandlinglibs/utils/ReusableThread.hpp"
23
24// #include <folly/ProducerConsumerQueue.h>
25// #include <nlohmann/json.hpp>
26
27#include <atomic>
28#include <memory>
29#include <mutex>
30#include <string>
31#include <utility>
32
33namespace dunedaq::asiolibs {
34
35template<class TargetPayloadType>
37{
38public:
41 using data_t = nlohmann::json;
42
49 {}
51
52 void acquire_callback() override
53 {
55 TLOG_DEBUG(5) << "SourceModel callback is already acquired!";
56 } else {
57 // Getting DataMoveCBRegistry
59 m_sink_callback = dmcbr->get_callback<TargetPayloadType>(inherited::m_sink_conf);
61 }
62 }
63
64 // Process an incoming raw byte buffer and extract complete frames of type TargetPayloadType.
65 void handle_daq_frame(char* buffer) override
66 {
67 // Materialize a real TargetPayloadType object by copying bytes from the buffer.
68 // This is defined behavior, alignment-safe, and fast, without pointer vodoo
69 // Previously reinterpret_cast to TargetPayloadType* introduced alignment traps
70 // “pretend there’s a constructed object there” UB.
71 TargetPayloadType frame;
72 std::memcpy(&frame, buffer, m_expected_frame_size);
73
74 // Pass by value (moved); no references into 'buffer', so no UAF.
75 (*m_sink_callback)(std::move(frame));
76 }
77
78 std::size_t get_expected_frame_size() const override {
80 }
81
82 void generate_opmon_data() override {
83
85 info.set_leftover_bytes_encountered( m_leftover_bytes_encountered.exchange(0) );
86
87 publish( std::move(info) );
88 }
89
90private:
91 // Constants
92 const std::size_t m_expected_frame_size = sizeof(TargetPayloadType);
93
94 // Callback internals
96 using sink_cb_t = std::shared_ptr<std::function<void(TargetPayloadType&&)>>;
98
99 // Stats
100 std::atomic<uint64_t> m_leftover_bytes_encountered{0}; // NOLINT(build/unsigned)
101
102};
103
104} // namespace dunedaq::asiolibs
105
106#endif // ASIOLIBS_SRC_SOURCEMODEL_HPP_
const appmodel::DataMoveCallbackConf * m_sink_conf
void generate_opmon_data() override
const std::size_t m_expected_frame_size
std::atomic< uint64_t > m_leftover_bytes_encountered
std::shared_ptr< std::function< void(TargetPayloadType &&)> > sink_cb_t
SourceModel()
SourceModel Constructor.
std::size_t get_expected_frame_size() const override
void handle_daq_frame(char *buffer) override
iomanager::SenderConcept< TargetPayloadType > sink_t
static std::shared_ptr< DataMoveCallbackRegistry > get()
void publish(google::protobuf::Message &&, CustomOrigin &&co={}, OpMonLevel l=to_level(EntryOpMonLevel::kDefault)) const noexcept
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112