Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::ipc::IpcServer Class Referenceabstract

Abstract interface for IPC server. More...

#include <ipc_server.hpp>

Inheritance diagram for bb::ipc::IpcServer:
bb::ipc::ShmServer bb::ipc::SocketServer

Public Types

using Handler = std::function< std::vector< uint8_t >(int client_id, std::span< const uint8_t > request)>
 High-level request handler function type.
 

Public Member Functions

 IpcServer ()=default
 
virtual ~IpcServer ()=default
 
 IpcServer (const IpcServer &)=delete
 
IpcServeroperator= (const IpcServer &)=delete
 
 IpcServer (IpcServer &&)=delete
 
IpcServeroperator= (IpcServer &&)=delete
 
virtual bool listen ()=0
 Start listening for client connections.
 
virtual int wait_for_data (uint64_t timeout_ns)=0
 Wait for data from any connected client.
 
virtual std::span< const uint8_t > receive (int client_id)=0
 Receive next message from a specific client.
 
virtual void release (int client_id, size_t message_size)=0
 Release/consume the previously received message.
 
virtual bool send (int client_id, const void *data, size_t len)=0
 Send a message to a specific client.
 
virtual void close ()=0
 Close the server and all client connections.
 
virtual void request_shutdown ()
 Request graceful shutdown.
 
virtual int accept ()
 Accept a new client connection (optional for some transports)
 
virtual void run (const Handler &handler)
 Run server event loop with handler.
 

Static Public Member Functions

static std::unique_ptr< IpcServercreate_socket (const std::string &socket_path, int max_clients)
 
static std::unique_ptr< IpcServercreate_shm (const std::string &base_name, size_t request_ring_size=static_cast< size_t >(1024 *1024), size_t response_ring_size=static_cast< size_t >(1024 *1024))
 

Protected Member Functions

virtual void wakeup_all ()
 Wake all blocked threads (for graceful shutdown)
 

Protected Attributes

std::atomic< bool > shutdown_requested_ { false }
 

Detailed Description

Abstract interface for IPC server.

Provides a unified interface for accepting client connections and exchanging messages. Implementations handle transport-specific details (Unix domain sockets, shared memory, etc).

Definition at line 39 of file ipc_server.hpp.

Member Typedef Documentation

◆ Handler

using bb::ipc::IpcServer::Handler = std::function<std::vector<uint8_t>(int client_id, std::span<const uint8_t> request)>

High-level request handler function type.

Takes client_id and request data, returns response data. Return empty vector to skip sending a response.

Definition at line 123 of file ipc_server.hpp.

Constructor & Destructor Documentation

◆ IpcServer() [1/3]

bb::ipc::IpcServer::IpcServer ( )
default

◆ ~IpcServer()

virtual bb::ipc::IpcServer::~IpcServer ( )
virtualdefault

◆ IpcServer() [2/3]

bb::ipc::IpcServer::IpcServer ( const IpcServer )
delete

◆ IpcServer() [3/3]

bb::ipc::IpcServer::IpcServer ( IpcServer &&  )
delete

Member Function Documentation

◆ accept()

virtual int bb::ipc::IpcServer::accept ( )
inlinevirtual

Accept a new client connection (optional for some transports)

Parameters
timeout_nsTimeout in nanoseconds (0 = non-blocking, <0 = infinite)
Returns
Client ID if successful, -1 if no pending connection or error

Note: Some transports (like shared memory) may not need explicit accept calls.

Reimplemented in bb::ipc::SocketServer.

Definition at line 132 of file ipc_server.hpp.

◆ close()

virtual void bb::ipc::IpcServer::close ( )
pure virtual

Close the server and all client connections.

Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.

◆ create_shm()

std::unique_ptr< IpcServer > bb::ipc::IpcServer::create_shm ( const std::string &  base_name,
size_t  request_ring_size = static_cast<size_t>(1024 * 1024),
size_t  response_ring_size = static_cast<size_t>(1024 * 1024) 
)
static

Definition at line 15 of file ipc_server.cpp.

◆ create_socket()

std::unique_ptr< IpcServer > bb::ipc::IpcServer::create_socket ( const std::string &  socket_path,
int  max_clients 
)
static

Definition at line 10 of file ipc_server.cpp.

◆ listen()

virtual bool bb::ipc::IpcServer::listen ( )
pure virtual

Start listening for client connections.

Returns
true if successful, false otherwise

Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.

◆ operator=() [1/2]

IpcServer & bb::ipc::IpcServer::operator= ( const IpcServer )
delete

◆ operator=() [2/2]

IpcServer & bb::ipc::IpcServer::operator= ( IpcServer &&  )
delete

◆ receive()

virtual std::span< const uint8_t > bb::ipc::IpcServer::receive ( int  client_id)
pure virtual

Receive next message from a specific client.

Blocks until a complete message is available. Returns a span pointing to the message data. For shared memory, this is a zero-copy view directly into the ring buffer. For sockets, this is a view into an internal buffer.

The message remains valid until release() is called with the message size.

Parameters
client_idClient to receive from
Returns
Span of message data (empty only on error/disconnect)

Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.

◆ release()

virtual void bb::ipc::IpcServer::release ( int  client_id,
size_t  message_size 
)
pure virtual

Release/consume the previously received message.

Must be called after receive() to advance to the next message. For shared memory, this releases space in the ring buffer. For sockets, this is a no-op (message already consumed during receive).

Parameters
client_idClient whose message to release
message_sizeSize of the message being released (from span.size())

Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.

◆ request_shutdown()

virtual void bb::ipc::IpcServer::request_shutdown ( )
inlinevirtual

Request graceful shutdown.

Sets shutdown flag and wakes all blocked threads. Safe to call from signal handlers. After this returns, the run() loop will exit on its next iteration. Call close() afterward to clean up resources.

Definition at line 111 of file ipc_server.hpp.

◆ run()

virtual void bb::ipc::IpcServer::run ( const Handler handler)
inlinevirtual

Run server event loop with handler.

Continuously waits for client requests and invokes handler. Handler is responsible for deserializing request, processing, and serializing response. This is a convenience method that encapsulates the typical server loop.

Uses peek/release pattern:

  • peek() returns a span (zero-copy for SHM, internal buffer for sockets)
  • handler processes the request
  • release() explicitly consumes the message

This design ensures no messages are lost and enables zero-copy for shared memory.

Server exits gracefully when handler throws ShutdownRequested exception.

Parameters
handlerFunction to process requests and generate responses

Definition at line 152 of file ipc_server.hpp.

◆ send()

virtual bool bb::ipc::IpcServer::send ( int  client_id,
const void *  data,
size_t  len 
)
pure virtual

Send a message to a specific client.

Parameters
client_idClient to send to
dataPointer to message data
lenLength of message in bytes
Returns
true if sent successfully, false on error

Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.

◆ wait_for_data()

virtual int bb::ipc::IpcServer::wait_for_data ( uint64_t  timeout_ns)
pure virtual

Wait for data from any connected client.

Parameters
timeout_nsMaximum time to wait in nanoseconds (0 = non-blocking poll)
Returns
Client ID that has data available, or -1 on timeout/error

Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.

◆ wakeup_all()

virtual void bb::ipc::IpcServer::wakeup_all ( )
inlineprotectedvirtual

Wake all blocked threads (for graceful shutdown)

Wakes any threads blocked in wait_for_data() or other blocking operations. Used by signal handlers to trigger graceful shutdown without waiting for timeouts.

Reimplemented in bb::ipc::ShmServer.

Definition at line 207 of file ipc_server.hpp.

Member Data Documentation

◆ shutdown_requested_

std::atomic<bool> bb::ipc::IpcServer::shutdown_requested_ { false }
protected

Definition at line 199 of file ipc_server.hpp.


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