One listener, many slots — and why a resolved endpoint is not an identity (transport plane)¶
tcp_transport_t’s LISTEN constructor accepts one peer at a time — the board↔board shape. A
node that fans out to browser tabs or to a fleet needs the other one: tcp_server_transport_t
(and its RFC 6455 sibling ws_server_transport_t, sharing the same slot/poll machinery since
#871) runs one poll thread over a slot table, so steady-state memory is bounded by the
concurrent-peer high-water mark or by max_peers, whichever is smaller.
With peer_named, that slot table is exposed through the same bus_link_t facet CAN uses — and
that is where the two naming regimes part company.
What to notice¶
CAN names by IDENTITY, a slot server names by SLOT AND TENANCY.
n<node-id>is derived from the peer’s own bus id, so the name, the table key and the endpoint are one thing no other peer can inherit.p<slot>~<gen>carries the slot’s tenancy generation, so a reused slot gets a fresh name and a stale name resolves nothing. The ENDPOINT is still the slot’s: after that peer departs, a pointer resolved for it addresses whatever session inherits the slot, and the endpoint’s own liveness check is satisfied by that stranger. The pointer never dangles; it silently changes who it means (#1153).So: resolve per use.
child_registry_tresolves and sends in one expression, which is why no shipping caller is exposed, and a remote subscriber edge stores the peer NAME rather than the pointer.max_peersis an injected bound, not a backlog (RFC-0006). A connection past the cap is accepted and immediately closed — a clean refusal rather than a hung SYN. It is also resolved once at construction (a request of0takeskDefaultMaxPeers), so the example reads back what the server enforces instead of assuming it got what it asked for.One object, two addressing surfaces.
server.send(frame)fans out to every open peer;facet.peer_link("p1~1")->send(frame)reaches exactly one. The example checks the directed send arrived at p1 and that p0 and p2 got nothing.A name outside the live slot set resolves to
nullptr. Endpoints are not synthesized on demand, so a stale name cannot be sent to.This target needs the TCP transport (
LIBTRACER_TRANSPORT_TCP, the default).
The one run-time skip in this group, and why it is not silent¶
The subject here is the ADR-0044 peer-named tier, which a target carries only when it opts in
with kBusLinks = true (the default is false since v0.17.0, #1670; the core test build opts
in) — a C++ binding in config_override.hpp
(ADR-0068),
invisible to CMake, so no build guard can express it. Neither move the earlier domains used is
available: there is no second arm to name (with the module closed out there is no peer-named
tier at all), and a return 0 would make ctest record a pass for an example that ran
nothing — the exact defect the index warns about.
So the example states the skip and exits 77, and its add_test carries
SKIP_RETURN_CODE 77, which makes ctest report it as Skipped. That is strictly better than
the two older run-time skips in this tree, which exit 0 and are therefore indistinguishable
from a real pass. Verified: in a kBusLinks = false build ctest -R example_ reports
example_net_multi_peer_listener (Skipped) and passes the rest.
Source¶
1/*
2 * SPDX-License-Identifier: Apache-2.0
3 * SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
4 */
5
6/**
7 * @file
8 * @brief ONE CONCEPT — a LISTEN link is not one link per peer: `tcp_server_transport_t` holds
9 * many peers in recycled SLOTS behind one `transport_t`, names each session
10 * `p<slot>~<gen>`, and hands out a SLOT-scoped endpoint — which is precisely why a
11 * resolved peer endpoint must be re-resolved per use instead of cached.
12 *
13 * `tcp_transport_t`'s LISTEN constructor accepts one peer at a time — the board↔board
14 * shape. A node that fans out to browser tabs or to a fleet needs the other one:
15 * `tcp_server_transport_t` (and its RFC 6455 sibling `ws_server_transport_t`) share one
16 * poll thread over a slot table, so steady-state memory is bounded by the concurrent-peer
17 * high-water mark or by `max_peers`, whichever is smaller — an injected bound (RFC-0006),
18 * never a synthetic backlog. A connection past the cap is accepted and immediately closed,
19 * which is a clean refusal rather than a hung SYN.
20 *
21 * With `peer_named`, that slot table is exposed through the same @ref tr::net::bus_link_t
22 * facet CAN uses, and this is where the two naming regimes part company:
23 *
24 * - CAN names a peer `n<node-id>` — an IDENTITY. The name, the table key and the endpoint
25 * are one thing that no other peer can inherit.
26 * - A slot server names a peer `p<slot>~<gen>` — a slot and its tenancy, so a reused slot
27 * gets a fresh name. The ENDPOINT, though, is scoped to the SLOT,
28 * so after that peer departs a pointer resolved for it addresses whatever session
29 * inherits the slot, and the endpoint's own liveness check is satisfied by that stranger.
30 * The pointer never dangles; it silently changes who it means (#1153). Resolve, send,
31 * discard — `child_registry_t` does exactly that in one expression, which is why no
32 * shipping caller is exposed.
33 *
34 * The subject of this example is the ADR-0044 peer-named tier, so a target that closed the
35 * bus module out (`kBusLinks = false`) does not contain it. That is stated and SKIPPED with
36 * exit code 77 — which ctest reads as `Skipped`, not as a pass — rather than returning 0
37 * from a `main` that demonstrated nothing.
38 *
39 * Needs the TCP transport (`LIBTRACER_TRANSPORT_TCP`, on by default). Runs under ctest as
40 * `example_net_multi_peer_listener`; returns non-zero on any failed check.
41 */
42
43#include <arpa/inet.h>
44#include <netinet/in.h>
45#include <poll.h>
46#include <sys/socket.h>
47#include <unistd.h>
48
49#include <algorithm>
50#include <chrono>
51#include <cstddef>
52#include <cstdint>
53#include <cstdio>
54#include <memory>
55#include <span>
56#include <string>
57#include <string_view>
58#include <thread>
59#include <vector>
60
61#include "libtracer/config.hpp"
62#include "libtracer/transport_tcp.hpp"
63
64namespace {
65
66using namespace std::chrono_literals;
67
68/**
69 * @brief The exit code ctest reads as SKIPPED, via the target's `SKIP_RETURN_CODE` property.
70 *
71 * 77 is the autotools convention ctest documents. The same value `tr::testing::kSkipExitCode`
72 * spells on the test side; the `add_test` site in `core/examples/CMakeLists.txt` and this
73 * constant have to agree, so changing one means changing both.
74 */
75constexpr int kSkipExitCode = 77;
76
77/** @brief Report expectation @p what and record a failure on @p ok. */
78void check(bool& ok, bool cond, const char* what) {
79 std::printf(" [%s] %s\n", cond ? "ok" : "FAIL", what);
80 ok = ok && cond;
81}
82
83/** @brief A raw POSIX TCP client — one peer of the listener, with its own eye on the wire. */
84class raw_peer_t {
85 public:
86 /** @brief Connect to `127.0.0.1:@p port`; @ref ok reports whether it succeeded. */
87 explicit raw_peer_t(std::uint16_t port) {
88 fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
89 sockaddr_in peer{};
90 peer.sin_family = AF_INET;
91 peer.sin_port = htons(port);
92 ::inet_pton(AF_INET, "127.0.0.1", &peer.sin_addr);
93 if (::connect(fd_, reinterpret_cast<sockaddr*>(&peer), sizeof(peer)) < 0) {
94 ::close(fd_);
95 fd_ = -1;
96 }
97 }
98 ~raw_peer_t() {
99 if (fd_ >= 0) ::close(fd_);
100 }
101
102 raw_peer_t(const raw_peer_t&) = delete;
103 raw_peer_t& operator=(const raw_peer_t&) = delete;
104
105 /** @brief True iff the connect succeeded. */
106 [[nodiscard]] bool ok() const noexcept { return fd_ >= 0; }
107
108 /** @brief Read up to @p want bytes within @p budget, answering whatever arrived. */
109 [[nodiscard]] std::vector<std::byte> read_within(std::size_t want,
110 std::chrono::milliseconds budget) {
111 std::vector<std::byte> got;
112 const auto deadline = std::chrono::steady_clock::now() + budget;
113 while (got.size() < want) {
114 const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(
115 deadline - std::chrono::steady_clock::now());
116 if (left.count() <= 0) break;
117 pollfd p{fd_, POLLIN, 0};
118 if (::poll(&p, 1, static_cast<int>(left.count())) <= 0) break;
119 std::byte buf[256];
120 const ssize_t n = ::recv(fd_, buf, sizeof(buf), 0);
121 if (n <= 0) break;
122 got.insert(got.end(), buf, buf + n);
123 }
124 return got;
125 }
126
127 private:
128 int fd_ = -1;
129};
130
131/** @brief @p n bytes counting up from @p seed — a stand-in for an encoded frame. */
132std::vector<std::byte> frame_of(std::size_t n, unsigned seed) {
133 std::vector<std::byte> f(n);
134 for (std::size_t i = 0; i < n; ++i) f[i] = static_cast<std::byte>(seed + i);
135 return f;
136}
137
138/** @brief `u32-LE length ++ @p payload` — what one record looks like on this kind's wire. */
139std::vector<std::byte> record(std::span<const std::byte> payload) {
140 const auto len = static_cast<std::uint32_t>(payload.size());
141 std::vector<std::byte> out;
142 for (unsigned shift = 0; shift < 32; shift += 8)
143 out.push_back(static_cast<std::byte>((len >> shift) & 0xFFu));
144 out.insert(out.end(), payload.begin(), payload.end());
145 return out;
146}
147
148/** @brief The peer names @p facet currently lists, sorted so the order is this example's. */
149std::vector<std::string> peers_of(tr::net::bus_link_t& facet) {
150 std::vector<std::string> names;
151 facet.enumerate_peers([&](std::string_view n) { names.emplace_back(n); });
152 std::sort(names.begin(), names.end());
153 return names;
154}
155
156/**
157 * @brief Poll until @p facet lists @p n peers, or @p budget expires.
158 *
159 * A bounded poll, not a wait: acceptance happens on the server's own poll thread and the
160 * seam publishes no completion edge to wait on. The loop is the honest shape for observing
161 * a state that becomes true asynchronously with no notification.
162 */
163bool wait_for_peers(tr::net::bus_link_t& facet, std::size_t n, std::chrono::milliseconds budget) {
164 const auto deadline = std::chrono::steady_clock::now() + budget;
165 while (std::chrono::steady_clock::now() < deadline) {
166 if (peers_of(facet).size() >= n) return true;
167 std::this_thread::sleep_for(2ms);
168 }
169 return peers_of(facet).size() >= n;
170}
171
172} // namespace
173
174int main() {
175 // The subject is the peer-named tier itself. On a target that compiled it out there is
176 // nothing here to demonstrate, and a `return 0` would make ctest record a pass for an
177 // example that ran nothing — the failure mode this whole batch exists to avoid.
178 if constexpr (!tr::net::kBusLinks) {
179 std::printf(
180 "net_multi_peer_listener: SKIPPED — this build closed the ADR-0044 bus module "
181 "out (kBusLinks = false), so a listener has no peer-named tier to show.\n");
182 return kSkipExitCode;
183 }
184
185 bool ok = true;
186 std::printf("kBusLinks = true — the peer-named tier is present in this build\n");
187
188 // max_peers is the injected admission bound. It is resolved once at construction (a
189 // request of 0 takes kDefaultMaxPeers), so the value the server ENFORCES
190 // is read back rather than assumed to be what was asked for.
191 constexpr std::size_t kRequestedPeers = 4;
192 tr::net::tcp_server_transport_t server(std::uint16_t{0},
193 {.max_peers = kRequestedPeers, .peer_named = true});
194 check(ok, server.ok(), "the multi-peer listener bound an ephemeral port");
195 check(ok, server.max_peers() == kRequestedPeers, "the admission cap is the one requested");
196 check(ok, server.bus() != nullptr, "peer_named=true exposes the bus facet");
197
198 tr::net::bus_link_t& facet = *server.bus();
199 check(ok, peers_of(facet).empty(), "no peers before anyone connects");
200
201 // Three peers on ONE listener, one poll thread, one transport_t.
202 std::printf("three peers on one listener:\n");
203 std::vector<std::unique_ptr<raw_peer_t>> clients;
204 for (int i = 0; i < 3; ++i)
205 clients.push_back(std::make_unique<raw_peer_t>(server.local_port()));
206 check(ok, clients[0]->ok() && clients[1]->ok() && clients[2]->ok(), "all three connected");
207 check(ok, wait_for_peers(facet, 3, 2s), "the listener accepted all three into slots");
208
209 const auto names = peers_of(facet);
210 check(ok, names.size() == 3, "…and lists exactly three peers");
211 check(ok, names == std::vector<std::string>{"p0~1", "p1~1", "p2~1"},
212 "…named p0/p1/p2 — by SLOT, which is a position and not an identity");
213
214 // A DIRECTED send: one named peer's socket, and nobody else's.
215 std::printf("a directed send:\n");
216 const auto only_for_p1 = frame_of(6, 0x10);
217 tr::net::transport_t* to_p1 = facet.peer_link("p1~1");
218 check(ok, to_p1 != nullptr, "peer_link('p1') resolved a directed endpoint");
219 if (to_p1 != nullptr) to_p1->send(only_for_p1);
220 const auto want = record(only_for_p1);
221 check(ok, clients[1]->read_within(want.size(), 2s) == want,
222 "the addressed peer got the record, prefix and all");
223 check(ok, clients[0]->read_within(1, 200ms).empty(), "…and peer p0 got nothing");
224 check(ok, clients[2]->read_within(1, 200ms).empty(), "…and peer p2 got nothing");
225
226 // The FLAT surface of the same object: `send` on the server itself fans out to every open
227 // peer. One link, two addressing surfaces — the flat one for "this link", the facet for
228 // "that peer on this link".
229 std::printf("the flat broadcast on the same object:\n");
230 const auto for_everyone = frame_of(4, 0x20);
231 server.send(for_everyone);
232 const auto expect = record(for_everyone);
233 check(ok, clients[0]->read_within(expect.size(), 2s) == expect, "p0 got the broadcast");
234 check(ok, clients[1]->read_within(expect.size(), 2s) == expect, "p1 got it too");
235 check(ok, clients[2]->read_within(expect.size(), 2s) == expect, "and so did p2");
236
237 // A name outside the live slot set resolves to nothing — the endpoint is not synthesized
238 // on demand, so a stale name cannot be sent to.
239 check(ok, facet.peer_link("p9") == nullptr, "a name no slot holds resolves to no endpoint");
240
241 check(ok, server.drop_stats().dropped_tx == 0, "nothing was shed on the way out");
242
243 std::printf("one listener, %zu peers in slots, cap %zu, 1 directed + 1 broadcast\n",
244 names.size(), server.max_peers());
245 return ok ? 0 : 1;
246}
See also: transport module · concurrency and scaling · the identity-named bus · the single-peer pair.