Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::nodejs::msgpack_client::MsgpackClientAsync Class Reference

Asynchronous NAPI wrapper for msgpack calls via shared memory IPC. More...

#include <msgpack_client_async.hpp>

Inheritance diagram for bb::nodejs::msgpack_client::MsgpackClientAsync:

Public Member Functions

 MsgpackClientAsync (const Napi::CallbackInfo &info)
 
Napi::Value setResponseCallback (const Napi::CallbackInfo &info)
 Set the JavaScript callback to be invoked when responses arrive.
 
Napi::Value call (const Napi::CallbackInfo &info)
 Send a msgpack buffer asynchronously.
 
Napi::Value acquire (const Napi::CallbackInfo &info)
 Acquire a reference to keep the event loop alive Called by TypeScript when there are pending callbacks.
 
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 callbacks.
 

Static Public Member Functions

static Napi::Function get_class (Napi::Env env)
 

Private Member Functions

void poll_responses ()
 Background thread function that polls for responses.
 

Private Attributes

std::unique_ptr< bb::ipc::IpcClientclient_
 
std::thread poll_thread_
 
std::mutex tsfn_mutex_
 
Napi::FunctionReference js_callback_
 
Napi::ThreadSafeFunction tsfn_
 
int ref_count_ = 0
 

Detailed Description

Asynchronous NAPI wrapper for msgpack calls via shared memory IPC.

Provides an asynchronous interface with request pipelining for sending msgpack buffers to the bb binary via shared memory. Multiple requests can be in flight simultaneously, with responses matched to requests in FIFO order by TypeScript.

Architecture (matches socket backend pattern):

  • TypeScript: Creates promises, manages queue, handles request/response matching
  • C++ Main thread: Sends requests to shared memory ring buffer
  • C++ Background thread: Polls response ring buffer, invokes JS callback via ThreadSafeFunction
  • ThreadSafeFunction: Safely bridges C++ background thread to JavaScript main thread

This design eliminates the need for C++ mutex/queue by leveraging JavaScript's single-threaded nature for queue management.

Definition at line 27 of file msgpack_client_async.hpp.

Constructor & Destructor Documentation

◆ MsgpackClientAsync()

MsgpackClientAsync::MsgpackClientAsync ( const Napi::CallbackInfo &  info)

Definition at line 9 of file msgpack_client_async.cpp.

Member Function Documentation

◆ acquire()

Napi::Value MsgpackClientAsync::acquire ( const Napi::CallbackInfo &  info)

Acquire a reference to keep the event loop alive Called by TypeScript when there are pending callbacks.

Definition at line 125 of file msgpack_client_async.cpp.

◆ call()

Napi::Value MsgpackClientAsync::call ( const Napi::CallbackInfo &  info)

Send a msgpack buffer asynchronously.

Parameters
info[0]- Buffer containing msgpack data
Returns
undefined (promise management handled in TypeScript)

Writes request to shared memory. TypeScript should create and manage promises.

Definition at line 102 of file msgpack_client_async.cpp.

◆ get_class()

Napi::Function MsgpackClientAsync::get_class ( Napi::Env  env)
static

Definition at line 160 of file msgpack_client_async.cpp.

◆ poll_responses()

void MsgpackClientAsync::poll_responses ( )
private

Background thread function that polls for responses.

Continuously polls the response ring buffer using recv() with timeout. When a response arrives, invokes the registered JavaScript callback via ThreadSafeFunction. Runs until process exits (thread is detached, no explicit shutdown needed).

Definition at line 52 of file msgpack_client_async.cpp.

◆ release()

Napi::Value MsgpackClientAsync::release ( const Napi::CallbackInfo &  info)

Release a reference to allow the event loop to exit Called by TypeScript when there are no pending callbacks.

Definition at line 145 of file msgpack_client_async.cpp.

◆ setResponseCallback()

Napi::Value MsgpackClientAsync::setResponseCallback ( const Napi::CallbackInfo &  info)

Set the JavaScript callback to be invoked when responses arrive.

Parameters
info[0]- JavaScript function to call with response buffer

The callback will be invoked from the background thread via ThreadSafeFunction. TypeScript code should use this to resolve promises from its queue.

Definition at line 29 of file msgpack_client_async.cpp.

Member Data Documentation

◆ client_

std::unique_ptr<bb::ipc::IpcClient> bb::nodejs::msgpack_client::MsgpackClientAsync::client_
private

Definition at line 74 of file msgpack_client_async.hpp.

◆ js_callback_

Napi::FunctionReference bb::nodejs::msgpack_client::MsgpackClientAsync::js_callback_
private

Definition at line 83 of file msgpack_client_async.hpp.

◆ poll_thread_

std::thread bb::nodejs::msgpack_client::MsgpackClientAsync::poll_thread_
private

Definition at line 77 of file msgpack_client_async.hpp.

◆ ref_count_

int bb::nodejs::msgpack_client::MsgpackClientAsync::ref_count_ = 0
private

Definition at line 91 of file msgpack_client_async.hpp.

◆ tsfn_

Napi::ThreadSafeFunction bb::nodejs::msgpack_client::MsgpackClientAsync::tsfn_
private

Definition at line 87 of file msgpack_client_async.hpp.

◆ tsfn_mutex_

std::mutex bb::nodejs::msgpack_client::MsgpackClientAsync::tsfn_mutex_
private

Definition at line 80 of file msgpack_client_async.hpp.


The documentation for this class was generated from the following files: