12 Napi::Env env =
info.Env();
15 if (
info.Length() < 1 || !
info[0].IsString()) {
16 throw Napi::TypeError::New(env,
"First argument must be a string (shared memory name)");
18 std::string shm_name =
info[0].As<Napi::String>();
21 if (
info.Length() >= 2 &&
info[1].IsNumber()) {
22 client_id =
static_cast<size_t>(
info[1].As<Napi::Number>().Uint32Value());
29 throw Napi::Error::New(env,
"Failed to connect to shared memory server");
35 Napi::Env env =
info.Env();
38 if (
info.Length() < 1 || !
info[0].IsFunction()) {
39 throw Napi::TypeError::New(env,
"First argument must be a function");
53 return env.Undefined();
58 constexpr uint64_t TIMEOUT_NS = 1000000000;
64 if (response.empty()) {
70 auto* response_data =
new std::vector<uint8_t>(response.begin(), response.end());
73 client_->release(response.size());
81 auto status =
tsfn_.NonBlockingCall(
82 response_data, [](Napi::Env env, Napi::Function js_callback, std::vector<uint8_t>*
data) {
87 auto js_buffer = Napi::Buffer<uint8_t>::Copy(env,
data->data(),
data->size());
91 js_callback.Call({ js_buffer });
97 if (status != napi_ok) {
100 delete response_data;
108 Napi::Env env =
info.Env();
111 if (
info.Length() < 1 || !
info[0].IsBuffer()) {
112 throw Napi::TypeError::New(env,
"First argument must be a Buffer");
115 auto input_buffer =
info[0].As<Napi::Buffer<uint8_t>>();
116 const uint8_t* input_data = input_buffer.Data();
117 size_t input_len = input_buffer.Length();
121 if (!
client_->send(input_data, input_len, 0)) {
122 throw Napi::Error::New(env,
"Failed to send request, ring buffer full. Make it bigger?");
126 return env.Undefined();
131 Napi::Env env =
info.Env();
137 tsfn_ = Napi::ThreadSafeFunction::New(env,
139 "ShmResponseCallback",
146 return env.Undefined();
161 return info.Env().Undefined();
168 "MsgpackClientAsync",
static std::unique_ptr< IpcClient > create_mpsc_shm(const std::string &base_name, size_t client_id)
static Napi::Function get_class(Napi::Env env)
void poll_responses()
Background thread function that polls for responses.
Napi::Value call(const Napi::CallbackInfo &info)
Send a msgpack buffer asynchronously.
Napi::Value release(const Napi::CallbackInfo &info)
Release a reference to allow the event loop to exit Called by TypeScript when there are no pending ca...
Napi::Value setResponseCallback(const Napi::CallbackInfo &info)
Set the JavaScript callback to be invoked when responses arrive.
Napi::FunctionReference js_callback_
MsgpackClientAsync(const Napi::CallbackInfo &info)
std::unique_ptr< bb::ipc::IpcClient > client_
Napi::Value acquire(const Napi::CallbackInfo &info)
Acquire a reference to keep the event loop alive Called by TypeScript when there are pending callback...
Napi::ThreadSafeFunction tsfn_
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept