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 // Create shared memory client (SPSC-only, no max_clients needed)
22
23 // Connect to bb server
24 if (!client_->connect()) {
25 throw Napi::Error::New(env, "Failed to connect to shared memory server");
26 }
27
28 connected_ = true;
29}
30
37
38Napi::Value MsgpackClientWrapper::call(const Napi::CallbackInfo& info)
39{
40 Napi::Env env = info.Env();
41
42 if (!connected_) {
43 throw Napi::Error::New(env, "Client is not connected");
44 }
45
46 // Arg 0: msgpack buffer to send
47 if (info.Length() < 1 || !info[0].IsBuffer()) {
48 throw Napi::TypeError::New(env, "First argument must be a Buffer");
49 }
50
51 auto input_buffer = info[0].As<Napi::Buffer<uint8_t>>();
52 const uint8_t* input_data = input_buffer.Data();
53 size_t input_len = input_buffer.Length();
54
55 // Send request with retry on backpressure (1s timeout per attempt)
56 // NOTE: timeout_ns=0 means IMMEDIATE timeout (not infinite wait!)
57 // Loop until send succeeds - handles case where consumer is temporarily behind
58 constexpr uint64_t TIMEOUT_NS = 1000000000; // 1 second
59 while (!client_->send(input_data, input_len, TIMEOUT_NS)) {
60 // Ring buffer full, consumer is behind - retry
61 }
62
63 // Receive response with retry (1s timeout per attempt)
64 // Loop until response is ready - handles case where server is processing
66 while ((response = client_->receive(TIMEOUT_NS)).empty()) {
67 // Response not ready yet, server is processing - retry
68 }
69
70 // Create JavaScript Buffer with the response (copy to JS land)
71 auto js_buffer = Napi::Buffer<uint8_t>::Copy(env, response.data(), response.size());
72
73 // Release the message (for shared memory this frees space in ring buffer)
74 client_->release(response.size());
75
76 return js_buffer;
77}
78
79Napi::Value MsgpackClientWrapper::close(const Napi::CallbackInfo& info)
80{
81 Napi::Env env = info.Env();
82
83 if (client_ && connected_) {
84 client_->close();
85 connected_ = false;
86 }
87
88 return env.Undefined();
89}
90
91Napi::Function MsgpackClientWrapper::get_class(Napi::Env env)
92{
93 return DefineClass(env,
94 "MsgpackClient",
95 {
96 MsgpackClientWrapper::InstanceMethod("call", &MsgpackClientWrapper::call),
97 MsgpackClientWrapper::InstanceMethod("close", &MsgpackClientWrapper::close),
98 });
99}
static std::unique_ptr< IpcClient > create_shm(const std::string &base_name)
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.
void info(Args... args)
Definition log.hpp:75
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13