Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
socket_client.cpp
Go to the documentation of this file.
2#include <cerrno>
3#include <cstdint>
4#include <cstring>
5#include <string>
6#include <sys/socket.h>
7#include <sys/types.h>
8#include <sys/un.h>
9#include <unistd.h>
10#include <utility>
11
12namespace bb::ipc {
13
14SocketClient::SocketClient(std::string socket_path)
15 : socket_path_(std::move(socket_path))
16{}
17
22
24{
25 if (fd_ >= 0) {
26 return true; // Already connected
27 }
28
29 // Create socket
30 fd_ = socket(AF_UNIX, SOCK_STREAM, 0);
31 if (fd_ < 0) {
32 return false;
33 }
34
35 // Connect to server
36 struct sockaddr_un addr;
37 std::memset(&addr, 0, sizeof(addr));
38 addr.sun_family = AF_UNIX;
39 std::strncpy(addr.sun_path, socket_path_.c_str(), sizeof(addr.sun_path) - 1);
40
41 if (::connect(fd_, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
42 ::close(fd_);
43 fd_ = -1;
44 return false;
45 }
46
47 return true;
48}
49
50bool SocketClient::send(const void* data, size_t len, uint64_t /*timeout_ns*/)
51{
52 if (fd_ < 0) {
53 errno = EINVAL;
54 return false;
55 }
56
57 // Send length prefix (4 bytes, little-endian)
58 auto msg_len = static_cast<uint32_t>(len);
59 ssize_t n = ::send(fd_, &msg_len, sizeof(msg_len), 0);
60 if (n < 0 || static_cast<size_t>(n) != sizeof(msg_len)) {
61 return false;
62 }
63
64 // Send message data
65 n = ::send(fd_, data, len, 0);
66 if (n < 0) {
67 return false;
68 }
69 const auto bytes_sent = static_cast<size_t>(n);
70 return bytes_sent == len;
71}
72
74{
75 if (fd_ < 0) {
76 return {};
77 }
78
79 // Read length prefix (4 bytes)
80 uint32_t msg_len = 0;
81 ssize_t n = ::recv(fd_, &msg_len, sizeof(msg_len), MSG_WAITALL);
82 if (n < 0 || static_cast<size_t>(n) != sizeof(msg_len)) {
83 return {};
84 }
85
86 // Ensure buffer is large enough
87 if (recv_buffer_.size() < msg_len) {
88 recv_buffer_.resize(msg_len);
89 }
90
91 // Read message data into internal buffer
92 n = ::recv(fd_, recv_buffer_.data(), msg_len, MSG_WAITALL);
93 if (n < 0 || static_cast<size_t>(n) != msg_len) {
94 return {};
95 }
96
97 // Return span into internal buffer
98 return std::span<const uint8_t>(recv_buffer_.data(), msg_len);
99}
100
101void SocketClient::release(size_t /*message_size*/)
102{
103 // No-op for sockets - data is already consumed from kernel buffer during recv()
104}
105
107{
109}
110
112{
113 if (fd_ >= 0) {
114 ::close(fd_);
115 fd_ = -1;
116 }
117}
118
119} // namespace bb::ipc
void release(size_t message_size) override
Release the previously received message.
bool connect() override
Connect to the server.
bool send(const void *data, size_t len, uint64_t timeout_ns) override
Send a message to the server.
SocketClient(std::string socket_path)
std::span< const uint8_t > receive(uint64_t timeout_ns) override
Receive a message from the server (zero-copy for shared memory)
std::vector< uint8_t > recv_buffer_
void close() override
Close the connection.
const std::vector< MemoryValue > data
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
uint8_t len