Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
msgpack_client_wrapper.cpp
Go to the documentation of this file.
3#include "napi.h"
4#include <cstdint>
5#include <vector>
6
7using namespace bb::nodejs::msgpack_client;
8
10 : ObjectWrap(info)
11{
12 Napi::Env env = info.Env();
13
14 // Arg 0: shared memory base name (string)
15 if (info.Length() < 1 || !info[0].IsString()) {
16 throw Napi::TypeError::New(env, "First argument must be a string (shared memory name)");
17 }
18 std::string shm_name = info[0].As<Napi::String>();
19
20 size_t client_id = 0;
21 if (info.Length() >= 2 && info[1].IsNumber()) {
22 client_id = static_cast<size_t>(info[1].As<Napi::Number>().Uint32Value());
23 }
24
25 client_ = bb::ipc::IpcClient::create_mpsc_shm(shm_name, client_id);
26
27 // Connect to bb server
28 if (!client_->connect()) {
29 throw Napi::Error::New(env, "Failed to connect to shared memory server");
30 }
31
32 connected_ = true;
33}
34
41
42Napi::Value MsgpackClientWrapper::call(const Napi::CallbackInfo& info)
43{
44 Napi::Env env = info.Env();
45
46 if (!connected_) {
47 throw Napi::Error::New(env, "Client is not connected");
48 }
49
50 // Arg 0: msgpack buffer to send
51 if (info.Length() < 1 || !info[0].IsBuffer()) {
52 throw Napi::TypeError::New(env, "First argument must be a Buffer");
53 }
54
55 auto input_buffer = info[0].As<Napi::Buffer<uint8_t>>();
56 const uint8_t* input_data = input_buffer.Data();
57 size_t input_len = input_buffer.Length();
58
59 // Send request with retry on backpressure (1s timeout per attempt)
60 // NOTE: timeout_ns=0 means IMMEDIATE timeout (not infinite wait!)
61 // Loop until send succeeds - handles case where consumer is temporarily behind
62 constexpr uint64_t TIMEOUT_NS = 1000000000; // 1 second
63 while (!client_->send(input_data, input_len, TIMEOUT_NS)) {
64 // Ring buffer full, consumer is behind - retry
65 }
66
67 // Receive response with retry (1s timeout per attempt)
68 // Loop until response is ready - handles case where server is processing
70 while ((response = client_->receive(TIMEOUT_NS)).empty()) {
71 // Response not ready yet, server is processing - retry
72 }
73
74 // Create JavaScript Buffer with the response (copy to JS land)
75 auto js_buffer = Napi::Buffer<uint8_t>::Copy(env, response.data(), response.size());
76
77 // Release the message (for shared memory this frees space in ring buffer)
78 client_->release(response.size());
79
80 return js_buffer;
81}
82
83Napi::Value MsgpackClientWrapper::close(const Napi::CallbackInfo& info)
84{
85 Napi::Env env = info.Env();
86
87 if (client_ && connected_) {
88 client_->close();
89 connected_ = false;
90 }
91
92 return env.Undefined();
93}
94
95Napi::Function MsgpackClientWrapper::get_class(Napi::Env env)
96{
97 return DefineClass(env,
98 "MsgpackClient",
99 {
100 MsgpackClientWrapper::InstanceMethod("call", &MsgpackClientWrapper::call),
101 MsgpackClientWrapper::InstanceMethod("close", &MsgpackClientWrapper::close),
102 });
103}
static std::unique_ptr< IpcClient > create_mpsc_shm(const std::string &base_name, size_t client_id)
Napi::Value close(const Napi::CallbackInfo &info)
Close the shared memory connection.
Napi::Value call(const Napi::CallbackInfo &info)
Send a msgpack buffer and receive response.
#define info(...)
Definition log.hpp:93
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13