Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
message_processor.hpp
Go to the documentation of this file.
1#pragma once
2
6#include "napi.h"
7#include <atomic>
8
9namespace bb::nodejs {
10
12 public:
13 template <typename T, typename R>
14 void register_handler(uint32_t msgType, T* self, R (T::*handler)() const, bool unique = false)
15 {
16 register_handler(msgType, self, handler, unique);
17 }
18
19 template <typename T, typename R>
20 void register_handler(uint32_t msgType, T* self, R (T::*handler)(), bool unique = false)
21 {
22 _register_handler<messaging::HeaderOnlyMessage, R>(
23 msgType, [=](auto, const msgpack::object&) { return (self->*handler)(); }, unique);
24 }
25
26 template <typename T, typename P, typename R>
27 void register_handler(uint32_t msgType, T* self, R (T::*handler)(const P&) const, bool unique = false)
28 {
29 register_handler(msgType, self, handler, unique);
30 }
31
32 template <typename T, typename P, typename R>
33 void register_handler(uint32_t msgType, T* self, R (T::*handler)(const P&), bool unique = false)
34 {
35 _register_handler<messaging::TypedMessage<P>, R>(
36 msgType,
37 [=](const messaging::TypedMessage<P>& req, const msgpack::object&) { return (self->*handler)(req.value); },
38 unique);
39 }
40
41 template <typename T, typename P, typename R>
42 void register_handler(uint32_t msgType,
43 T* self,
44 R (T::*handler)(const P&, const msgpack::object&) const,
45 bool unique = false)
46 {
47 register_handler(msgType, self, handler, unique);
48 }
49
50 template <typename T, typename P, typename R>
51 void register_handler(uint32_t msgType,
52 T* self,
53 R (T::*handler)(const P&, const msgpack::object&),
54 bool unique = false)
55 {
56 _register_handler<messaging::TypedMessage<P>, R>(
57 msgType,
58 [=](const messaging::TypedMessage<P>& req, const msgpack::object& obj) {
59 return (self->*handler)(req.value, obj);
60 },
61 unique);
62 }
63
64 Napi::Promise process_message(const Napi::CallbackInfo& info)
65 {
66 Napi::Env env = info.Env();
67 // keep this in a shared pointer so that AsyncOperation can resolve/reject the promise once the execution is
68 // complete on an separate thread
70
71 if (!open) {
72 deferred->Reject(Napi::TypeError::New(env, "Message processor is closed").Value());
73 } else if (info.Length() < 1) {
74 deferred->Reject(Napi::TypeError::New(env, "Wrong number of arguments").Value());
75 } else if (!info[0].IsBuffer()) {
76 deferred->Reject(Napi::TypeError::New(env, "Argument must be a buffer").Value());
77 } else {
78 auto buffer = info[0].As<Napi::Buffer<char>>();
79 size_t length = buffer.Length();
80 // we mustn't access the Napi::Env outside of this top-level function
81 // so copy the data to a variable we own
82 // and make it a shared pointer so that it doesn't get destroyed as soon as we exit this code block
84 std::copy_n(buffer.Data(), length, data->data());
85
86 auto* op = new bb::nodejs::AsyncOperation(env, deferred, [data, this, length](msgpack::sbuffer& buf) {
87 msgpack::object_handle obj_handle = msgpack::unpack(data->data(), length);
88 msgpack::object obj = obj_handle.get();
90 });
91
92 // Napi is now responsible for destroying this object
93 op->Queue();
94 }
95
96 return deferred->Promise();
97 }
98
99 void close() { open = false; }
100
101 private:
104
105 template <typename P, typename R>
106 void _register_handler(uint32_t msgType,
107 const std::function<R(const P&, const msgpack::object&)>& fn,
108 bool unique = false)
109 {
111 msgType,
112 [=](msgpack::object& obj, msgpack::sbuffer& buffer) {
113 P req_msg;
114 obj.convert(req_msg);
115
116 R response = fn(req_msg, obj);
117
118 bb::messaging::MsgHeader header(req_msg.header.messageId);
119 bb::messaging::TypedMessage<R> resp_msg(msgType, header, response);
120 msgpack::pack(buffer, resp_msg);
121
122 return true;
123 },
124 unique);
125 }
126};
127
128} // namespace bb::nodejs
bool on_new_data(msgpack::object &obj, msgpack::sbuffer &buffer) const
void register_target(uint32_t msgType, const message_handler &handler, bool unique=false)
void register_handler(uint32_t msgType, T *self, R(T::*handler)(const P &, const msgpack::object &), bool unique=false)
void register_handler(uint32_t msgType, T *self, R(T::*handler)() const, bool unique=false)
void register_handler(uint32_t msgType, T *self, R(T::*handler)(const P &) const, bool unique=false)
void register_handler(uint32_t msgType, T *self, R(T::*handler)(const P &, const msgpack::object &) const, bool unique=false)
bb::messaging::MessageDispatcher dispatcher
void register_handler(uint32_t msgType, T *self, R(T::*handler)(const P &), bool unique=false)
Napi::Promise process_message(const Napi::CallbackInfo &info)
void register_handler(uint32_t msgType, T *self, R(T::*handler)(), bool unique=false)
void _register_handler(uint32_t msgType, const std::function< R(const P &, const msgpack::object &)> &fn, bool unique=false)
Encapsulatest some work that can be done off the JavaScript main thread.
Definition async_op.hpp:27
void info(Args... args)
Definition log.hpp:70
const std::vector< FF > data
uint8_t const size_t length
Definition data_store.hpp:9
uint8_t const * buf
Definition data_store.hpp:9
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
std::vector< uint8_t > Value
Definition types.hpp:12
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13