One seam, every wire technology (transport plane)

tr::net::transport_t is the contract a transport kind has to satisfy. One call is required today, the one-span send(span). The scatter-gather send(iov) and the retained send are overridable and have default bodies, and frames come back through set_receiver / set_rope_receiver. The remaining members (drop_stats, start_receiving and the like) have defaults too.

There is no send_read, no send_reply, and no TLV type anywhere in it. That absence is the design: the router owns addressing (the dst source route), the codec owns structure, and a transport owns exactly one question — how do these bytes cross this wire. It is why udp, tcp, ws, can, quic, webtransport and the in-process loopback are interchangeable to everything above them, and why writing a new kind is writing this one class.

The example writes a complete kind in about twenty lines, then drives the shipped loopback_channel_t with the identical calls.

What to notice

  • A transport never sees TLV semantics. It is handed std::span<const std::byte> and hands back the same. Callback-plus-recv-thread is an implementation choice about this C++ seam, not a protocol property (ADR-0013) — two conforming nodes need not share it, only the wire.

  • The span send is the pure virtual; the iovec overload is the one worth overriding. The router sends forwards through send(iov), while some short answers, such as a refusal or a bare ERROR, go through the span send. The base send(iov) gathers the spans into a temporary and calls the span send, dropping and counting a gather it cannot complete. A kind with writev overrides it to avoid that copy, and one without can join the spans into memory it already owns, as the example’s kind does; the bytes on the wire are one record either way. ADR-0089 makes send(iov) the required call and moves the span send into the base.

  • Declaring one send hides the others. Ordinary C++ name hiding, and a kind that omits using transport_t::send; silently loses the retained send it meant to inherit. The example carries the line and says why.

  • The sink goes in before frames flow, and that is not advice. A kind whose receive thread starts in its own constructor is draining the wire while the owner is still wiring; a frame landing in an empty slot is dropped with no counter moving. The built-in dialling kinds start receiving only at start_receiving(), which closes that window (#1025 / #1045 / #2247).

  • Owning delivery is a capability, not an assumption. delivers_ropes() is false by default and the example’s own kind leaves it there. There is deliberately no adapter that wraps a borrowed span in a rope whose refcounts would lie (ADR-0042 §1).

  • Nothing here is conditional. transport_t and the loopback channel are the required core, so this target builds and runs under every CI leg — including the minimal module set with the net plane and all four transports off.

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 — every wire technology reaches the routing plane through the SAME
  9 *        three-call seam, and the seam is deliberately ignorant: a `transport_t` moves
 10 *        framed BYTES and is never told what they mean.
 11 *
 12 * `tr::net::transport_t` is the whole contract a kind has to satisfy (ADR-0013):
 13 *
 14 *  - `send(std::span<const std::span<const std::byte>>)` — emit one frame from scattered
 15 *    spans, as one record. The router sends through it, so a kind implements it: with a
 16 *    real writev, or by joining the spans into memory the kind already owns (ADR-0089).
 17 *  - `send(std::span<const std::byte>)` — the same frame as one span. Overridden today;
 18 *    ADR-0089 makes it a one-span gather in the base, so a kind will write only the above.
 19 *  - `set_receiver` / `set_rope_receiver` — where inbound frames land. Both may fire on
 20 *    the kind's own receive thread, and both must be installed BEFORE frames flow.
 21 *
 22 * That is the entire surface. There is no `send_read`, no `send_reply`, no TLV type
 23 * anywhere in it, and that absence is the design: the router owns addressing (the `dst`
 24 * source route), the codec owns structure, and the transport owns exactly one question —
 25 * how do these bytes cross this wire. It is why `udp`, `tcp`, `ws`, `can`, `quic`,
 26 * `webtransport` and the in-process loopback are interchangeable to everything above,
 27 * and why writing a new kind is writing this class and nothing else.
 28 *
 29 * The example writes its own kind — 22 lines, no sockets — and then swaps in the shipped
 30 * `loopback_channel_t` to show the identical calls driving a link with a real receive
 31 * thread. Runs under ctest as `example_net_transport_seam`; returns non-zero on any
 32 * failed check.
 33 */
 34
 35#include <chrono>
 36#include <condition_variable>
 37#include <cstddef>
 38#include <cstdio>
 39#include <mutex>
 40#include <span>
 41#include <vector>
 42
 43#include "libtracer/loopback.hpp"
 44#include "libtracer/transport.hpp"
 45
 46namespace {
 47
 48using namespace std::chrono_literals;
 49
 50/** @brief Report expectation @p what and record a failure on @p ok. */
 51void check(bool& ok, bool cond, const char* what) {
 52    std::printf("  [%s] %s\n", cond ? "ok" : "FAIL", what);
 53    ok = ok && cond;
 54}
 55
 56/**
 57 * @brief A complete transport kind, in the smallest form the seam admits: it keeps
 58 *        every frame handed to it and can hand one back.
 59 *
 60 * The scatter-gather `send(iov)` joins the spans into the kind's own record, which is all a
 61 * kind without writev does; the span `send` keeps the one-span case until the base takes it
 62 * over (ADR-0089).
 63 */
 64class memo_link_t final : public tr::net::transport_t {
 65   public:
 66    /** @brief Frames this link was asked to emit, in order. */
 67    std::vector<std::vector<std::byte>> sent;
 68
 69    /**
 70     * @brief Keep the base's retained `send(head, value, lane)` overload visible.
 71     *
 72     * Declaring one `send` HIDES the others — ordinary C++ name hiding, and a kind that omits
 73     * this line silently loses the retained entry point it meant to inherit.
 74     */
 75    using tr::net::transport_t::send;
 76
 77    void send(std::span<const std::byte> frame) override {
 78        sent.emplace_back(frame.begin(), frame.end());
 79    }
 80
 81    /** @brief Emit one frame from scattered spans: join them into this kind's own record. */
 82    void send(std::span<const std::span<const std::byte>> iov) override {
 83        std::vector<std::byte>& f = sent.emplace_back();
 84        for (const std::span<const std::byte>& s : iov) f.insert(f.end(), s.begin(), s.end());
 85    }
 86
 87    /** @brief Deliver @p frame inbound, exactly as a receive thread would. */
 88    void inject(std::span<const std::byte> frame) { rx_.deliver_borrowed(frame); }
 89};
 90
 91/** @brief A thread-safe borrowed-span sink: a receive thread pushes, `main` waits. */
 92class sink_t {
 93   public:
 94    /** @brief The receiver callback — copies the borrowed span, which dies at return. */
 95    void operator()(std::span<const std::byte> frame) {
 96        {
 97            const std::lock_guard lock(m_);
 98            frames_.emplace_back(frame.begin(), frame.end());
 99        }
100        cv_.notify_all();
101    }
102
103    /** @brief Wait until at least @p n frames have landed, or @p budget expires. */
104    [[nodiscard]] bool wait_for(std::size_t n, std::chrono::milliseconds budget) {
105        std::unique_lock lock(m_);
106        return cv_.wait_for(lock, budget, [&] { return frames_.size() >= n; });
107    }
108
109    /** @brief How many frames have landed so far. */
110    [[nodiscard]] std::size_t count() const {
111        const std::lock_guard lock(m_);
112        return frames_.size();
113    }
114
115    /** @brief Frame @p i, by value. */
116    [[nodiscard]] std::vector<std::byte> at(std::size_t i) const {
117        const std::lock_guard lock(m_);
118        return frames_.at(i);
119    }
120
121   private:
122    mutable std::mutex m_;
123    std::condition_variable cv_;
124    std::vector<std::vector<std::byte>> frames_;
125};
126
127/** @brief @p n bytes counting up from @p seed — a stand-in for an encoded frame. */
128std::vector<std::byte> frame_of(std::size_t n, unsigned seed) {
129    std::vector<std::byte> f(n);
130    for (std::size_t i = 0; i < n; ++i) f[i] = static_cast<std::byte>(seed + i);
131    return f;
132}
133
134}  // namespace
135
136int main() {
137    bool ok = true;
138
139    // 1. Egress. A frame is a span of bytes; the transport is told nothing else about it.
140    std::printf("the seam, on a kind written here:\n");
141    memo_link_t link;
142    const auto f1 = frame_of(6, 0x10);
143    link.send(f1);
144    check(ok, link.sent.size() == 1 && link.sent[0] == f1, "send(span) emitted the frame verbatim");
145
146    // 2. The scatter-gather overload, the one the router uses: a rope's `to_iovec()` reaches
147    // the kind as its spans, and the kind emits them as ONE record. This kind cannot writev,
148    // so it joins them into its own storage; a kind that can writes them straight out. The
149    // bytes on the wire are the same one record either way.
150    const auto head = frame_of(3, 0x20);
151    const auto tail = frame_of(4, 0x30);
152    const std::span<const std::byte> parts[] = {head, tail};
153    link.send(std::span<const std::span<const std::byte>>{parts});
154    std::vector<std::byte> joined = head;
155    joined.insert(joined.end(), tail.begin(), tail.end());
156    check(ok, link.sent.size() == 2 && link.sent[1] == joined,
157          "send(iov) arrived as ONE frame — the kind joined the spans itself");
158
159    // 3. Ingress. The sink is installed before anything is delivered, because that ordering is
160    // the contract: a kind whose receive thread starts in its constructor is already draining
161    // the wire while the owner is still wiring, and a frame that lands in an empty slot is
162    // dropped with no counter moving. The built-in dialling kinds start receiving only at
163    // `start_receiving()` for exactly this reason.
164    sink_t inbound;
165    link.set_receiver(inbound);
166    const auto f2 = frame_of(5, 0x40);
167    link.inject(f2);
168    check(ok, inbound.count() == 1 && inbound.at(0) == f2, "the borrowed-span sink got the frame");
169    check(ok, !link.delivers_ropes(), "and this kind claims no OWNING delivery (the default)");
170
171    // 4. The same three calls against a shipped kind with a real receive thread. Nothing about
172    // the call sites changes — that interchangeability IS the seam.
173    std::printf("the same seam, on the shipped in-process loopback kind:\n");
174    tr::net::loopback_channel_t channel;
175    sink_t at_b;
176    channel.b().set_receiver(at_b);
177    const auto f3 = frame_of(8, 0x50);
178    channel.a().send(f3);
179    check(ok, at_b.wait_for(1, 2s), "a frame sent on endpoint a arrived at endpoint b");
180    check(ok, at_b.count() == 1 && at_b.at(0) == f3, "byte-identical across the 'wire'");
181    channel.shutdown();
182
183    std::printf("one seam, %zu egress calls and %zu inbound frames, zero TLV semantics\n",
184                link.sent.size(), inbound.count() + at_b.count());
185    return ok ? 0 : 1;
186}

See also: transport module · module catalog reference · transports are vertices · a kind is a name.