|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "ipc_server.hpp" |
| 4 | +#include "shm/mpsc_shm.hpp" |
| 5 | +#include "shm/spsc_shm.hpp" |
| 6 | +#include "shm_common.hpp" |
| 7 | +#include <cstdint> |
| 8 | +#include <iostream> |
| 9 | +#include <optional> |
| 10 | +#include <string> |
| 11 | +#include <utility> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +namespace bb::ipc { |
| 15 | + |
| 16 | +/** |
| 17 | + * @brief IPC server implementation using shared memory with multi-client support |
| 18 | + * |
| 19 | + * Uses MPSC (multi-producer single-consumer) for requests and per-client SPSC |
| 20 | + * rings for responses. Supports up to max_clients concurrent clients. |
| 21 | + * |
| 22 | + * Shared memory layout: |
| 23 | + * - Request: MPSC consumer with one SPSC ring per client (client writes, server reads) |
| 24 | + * - Response: Separate SPSC ring per client (server writes, client reads) |
| 25 | + */ |
| 26 | +class MpscShmServer : public IpcServer { |
| 27 | + public: |
| 28 | + static constexpr size_t DEFAULT_RING_SIZE = 1 << 20; // 1MB |
| 29 | + |
| 30 | + MpscShmServer(std::string base_name, |
| 31 | + size_t max_clients, |
| 32 | + size_t request_ring_size = DEFAULT_RING_SIZE, |
| 33 | + size_t response_ring_size = DEFAULT_RING_SIZE) |
| 34 | + : base_name_(std::move(base_name)) |
| 35 | + , max_clients_(max_clients) |
| 36 | + , request_ring_size_(request_ring_size) |
| 37 | + , response_ring_size_(response_ring_size) |
| 38 | + {} |
| 39 | + |
| 40 | + ~MpscShmServer() override { close(); } |
| 41 | + |
| 42 | + // Non-copyable, non-movable |
| 43 | + MpscShmServer(const MpscShmServer&) = delete; |
| 44 | + MpscShmServer& operator=(const MpscShmServer&) = delete; |
| 45 | + MpscShmServer(MpscShmServer&&) = delete; |
| 46 | + MpscShmServer& operator=(MpscShmServer&&) = delete; |
| 47 | + |
| 48 | + bool listen() override |
| 49 | + { |
| 50 | + if (request_consumer_.has_value()) { |
| 51 | + return true; // Already listening |
| 52 | + } |
| 53 | + |
| 54 | + // Clean up any leftover shared memory |
| 55 | + MpscConsumer::unlink(base_name_ + "_req", max_clients_); |
| 56 | + for (size_t i = 0; i < max_clients_; i++) { |
| 57 | + SpscShm::unlink(base_name_ + "_resp_" + std::to_string(i)); |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + // Create MPSC consumer for requests (one ring per client) |
| 62 | + request_consumer_ = MpscConsumer::create(base_name_ + "_req", max_clients_, request_ring_size_); |
| 63 | + |
| 64 | + // Create per-client SPSC response rings |
| 65 | + response_rings_.reserve(max_clients_); |
| 66 | + for (size_t i = 0; i < max_clients_; i++) { |
| 67 | + std::string resp_name = base_name_ + "_resp_" + std::to_string(i); |
| 68 | + response_rings_.push_back(SpscShm::create(resp_name, response_ring_size_)); |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | + } catch (...) { |
| 73 | + close(); |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + int wait_for_data(uint64_t timeout_ns) override |
| 79 | + { |
| 80 | + if (!request_consumer_.has_value()) { |
| 81 | + return -1; |
| 82 | + } |
| 83 | + // MpscConsumer::wait_for_data returns ring index = client_id |
| 84 | + return request_consumer_->wait_for_data(static_cast<uint32_t>(timeout_ns)); |
| 85 | + } |
| 86 | + |
| 87 | + std::span<const uint8_t> receive(int client_id) override |
| 88 | + { |
| 89 | + if (!request_consumer_.has_value() || client_id < 0 || static_cast<size_t>(client_id) >= max_clients_) { |
| 90 | + return {}; |
| 91 | + } |
| 92 | + // Peek on the specific client's request ring via MpscConsumer |
| 93 | + void* len_ptr = request_consumer_->peek(static_cast<size_t>(client_id), sizeof(uint32_t), 100000000); |
| 94 | + if (len_ptr == nullptr) { |
| 95 | + return {}; |
| 96 | + } |
| 97 | + uint32_t msg_len = 0; |
| 98 | + std::memcpy(&msg_len, len_ptr, sizeof(uint32_t)); |
| 99 | + |
| 100 | + void* msg_ptr = request_consumer_->peek(static_cast<size_t>(client_id), sizeof(uint32_t) + msg_len, 100000000); |
| 101 | + if (msg_ptr == nullptr) { |
| 102 | + return {}; |
| 103 | + } |
| 104 | + return std::span<const uint8_t>(static_cast<const uint8_t*>(msg_ptr) + sizeof(uint32_t), msg_len); |
| 105 | + } |
| 106 | + |
| 107 | + void release(int client_id, size_t message_size) override |
| 108 | + { |
| 109 | + if (!request_consumer_.has_value() || client_id < 0 || static_cast<size_t>(client_id) >= max_clients_) { |
| 110 | + return; |
| 111 | + } |
| 112 | + request_consumer_->release(static_cast<size_t>(client_id), sizeof(uint32_t) + message_size); |
| 113 | + } |
| 114 | + |
| 115 | + bool send(int client_id, const void* data, size_t len) override |
| 116 | + { |
| 117 | + if (client_id < 0 || static_cast<size_t>(client_id) >= response_rings_.size()) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + return ring_send_msg(response_rings_[static_cast<size_t>(client_id)], data, len, 100000000); |
| 121 | + } |
| 122 | + |
| 123 | + void close() override |
| 124 | + { |
| 125 | + request_consumer_.reset(); |
| 126 | + response_rings_.clear(); |
| 127 | + |
| 128 | + // Clean up shared memory |
| 129 | + MpscConsumer::unlink(base_name_ + "_req", max_clients_); |
| 130 | + for (size_t i = 0; i < max_clients_; i++) { |
| 131 | + SpscShm::unlink(base_name_ + "_resp_" + std::to_string(i)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + void wakeup_all() override |
| 136 | + { |
| 137 | + if (request_consumer_.has_value()) { |
| 138 | + request_consumer_->wakeup_all(); |
| 139 | + } |
| 140 | + for (auto& ring : response_rings_) { |
| 141 | + ring.wakeup_all(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private: |
| 146 | + std::string base_name_; |
| 147 | + size_t max_clients_; |
| 148 | + size_t request_ring_size_; |
| 149 | + size_t response_ring_size_; |
| 150 | + std::optional<MpscConsumer> request_consumer_; |
| 151 | + std::vector<SpscShm> response_rings_; |
| 152 | +}; |
| 153 | + |
| 154 | +} // namespace bb::ipc |
0 commit comments