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>();
25 throw Napi::Error::New(env,
"Failed to connect to shared memory server");
31 Napi::Env env =
info.Env();
34 if (
info.Length() < 1 || !
info[0].IsFunction()) {
35 throw Napi::TypeError::New(env,
"First argument must be a function");
49 return env.Undefined();
54 constexpr uint64_t TIMEOUT_NS = 1000000000;
60 if (response.empty()) {
66 auto* response_data =
new std::vector<uint8_t>(response.begin(), response.end());
69 client_->release(response.size());
77 auto status =
tsfn_.NonBlockingCall(
78 response_data, [](Napi::Env env, Napi::Function js_callback, std::vector<uint8_t>*
data) {
83 auto js_buffer = Napi::Buffer<uint8_t>::Copy(env,
data->data(),
data->size());
87 js_callback.Call({ js_buffer });
93 if (status != napi_ok) {
104 Napi::Env env =
info.Env();
107 if (
info.Length() < 1 || !
info[0].IsBuffer()) {
108 throw Napi::TypeError::New(env,
"First argument must be a Buffer");
111 auto input_buffer =
info[0].As<Napi::Buffer<uint8_t>>();
112 const uint8_t* input_data = input_buffer.Data();
113 size_t input_len = input_buffer.Length();
117 if (!
client_->send(input_data, input_len, 0)) {
118 throw Napi::Error::New(env,
"Failed to send request, ring buffer full. Make it bigger?");
122 return env.Undefined();
127 Napi::Env env =
info.Env();
133 tsfn_ = Napi::ThreadSafeFunction::New(env,
135 "ShmResponseCallback",
142 return env.Undefined();
157 return info.Env().Undefined();
164 "MsgpackClientAsync",
static std::unique_ptr< IpcClient > create_shm(const std::string &base_name)
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_
const std::vector< MemoryValue > data
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept