|
Barretenberg
The ZK-SNARK library at the core of Aztec
|
Abstract interface for IPC server. More...
#include <ipc_server.hpp>
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 | |
| IpcServer & | operator= (const IpcServer &)=delete |
| IpcServer (IpcServer &&)=delete | |
| IpcServer & | operator= (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< IpcServer > | create_socket (const std::string &socket_path, int max_clients) |
| static std::unique_ptr< 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)) |
Protected Member Functions | |
| virtual void | wakeup_all () |
| Wake all blocked threads (for graceful shutdown) | |
Protected Attributes | |
| std::atomic< bool > | shutdown_requested_ { false } |
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.
| 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.
|
default |
|
virtualdefault |
|
delete |
|
delete |
|
inlinevirtual |
Accept a new client connection (optional for some transports)
| timeout_ns | Timeout in nanoseconds (0 = non-blocking, <0 = infinite) |
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.
|
pure virtual |
Close the server and all client connections.
Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.
|
static |
Definition at line 15 of file ipc_server.cpp.
|
static |
Definition at line 10 of file ipc_server.cpp.
|
pure virtual |
Start listening for client connections.
Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.
|
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.
| client_id | Client to receive from |
Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.
|
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).
| client_id | Client whose message to release |
| message_size | Size of the message being released (from span.size()) |
Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.
|
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.
|
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:
This design ensures no messages are lost and enables zero-copy for shared memory.
Server exits gracefully when handler throws ShutdownRequested exception.
| handler | Function to process requests and generate responses |
Definition at line 152 of file ipc_server.hpp.
|
pure virtual |
Send a message to a specific client.
| client_id | Client to send to |
| data | Pointer to message data |
| len | Length of message in bytes |
Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.
|
pure virtual |
Wait for data from any connected client.
| timeout_ns | Maximum time to wait in nanoseconds (0 = non-blocking poll) |
Implemented in bb::ipc::ShmServer, and bb::ipc::SocketServer.
|
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.
|
protected |
Definition at line 199 of file ipc_server.hpp.