pars 0.2.1
Loading...
Searching...
No Matches
pars::ev::serialize Struct Reference

#include <serializer.h>

Static Public Member Functions

template<event_c event_t>
static nngxx::msg to_network (event_t &ev)
 
template<event_c event_t>
static event_t to_event (const nngxx::msg &m)
 

Detailed Description

Definition at line 50 of file serializer.h.

Member Function Documentation

◆ to_event()

template<event_c event_t>
static event_t pars::ev::serialize::to_event ( const nngxx::msg & m)
inlinestatic

Definition at line 83 of file serializer.h.

84 {
85 // 1. compute received and requested event hash
86 auto recv_event_hash = hash_from_msg(m);
87 auto req_event_hash = uuid<klass<event_t>>::hash;
88
89 // 2. check they correspond
90 if (recv_event_hash - req_event_hash != 0)
91 throw std::runtime_error("Requested event mismatch!");
92
93 // 3. deserialize event
94 auto body = m.body();
95 auto view =
96 std::string_view(body.data<char>() + sizeof(uint64_t), body.size());
97 auto istring = std::ispanstream(view);
98 auto istream = std::istream(istring.rdbuf());
99 auto ar = cereal::BinaryInputArchive(istream);
100
101 auto ev = event_t{};
102 klass<event_t>::serialize(ev, ar);
103
104 pars::debug(SL, lf::event, "Serialized Message [{}] to Event [{}]", m, ev);
105
106 return ev;
107 }
#define SL
Definition log.h:58
@ event
Definition flags.h:43
void debug(spdlog::source_loc loc, pars::lf lf, spdlog::format_string_t< args_t... > fmt, args_t &&... args)
Definition log.h:129

References pars::debug(), pars::event, and SL.

Referenced by pars::ev::job::event().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_network()

template<event_c event_t>
static nngxx::msg pars::ev::serialize::to_network ( event_t & ev)
inlinestatic

Definition at line 53 of file serializer.h.

54 {
55 // 1. serialize the event to a stringstream
56 auto ostring = std::ostringstream();
57 auto ostream = std::ostream(ostring.rdbuf());
58 auto ar = cereal::BinaryOutputArchive(ostream);
59 klass<event_t>::serialize(ev, ar);
60
61 // 2. compute the event_hash
62 auto event_hash = uuid<klass<event_t>>::hash;
63
64 // 3. create the nngxx::msg to hold the hash+event
65 auto serialization = ostring.rdbuf()->view();
66 auto m = nngxx::make_msg(sizeof(event_hash) + serialization.size())
67 .value_or_abort();
68 auto b = m.body();
69
70 // 4. append the event hash
71 std::memcpy(b.data<char>(), &event_hash, sizeof(event_hash));
72
73 // 5. append the serialized event
74 std::memcpy(b.data<char>() + sizeof(event_hash), serialization.data(),
75 serialization.size());
76
77 pars::debug(SL, lf::event, "Serialized Event [{}] to Message [{}]", ev, m);
78
79 return m;
80 }

References pars::debug(), pars::event, and SL.

Referenced by pars::net::op::send().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this struct was generated from the following file: