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 size_t client_id =
info[1].As<Napi::Number>().Uint32Value();
30 throw Napi::Error::New(env,
"Failed to connect to shared memory server");
36 Napi::Env env =
info.Env();
39 if (
info.Length() < 1 || !
info[0].IsFunction()) {
40 throw Napi::TypeError::New(env,
"First argument must be a function");
54 return env.Undefined();
59 constexpr uint64_t TIMEOUT_NS = 1000000000;
65 if (response.empty()) {
71 auto* response_data =
new std::vector<uint8_t>(response.begin(), response.end());
74 client_->release(response.size());
82 auto status =
tsfn_.NonBlockingCall(
83 response_data, [](Napi::Env env, Napi::Function js_callback, std::vector<uint8_t>*
data) {
88 auto js_buffer = Napi::Buffer<uint8_t>::Copy(env,
data->data(),
data->size());
92 js_callback.Call({ js_buffer });
98 if (status != napi_ok) {
101 delete response_data;
109 Napi::Env env =
info.Env();
112 if (
info.Length() < 1 || !
info[0].IsBuffer()) {
113 throw Napi::TypeError::New(env,
"First argument must be a Buffer");
116 auto input_buffer =
info[0].As<Napi::Buffer<uint8_t>>();
117 const uint8_t* input_data = input_buffer.Data();
118 size_t input_len = input_buffer.Length();
122 if (!
client_->send(input_data, input_len, 0)) {
123 throw Napi::Error::New(env,
"Failed to send request, ring buffer full. Make it bigger?");
127 return env.Undefined();
132 Napi::Env env =
info.Env();
138 tsfn_ = Napi::ThreadSafeFunction::New(env,
140 "ShmResponseCallback",
147 return env.Undefined();
162 return info.Env().Undefined();
169 "MsgpackClientAsync",
static std::unique_ptr< IpcClient > create_mpsc_shm(const std::string &base_name, size_t client_id)
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