Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
shm_client.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ipc_client.hpp"
4#include "shm/spsc_shm.hpp"
5#include "shm_common.hpp"
6#include <cassert>
7#include <cstdint>
8#include <cstring>
9#include <fcntl.h>
10#include <optional>
11#include <string>
12#include <sys/mman.h>
13#include <sys/types.h>
14#include <unistd.h>
15#include <utility>
16
17namespace bb::ipc {
18
25class ShmClient : public IpcClient {
26 public:
27 explicit ShmClient(std::string base_name)
28 : base_name_(std::move(base_name))
29 {}
30
31 ~ShmClient() override = default;
32
33 // Non-copyable, non-movable (owns shared memory resources)
34 ShmClient(const ShmClient&) = delete;
35 ShmClient& operator=(const ShmClient&) = delete;
36 ShmClient(ShmClient&&) = delete;
38
39 bool connect() override
40 {
41 if (request_ring_.has_value()) {
42 return true; // Already connected
43 }
44
45 try {
46 // Connect to request ring (client writes, server reads)
47 std::string req_name = base_name_ + "_request";
49
50 // Connect to response ring (server writes, client reads)
51 std::string resp_name = base_name_ + "_response";
53
54 return true;
55 } catch (...) {
56 request_ring_.reset();
57 response_ring_.reset();
58 return false;
59 }
60 }
61
62 bool send(const void* data, size_t len, uint64_t timeout_ns) override
63 {
64 if (!request_ring_.has_value()) {
65 return false;
66 }
67 return ring_send_msg(request_ring_.value(), data, len, timeout_ns);
68 }
69
70 std::span<const uint8_t> receive(uint64_t timeout_ns) override
71 {
72 if (!response_ring_.has_value()) {
73 return {};
74 }
75 return ring_receive_msg(response_ring_.value(), timeout_ns);
76 }
77
78 void release(size_t message_size) override
79 {
80 if (!response_ring_.has_value()) {
81 return;
82 }
83 response_ring_->release(sizeof(uint32_t) + message_size);
84 }
85
86 void close() override
87 {
88 request_ring_.reset();
89 response_ring_.reset();
90 }
91
92 void debug_dump() const
93 {
94 if (request_ring_.has_value()) {
95 request_ring_->debug_dump("Client REQ");
96 }
97 if (response_ring_.has_value()) {
98 response_ring_->debug_dump("Client RESP");
99 }
100 }
101
102 private:
103 std::string base_name_;
104 std::optional<SpscShm> request_ring_; // Client writes to this
105 std::optional<SpscShm> response_ring_; // Client reads from this
106};
107
108} // namespace bb::ipc
Abstract interface for IPC client.
IPC client implementation using shared memory.
bool connect() override
Connect to the server.
ShmClient & operator=(const ShmClient &)=delete
std::optional< SpscShm > response_ring_
void release(size_t message_size) override
Release the previously received message.
void debug_dump() const
std::string base_name_
std::optional< SpscShm > request_ring_
~ShmClient() override=default
ShmClient & operator=(ShmClient &&)=delete
std::span< const uint8_t > receive(uint64_t timeout_ns) override
Receive a message from the server (zero-copy for shared memory)
ShmClient(ShmClient &&)=delete
bool send(const void *data, size_t len, uint64_t timeout_ns) override
Send a message to the server.
ShmClient(const ShmClient &)=delete
void close() override
Close the connection.
ShmClient(std::string base_name)
static SpscShm connect(const std::string &name)
Connect to existing SPSC ring buffer.
Definition spsc_shm.cpp:157
const std::vector< MemoryValue > data
bool ring_send_msg(SpscShm &ring, const void *data, size_t len, uint64_t timeout_ns)
std::span< const uint8_t > ring_receive_msg(SpscShm &ring, uint64_t timeout_ns)
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint8_t len
Single-producer/single-consumer shared-memory ring buffer (Linux, x86-64 optimized)