From b0d122bc18f9280bb05e569d9d32bb2da844f1ad Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 11:38:53 -0800 Subject: [PATCH 1/3] chore(quest): claim quest/m2/sockets/remove.md Quest-Claim: 003da3a7b62bc08b From 0a10b1c70d13ba3777f6d63d750a403ce768f88d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 12:02:39 -0800 Subject: [PATCH 2/3] feat!: socket endpoints leave every package for Kixel Deletes moq_net::socket, @moq/net's Socket, the moq-ffi and moq-c socket handles, the Python, Swift, Kotlin, Dart, and Go wrappers, the interop sockets lane, and the concept page. What existed only for sockets goes with them (origin hidden_withheld and its pre-lite-07 session hold, the JS hiddenWithheld hook). reserve_unlisted_track becomes public, and track::Producer::abort_handle adds a non-owning AbortHandle, both for Kixel's port. Co-Authored-By: Claude Opus 5.5 --- bench/README.md | 9 - dart/moq/lib/socket.dart | 18 - doc/.vitepress/config.ts | 1 - doc/concept/index.md | 1 - doc/concept/socket.md | 225 ---- doc/lib/js/net.md | 41 - doc/lib/py/index.md | 36 - doc/lib/rs/moq-net.md | 2 +- go/wrapper/bridge.go | 5 - go/wrapper/internal/bridge/handles.go | 9 - go/wrapper/socket/socket.go | 105 -- js/net/api/index.api.md | 28 - js/net/bench/socket.ts | 81 -- js/net/src/index.ts | 2 - js/net/src/internal.ts | 14 - js/net/src/lite/connection.ts | 9 - js/net/src/lite/version.ts | 16 - js/net/src/origin.ts | 14 - js/net/src/socket.test.ts | 694 ---------- js/net/src/socket.ts | 541 -------- .../kotlin/dev/moq/socket/Socket.kt | 16 - py/moq-rs/README.md | 23 - py/moq-rs/benches/ffi.py | 56 - py/moq-rs/docs/index.md | 17 - py/moq-rs/moq/__init__.py | 1 - py/moq-rs/moq/socket.py | 119 -- py/moq-rs/tests/test_socket.py | 194 --- .../m0/test-flakes-3/py-client-body-error.md | 25 + quest/m1/README.md | 2 +- quest/m2/README.md | 2 +- quest/m2/sockets/README.md | 56 - quest/m2/sockets/remove.md | 109 -- quest/m2/unused-api/README.md | 2 +- quest/m3/README.md | 2 +- quest/m3/release/demos-deploy.md | 2 +- rs/kio/src/producer.rs | 5 + rs/moq-c/README.md | 15 - rs/moq-c/src/consume.rs | 26 - rs/moq-c/src/lib.rs | 2 - rs/moq-c/src/publish.rs | 55 +- rs/moq-c/src/socket.rs | 511 -------- rs/moq-c/src/state.rs | 3 - rs/moq-c/src/test/parity.rs | 264 ---- rs/moq-cli/Cargo.toml | 8 - rs/moq-ffi/src/lib.rs | 1 - rs/moq-ffi/src/producer.rs | 26 +- rs/moq-ffi/src/socket.rs | 318 ----- rs/moq-ffi/src/test.rs | 10 - rs/moq-ffi/uniffi.toml | 1 - rs/moq-net/CHANGELOG.md | 3 +- rs/moq-net/src/ietf/subscriber.rs | 5 +- rs/moq-net/src/lite/session.rs | 10 - rs/moq-net/src/lite/version.rs | 9 - rs/moq-net/src/model/broadcast.rs | 13 +- rs/moq-net/src/model/front.rs | 9 +- rs/moq-net/src/model/listing.rs | 62 + rs/moq-net/src/model/mod.rs | 4 - rs/moq-net/src/model/origin.rs | 45 - rs/moq-net/src/model/socket.rs | 1150 ----------------- rs/moq-net/src/model/track.rs | 48 +- rs/moq-net/tests/integration/main.rs | 1 - rs/moq-net/tests/integration/socket.rs | 705 ---------- rs/moq-relay/tests/integration/main.rs | 1 - rs/moq-relay/tests/integration/quic_sim.rs | 99 +- rs/moq-relay/tests/integration/socket_auth.rs | 350 ----- sh/gh/nightly.sh | 2 +- swift/Sources/Moq/Socket.swift | 72 -- test/interop/README.md | 14 - test/interop/clients/js-native/socket.ts | 84 -- test/interop/clients/python/sockets.py | 92 -- test/interop/clients/rust/socket-js.rs | 131 -- test/interop/clients/rust/socket.rs | 88 -- test/interop/interop.sh | 46 +- test/interop/socket.sh | 89 -- 74 files changed, 201 insertions(+), 6653 deletions(-) delete mode 100644 dart/moq/lib/socket.dart delete mode 100644 doc/concept/socket.md delete mode 100644 go/wrapper/socket/socket.go delete mode 100644 js/net/bench/socket.ts delete mode 100644 js/net/src/socket.test.ts delete mode 100644 js/net/src/socket.ts delete mode 100644 kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/socket/Socket.kt delete mode 100644 py/moq-rs/moq/socket.py delete mode 100644 py/moq-rs/tests/test_socket.py create mode 100644 quest/m0/test-flakes-3/py-client-body-error.md delete mode 100644 quest/m2/sockets/README.md delete mode 100644 quest/m2/sockets/remove.md delete mode 100644 rs/moq-c/src/socket.rs delete mode 100644 rs/moq-ffi/src/socket.rs delete mode 100644 rs/moq-net/src/model/socket.rs delete mode 100644 rs/moq-net/tests/integration/socket.rs delete mode 100644 rs/moq-relay/tests/integration/socket_auth.rs delete mode 100644 swift/Sources/Moq/Socket.swift delete mode 100644 test/interop/clients/js-native/socket.ts delete mode 100644 test/interop/clients/python/sockets.py delete mode 100644 test/interop/clients/rust/socket-js.rs delete mode 100644 test/interop/clients/rust/socket.rs delete mode 100644 test/interop/socket.sh diff --git a/bench/README.md b/bench/README.md index 342706b915f..b958ebcd6e3 100644 --- a/bench/README.md +++ b/bench/README.md @@ -1041,12 +1041,3 @@ OOM kill does too, even inside a Quest scope: each worker watches `local.py` through a pidfd and stops once it exits. macOS has no pidfd, so there killing `local.py` with SIGKILL frees the lock while its workers keep measuring until their own gates exit, an accepted gap. Stop it with Ctrl-C or SIGTERM instead. - -### Python socket sessions - -The Python binding runner also sweeps both TCP and track sockets over 1, 4, -and 16 accepting endpoints and 1, 4, and 16 concurrent sessions per endpoint. -Each session sends a checked payload and finishes both halves. The -`socket//endpoints=N/sessions=M` entries report complete sessions per -second, including handshake, FFI calls, reads, and teardown. Run with -`just py bench`; Nightly's binding lane runs the same sweep. diff --git a/dart/moq/lib/socket.dart b/dart/moq/lib/socket.dart deleted file mode 100644 index a9156056454..00000000000 --- a/dart/moq/lib/socket.dart +++ /dev/null @@ -1,18 +0,0 @@ -/// Point-to-point socket endpoints and sessions. -library; - -import 'package:moq_ffi/moq_ffi.dart'; - -/// A server, a client, or both: its first dial announces it as a client, and its -/// first accept as a server. -typedef Endpoint = MoqSocketEndpoint; - -/// An incoming track dial, consumed by accepting or rejecting it. -typedef IncomingTrack = MoqSocketIncomingTrack; - -/// Whether [path] is an endpoint's broadcast, which only routes sessions and -/// carries no media. -bool isEndpoint(String path) => moqSocketIsEndpoint(path: path); - -/// A track session with producer and reliably subscribed consumer halves. -typedef TrackStream = MoqSocketTrackStream; diff --git a/doc/.vitepress/config.ts b/doc/.vitepress/config.ts index 990dd48b780..20441d1fe73 100644 --- a/doc/.vitepress/config.ts +++ b/doc/.vitepress/config.ts @@ -117,7 +117,6 @@ export default defineConfig({ { text: "Transport", link: "/concept/transport" }, { text: "moq-lite", link: "/concept/moq-lite" }, { text: "Identity and recovery", link: "/concept/identity" }, - { text: "Sockets", link: "/concept/socket" }, { text: "hang", link: "/concept/hang" }, { text: "Archives", link: "/concept/archives" }, { text: "Audio jitter", link: "/concept/audio-jitter" }, diff --git a/doc/concept/index.md b/doc/concept/index.md index 9276eafd744..0343c51b985 100644 --- a/doc/concept/index.md +++ b/doc/concept/index.md @@ -38,7 +38,6 @@ watched at 100 ms by one viewer and 10 s by another. - [Transport](/concept/transport): why QUIC, and the WebTransport, WebSocket, and raw QUIC paths onto it. - [moq-lite](/concept/moq-lite): the pub/sub protocol, discovery, path patterns, subscriptions, and congestion behavior. - [Identity and recovery](/concept/identity): choosing stream instances, following restarts, and answering work on demand without mixing streams. -- [Sockets](/concept/socket): dial and accept point-to-point sessions through relays, each a track of messages each way. - [hang](/concept/hang): the media catalog, containers, and how to extend both. - [Archives](/concept/archives): record listed tracks into object storage and serve them back from a host. - [Audio jitter](/concept/audio-jitter): how a receiver sizes its audio playout target from arrival timing. diff --git a/doc/concept/socket.md b/doc/concept/socket.md deleted file mode 100644 index 53714a78420..00000000000 --- a/doc/concept/socket.md +++ /dev/null @@ -1,225 +0,0 @@ ---- -title: Sockets -description: Dial and accept point-to-point sessions over MoQ, each a track of messages each way ---- - -# Sockets - -A socket connects two endpoints through relays, the way a TCP connection -connects two hosts. A server accepts dials and a client dials; an endpoint may -be both. Each role an endpoint takes is one hidden broadcast, so ordinary -grants decide who may be a server. Each connection is a session that only the -two ends can read while its id stays secret (see [Hidden names](#hidden-names)): -a track each way whose groups are messages. Use a -socket for per-client state that only the two ends need, such as a viewer's -commands and the replies to them, while whatever every client needs stays in an -ordinary broadcast. - -A socket is a convention over announcements, broadcasts, and track -subscriptions, not a wire extension. It uses no instances and no track -listings, and relays carry it unchanged. It needs moq-lite-07 or -moq-transport on every hop: an endpoint's broadcasts are -[hidden](/concept/moq-lite#hidden-broadcasts), and an older moq-lite hop -never carries hidden broadcasts, so a dial across one would never find its -peer. Clustered relays carry hidden broadcasts to each other on every version, -so the hop that decides is each endpoint's own session: while an endpoint's -client session is moq-lite before 07, its dials and accepts fail with -`Unsupported` instead of waiting, unless that session's publish half opts into -hidden broadcasts. A dialer can't tell a peer behind such a hop from one not -online yet, so its dial waits as for any absent peer, while the peer's own -accepts fail. This page is the convention, enough to -implement it in any language. -[`moq_net::socket`](https://docs.rs/moq-net/latest/moq_net/socket/) implements -it in Rust. - -## Endpoints - -An endpoint with id `` announces one broadcast for each role it takes: - -```text -/.server/ a server, which peers dial -/.client/ a client, which dials servers -``` - -| Role | Publishes | Subscribes | -| --- | --- | --- | -| Server | `.server/` | `.client/**` | -| Client | `.client/` | `.server/**` | - -A server announces its broadcast at its first accept, and a client at its -first dial, so a pure client never announces a server broadcast. An endpoint -that both dials and accepts announces both broadcasts under one id. - -The id is one path segment: a well-known name for a server, such as a game, or -a random id for a client. The prefix is shared by every endpoint that talks to -each other. Both role segments start with `.`, so endpoints are -[hidden](/concept/moq-lite#hidden-broadcasts) from anyone listing the prefix. - -The announcements are used only for routing: they are how a relay finds an -endpoint. Connections are never announced. An endpoint's broadcasts hold only -session tracks, served on request. A worker that acts on every announced -broadcast, such as a recorder, skips them: a path whose parent segment is -`.server` or `.client` is an endpoint's. Every library checks it as -`is_endpoint` (`isEndpoint` in JS, Swift, Kotlin, and Dart, `IsEndpoint` in Go, -and `moq_socket_is_endpoint` in C). - -## Sessions - -A session between a dialer `A` and an acceptor `B` is named by both ends and a -session id `S` that `A` picks: random and unguessable, at least 128 bits, such -as 32 hex digits. Both halves of the session are tracks with the same name, -`track///`, one in the dialer's client broadcast and -one in the acceptor's server broadcast: - -| Track | In broadcast | Written by | Read by | -| --- | --- | --- | --- | -| `track/A/B/S` | `/.client/A` | `A` | `B` | -| `track/A/B/S` | `/.server/B` | `B` | `A` | - -The leading `track` names the protocol; see [Messages](#messages). A server -resets a dial in any other protocol as unsupported. - -A request on a server broadcast is a dial, and one on a client broadcast -answers a dial. A server resets a dial that doesn't name another endpoint as -the dialer and itself as the acceptor, such as one naming another acceptor, as -not found, and a client resets any request but the answer to a dial it holds -the same way. Since `S` is random per dial, every session is distinct, and one -endpoint can dial and accept any number of sessions at once, with the same peer -or others. - -### Dial - -1. `A` picks `S` and holds its own half, `track/A/B/S` in `/.client/A`, - unanswered, announcing that broadcast if this is its first dial. -2. `A` subscribes to `/.server/B` for the track `track/A/B/S`, waiting for - `B`'s server endpoint to be announced if it is not yet. -3. `B` receives that subscription as a request for a track it does not have: - an incoming dial, claiming to be from `A`, with session `S`. - -### Accept - -1. `B` subscribes to `/.client/A` for the track `track/A/B/S`, and holds - `A`'s subscription unanswered. A relay can deliver the dial before `A`'s - announcement, so `B` waits for `A`'s client endpoint to be announced for as - long as `A`'s subscription stays open, and gives up once it closes. -2. `A` answers that subscription only if it dialed `S` to `B` and is still - waiting, and resets any other. That makes the exchange a return-routability - check: a dial forged in `A`'s name, by anyone who can subscribe to `B`'s - server endpoint, reaches an `A` that never dialed it, so `A` resets `B`'s - subscription and `B` resets the forger's. -3. Once `A` answers, `B` answers `A`'s subscription, and the session is open - for both. `B` writes nothing before `A` answers. - -### Reject - -`B` resets `A`'s subscription, with not found, as for a session that does not -exist. An endpoint that has never accepted has no server endpoint, so a dial to -it waits, as for one not online yet. A dial that gives up resets `B`'s -subscription in turn. - -## Messages - -Each side writes its own half and reads the peer's. A reader subscribes -reliably and in order: from group 0, oldest first, with no delay budget, so it -waits for a late group rather than skipping it. - -Each half is a whole track, and a message is one group. A slow message never -holds up the next, and each group can carry its own priority. Messages are -ordered by group sequence. A half has no max age unless its writer sets one, so -a newer message never makes an older one stale. A reader that keeps up gets -every message, but each hop's cache reclaims a message nobody read within its idle window (30 s by -default), or under memory pressure, except the newest, so a reader that stalls -longer can miss messages. Any message a hop reports it will never deliver is -skipped the same way, rather than ending the session. Over moq-transport, the -reader starts at group 0: a `SUBSCRIBE` with no filter, plus one range `FETCH` -for the groups below the answer's largest, so a publisher that refuses that -`FETCH` leaves a burst written before the answer missed. - -An application may make the reader lossy for messages that go stale, such as -positions or controls, by updating its subscription with a finite delay budget: -a message that is late past the budget is skipped rather than holding up later -ones. It may also read newest first. - -Finishing a half closes the session that way. Resetting it is an error. - -Use a socket for independent messages, where a late one should not block the -next: commands, controls, acknowledgements, or telemetry. - -## Closing - -An endpoint closing, or either of its broadcasts ending, resets every session -it holds, and stops reading them. A side that sees the peer's half reset treats the -session as closed and resets its own half, so both halves end. - -There is no multiplexing within a session: another channel is another session. -Several sessions between the same two endpoints are independent, and a -restarted endpoint starts new sessions. A channel that every client reads, such -as a game's status, stays an ordinary broadcast. - -## Hidden names - -The session id is the secret that keeps a session to its two ends: the token -grants below let every client subscribe to every server, and every server to -every client, so whoever learns a session's track name can read it, or answer -it before its peer. - -The acceptor learns the name too, but cannot replay it to a third endpoint -`C`: the name binds both ends. `C` resets `track/A/B/S` as a session for another -acceptor, and `A` never dialed `track/A/C/S`, so it resets `C`'s accept of that. -The peer id of an accepted session is the endpoint that dialed it to this one. - -A session track therefore never appears where a third party can read it: - -- It is never announced. -- It is never listed: an endpoint serves its session tracks only on request, - so a moq-lite 07 track listing (`TRACKS`) of either of an endpoint's - broadcasts names none of them, even to a reader allowed everything. -- Logs and metrics that a third party can read must not carry it. - -## Authorization - -Sockets need nothing beyond ordinary -[token grants](/bin/relay/auth#path-matching). With the token's root at the -socket prefix, each role is two prefix grants: publish the endpoint's own -broadcast, and subscribe to the other role's. - -```jsonc -// The server : publishes its endpoint, reads every client's. -{ "root": "", "publish": [".server/"], "subscribe": [".client/**"] } -// The client : publishes its endpoint, reads every server's. -{ "root": "", "publish": [".client/"], "subscribe": [".server/**"] } -``` - -An endpoint that both dials and accepts holds both. The publish grant pins the -role and the id, so a client can't announce a server endpoint, or announce as -another endpoint, and a client grant reads no other client's endpoint. The -subscribe grant lets an endpoint reach every peer of the other role, which is -what dialing and accepting need; the session id keeps it out of other -endpoints' sessions. A narrower subscribe grant, such as a client allowed only -`.server/`, also works: it can then dial only that server. - -A grant shared by many clients, such as an anonymous one publishing -`.client/*`, lets each announce any client id, so a client's id must stay a -secret too: random, and never published, such as in a status track. Two -announcements of one path coexist, so an impostor announcing a client's id can -break that client's next dial, though not read or join its sessions. - -`moq auth sign` writes these as, for example: - -```bash -moq auth sign --key key.jwk --root "$PREFIX" --publish ".server/$ID" --subscribe '.client/**' > server.jwt -moq auth sign --key key.jwk --root "$PREFIX" --publish ".client/$ID" --subscribe '.server/**' > client.jwt -``` - -Each client then connects to the relay at the prefix with its token, and its -endpoint's prefix is the session root. - -Signing and encryption are not part of the convention: an endpoint id is a -name the token pins, not a key, and relays are trusted with session names. A -planned mesh layer makes ids public keys. - -## Later - -Later protocols are other prefixes on the same endpoint broadcasts, with no -new announcement, such as `quic///` for a -multiplexed mode with many streams per session. diff --git a/doc/lib/js/net.md b/doc/lib/js/net.md index 14466992d25..6a8e08cc3fd 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -95,44 +95,3 @@ frame.finish(); const reader = await consumer.nextFrame(); for (let chunk = await reader?.readChunk(); chunk; chunk = await reader?.readChunk()) save(chunk); ``` - -## Sockets - -Use `Moq.Socket.Endpoint` for [point-to-point sessions](/concept/socket), with the same -origin publishing and consuming through the relay. An endpoint's first dial announces its -hidden client broadcast and its first accept its hidden server broadcast, so grants decide -which roles it may take; accepting verifies that the named peer actually dialed this session. -Every hop needs moq-lite-07 or moq-transport. Lite-07 currently requires explicitly -offering `moq-lite-07-wip`; older moq-lite hops do not carry these hidden endpoints. While -the endpoint's own client connection is moq-lite before 07, its dials and accepts fail as -unsupported instead of waiting. A dialer can't tell a peer behind such a hop from one not -online yet, so its dial waits as for any absent peer. - -```ts -const connection = await Moq.Connection.connect({ - url, versions: ["moq-lite-07-wip"], - publish: origin.consume(), consume: origin, -}); -const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from("game"), "viewer"); -const session = await endpoint.dialTrack("server", undefined, { signal: abort.signal }); -// A socket session is scale-less, so a message's frame carries its group's key, here the first. -session.producer.writeFrame({ payload: new TextEncoder().encode("hello"), timestamp: Moq.Time.Timestamp.unscaled(0n) }); -session.producer.finish(); // Ends this direction; the peer may still reply. -const reader = session.consumer.ordered(); -for (let frame = await reader.readFrame(); frame; frame = await reader.readFrame()) { - receive(frame.payload); -} -endpoint.close(); -connection.close(); -``` - -A server takes `await endpoint.acceptTrack()`, then accepts or rejects that incoming dial. -The incoming peer and session are unverified claims; `accept()` can fail if the original -dial has closed. A pending dial waits for acceptance until it is cancelled or the endpoint -or session closes, with no acceptance timeout. -The stream exposes a track producer, where each group is one message, and an already -subscribed consumer in ascending order until updated, plus `peer` and `session`. It skips -groups that never arrive whole, including refused, dropped, or evicted groups; a cut group -is never a complete message. Finishing a producer ends that direction; aborting it or -closing the endpoint resets the session. Close endpoints explicitly, and use `AbortSignal` -to cancel pending dials and accepts. Keep session ids secret. diff --git a/doc/lib/py/index.md b/doc/lib/py/index.md index 7ca9ce4f51f..87f965ad3f3 100644 --- a/doc/lib/py/index.md +++ b/doc/lib/py/index.md @@ -157,39 +157,3 @@ A catalog that needs a newer moq-hang draft than this build reads raises `Error. - API source: [py/moq-rs/moq](https://github.com/moq-dev/moq/tree/main/py/moq-rs/moq) - Source and examples: [`py/moq-rs`](https://github.com/moq-dev/moq/tree/main/py/moq-rs) - Raw bindings: [`moq-ffi`](https://pypi.org/project/moq-ffi/) on PyPI, for the unwrapped API - -## Socket sessions - -`moq.socket` connects endpoints through relays in either direction. Wire one -origin to both session directions, then hold an endpoint while its sessions run: - -```python -import asyncio, moq - -async def socket_client(): - origin = moq.OriginProducer() - versions = ["moq-lite-07-wip"] - async with moq.Client("https://cdn.moq.dev/anon", versions=versions, publish=origin, consume=origin): - async with moq.socket.Endpoint(origin, "room", "client") as endpoint: - stream = await endpoint.dial_track("server") - stream.producer.write_frame(b"command", moq.Timestamp.unscaled(0)) # One message, the first group: key 0. - stream.producer.finish() # abort(error_code) ends it with an error instead. - async for group in stream.consumer: - async for frame in group: - print(frame.payload) - -asyncio.run(socket_client()) -``` - -Sockets need lite-07 or moq-transport on every hop, so the example opts into -`moq-lite-07-wip`. No client offers it by default while its wire format is a -work in progress, and the relay must accept it too. - -A server awaits `endpoint.accept_track()`, then calls `await incoming.accept()` -or `incoming.reject()`. Accepting verifies that the claimed peer really dialed. -`TrackStream` exposes a track producer and a reliably subscribed, ascending -consumer, plus `peer` and `session`; each message is a group. Keep the session -id private. Omitted `info` keeps messages without a publisher age limit; update -the consumer's subscription to change delivery. Cancelling a dial or acceptance resets that dial; -closing the endpoint resets all its sessions. See [Sockets](/concept/socket) -for routing and authorization. diff --git a/doc/lib/rs/moq-net.md b/doc/lib/rs/moq-net.md index 00e6e89f273..542557be0f9 100644 --- a/doc/lib/rs/moq-net.md +++ b/doc/lib/rs/moq-net.md @@ -22,8 +22,8 @@ What you use it for, beyond what the concept page already describes: - **Request one instance, or follow them all.** `consumer.request(path, None)` resolves the instance serving a path now, and fails `Unroutable` only once discovery has synced with no route for it. `consumer.follow(path, None)` hands out each publisher instance serving a path from `available()`: the first once announced, then each one that takes over, `None` once the path goes offline, and whatever serves it next, like JS `follow(path)`. `consumer.announced()` lists paths as present, then unavailable or available as a held route loses and recovers its path, and finally absent once the origin withdraws one or offline once this side lets go of it, which says nothing of whether the origin still serves it. A broadcast already resolved stays on the old instance until dropped or its route goes. See [identity and recovery](/concept/identity) to choose between following and holding one instance. - **Discovery follows what you watch.** A client session asks the relay for announcements only under the prefixes its subscribe origin's readers watch, with `announced()`, `request`, or `follow`, stops asking about a prefix 2s after nothing at or beneath it is watched, and asks again on reconnect. A broadcast resolved through a discovered route keeps its path watched until dropped. `request` owns discovery of its path, so no separate watch is needed. A server session, or a client fed into an origin handle marked `with_link(origin::Link::Peer)` or `Link::Upstream`, asks for everything it may, so a relay discovers everything. An HTTP gateway (the moq-hls `Server`, the moq-webrtc WHEP router) watches its whole scope while it lives, so the paths its callers name never open a request each. moq-lite-03/04 sessions, whose announcements mark no initial set, still ask for everything. - **Relay by opting into proxying.** On moq-lite-07 (`moq-lite-07-wip`, opt-in) a session sends its peer only the routes to its origin's own claims, so an application never becomes transit for anyone else. A relay built on the library marks the handle it publishes with `origin.clone().with_proxy(true)` to carry what it learned from other peers too; `moq-relay` does so for every session. Older versions advertise everything either way. -- **Dial and accept sockets.** `socket::Endpoint::new(&origin, prefix, id)` is a server, a client, or both: its first dial announces a hidden client endpoint at `/.client/`, and its first accept a hidden server endpoint at `/.server/`, both used only to route sessions, so grants decide which roles it may take. `accept_track()` yields an `Incoming` dial to `accept` or `reject` (dropping it rejects). `dial_track(server, info)` and an accepted dial yield a `socket::TrackStream`, this side's `track::Producer` and a `track::Subscriber` of the peer's half, subscribed reliably and in order until the application updates it, one message per group. A dial resolves once the peer accepts, and an accept checks that the claimed dialer really dialed this endpoint, waiting for its endpoint while the dial stays open. Every hop needs moq-lite-07 or moq-transport: while the origin has an older moq-lite client session whose publish half doesn't opt into hidden broadcasts (`with_hidden(true)`), dials and accepts fail with `Unsupported`. A dial to a peer behind such a hop waits, as for a peer not online yet. Session tracks are never announced or listed. See [Sockets](/concept/socket). - **One track per name.** Concurrent subscriptions share one request and producer. A group is keyed by its start, its first frame's timestamp in the track's timescale, and starts only rise. An untimed track has no timescale, so its frames carry `Timestamp::unscaled(key)`, the group's key, and a timestamp's `scale()` is `None`: conversions such as `as_millis()` fail on it, and it compares with no scaled timestamp. Every frame write takes a `Timestamp`, and one that doesn't match its track is refused with `TimestampMismatch`. `append_group(start)` opens the next one at a `Timestamp`, converted into the track's timescale as a frame's is (`Timestamp::unscaled(key)` on an untimed track), declaring the range below it skipped; `create_group` places one without declaring anything. The broadcast keeps nothing of an ended track, and a name always means the same content, so a producer that replaces it starts its groups past its predecessor's. Readers of an aborted track resume onto the replacement when they read through an origin request, whose route always names an instance; otherwise their subscription ends with the error. +- **Keep a track out of listings.** `broadcast.reserve_unlisted_track(name)` reserves a track no track listing names, so only a subscriber who already knows the name reads it. It hides the track from discovery and authorizes nothing: anyone allowed to subscribe to the broadcast can read it by name. A producer you hand out can still be ended on your own shutdown with `producer.abort_handle()`, which aborts the track without keeping it open. - **Authored tracks default to a zero `max_age`.** `track::Info::default()` sets `max_age` to `Some(Duration::ZERO)`, the live edge only; a publisher that wants readers to reach history sets a longer bound, or `with_max_age(None)` for no publisher limit. Media tracks from `hang::container::track_info` declare `hang::container::MAX_AGE` (30 seconds). `max_age` is how long a superseded group stays fresh, never the newest group; it frees no bytes, which the cache pool's expiry reclaims. Received metadata keeps absence as no limit, and an origin's `cache_duration` can only cap it lower. - **End content by finishing tracks.** A broadcast has no finished state. When the input ends, finish each track: `finish(self)` consumes the producer and returns a `track::Finished`, and `fail(err)` does the same with an error that readers get after every group below the end. A finished track can still take an older group below the end, be read, or be aborted, but not append. Hold it, and the broadcast stays announced and keeps serving the track until you close it or drop its route; dropping the last handle releases the track, so new readers no longer find it. How long to keep it up for late readers is yours to decide: `moq_mux::Linger` holds a hang broadcast for a fixed window. Ending a group returns a `group::Finished`, which can be read or aborted to release its cache: `finish_at(end)` ends it whole where its content stops, so a reader can tell the group starting there continues it, and `finish()` ends it without one. - **Demand says whether anyone is watching.** `demand()` on a track, group, or broadcast says whether a subscriber is attached. First-party capture does not wait for it: video encodes while enabled and audio keeps raw PCM to encode when someone listens, so a late subscription's `delay.max` or a FETCH finds the recent past. Demand gates bandwidth reservations, transcode rungs, and on-demand video where a publisher opts in, which saves the encode but leaves nothing from before the first subscriber. Holding a broadcast or track consumer is not demand: a track consumer requests nothing until it queries, subscribes, or fetches, and those count until dropped. A shared fetch stays up until its last reader leaves. Demand rolls up: a frame being read keeps its group wanted, a group a subscription or fetch handed out keeps its track wanted until read to its end, and any wanted track keeps its broadcast wanted. An on-demand publisher ends a track nobody wants with `abort_unwanted`, which keeps it for a reader that joined since, rather than by dropping its producer. diff --git a/go/wrapper/bridge.go b/go/wrapper/bridge.go index be29fbb7566..964965f008e 100644 --- a/go/wrapper/bridge.go +++ b/go/wrapper/bridge.go @@ -8,11 +8,6 @@ import ( // Hand the layer subpackages the handles behind these types // without exporting them. func init() { - bridge.OriginProducer = func(v any) *ffi.MoqOriginProducer { return v.(*OriginProducer).inner } - bridge.WrapTrackProducer = func(v *ffi.MoqTrackProducer) any { return &TrackProducer{inner: v} } - bridge.WrapTrackConsumer = func(v *ffi.MoqTrackConsumer) any { return &TrackConsumer{inner: v} } - bridge.TrackInfo = func(v any) (*ffi.MoqTrackInfo, error) { return trackInfoFFI(v.(*TrackInfo)) } - bridge.BroadcastConsumer = func(v any) *ffi.MoqBroadcastConsumer { return v.(*BroadcastConsumer).inner } bridge.TrackRequest = func(v any) *ffi.MoqTrackRequest { return v.(*TrackRequest).inner } bridge.Subscription = func(v any) (*ffi.MoqSubscription, error) { return subscriptionFFI(v.(*Subscription)) } diff --git a/go/wrapper/internal/bridge/handles.go b/go/wrapper/internal/bridge/handles.go index 1de15778a45..aef718e8eda 100644 --- a/go/wrapper/internal/bridge/handles.go +++ b/go/wrapper/internal/bridge/handles.go @@ -12,15 +12,6 @@ import ( // moq, so they are set before a subpackage can call them. They take and return // `any` because this package cannot import moq's types without a cycle. var ( - // OriginProducer returns the handle behind a *moq.OriginProducer. - OriginProducer func(any) *ffi.MoqOriginProducer - // WrapTrackProducer wraps a socket write half. - WrapTrackProducer func(*ffi.MoqTrackProducer) any - // WrapTrackConsumer retains a socket's subscribed read half. - WrapTrackConsumer func(*ffi.MoqTrackConsumer) any - // TrackInfo converts a *moq.TrackInfo to the generated record. - TrackInfo func(any) (*ffi.MoqTrackInfo, error) - // BroadcastProducer returns the handle behind a *moq.BroadcastProducer. BroadcastProducer func(any) *ffi.MoqBroadcastProducer // TrackProducer returns the handle behind a *moq.TrackProducer. diff --git a/go/wrapper/socket/socket.go b/go/wrapper/socket/socket.go deleted file mode 100644 index 4cde271d460..00000000000 --- a/go/wrapper/socket/socket.go +++ /dev/null @@ -1,105 +0,0 @@ -// Package socket dials and accepts point-to-point sessions between server and client endpoints. -package socket - -import ( - "context" - moq "moq.dev/moq" - ffi "moq.dev/moq-ffi/moq" - "moq.dev/moq/internal/bridge" -) - -// IsEndpoint reports whether path is an endpoint's broadcast, which only routes sessions and -// carries no media. -func IsEndpoint(path string) (bool, error) { return ffi.MoqSocketIsEndpoint(path) } - -// Endpoint is a server, a client, or both, and dials and accepts track sessions. -type Endpoint struct{ inner *ffi.MoqSocketEndpoint } - -// NewEndpoint makes the endpoint id beneath prefix, using origin to find peers. Its first -// dial announces it as a client, and its first accept as a server. -func NewEndpoint(origin *moq.OriginProducer, prefix, id string) (*Endpoint, error) { - inner, err := ffi.NewMoqSocketEndpoint(bridge.OriginProducer(origin), prefix, id) - if err != nil { - return nil, err - } - return &Endpoint{inner: inner}, nil -} - -// ID returns this endpoint's id. -func (e *Endpoint) ID() string { return e.inner.Id() } - -// Prefix returns the shared endpoint prefix. -func (e *Endpoint) Prefix() string { return e.inner.Prefix() } - -// Close retracts the endpoint's broadcasts and resets pending dials and sessions. -func (e *Endpoint) Close() { e.inner.Close() } - -// DialTrack dials a new track session; cancelling resets only this dial. -func (e *Endpoint) DialTrack(ctx context.Context, peer string, info *moq.TrackInfo) (*TrackStream, error) { - ffiInfo, err := bridge.TrackInfo(info) - if err != nil { - return nil, err - } - inner, err := bridge.CallHandle(ctx, nil, func(ctx context.Context) (*ffi.MoqSocketTrackStream, error) { - return e.inner.DialTrack(ctx, peer, ffiInfo) - }) - if err != nil { - return nil, err - } - return newTrackStream(inner), nil -} - -// AcceptTrack starts listening for track and awaits the next dial. -func (e *Endpoint) AcceptTrack(ctx context.Context) (*IncomingTrack, error) { - inner, err := bridge.CallHandle(ctx, nil, e.inner.AcceptTrack) - if err != nil { - return nil, err - } - return &IncomingTrack{inner: inner}, nil -} - -// IncomingTrack is a track dial, consumed by accepting or rejecting it. -type IncomingTrack struct{ inner *ffi.MoqSocketIncomingTrack } - -// Peer returns the claimed endpoint; accepting verifies return routability. -func (i *IncomingTrack) Peer() string { return i.inner.Peer() } - -// Session returns the private session id. -func (i *IncomingTrack) Session() string { return i.inner.Session() } - -// Reject refuses the dial as not found. -func (i *IncomingTrack) Reject() error { return i.inner.Reject() } - -// Accept verifies return routability and accepts this dial. -func (i *IncomingTrack) Accept(ctx context.Context, info *moq.TrackInfo) (*TrackStream, error) { - ffiInfo, err := bridge.TrackInfo(info) - if err != nil { - return nil, err - } - inner, err := bridge.CallHandle(ctx, nil, func(ctx context.Context) (*ffi.MoqSocketTrackStream, error) { return i.inner.Accept(ctx, ffiInfo) }) - if err != nil { - return nil, err - } - return newTrackStream(inner), nil -} - -// TrackStream exposes a track session's two halves and identities. -type TrackStream struct { - // Producer is this side's write half. - Producer *moq.TrackProducer - // Consumer is the peer's subscribed read half. - Consumer *moq.TrackConsumer - // Peer is the peer endpoint's id. - Peer string - // Session is the private session id. - Session string -} - -func newTrackStream(inner *ffi.MoqSocketTrackStream) *TrackStream { - defer inner.Destroy() - return &TrackStream{ - Producer: bridge.WrapTrackProducer(inner.Producer()).(*moq.TrackProducer), - Consumer: bridge.WrapTrackConsumer(inner.Consumer()).(*moq.TrackConsumer), - Peer: inner.Peer(), Session: inner.Session(), - } -} diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index b1c2986f661..7c165de61c5 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -604,34 +604,6 @@ export type SessionCode = number & { export * as Signals from "@moq/signals"; -export namespace Socket { - export class Endpoint { - constructor(origin: Origin.Producer, prefix: Path.Valid, id: string); - acceptTrack(options?: Socket.Options): Promise; - close(): void; - get closed(): Signals.GetPromise; - dialTrack(peer: string, info?: Track.InfoInit, options?: Socket.Options): Promise; - readonly id: string; - readonly prefix: Path.Valid; - } - export class Incoming { - accept(info?: Track.InfoInit, options?: Socket.Options): Promise; - readonly peer: string; - reject(): void; - readonly session: string; - } - export interface Options { - signal?: AbortSignal; - } - export class TrackStream { - readonly consumer: Track.Subscriber; - readonly peer: string; - readonly producer: Track.Producer; - readonly session: string; - } - export function isEndpoint(path: Path.Valid): boolean; -} - export const StreamCode: ((code: number) => StreamCode) & { readonly Cancel: StreamCode; readonly ControlTimeout: StreamCode; diff --git a/js/net/bench/socket.ts b/js/net/bench/socket.ts deleted file mode 100644 index bcb871268d7..00000000000 --- a/js/net/bench/socket.ts +++ /dev/null @@ -1,81 +0,0 @@ -/** Per-session track socket handshake, frame delivery and teardown swept over endpoints x sessions per endpoint. */ -import { yieldNow } from "@moq/clock"; -import { accept } from "../src/connection/accept.ts"; -import { connect } from "../src/connection/connect.ts"; -import { Origin, Path, Socket, Time } from "../src/index.ts"; -import * as Lite from "../src/lite/index.ts"; -import { createMockTransportPair } from "../src/mock.ts"; - -console.debug = () => {}; -console.log("mode,endpoints,sessions_per_endpoint,open_us,frame_us,close_us"); -for (const mode of ["local", "lite07"] as const) - for (const endpoints of [1, 8, 32]) - for (const sessions of [1, 8, 32]) { - const samples: number[][] = []; - for (let round = 0; round < 4; round++) { - const origin = new Origin.Producer(); - const remote = mode === "local" ? origin : new Origin.Producer(); - const pair = mode === "lite07" ? createMockTransportPair(Lite.ALPN_07_WIP) : undefined; - const connections = pair - ? await Promise.all([ - connect({ - url: new URL("https://localhost/bench"), - transport: pair.client, - publish: origin.consume(), - consume: origin, - }), - accept({ - url: new URL("https://localhost/bench"), - transport: pair.server, - publish: remote.consume(), - consume: remote, - }), - ]) - : []; - const dialer = new Socket.Endpoint(origin, Path.from("bench"), "dialer"); - const peers = Array.from( - { length: endpoints }, - (_, i) => new Socket.Endpoint(remote, Path.from("bench"), `peer-${i}`), - ); - const start = performance.now(); - const pairs = await Promise.all( - peers.flatMap((peer) => - Array.from({ length: sessions }, async () => { - const incoming = peer.acceptTrack(); - const dialing = dialer.dialTrack(peer.id); - return Promise.all([dialing, (await incoming).accept()]); - }), - ), - ); - const opened = performance.now(); - await Promise.all( - pairs.map(async ([local, remote]) => { - local.producer.writeFrame({ - payload: new Uint8Array(1024), - timestamp: Time.Timestamp.unscaled(0n), - }); - const frame = await remote.consumer.ordered().readFrame(); - if (frame?.payload.length !== 1024) throw new Error("missing socket payload"); - }), - ); - const received = performance.now(); - dialer.close(); - for (const peer of peers) peer.close(); - origin.close(); - remote.close(); - for (const connection of connections) connection.abort(); - await yieldNow(); - const closed = performance.now(); - if (round) - samples.push( - [opened - start, received - opened, closed - received].map((ms) => (ms * 1000) / pairs.length), - ); - } - const median = [0, 1, 2].map((i) => - samples - .map((sample) => sample[i]) - .sort((a, b) => a - b)[1] - .toFixed(2), - ); - console.log(`${mode},${endpoints},${sessions},${median.join(",")}`); - } diff --git a/js/net/src/index.ts b/js/net/src/index.ts index 90c4b790f2f..774bdafc81d 100644 --- a/js/net/src/index.ts +++ b/js/net/src/index.ts @@ -35,8 +35,6 @@ export * as Instance from "./instance.ts"; export * as Origin from "./origin.ts"; /** Broadcast path utilities with delimiter-aware prefix matching. Path patterns are re-exported from `@moq/pattern`. */ export * as Path from "./path.ts"; -/** Point-to-point track sessions through an origin. */ -export * as Socket from "./socket.ts"; /** Presentation timestamps and their timescales; durations live in {@link Clock}. */ export * as Time from "./time.ts"; /** A value paired with its frame's timestamp: when it was captured, or a scale-less track's group key. */ diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 9b6c99cc64f..0c6809ab2ad 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -16,7 +16,6 @@ import type { Consumer as FrameConsumer } from "./frame.ts"; import type { Frame, Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; import { isAnonymous, type WireRoute } from "./hop.ts"; import * as Instance from "./instance.ts"; -import type { Consumer as OriginConsumer, Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; import type { Timestamp } from "./time.ts"; import type { @@ -971,13 +970,6 @@ export const hooks: { keepRequest: (request: Request) => void; /** Observe the peer's reset without waiting for routed delivery recovery. */ watchSubscriberError: (subscriber: Subscriber, fn: (error: Error) => void) => Dispose; - /** - * Count a session withholding the origin's hidden broadcasts from its peer, such as a - * moq-lite client before 07, whose peer can't ask for them, until disposed. - */ - withholdHidden: (origin: OriginConsumer) => Dispose; - /** How many sessions withhold the origin's hidden broadcasts from their peer. */ - hiddenWithheld: (origin: OriginProducer) => Getter; /** Name a broadcast handle by the path an origin created or resolved it at. */ stampPath: (target: BroadcastProducer | BroadcastConsumer, path: Path.Valid) => void; /** Name the instance of the route an origin resolved a broadcast handle through. */ @@ -1344,12 +1336,6 @@ export const hooks: { watchSubscriberError: () => { throw new Error("track.ts not loaded"); }, - withholdHidden: () => { - throw new Error("origin.ts not loaded"); - }, - hiddenWithheld: () => { - throw new Error("origin.ts not loaded"); - }, stampPath: () => { throw new Error("broadcast.ts not loaded"); }, diff --git a/js/net/src/lite/connection.ts b/js/net/src/lite/connection.ts index 65dbdad3d27..f442e612353 100644 --- a/js/net/src/lite/connection.ts +++ b/js/net/src/lite/connection.ts @@ -10,7 +10,6 @@ import { type Transport, transportOf } from "../connection/transport.ts"; import { closeError, error, fromClose, ProtocolViolation, StreamCode, sessionCause } from "../error.ts"; import { type Hop, randomHop, toHop } from "../hop.ts"; import { IMPLEMENTATION } from "../implementation.ts"; -import { hooks } from "../internal.ts"; import { lines } from "../open.ts"; import type { Consumer as OriginConsumer } from "../origin.ts"; import * as Path from "../path.ts"; @@ -41,7 +40,6 @@ import { hasDatagrams, hasDatagramTracks, hasGoaway, - hasHidden, hasProbeRtt, hasRouteKey, hasRoutes, @@ -254,13 +252,6 @@ export class Connection implements Session { this.#fail(error(err)); }); } - // Before lite-07 the peer can't ask for hidden routes, so a client publishes none of - // them, and a socket endpoint behind it is unreachable. A server leaves it to the - // client, whose origin holds that endpoint. - if (client && publish && !hasHidden(version)) { - const withholding = hooks.withholdHidden(publish); - void quic.closed.then(withholding, withholding); - } this.#auth.violated.subscribe((err) => { if (!err) return; closeViolation(this.#quic, err); diff --git a/js/net/src/lite/version.ts b/js/net/src/lite/version.ts index 683001cf0ed..b4f55cb2262 100644 --- a/js/net/src/lite/version.ts +++ b/js/net/src/lite/version.ts @@ -399,22 +399,6 @@ export function fillsFromStart(version: Version): boolean { return resolvesStart(version) && !hasMediaTime(version) && !hasFetchMaxDelay(version); } -/** Whether ANNOUNCE_REQUEST carries the hidden opt-in. Added in lite-07. */ -export function hasHidden(version: Version): boolean { - // Explicitly list older versions so future versions keep the lite-07+ behavior. - switch (version) { - case Version.DRAFT_01: - case Version.DRAFT_02: - case Version.DRAFT_03: - case Version.DRAFT_04: - case Version.DRAFT_05: - case Version.DRAFT_06: - return false; - default: - return true; - } -} - /** * Whether a served SUBSCRIBE completes only once the subscriber FINs or resets its half of the * Subscribe Stream. A subscriber FINs once its tail accounting settles, since a transport ACK diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index c9762cb6bdb..dade7cd1193 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -686,9 +686,6 @@ class OriginState { // retain their old broadcast without participating in routing or same-instance failover. retained = new WeakSet(); - // The sessions withholding hidden broadcasts from their peer; see `hooks.withholdHidden`. - withheld = new Signal(0); - // How many sessions are attached, and how many of those support broadcast discovery. // What backs the public `discovery` getter. sessions = new Signal({ total: 0, discovery: 0 }); @@ -1401,7 +1398,6 @@ export class Producer implements Table { #reader = makeConsumer(this.#state, this.#scope); static { - hooks.hiddenWithheld = (origin) => origin.#state.withheld; exactRoute = (producer, path, options, request) => producer.#exact(path, options, request); } @@ -2231,16 +2227,6 @@ export class Consumer implements Reader { static { makeConsumer = (state, scope) => new Consumer(state, scope); - hooks.withholdHidden = (origin) => { - const withheld = origin.#state.withheld; - withheld.update((count) => count + 1); - let held = true; - return () => { - if (!held) return; - held = false; - withheld.update((count) => count - 1); - }; - }; } /** Settles once the origin closes; see {@link Producer.closed}. */ diff --git a/js/net/src/socket.test.ts b/js/net/src/socket.test.ts deleted file mode 100644 index 6ecc88bba72..00000000000 --- a/js/net/src/socket.test.ts +++ /dev/null @@ -1,694 +0,0 @@ -import { afterEach, expect, test } from "bun:test"; -import { Duration } from "@moq/clock"; -import { Manual } from "@moq/clock/test"; -import { type Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; -import { accept as acceptConnection } from "./connection/accept.ts"; -import { connect } from "./connection/connect.ts"; -import { NotFound, StreamCode, StreamError } from "./error.ts"; -import * as Ietf from "./ietf/index.ts"; -import { Origin, Path, Socket } from "./index.ts"; -import { hooks } from "./internal.ts"; -import * as Lite from "./lite/index.ts"; -import { createMockTransportPair } from "./mock.ts"; -import { Timestamp } from "./time.ts"; -import type * as Track from "./track.ts"; -import { wireOf } from "./wire.ts"; - -const origins = new Set(); -const clocks: { close(): void }[] = []; -afterEach(() => { - for (const origin of origins) origin.close(); - origins.clear(); - for (const clock of clocks.reverse()) clock.close(); - clocks.length = 0; -}); -function time() { - const clock = new Manual(); - clocks.push(clock.install()); - return clock; -} -// A scale-less frame of `value`, in the group `key` names: a TCP half's only group, or a track -// message's own group. -const bytes = (value: number, key = 0n) => ({ payload: new Uint8Array([value]), timestamp: Timestamp.unscaled(key) }); -function setup() { - const origin = new Origin.Producer(); - origins.add(origin); - const a = new Socket.Endpoint(origin, Path.from("room"), "alice"); - const b = new Socket.Endpoint(origin, Path.from("room"), "bob"); - return { origin, a, b }; -} -async function track() { - const state = setup(); - const incoming = state.b.acceptTrack(); - const dialing = state.a.dialTrack("bob"); - const remote = await (await incoming).accept(); - return { ...state, local: await dialing, remote }; -} -type Role = ".server" | ".client"; -function announced(origin: Origin.Producer, role: Role, id: string): BroadcastConsumer | undefined { - return origin.request(Path.from("room", role, id)).active.peek(); -} -async function broadcast(origin: Origin.Producer, role: Role, id: string): Promise { - const active = announced(origin, role, id); - if (!active) throw new Error("endpoint not routed locally"); - return active; -} -async function raw(origin: Origin.Producer, role: Role, id: string, name: string): Promise { - return (await broadcast(origin, role, id)).track(name).subscribe({ order: "ascending", delay: { max: null } }); -} - -test("Socket namespace exposes endpoint and stream", () => { - expect(Socket.Endpoint).toBeFunction(); - expect(Socket.TrackStream).toBeFunction(); -}); - -test("isEndpoint tells an endpoint's broadcast from media", () => { - for (const role of [".server", ".client"]) { - expect(Socket.isEndpoint(Path.from("room", role, "alice"))).toBeTrue(); - } - for (const path of ["room/alice", ".server", "room/.server", "room/.server/alice/cam", "room/server/alice"]) { - expect(Socket.isEndpoint(Path.from(path))).toBeFalse(); - } -}); - -test("an endpoint closes with its origin before it dials or accepts", async () => { - const origin = new Origin.Producer(); - const endpoint = new Socket.Endpoint(origin, Path.from("room"), "alice"); - origin.close(); - expect(await endpoint.closed).toBeNull(); - expect(new Socket.Endpoint(origin, Path.from("room"), "bob").closed.peek()).toBeNull(); -}); - -test("a peer route replaced between metadata and opening cannot inherit a socket session", async () => { - const clock = time(); - const { origin, a } = setup(); - const peer = new BroadcastProducer(); - const keep = peer.consume(); - const routes: { close(): void }[] = []; - const tasks: Promise[] = []; - const advertise = (front: BroadcastConsumer) => { - const route = wireOf(origin).receive(Path.from("room/.server/remote")); - routes.push(route); - tasks.push( - (async () => { - try { - for await (const request of route.requested()) request.serve(front.clone()); - } catch { - /* Closing the route ends its request loop. */ - } - })(), - ); - return route; - }; - const first = advertise(keep); - const requested = wireOf(peer).requested(); - let outcome: unknown; - const dialing = a.dialTrack("remote").then( - (value) => { - outcome = value; - }, - (error) => { - outcome = error; - }, - ); - let returning: Track.Subscriber | undefined; - const replacement = new BroadcastProducer(); - const replacementKeep = replacement.consume(); - try { - const request = await requested; - if (!request) throw new Error("missing dial"); - returning = await raw(origin, ".client", "alice", request.name); - await returning.info(); - request.accept({ maxAge: null }); - // Both route changes happen before the info promise's continuation opens the session. - first.close(); - advertise(replacementKeep); - await clock.advance(Duration.ZERO); - expect(outcome).toBeInstanceOf(Error); - } finally { - a.close(); - for (const route of routes) route.close(); - returning?.close(); - keep.close(); - replacementKeep.close(); - peer.close(); - replacement.close(); - await Promise.all(tasks); - await dialing; - } -}); - -test("track halves start reliable and ascending, preserve old messages and accept independent info", async () => { - const { local, remote } = await track(); - expect(local.peer).toBe("bob"); - expect(remote.peer).toBe("alice"); - expect(local.session).toBe(remote.session); - expect(local.session).toMatch(/^[0-9a-f]{32}$/); - expect((await local.producer.info()).maxAge).toBeNull(); - expect(remote.consumer.subscription.peek()?.order).toBe("ascending"); - expect(remote.consumer.subscription.peek()?.delay?.max).toEqual(Duration.millis(Number.MAX_SAFE_INTEGER)); - for (let i = 0; i < 3; i++) local.producer.writeFrame(bytes(i, BigInt(i))); - local.producer.finish(); - const reader = remote.consumer.ordered(); - for (let i = 0; i < 3; i++) { - const frame = await reader.readFrame(); - expect(frame?.group).toBe(BigInt(i)); - expect(frame?.payload).toEqual(bytes(i).payload); - } - expect(await reader.readFrame()).toBeUndefined(); - remote.producer.writeFrame(bytes(7)); - remote.producer.finish(); - expect((await local.consumer.ordered().readFrame())?.payload).toEqual(bytes(7).payload); -}); - -test("a track session skips dropped groups and keeps the next message", async () => { - const clock = time(); - const { local, remote } = await track(); - const reader = remote.consumer.ordered(); - const reading = reader.readFrame(); - hooks.dropTrackGroups(local.producer, 0n, 1n, { error: new NotFound("message unavailable") }); - const group = local.producer.createGroup(1n); - group.writeFrame(bytes(7, 1n)); - group.finish(); - local.producer.finish(); - await clock.advance(Duration.ZERO); - const frame = await reading; - expect(frame?.group).toBe(1n); - expect(frame?.payload).toEqual(bytes(7).payload); - expect(await reader.readFrame()).toBeUndefined(); -}); - -test("concurrent sessions between the same endpoints never reuse an identity", async () => { - const { a, b } = setup(); - const nextFirst = b.acceptTrack(); - const nextSecond = b.acceptTrack(); - const dialingFirst = a.dialTrack("bob"); - const dialingSecond = a.dialTrack("bob"); - const [incomingFirst, incomingSecond] = await Promise.all([nextFirst, nextSecond]); - expect(incomingFirst.session).not.toBe(incomingSecond.session); - const [remoteFirst, remoteSecond, localFirst, localSecond] = await Promise.all([ - incomingFirst.accept(), - incomingSecond.accept(), - dialingFirst, - dialingSecond, - ]); - const remotes = new Map([ - [remoteFirst.session, remoteFirst], - [remoteSecond.session, remoteSecond], - ]); - localFirst.producer.writeFrame(bytes(1)); - localSecond.producer.writeFrame(bytes(2)); - const first = remotes.get(localFirst.session); - const second = remotes.get(localSecond.session); - expect((await first?.consumer.ordered().readFrame())?.payload).toEqual(bytes(1).payload); - expect((await second?.consumer.ordered().readFrame())?.payload).toEqual(bytes(2).payload); -}); - -test("finished track sessions release their unlisted producers after both halves end", async () => { - const clock = time(); - const { local, remote } = await track(); - const localReader = local.consumer.ordered(); - const remoteReader = remote.consumer.ordered(); - local.producer.writeFrame(bytes(1)); - remote.producer.writeFrame(bytes(2)); - local.producer.finish(); - remote.producer.finish(); - expect((await localReader.readFrame())?.payload).toEqual(bytes(2).payload); - expect((await remoteReader.readFrame())?.payload).toEqual(bytes(1).payload); - expect(await localReader.readFrame()).toBeUndefined(); - expect(await remoteReader.readFrame()).toBeUndefined(); - await clock.advance(Duration.ZERO); - expect(hooks.trackReleased(local.producer).peek()).toBeNull(); - expect(hooks.trackReleased(remote.producer).peek()).toBeNull(); -}); - -test("late reads retain return-routability checks after the original dial closes", async () => { - const clock = time(); - const { origin, a, b, local, remote } = await track(); - const next = b.acceptTrack(); - void next.catch(() => {}); - const reset = new StreamError(StreamCode(70)); - const oldReader = local.consumer.ordered(); - remote.producer.abort(reset); - await expect(oldReader.readFrame()).rejects.toBeInstanceOf(Error); - const source = await broadcast(origin, ".server", "bob"); - let outcome: unknown; - const fetched = source - .track(`track/alice/bob/${local.session}`) - .fetchGroup(0n) - .then( - (value) => { - outcome = value; - }, - (error) => { - outcome = error; - }, - ); - try { - await clock.advance(Duration.ZERO); - const unverified = await next; - expect(unverified.session).toBe(local.session); - // A late query has the old name, but the original dial no longer answers. - // Verification must fail before a new producer can publish under that identity. - await expect(unverified.accept()).rejects.toBeInstanceOf(NotFound); - await fetched; - expect(outcome).toBeInstanceOf(Error); - const freshIncoming = b.acceptTrack(); - const dialing = a.dialTrack("bob"); - const incoming = await freshIncoming; - expect(incoming.session).not.toBe(local.session); - const remoteFresh = await incoming.accept(); - const localFresh = await dialing; - localFresh.producer.writeFrame(bytes(9)); - const frame = await remoteFresh.consumer.ordered().readFrame(); - expect(frame?.payload).toEqual(bytes(9).payload); - await expect(oldReader.readFrame()).rejects.toBeInstanceOf(Error); - } finally { - a.close(); - b.close(); - await fetched; - } -}); - -test("an endpoint announces only the roles it takes, and its client answers only its reserved sessions", async () => { - const { origin, a, b } = setup(); - await expect(a.dialTrack("alice")).rejects.toBeInstanceOf(Error); - expect(announced(origin, ".client", "alice")).toBeUndefined(); - const cancel = new AbortController(); - const dialing = a.dialTrack("bob", undefined, { signal: cancel.signal }); - const failure = dialing.catch((error: unknown) => error); - expect(announced(origin, ".client", "alice")).toBeDefined(); - expect(announced(origin, ".server", "alice")).toBeUndefined(); - const unsupported = await raw(origin, ".client", "alice", "track/bob/alice/not-a-dial"); - await expect(unsupported.info()).rejects.toBeInstanceOf(NotFound); - unsupported.close(); - - // Bob has not accepted yet, so the dial waits for him to serve. - const clock = time(); - await clock.advance(Duration.secs(3600)); - expect(announced(origin, ".server", "bob")).toBeUndefined(); - const incoming = b.acceptTrack(); - expect(announced(origin, ".server", "bob")).toBeDefined(); - expect(announced(origin, ".client", "bob")).toBeUndefined(); - expect((await incoming).peer).toBe("alice"); - cancel.abort(); - expect(await failure).toBeInstanceOf(Error); -}); - -test("dials in another protocol, tcp included, reset without entering the queue", async () => { - const { origin, b } = setup(); - const hold = new AbortController(); - const incoming = b.acceptTrack({ signal: hold.signal }); - const rejected = incoming.catch((error: unknown) => error); - for (const protocol of ["tcp", "quic"]) { - const subscriber = await raw(origin, ".server", "bob", `${protocol}/alice/bob/session-${protocol}`); - await expect(subscriber.info()).rejects.toBeInstanceOf(Error); - subscriber.close(); - } - hold.abort(); - expect(await rejected).toBeInstanceOf(Error); -}); - -test("reject refuses one dial and leaves another independent session usable", async () => { - const { a, b } = setup(); - const incoming = b.acceptTrack(); - const refused = a.dialTrack("bob"); - const refusal = refused.catch((error: unknown) => error); - (await incoming).reject(); - expect(await refusal).toBeInstanceOf(NotFound); - const again = b.acceptTrack(); - const dialing = a.dialTrack("bob"); - const remote = await (await again).accept(); - const local = await dialing; - local.producer.writeFrame(bytes(42)); - expect((await remote.consumer.ordered().readFrame())?.payload).toEqual(bytes(42).payload); -}); - -test("cancelled dials leave queues and cannot be accepted later", async () => { - const clock = time(); - const { a, b } = setup(); - const next = b.acceptTrack(); - const cancel = new AbortController(); - const reason = new Error("gave up"); - const dialing = a.dialTrack("bob", undefined, { signal: cancel.signal }); - const failure = dialing.catch((error: unknown) => error); - const incoming = await next; - cancel.abort(reason); - expect(await failure).toBe(reason); - await expect(incoming.accept()).rejects.toBeInstanceOf(Error); - // Queue a dial after the application stopped pulling, then abandon it. - const pending = new AbortController(); - const dropped = a.dialTrack("bob", undefined, { signal: pending.signal }); - const observed = dropped.catch((error: unknown) => error); - await clock.advance(Duration.ZERO); - pending.abort(); - expect(await observed).toBeInstanceOf(Error); - const newIncoming = b.acceptTrack(); - const fresh = a.dialTrack("bob"); - const accepted = await newIncoming; - expect(accepted.session).not.toBe(incoming.session); - await Promise.all([accepted.accept(), fresh]); -}); - -test("dial and accept cancellation before announcement release their routing holds", async () => { - const clock = time(); - const { origin, a, b } = setup(); - const cancel = new AbortController(); - const dialing = a.dialTrack("later", undefined, { signal: cancel.signal }); - const failure = dialing.catch((error: unknown) => error); - await clock.advance(Duration.secs(3600)); - cancel.abort(); - expect(await failure).toBeInstanceOf(Error); - const next = b.acceptTrack(); - const subscriber = await raw(origin, ".server", "bob", "track/absent/bob/never-announced"); - const accepted = (await next).accept(); - const acceptanceFailure = accepted.catch((error: unknown) => error); - await clock.advance(Duration.secs(3600)); - subscriber.close(); - expect(await acceptanceFailure).toBeInstanceOf(Error); -}); - -test("a dial waits for the peer to announce, without an arbitrary timeout", async () => { - const clock = time(); - const { origin, a } = setup(); - const dialing = a.dialTrack("later"); - await clock.advance(Duration.secs(3600)); - const later = new Socket.Endpoint(origin, Path.from("room"), "later"); - const incoming = await later.acceptTrack(); - const remote = await incoming.accept(); - const local = await dialing; - local.producer.writeFrame(bytes(1)); - expect((await remote.consumer.ordered().readFrame())?.payload).toEqual(bytes(1).payload); -}); - -test("accept waits for a delayed dialer announcement only while its original dial stays open", async () => { - const clock = time(); - const { origin, b } = setup(); - const next = b.acceptTrack(); - const subscriber = await raw(origin, ".server", "bob", "track/late/bob/delayed-announcement"); - const incoming = await next; - const acceptance = incoming.accept(); - await clock.advance(Duration.secs(3600)); - const route = origin.route(Path.from("room/.client/late"), { hidden: true }); - const answer = hooks.reserveUnlistedTrack(route.broadcast, "track/late/bob/delayed-announcement"); - route.announce(); - await clock.advance(Duration.ZERO); - expect(answer.demand().wanted.peek()).toBeDefined(); - const producer = answer.accept({ maxAge: null }); - const remote = await acceptance; - producer.writeFrame(bytes(3)); - expect((await remote.consumer.ordered().readFrame())?.payload).toEqual(bytes(3).payload); - subscriber.close(); -}); - -test("forged and replayed dials cannot pass return routability", async () => { - const { origin, a, b } = setup(); - // Alice dials elsewhere, so her client broadcast is there to refuse bob's check. - void a.dialTrack("carol").catch(() => {}); - for (const name of ["track/alice/bob/forged", "track/alice/someone-else/replayed"]) { - const cancel = new AbortController(); - const wait = b.acceptTrack({ signal: cancel.signal }); - void wait.catch(() => {}); - const subscriber = await raw(origin, ".server", "bob", name); - const refusal = subscriber.info().catch((error: unknown) => error); - if (name.includes("/bob/")) await expect((await wait).accept()).rejects.toBeInstanceOf(NotFound); - else cancel.abort(); - expect(await refusal).toBeInstanceOf(NotFound); - subscriber.close(); - } - a.close(); -}); - -test("session names are absent from listings while reserved and established", async () => { - const { origin, a, b } = setup(); - const next = b.acceptTrack(); - const dialing = a.dialTrack("bob"); - const incoming = await next; - const endpoints = [ - [".client", "alice"], - [".server", "bob"], - ] as const; - for (const [role, id] of endpoints) { - const listing = (await broadcast(origin, role, id)).tracks(); - expect(await listing.next()).toEqual({ kind: "synced" }); - listing.close(); - } - const remote = await incoming.accept(); - await dialing; - for (const [role, id] of endpoints) { - const listing = (await broadcast(origin, role, id)).tracks(); - expect(await listing.next()).toEqual({ kind: "synced" }); - listing.close(); - } - expect(remote.session).toBe(incoming.session); -}); - -test("a track reset or endpoint close fails both halves and leaves no open accepts", async () => { - for (const action of ["abort", "endpoint"] as const) { - const { a, b, local, remote } = await track(); - if (action === "abort") local.producer.abort(new StreamError(StreamCode(71))); - else a.close(); - await expect(remote.consumer.ordered().readFrame()).rejects.toBeInstanceOf(Error); - expect(await remote.producer.closed).toBeInstanceOf(Error); - const pending = b.acceptTrack(); - const failure = pending.catch((error: unknown) => error); - b.close(); - a.close(); - expect(await failure).toBeInstanceOf(Error); - } -}); - -test("endpoint ids refuse separators, and close is idempotent", async () => { - const { origin, a } = setup(); - for (const id of ["", "a/b", "a\\b"]) { - expect(() => new Socket.Endpoint(origin, Path.from("room"), id)).toThrow(); - await expect(a.dialTrack(id)).rejects.toBeInstanceOf(Error); - } - a.close(); - a.close(); - await expect(a.acceptTrack()).rejects.toBeInstanceOf(Error); - await expect(a.dialTrack("bob")).rejects.toBeInstanceOf(Error); -}); - -test(`a socket preserves an early answer burst over ${Lite.ALPN_07_WIP}`, async () => { - const alpn = Lite.ALPN_07_WIP; - const clock = time(); - const ao = new Origin.Producer(); - const bo = new Origin.Producer(); - origins.add(ao); - origins.add(bo); - const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); - const route = bo.route(Path.from("room/.server/bob"), { hidden: true }); - const requested = wireOf(route.broadcast).requested(); - route.announce(); - const pair = createMockTransportPair(alpn); - const url = new URL("https://localhost/socket"); - const [client, server] = await Promise.all([ - connect({ url, transport: pair.client, publish: ao.consume(), consume: ao }), - acceptConnection({ url, transport: pair.server, publish: bo.consume(), consume: bo }), - ]); - let returning: Track.Subscriber | undefined; - let drained: Promise | undefined; - const following = bo.follow(Path.from("room/.client/alice")); - try { - const dialing = a.dialTrack("bob"); - void dialing.catch(() => {}); - const request = await requested; - if (!request) throw new Error("missing dial"); - let caller = following.active.peek(); - while (!caller) { - await following.active.changed(); - caller = following.active.peek(); - } - returning = caller.track(request.name).subscribe({ - order: "ascending", - delay: { max: null }, - ranges: [{ start: { absolute: 0n } }], - }); - await returning.info(); - // The raw peer commits metadata only after writing its entire half, so - // SUBSCRIBE_OK carries the burst's last position, without a timing race. - hooks.keepRequest(request); - const staged = hooks.stageRequest(request, { maxAge: null }); - for (let i = 1; i <= 3; i++) staged.producer.writeFrame(bytes(i, BigInt(i - 1))); - staged.producer.finish(); - staged.commit(); - await clock.advance(Duration.ZERO); - const local = await dialing; - const reader = local.consumer.ordered(); - const received: number[] = []; - let completed = false; - let failure: unknown; - drained = (async () => { - for (let frame = await reader.readFrame(); frame; frame = await reader.readFrame()) - received.push(...frame.payload); - })().then( - () => { - completed = true; - }, - (error) => { - failure = error; - }, - ); - await clock.advance(Duration.ZERO); - expect(failure).toBeUndefined(); - expect(received).toEqual([1, 2, 3]); - expect(completed).toBe(true); - local.producer.finish(); - } finally { - returning?.close(); - following.close(); - a.close(); - route.broadcast.close(); - client.abort(); - server.abort(); - await drained; - } -}); - -for (const alpn of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18, Ietf.ALPN.DRAFT_20, Ietf.ALPN.DRAFT_22]) { - test(`a socket refusal reaches a routed dialer over ${alpn}`, async () => { - const clock = time(); - const ao = new Origin.Producer(); - const bo = new Origin.Producer(); - origins.add(ao); - origins.add(bo); - const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); - const b = new Socket.Endpoint(bo, Path.from("room"), "bob"); - const pair = createMockTransportPair(alpn); - const url = new URL("https://localhost/socket"); - const [client, server] = await Promise.all([ - connect({ url, transport: pair.client, publish: ao.consume(), consume: ao }), - acceptConnection({ url, transport: pair.server, publish: bo.consume(), consume: bo }), - ]); - let outcome: unknown; - const next = b.acceptTrack(); - const dialing = a.dialTrack("bob"); - const settled = dialing.then( - (stream) => { - outcome = stream; - }, - (error) => { - outcome = error; - }, - ); - try { - (await next).reject(); - await clock.advance(Duration.ZERO); - expect(outcome).toBeInstanceOf(Error); - } finally { - a.close(); - b.close(); - client.abort(); - server.abort(); - await settled; - } - }); -} - -for (const alpn of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18, Ietf.ALPN.DRAFT_20, Ietf.ALPN.DRAFT_22]) { - test(`a pending socket acceptance over ${alpn} has no arbitrary deadline`, async () => { - const clock = time(); - const ao = new Origin.Producer(); - const bo = new Origin.Producer(); - origins.add(ao); - origins.add(bo); - const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); - const b = new Socket.Endpoint(bo, Path.from("room"), "bob"); - const pair = createMockTransportPair(alpn); - const url = new URL("https://localhost/socket"); - const [client, server] = await Promise.all([ - connect({ url, transport: pair.client, publish: ao.consume(), consume: ao }), - acceptConnection({ url, transport: pair.server, publish: bo.consume(), consume: bo }), - ]); - const next = b.acceptTrack(); - let outcome: unknown; - const dialing = a.dialTrack("bob"); - const settled = dialing.then( - (value) => { - outcome = value; - }, - (error) => { - outcome = error; - }, - ); - try { - const incoming = await next; - await clock.advance(Duration.secs(3600)); - expect(outcome).toBeUndefined(); - const remote = await incoming.accept(); - const local = await dialing; - local.producer.writeFrame(bytes(7)); - const frame = await remote.consumer.ordered().readFrame(); - expect(frame?.payload).toEqual(bytes(7).payload); - } finally { - a.close(); - b.close(); - client.abort(); - server.abort(); - await settled; - } - }); -} - -// A moq-lite client session before 07 never carries the endpoint's hidden broadcast, so its peer -// could never find it. Its dials and accepts fail as unsupported instead of waiting, as in Rust. -test("a socket endpoint behind a lite-06 client fails its dials and accepts, not the server's", async () => { - const clock = time(); - const ao = new Origin.Producer(); - const bo = new Origin.Producer(); - origins.add(ao); - origins.add(bo); - const a = new Socket.Endpoint(ao, Path.from("room"), "alice"); - const b = new Socket.Endpoint(bo, Path.from("room"), "bob"); - // Already waiting before the session attaches, as when an endpoint's client reconnects. - const waiting = a.dialTrack("bob").then( - () => undefined, - (error: unknown) => error, - ); - const pair = createMockTransportPair(Lite.ALPN_06); - const url = new URL("https://localhost/socket"); - const [client, server] = await Promise.all([ - connect({ url, transport: pair.client, publish: ao.consume(), consume: ao }), - acceptConnection({ url, transport: pair.server, publish: bo.consume(), consume: bo }), - ]); - let heard = false; - const next = b.acceptTrack().then( - () => { - heard = true; - }, - () => {}, - ); - try { - const unsupported = /unreachable behind a moq-lite hop before 07/; - expect(((await waiting) as Error).message).toMatch(unsupported); - await expect(a.dialTrack("bob")).rejects.toThrow(unsupported); - await expect(a.acceptTrack()).rejects.toThrow(unsupported); - // The server's endpoint keeps accepting, and never heard a dial it can't answer. - await clock.advance(Duration.secs(3600)); - expect(heard).toBe(false); - - // The session's end releases the endpoint: an accept waits again. - client.abort(); - await client.closed; - let accepted: unknown; - const accepting = a.acceptTrack().then( - (value) => { - accepted = value; - }, - (error: unknown) => { - accepted = error; - }, - ); - await clock.advance(Duration.ZERO); - expect(accepted).toBeUndefined(); - a.close(); - await accepting; - } finally { - client.abort(); - server.abort(); - a.close(); - b.close(); - await next; - } -}); diff --git a/js/net/src/socket.ts b/js/net/src/socket.ts deleted file mode 100644 index e37e410d338..00000000000 --- a/js/net/src/socket.ts +++ /dev/null @@ -1,541 +0,0 @@ -/** - * Point-to-point `track` sessions between server and client endpoints: each half is a - * track, and each message a group. - * - * An endpoint's first dial announces its hidden client broadcast, `/.client/`, - * and its first accept its hidden server broadcast, `/.server/`, so ordinary - * grants decide which roles it may take. Sessions use the existing MoQ wire and need lite-07 (currently an explicit - * `moq-lite-07-wip` offer) or moq-transport on every hop. While the endpoint's origin - * has a moq-lite client session before 07, dials and accepts fail as unsupported. - * Their track names are secrets and are never listed. See the socket concept page. - * - * @module - */ -import { type Dispose, type GetPromise, type Getter, Once, Signal } from "@moq/signals"; -import { NotFound, StreamCode, StreamError } from "./error.ts"; -import { hooks } from "./internal.ts"; -import type * as Origin from "./origin.ts"; -import * as Path from "./path.ts"; -import type * as Track from "./track.ts"; -import { untilAborted } from "./util/abort.ts"; -import { isUnrequested as abandoned } from "./util/demand.ts"; -import { fromBytes } from "./util/hex.ts"; -import { wireOf } from "./wire.ts"; - -/** The protocol segment of a session's track name. */ -const PROTOCOL = "track"; -/** A server accepts dials, and a client dials. */ -type Role = "server" | "client"; -const SEGMENT: Record = { server: ".server", client: ".client" }; -// Every message, oldest first, from group 0, so a burst written before the answer is kept. -const reliable = (): Track.Subscription => ({ - order: "ascending", - delay: { max: null }, - ranges: [{ start: { absolute: 0n } }], -}); -const cancelled = () => new StreamError(StreamCode.Cancel, { message: "socket operation cancelled" }); -const ended = () => new StreamError(StreamCode.SessionClosed, { message: "socket endpoint closed" }); -// Unsupported has no registered stream code, as in Rust: it maps to Internal. -const unreachable = () => new Error("socket endpoint is unreachable behind a moq-lite hop before 07"); - -function segment(id: string): void { - if (!id || id.includes("/") || id.includes("\\")) throw new TypeError("socket id must be one plain segment"); -} - -function endpointPath(prefix: Path.Valid, role: Role, id: string): Path.Valid { - segment(id); - return Path.encode(Path.from(prefix, SEGMENT[role], id)); -} - -/** - * Whether `path` is an endpoint's broadcast, which only routes sessions and carries no - * media, so a worker that acts on every announced broadcast can skip it. - */ -export function isEndpoint(path: Path.Valid): boolean { - const parent = Path.parts(path).at(-2); - return Object.values(SEGMENT).some((role) => parent === role); -} - -function parse(name: string): { protocol: string; dialer: string; acceptor: string; session: string } | undefined { - const parts = name.split("/"); - if (parts.length !== 4 || parts.some((part) => !part || part.includes("\\"))) return; - const [protocol, dialer, acceptor, session] = parts; - return { protocol, dialer, acceptor, session }; -} - -// A cancellable signal wait removes its subscriptions even if the value never changes. -function waitFor(value: Getter, ready: (value: T) => boolean, signal: AbortSignal): Promise { - return new Promise((resolve, reject) => { - let dispose: Dispose = () => {}; - const finish = () => { - dispose(); - signal.removeEventListener("abort", abort); - }; - const abort = () => { - finish(); - reject(signal.reason); - }; - const check = () => { - const current = value.peek(); - if (!ready(current)) return; - finish(); - resolve(current); - }; - if (signal.aborted) return abort(); - dispose = value.subscribe(check); - signal.addEventListener("abort", abort, { once: true }); - check(); - }); -} - -/** Cancellation for a pending dial or accept; aborting after it resolves has no effect. */ -export interface Options { - /** Abandon this operation with the signal's reason. */ - signal?: AbortSignal; -} - -let makeTrack: (parts: Parts) => TrackStream; -let makeIncoming: (endpoint: Endpoint, request: Track.Request) => Incoming; -let accept: ( - endpoint: Endpoint, - request: Track.Request, - info: Track.InfoInit, - options?: Options, -) => Promise; - -/** A track session whose ascending reader skips unavailable groups, with no default publisher max age. */ -export class TrackStream { - /** This side's track; write groups as independent messages. */ - readonly producer: Track.Producer; - /** The peer's subscribed track; ordered() skips groups that are refused, dropped, or evicted. */ - readonly consumer: Track.Subscriber; - /** The peer endpoint's id, verified by accepting the dial. */ - readonly peer: string; - /** The fresh, secret session id chosen by the dialer. */ - readonly session: string; - - private constructor(parts: Parts) { - this.producer = parts.producer; - this.consumer = parts.consumer; - this.peer = parts.peer; - this.session = parts.session; - } - static { - makeTrack = (parts) => new TrackStream(parts); - } -} - -interface Parts { - producer: Track.Producer; - consumer: Track.Subscriber; - peer: string; - session: string; -} - -/** An unverified incoming request to accept (checking its original dial is still open) or reject. */ -export class Incoming { - /** The id the dialer claims; accept verifies that endpoint really dialed this session. */ - readonly peer: string; - /** The claimed session id; only accept verifies that its original dial is still open. */ - readonly session: string; - #endpoint: Endpoint; - #request?: Track.Request; - - private constructor(endpoint: Endpoint, request: Track.Request) { - const name = parse(request.name); - if (!name) throw new Error("invalid incoming socket name"); - this.peer = name.dialer; - this.session = name.session; - this.#endpoint = endpoint; - this.#request = request; - } - static { - makeIncoming = (endpoint, request) => new Incoming(endpoint, request); - } - - /** Accept while the original dial stays open; omitted info keeps every message with no publisher max age. */ - accept(info: Track.InfoInit = { maxAge: null }, options?: Options): Promise { - const request = this.#request; - this.#request = undefined; - if (!request) return Promise.reject(cancelled()); - return accept(this.#endpoint, request, info, options); - } - - /** Refuse this dial as not found; idempotent. */ - reject(): void { - this.#request?.reject(new NotFound("socket session")); - this.#request = undefined; - } -} - -class Queue { - dials = new Signal(new Map()); - close(): void { - for (const [request, dispose] of this.dials.peek()) { - dispose(); - request.reject(ended()); - } - this.dials.mutate((dials) => dials.clear()); - } -} - -/** - * A server, a client, or both: dials servers under the same prefix, and accepts their dials. - * - * Its first dial announces its client broadcast, which holds its half of each session it dials, - * and its first accept announces its server broadcast, where peers dial it. A pure client never - * announces a server broadcast, and until the first accept a dial to it waits as for a peer not - * online yet. - * - * Every hop between two endpoints must carry hidden broadcasts: moq-lite-07 or moq-transport. - * While the origin has a moq-lite client session before 07, whose peer never hears this - * endpoint, dials and accepts fail as unsupported. Nothing tells a dialer that its peer sits - * behind such a hop, so a dial to it waits as for a peer not announced yet. - */ -export class Endpoint { - /** This endpoint's id, a single plain segment. */ - readonly id: string; - /** The shared prefix containing the endpoints. */ - readonly prefix: Path.Valid; - /** Settles when the endpoint closes. */ - get closed(): GetPromise { - return this.#closed; - } - #closed = new Once(); - #abort = new AbortController(); - #origin: Origin.Producer; - #routes: Partial> = {}; - #queue = new Queue(); - #sessions = new Set(); - #unclose: Dispose[] = []; - #withheld: Getter; - - /** An endpoint on origin, which must also reach its peers; nothing is announced until it dials or accepts. */ - constructor(origin: Origin.Producer, prefix: Path.Valid, id: string) { - this.id = id; - this.prefix = Path.from(prefix); - for (const role of ["server", "client"] as const) endpointPath(this.prefix, role, id); - this.#origin = origin; - this.#withheld = hooks.hiddenWithheld(origin); - // Before the first dial or accept no route watches the origin, so watch its close here. - this.#unclose.push(origin.closed.subscribe(() => this.close())); - if (origin.closed.peek() !== undefined) this.close(); - } - - // This endpoint's broadcast in role, announced on first use. - #route(role: Role): Origin.Route { - const existing = this.#routes[role]; - if (existing) return existing; - const route = this.#origin.route(endpointPath(this.prefix, role, this.id)); - this.#routes[role] = route; - this.#unclose.push(hooks.onClosed(route.broadcast, () => this.close())); - return route; - } - - static { - accept = (endpoint, request, info, options) => endpoint.#accept(request, info, options); - } - - /** - * Dial the server peer in a track session, waiting for its announcement and acceptance until - * cancelled; omitted info keeps every message with no publisher max age. - */ - dialTrack(peer: string, info: Track.InfoInit = { maxAge: null }, options?: Options): Promise { - return this.#dial(peer, info, options); - } - /** Take the next track dial; the first call starts queueing dials until close, and announces the server. */ - async acceptTrack(options?: Options): Promise { - const abort = new AbortController(); - const signal = AbortSignal.any([this.#signal(options), abort.signal]); - const unwatch = this.#reachable(abort); - try { - signal.throwIfAborted(); - const queue = this.#queue; - if (!this.#routes.server) void this.#serve(this.#route("server")); - for (;;) { - await waitFor(queue.dials, (dials) => dials.size > 0, signal); - signal.throwIfAborted(); - const first = queue.dials.peek().entries().next().value; - if (!first) continue; - const [request, dispose] = first; - dispose(); - queue.dials.mutate((dials) => dials.delete(request)); - if (!abandoned(request)) return makeIncoming(this, request); - request.reject(cancelled()); - } - } finally { - unwatch(); - } - } - - // Abort with unsupported while a session withholds this endpoint from its peer, until disposed. - #reachable(abort: AbortController): Dispose { - const check = () => { - if (this.#withheld.peek() > 0) abort.abort(unreachable()); - }; - const unwatch = this.#withheld.subscribe(check); - check(); - return unwatch; - } - - async #serve(route: Origin.Route): Promise { - try { - for (;;) { - const request = await wireOf(route.broadcast).requested(); - if (!request) return; - const name = parse(request.name); - if (!name || name.acceptor !== this.id || name.dialer === this.id) { - request.reject(new NotFound("socket session")); - continue; - } - const queue = this.#queue; - if (name.protocol !== PROTOCOL) { - // Unsupported has no registered stream code, as in Rust: it maps to Internal. - request.reject(new Error("socket protocol is unsupported")); - continue; - } - const demand = request.demand(); - const prune = () => { - if (!abandoned(request)) return; - queue.dials.peek().get(request)?.(); - queue.dials.mutate((dials) => dials.delete(request)); - request.reject(cancelled()); - }; - const stop = hooks.requestWanted(request).subscribe(prune); - const release = hooks.demandReleased(demand).subscribe(prune); - queue.dials.mutate((dials) => - dials.set(request, () => { - stop(); - release(); - }), - ); - prune(); - } - } catch (error) { - this.#end(error instanceof Error ? error : new Error(String(error))); - } - } - - #signal(options?: Options): AbortSignal { - return options?.signal ? AbortSignal.any([this.#abort.signal, options.signal]) : this.#abort.signal; - } - - // Subscribe to the session's half in the peer's broadcast, its server or client one by role. - async #remote( - role: Role, - peer: string, - name: string, - signal: AbortSignal, - hold: (following: Origin.Following) => void, - ): Promise<{ consumer: Track.Subscriber; peer: GetPromise }> { - const following = this.#origin.follow(endpointPath(this.prefix, role, peer)); - hold(following); - const abort = new AbortController(); - const stop = following.closed.subscribe(() => abort.abort(following.closed.peek() ?? ended())); - if (following.closed.peek() !== undefined) abort.abort(following.closed.peek() ?? ended()); - const pending = AbortSignal.any([signal, abort.signal]); - try { - const broadcast = await waitFor(following.active, (active) => active !== undefined, pending); - if (!broadcast) throw ended(); - return { - consumer: broadcast.track(name).subscribe(reliable()), - peer: broadcast.closed, - }; - } finally { - stop(); - } - } - - async #dial(peer: string, info: Track.InfoInit, options?: Options): Promise { - segment(peer); - if (peer === this.id) throw new Error("dialing the same socket endpoint is unsupported"); - const abort = new AbortController(); - const signal = AbortSignal.any([this.#signal(options), abort.signal]); - signal.throwIfAborted(); - const session = fromBytes(crypto.getRandomValues(new Uint8Array(16))); - const name = `${PROTOCOL}/${this.id}/${peer}/${session}`; - const answer = hooks.reserveUnlistedTrack(this.#route("client").broadcast, name); - let following: Origin.Following | undefined; - let consumer: Track.Subscriber | undefined; - let producer: Track.Producer | undefined; - let stop: Dispose = () => {}; - const unreached = this.#reachable(abort); - try { - const remote = await this.#remote("server", peer, name, signal, (value) => { - following = value; - }); - consumer = remote.consumer; - stop = hooks.watchSubscriberError(consumer, (error) => abort.abort(error)); - const serving = waitFor(hooks.requestWanted(answer), (wanted) => wanted, signal).then(() => { - producer = answer.accept(info); - }); - await untilAborted(Promise.all([serving, consumer.info()]), signal); - signal.throwIfAborted(); - if (!producer || !following) throw cancelled(); - return this.#open({ producer, consumer, peer, session }, following, remote.peer); - } catch (error) { - answer.reject(error instanceof Error ? error : cancelled()); - consumer?.close(); - following?.close(); - throw error; - } finally { - stop(); - unreached(); - abort.abort(cancelled()); - } - } - - async #accept(request: Track.Request, info: Track.InfoInit, options?: Options): Promise { - const name = parse(request.name); - if (!name) throw cancelled(); - const abort = new AbortController(); - const signal = AbortSignal.any([this.#signal(options), abort.signal]); - const demand = request.demand(); - const check = () => { - if (abandoned(request)) abort.abort(cancelled()); - }; - const stop = hooks.requestWanted(request).subscribe(check); - const release = hooks.demandReleased(demand).subscribe(check); - const unreached = this.#reachable(abort); - let following: Origin.Following | undefined; - let consumer: Track.Subscriber | undefined; - let stopError: Dispose = () => {}; - try { - check(); - signal.throwIfAborted(); - const remote = await this.#remote("client", name.dialer, request.name, signal, (value) => { - following = value; - }); - consumer = remote.consumer; - stopError = hooks.watchSubscriberError(consumer, (error) => abort.abort(error)); - await untilAborted(consumer.info(), signal); - signal.throwIfAborted(); - if (!following) throw cancelled(); - hooks.keepRequest(request); - return this.#open( - { producer: request.accept(info), consumer, peer: name.dialer, session: name.session }, - following, - remote.peer, - ); - } catch (error) { - request.reject(error instanceof Error ? error : cancelled()); - consumer?.close(); - following?.close(); - throw error; - } finally { - stop(); - release(); - unreached(); - stopError(); - } - } - - #open(parts: Parts, following: Origin.Following, peer: GetPromise): TrackStream { - this.#abort.signal.throwIfAborted(); - if (following.active.peek()?.closed !== peer) throw ended(); - const session = new Session(parts, following, peer, () => this.#sessions.delete(session)); - this.#sessions.add(session); - try { - return session.track(); - } catch (error) { - session.reset(error instanceof Error ? error : cancelled()); - throw error; - } - } - - /** Reset every held session, cancel pending operations, and retract the endpoint's broadcasts; idempotent. */ - close(): void { - this.#end(null); - } - - #end(error: Error | null): void { - if (this.#closed.peek() !== undefined) return; - this.#closed.set(error); - for (const unclose of this.#unclose) unclose(); - this.#abort.abort(error ?? ended()); - this.#queue.close(); - for (const session of this.#sessions) session.reset(ended()); - for (const route of Object.values(this.#routes)) route.broadcast.close(); - } -} - -// Owns the routing hold and monitors both halves, releasing the session once both end. -class Session { - #parts: Parts; - #following: Origin.Following; - #peer: GetPromise; - #done: Dispose; - #abort = new AbortController(); - #stops: Dispose[] = []; - #sent = false; - #received = false; - constructor(parts: Parts, following: Origin.Following, peer: GetPromise, done: Dispose) { - this.#parts = parts; - this.#following = following; - this.#peer = peer; - this.#done = done; - } - - #hold(stop: Dispose): void { - if (this.#abort.signal.aborted) stop(); - else this.#stops.push(stop); - } - - #monitor(): void { - // A socket reset ends the session even when a generic routed reader would recover. - this.#hold(hooks.watchSubscriberError(this.#parts.consumer, (error) => this.reset(error))); - const check = () => { - if (this.#following.active.peek()?.closed !== this.#peer) this.reset(ended()); - }; - this.#hold(this.#following.active.subscribe(check)); - check(); - } - - #watch(value: GetPromise, done: () => void): void { - const check = (error: Error | null | undefined) => { - if (error === undefined) return; - if (error) this.reset(error); - else { - done(); - this.#complete(); - } - }; - this.#hold(value.subscribe(check)); - check(value.peek()); - } - - track(): TrackStream { - this.#monitor(); - this.#watch(this.#parts.producer.closed, () => { - this.#sent = true; - }); - this.#watch(this.#parts.consumer.closed, () => { - this.#received = true; - }); - return makeTrack(this.#parts); - } - - #complete(): void { - if (this.#sent && this.#received) this.#close(false); - } - - reset(error: Error): void { - if (this.#abort.signal.aborted) return; - this.#parts.producer.abort(error); - this.#parts.consumer.close(error); - this.#close(true); - } - - #close(reset: boolean): void { - if (this.#abort.signal.aborted) return; - this.#abort.abort(); - for (const stop of this.#stops) stop(); - this.#stops.length = 0; - // A settled end retains its cache for existing readers, while releasing the - // broadcast's unlisted reservation instead of accumulating completed sessions. - this.#parts.producer.close(); - if (reset) this.#parts.consumer.close(); - this.#following.close(); - this.#done(); - } -} diff --git a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/socket/Socket.kt b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/socket/Socket.kt deleted file mode 100644 index 0d8a719aa59..00000000000 --- a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/socket/Socket.kt +++ /dev/null @@ -1,16 +0,0 @@ -package dev.moq.socket - -/** - * A server, a client, or both: its first dial announces it as a client, and its first accept as a - * server. - */ -typealias Endpoint = uniffi.moq.MoqSocketEndpoint - -/** An incoming track dial, consumed by accepting or rejecting it. */ -typealias IncomingTrack = uniffi.moq.MoqSocketIncomingTrack - -/** Whether [path] is an endpoint's broadcast, which only routes sessions and carries no media. */ -fun isEndpoint(path: String): Boolean = uniffi.moq.moqSocketIsEndpoint(path) - -/** A track session with producer and reliably subscribed consumer halves. */ -typealias TrackStream = uniffi.moq.MoqSocketTrackStream diff --git a/py/moq-rs/README.md b/py/moq-rs/README.md index d0031a0af79..76b7803a124 100644 --- a/py/moq-rs/README.md +++ b/py/moq-rs/README.md @@ -251,29 +251,6 @@ same either way. Opaque bytes go on a plain `TrackProducer`. Every handle whose cleanup is `cancel()` is an async context manager, so exiting `async with` releases it: the consumers (`media.CatalogConsumer`, `media.TrackConsumer`, `media.GroupConsumer`, `TrackConsumer`, `audio.Consumer`, `video.Consumer`, `GroupConsumer`, `json.SnapshotConsumer`, `json.StreamConsumer`, `json.WindowConsumer`, `AnnounceConsumer`, `Following`) and the dynamic sources (`Route`, `Claim`, `BroadcastRequests`, `TrackRequests`). -### Sockets - -`from moq import socket` exposes `socket.Endpoint(origin, prefix, id)`. Use the -same origin as the session's publish and consume sides. Hold the endpoint while -sessions run, or use it as an async context manager. Its first dial announces -`/.client/` and its first accept `/.server/`, so grants -decide whether it may be a client, a server, or both; keep each session id private. - -- `await endpoint.dial_track(peer, info=None)` dials a server, and `await endpoint.accept_track()` takes the next dial. -- An incoming dial has `peer` and `session`; `await incoming.accept(info=None)` verifies - return routability, and `incoming.reject()` refuses it. -- `TrackStream` exposes `producer: TrackProducer`, `consumer: TrackConsumer`, `peer`, - and `session`. Its subscription starts reliable and ascending, and omitted - `info` keeps messages without a publisher age limit. Hold or cancel the consumer - to retain or release that subscription. A message no hop will deliver, such as - one a cache reclaimed after 30 s unread or a refused fetch, is skipped rather - than ending the session. - -Cancelling a dial or its acceptance resets that dial. Cancelling an accept wait -leaves the endpoint listening. `endpoint.close()` resets sessions and pending dials. -The [socket convention](https://doc.moq.dev/concept/socket) needs lite-07 or -moq-transport on every relay hop; this API does not add pre-07 capability detection. - ### Origin (advanced) - **`OriginProducer(*, cache_capacity_bytes=None)`**. Manage broadcast announcements. Set `cache_capacity_bytes` to bound cached groups under this origin. diff --git a/py/moq-rs/benches/ffi.py b/py/moq-rs/benches/ffi.py index 20aed450a48..5b4daa83b75 100644 --- a/py/moq-rs/benches/ffi.py +++ b/py/moq-rs/benches/ffi.py @@ -108,64 +108,8 @@ def call(name: str, fn: Callable[[], object], results: list[dict]) -> None: results.append({"name": f"call/{name}", "unit": "ns", "value": (time.perf_counter() - start) * 1e9 / calls}) -async def sockets(results: list[dict]) -> None: - """Sweep listening endpoints and simultaneous sessions per endpoint separately.""" - for endpoints in (1, 4, 16): - for sessions in (1, 4, 16): - origin = moq.OriginProducer() - client = moq.socket.Endpoint(origin, "bench", "client") - servers = [moq.socket.Endpoint(origin, "bench", f"server-{i}") for i in range(endpoints)] - - async def drain(stream: moq.socket.TrackStream) -> None: - assert [[frame.payload async for frame in group] async for group in stream.consumer] == [[b"echo"]] - stream.producer.finish() - - async def accept(server: moq.socket.Endpoint) -> None: - readers = [] - for _ in range(sessions): - incoming = await server.accept_track() - readers.append(asyncio.create_task(drain(await incoming.accept()))) - await asyncio.gather(*readers) - - async def dial(server: moq.socket.Endpoint) -> None: - stream = await client.dial_track(server.id) - stream.producer.write_frame(b"echo", moq.Timestamp.unscaled(0)) # Scale-less: the first group is key 0. - stream.producer.finish() - assert [item async for item in stream.consumer] == [] - - async def step() -> None: - accepts = [asyncio.create_task(accept(server)) for server in servers] - # Each accept reaches its first suspension before any dial can run. - polled = asyncio.get_running_loop().create_future() - asyncio.get_running_loop().call_soon(polled.set_result, None) - await polled - await asyncio.wait_for( - asyncio.gather(*(dial(server) for server in servers for _ in range(sessions)), *accepts), - ROUND_TIMEOUT, - ) - - for _ in range(WARMUP): - await step() - start, rounds = time.perf_counter(), 0 - while time.perf_counter() - start < BUDGET: - await step() - rounds += 1 - elapsed = time.perf_counter() - start - results.append( - { - "name": f"socket/track/endpoints={endpoints}/sessions={sessions}", - "unit": "sessions/s", - "value": rounds * endpoints * sessions / elapsed, - } - ) - client.close() - for server in servers: - server.close() - - async def main() -> None: results: list[dict] = [] - await sockets(results) for case in CASES: await throughput(case, results) diff --git a/py/moq-rs/docs/index.md b/py/moq-rs/docs/index.md index b39489453c6..5530f9682b2 100644 --- a/py/moq-rs/docs/index.md +++ b/py/moq-rs/docs/index.md @@ -267,20 +267,3 @@ Rust side ([`moq-ffi`](https://crates.io/crates/moq-ffi)). self ``` - -## Sockets - -`moq.socket` connects endpoints through relays, using the same origin for both -publishing and consuming. See the [socket convention](https://doc.moq.dev/concept/socket). - -```{eval-rst} -.. currentmodule:: moq.socket - -.. autosummary:: - :toctree: api - :nosignatures: - - Endpoint - IncomingTrack - TrackStream -``` diff --git a/py/moq-rs/moq/__init__.py b/py/moq-rs/moq/__init__.py index 0098af0db11..8a80139079e 100644 --- a/py/moq-rs/moq/__init__.py +++ b/py/moq-rs/moq/__init__.py @@ -9,7 +9,6 @@ # Layer namespaces stay out of __all__; json would shadow the stdlib module. from . import json as json -from . import socket as socket from . import video as video from .client import Client, CongestionControl, connect, versions from .datagram import DatagramConsumer, DatagramProducer, DatagramRequest, DatagramRequests diff --git a/py/moq-rs/moq/socket.py b/py/moq-rs/moq/socket.py deleted file mode 100644 index adf625770c2..00000000000 --- a/py/moq-rs/moq/socket.py +++ /dev/null @@ -1,119 +0,0 @@ -"""Point-to-point track sessions between server and client endpoints.""" - -from __future__ import annotations - -from moq_ffi import ( - MoqSocketEndpoint, - MoqSocketIncomingTrack, - MoqSocketTrackStream, - moq_socket_is_endpoint, -) - -from ._records import _track_info -from .origin import OriginProducer -from .publish import TrackProducer -from .subscribe import TrackConsumer -from .types import TrackInfo - -__all__ = ["Endpoint", "IncomingTrack", "TrackStream", "is_endpoint"] - - -def is_endpoint(path: str) -> bool: - """Whether ``path`` is an endpoint's broadcast, which only routes sessions and carries no media.""" - return moq_socket_is_endpoint(path) - - -class Endpoint: - """A server, a client, or both: dials or accepts track sessions. - - Its first dial announces it as a client, and its first accept as a server. - Hold it while sessions run. Closing it resets pending dials and sessions. - Cancelling a dial cancels only that dial; cancelling an accept wait leaves - the endpoint listening. Usable as an async context manager. - """ - - def __init__(self, origin: OriginProducer, prefix: str, id: str) -> None: - self._inner = MoqSocketEndpoint(origin._inner, prefix, id) - - @property - def id(self) -> str: - """This endpoint's id.""" - return self._inner.id() - - @property - def prefix(self) -> str: - """The prefix beneath which this endpoint finds peers.""" - return self._inner.prefix() - - async def __aenter__(self) -> Endpoint: - return self - - async def __aexit__(self, *exc) -> None: - self.close() - - def close(self) -> None: - """Retract the endpoint's broadcasts and reset its pending dials and sessions.""" - self._inner.close() - - async def dial_track(self, peer: str, *, info: TrackInfo | None = None) -> TrackStream: - """Dial a track session; omitted info keeps messages without an age limit.""" - return TrackStream(await self._inner.dial_track(peer, _track_info(info))) - - async def accept_track(self) -> IncomingTrack: - """Start listening for tracks and return the next incoming track dial.""" - return IncomingTrack(await self._inner.accept_track()) - - -class IncomingTrack: - """An incoming track dial, consumed by accepting or rejecting it.""" - - def __init__(self, inner: MoqSocketIncomingTrack) -> None: - self._inner = inner - - @property - def peer(self) -> str: - """The claimed peer id; accepting verifies return routability.""" - return self._inner.peer() - - @property - def session(self) -> str: - """The random session id, which must stay private to its endpoints.""" - return self._inner.session() - - async def accept(self, *, info: TrackInfo | None = None) -> TrackStream: - """Accept after verifying that the peer really dialed this session.""" - return TrackStream(await self._inner.accept(_track_info(info))) - - def reject(self) -> None: - """Refuse this dial as not found.""" - self._inner.reject() - - -class TrackStream: - """A track session's two halves and its peer and session identities.""" - - def __init__(self, inner: MoqSocketTrackStream) -> None: - self._producer = TrackProducer(inner.producer()) - self._consumer = TrackConsumer(inner.consumer()) - self._peer = inner.peer() - self._session = inner.session() - - @property - def producer(self) -> TrackProducer: - """This side's write half.""" - return self._producer - - @property - def consumer(self) -> TrackConsumer: - """The peer's read half, retaining its reliable ascending subscription.""" - return self._consumer - - @property - def peer(self) -> str: - """The peer endpoint's id.""" - return self._peer - - @property - def session(self) -> str: - """The random session id, which must stay private to its endpoints.""" - return self._session diff --git a/py/moq-rs/tests/test_socket.py b/py/moq-rs/tests/test_socket.py deleted file mode 100644 index 1895e3360c5..00000000000 --- a/py/moq-rs/tests/test_socket.py +++ /dev/null @@ -1,194 +0,0 @@ -"""Socket lifetime and cancellation tests use explicit task barriers, without timers.""" - -import asyncio -import gc -import subprocess -import sys -from collections.abc import Coroutine -from typing import Any, TypeVar, cast - -import moq -import pytest -from moq import socket - -T = TypeVar("T") - - -async def start(coro: Coroutine[Any, Any, T]) -> asyncio.Task[T]: - """Return after the coroutine has reached its first suspension.""" - loop = asyncio.get_running_loop() - polled = loop.create_future() - - async def run() -> T: - loop.call_soon(polled.set_result, None) - return await coro - - task = asyncio.create_task(run()) - await polled - return task - - -def endpoints() -> tuple[socket.Endpoint, socket.Endpoint]: - origin = moq.OriginProducer() - return socket.Endpoint(origin, "room", "a"), socket.Endpoint(origin, "room", "b") - - -async def connect(a: socket.Endpoint, b: socket.Endpoint) -> tuple[socket.TrackStream, socket.TrackStream]: - incoming = await start(b.accept_track()) - dial = await start(a.dial_track(b.id)) - accepted = await (await incoming).accept() - return await dial, accepted - - -async def test_refusal_and_cancelled_wait_leave_endpoint_usable(): - a, b = endpoints() - pending = await start(b.accept_track()) - pending.cancel() - with pytest.raises(asyncio.CancelledError): - await pending - dial = await start(a.dial_track("b")) - incoming = await b.accept_track() - assert incoming.peer == "a" - incoming.reject() - with pytest.raises(moq.Error.NotFound): - await dial - with pytest.raises(moq.Error.AlreadyResponded): - incoming.reject() - client, server = await connect(a, b) - assert client.session == server.session - a.close() - b.close() - - -async def test_cancelled_dial_releases_the_original_subscription(): - a, b = endpoints() - incoming = await start(b.accept_track()) - dial = await start(a.dial_track("b")) - offered = await incoming - dial.cancel() - with pytest.raises(asyncio.CancelledError): - await dial - with pytest.raises(moq.Error.Cancelled): - await offered.accept() - client, server = await connect(a, b) - assert client.session == server.session - a.close() - b.close() - - -async def test_abort_and_endpoint_close_are_errors(): - a, b = endpoints() - client, server = await connect(a, b) - client.producer.abort(17) - with pytest.raises(moq.Error.Protocol) as reset: - await server.consumer.read_frame() - assert reset.value.details.kind == moq.ProtocolKind.APP - assert reset.value.details.code == 64 + 17 - client, server = await connect(a, b) - a.close() - with pytest.raises(moq.Error.Closed): - await server.consumer.read_frame() - b.close() - - -async def test_track_default_retains_ascending_messages_and_subscription(): - a, b = endpoints() - client, server = await connect(a, b) - assert (a.id, a.prefix, client.peer, server.peer) == ("a", "room", "b", "a") - assert client.session == server.session - assert len(client.session) == 32 - producer, reverse, consumer = client.producer, server.producer, server.consumer - del server - gc.collect() - assert consumer.info().max_age is None - for start, payload in ((1, b"later"), (0, b"first")): - group = producer.create_group(start) - group.write_frame(payload, moq.Timestamp.unscaled(start)) - group.finish() - producer.finish() - assert [(group.start, [frame.payload async for frame in group]) async for group in consumer] == [ - (0, [b"first"]), - (1, [b"later"]), - ] - reverse.finish() - assert await client.consumer.read_frame() is None - a.close() - b.close() - - -async def test_a_dial_in_another_protocol_is_unsupported(): - origin = moq.OriginProducer() - endpoint = socket.Endpoint(origin, "room", "server") - waiting = await start(endpoint.accept_track()) - broadcast = await origin.consume().request("room/.server/server") - with pytest.raises(moq.Error.Unsupported): - await broadcast.subscribe_track("tcp/client/server/0123456789abcdef0123456789abcdef") - assert not waiting.done() - endpoint.close() - with pytest.raises(moq.Error.Closed): - await waiting - - -def test_a_dial_waits_for_a_late_peer_without_a_deadline(): - subprocess.run([sys.executable, __file__, "clock"], check=True) - - -async def late_peer(clock): - origin = moq.OriginProducer() - async with socket.Endpoint(origin, "room", "a") as a: - dial = await start(a.dial_track("b")) - clock.advance(24 * 60 * 60) - async with socket.Endpoint(origin, "room", "b") as b: - incoming = await b.accept_track() - accepted = await incoming.accept() - connected = await dial - assert connected.session == accepted.session - connected.producer.finish() - accepted.producer.finish() - assert await connected.consumer.read_frame() is None - assert await accepted.consumer.read_frame() is None - - -async def test_cancelling_acceptance_drops_the_original_dial_while_proving_routability(): - origin = moq.OriginProducer() - endpoint = socket.Endpoint(origin, "room", "server") - forged = origin.route("room/.client/forged", hidden=True) - returning = forged.broadcast.requests() - forged.announce() - incoming = await start(endpoint.accept_track()) - broadcast = await origin.consume().request("room/.server/server") - original = await start(broadcast.subscribe_track("track/forged/server/0123456789abcdef0123456789abcdef")) - offered = await incoming - accepting = await start(offered.accept()) - # Proof that accept is parked on the return path, before cancelling it. - returned = await returning.requested_track() - assert returned.name.startswith("track/forged/server/") - accepting.cancel() - with pytest.raises(asyncio.CancelledError): - await accepting - with pytest.raises(moq.Error.Cancelled): - await original - endpoint.close() - forged.broadcast.close() - - -async def test_dropping_the_endpoint_resets_a_live_session(): - a, b = endpoints() - client, server = await connect(a, b) - del a - gc.collect() - # Withdrawal and the session's cancellation can reach the reader first; the - # Rust contract guarantees a reset, with no clean EOF or promised cause. - reset = cast(type[Exception], moq.Error) - with pytest.raises(reset): - await server.consumer.read_frame() - # The peer's own half stays open until it ends it, here by closing. - b.close() - with pytest.raises(reset): - await client.consumer.read_frame() - - -if __name__ == "__main__": - from clock import Clock - - asyncio.run(late_peer(Clock())) diff --git a/quest/m0/test-flakes-3/py-client-body-error.md b/quest/m0/test-flakes-3/py-client-body-error.md new file mode 100644 index 00000000000..1904938e2e4 --- /dev/null +++ b/quest/m0/test-flakes-3/py-client-body-error.md @@ -0,0 +1,25 @@ +# [S] moq-rs's client body-error test is cancelled under load + +## Goal + +`py/moq-rs/tests/test_server.py`'s `test_client_context_keeps_the_body_error` +passes every loaded `just check`. + +## Plan + +Found 2026-11-17 by `just check` on +[moq-dev/moq#9029](https://github.com/moq-dev/moq/pull/9029), which deletes +socket endpoints and touches nothing the test uses. Under the loaded check the +body's `ZeroDivisionError` was replaced by `MoqError.Cancelled: cancelled` +raised from a uniffi call inside the `async with moq.Client(...)` body. Run +alone it passed 6 times in a row. + +So under load the client's session is cancelled before the body raises, and a +call in the body sees that cancel first. Trace which call raises it (the +announcement read or the subscribe) and why the session is cancelled before +the context exits: if the client cancels its session early, such as when its +connect races the server's accept, that is a bug in `py/moq-rs` or moq-ffi, +fixed there with a regression test. Only if the trace shows the test reads a +session it does not yet hold is the fix in the test, by the epic's rules. + +Public API: none. Wire: none. diff --git a/quest/m1/README.md b/quest/m1/README.md index f51cf4c82f7..a16f2224987 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -41,7 +41,7 @@ Decided 2026-11-12 (maintainer, /quest-audit before the cut): 2026-11-13, below, and 2026-11-14 (maintainer): byte streams moved to Kixel, which builds its own on moq-net's public API, and sockets dropped their `tcp` protocol. Socket endpoints followed on 2026-11-16 (maintainer, - /quest-plan of the pre-cut audit): [they leave moq](/quest/m2/sockets/README.md) + /quest-plan of the pre-cut audit): [they leave moq](https://github.com/moq-dev/moq/pull/9029) for Kixel before the cut. - ✅ Also moved here: the two reconnect fixes whose bugs came from PRs that are on `main` and not on `release`, so the cut would publish them; the Go diff --git a/quest/m2/README.md b/quest/m2/README.md index 0f156eb979f..6f865e1c9d2 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -36,7 +36,7 @@ removal, a rename, or a wire, catalog, or format change that is free until [switch](/quest/m2/multicast/switch.md) also settles the public route API and retires the moq-cluster draft. - **moq-net API.** [Session codes](/quest/m2/session-codes.md). - [Socket endpoints](/quest/m2/sockets/README.md) leave for Kixel, and the + [Socket endpoints](https://github.com/moq-dev/moq/pull/9029) leave for Kixel, and the demos return to their earlier format. - **Unused API.** [One sweep per area](/quest/m2/unused-api/README.md), each item re-checked for a downstream caller first. diff --git a/quest/m2/sockets/README.md b/quest/m2/sockets/README.md deleted file mode 100644 index e43b9b6725d..00000000000 --- a/quest/m2/sockets/README.md +++ /dev/null @@ -1,56 +0,0 @@ -# Socket endpoints leave moq for Kixel - -## Goal - -The cut publishes no socket endpoint API. `moq_net::socket`, `@moq/net`'s -`Socket`, and the socket handles of moq-ffi, moq-c, and every wrapper are -gone, with their concept page and interop lane, and the two demos that used -them, MoQ Boy and CARLA, send input the way they did before sockets: the -viewer publishes a broadcast and the publisher follows it. Kixel, the one -downstream that builds on sockets, carries its own copy on moq-net's public -API. - -## Plan - -Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): ✅ revert -the socket endpoint and put it in Kixel, and go back to the old wire format -for CARLA and MoQ Boy (rejected: confirming sockets as a moq feature, which -was the audit's recommendation; keeping them and trimming the leftover -`_track` vocabulary). AGENTS.md puts work a large live-media platform would -not use in Kixel. Byte streams and the sockets' `tcp` protocol already moved -there on 2026-11-14 -([moq-dev/moq#8772](https://github.com/moq-dev/moq/pull/8772)), which left -one downstream consumer, two demos, and a wrapper in every binding. - -Facts found by the audit: - -- Sockets are a naming convention over public API: a hidden broadcast per - endpoint (`/.server/`, `/.client/`) and a track - each way per session. `rs/moq-net/src/model/socket.rs` (1,150 lines) uses - one crate-private call, `reserve_unlisted_track` - (`rs/moq-net/src/model/broadcast.rs:406`). -- The surface that leaves: `js/net/src/socket.ts` (541 lines, 694 of tests), - `rs/moq-ffi/src/socket.rs` (318), `rs/moq-c/src/socket.rs` (511), a wrapper - in Python, Swift, Kotlin, Dart, and Go, `doc/concept/socket.md` (225), and - `test/interop/socket.sh` with four clients. -- It was built by the m1 socket epic between 2026-10-28 and 2026-11-15: - [moq-dev/moq#7677](https://github.com/moq-dev/moq/pull/7677), - [#7883](https://github.com/moq-dev/moq/pull/7883), - [#7884](https://github.com/moq-dev/moq/pull/7884), - [#8115](https://github.com/moq-dev/moq/pull/8115), - [#8468](https://github.com/moq-dev/moq/pull/8468), - [#8741](https://github.com/moq-dev/moq/pull/8741), - [#8825](https://github.com/moq-dev/moq/pull/8825), and the demo ports - [#8008](https://github.com/moq-dev/moq/pull/8008) and - [#8011](https://github.com/moq-dev/moq/pull/8011). None of it is on - `release`, so nothing published breaks. - -Order: the two demos move first, each in its own PR, and -[the removal](/quest/m2/sockets/remove.md) lands once nothing in the -repository dials or accepts. Kixel ports the Rust endpoint from history, from the -removal PR's parent commit, in [its own quest](https://github.com/kixel-ai/kixel.ai/blob/main/quest/m0/mesh/sockets.md), -and needs `reserve_unlisted_track` and the session cleanup [the removal](/quest/m2/sockets/remove.md) lists made public; it ports no JS. - -## Related - -- [moq.dev's hosted demos run the cut](/quest/m3/release/demos-deploy.md) - deploys the demos these children restore diff --git a/quest/m2/sockets/remove.md b/quest/m2/sockets/remove.md deleted file mode 100644 index 40cd134e7e0..00000000000 --- a/quest/m2/sockets/remove.md +++ /dev/null @@ -1,109 +0,0 @@ -# [M] moq-net, @moq/net, and the bindings have no socket endpoints - -## Goal - -No package this repository publishes has a socket endpoint: `moq_net::socket` -is deleted from Rust, `Socket` from `@moq/net`, the socket handles from -moq-ffi and moq-c, and the wrapper from Python, Swift, Kotlin, Dart, and Go, -with `doc/concept/socket.md`, the interop sockets lane, and -`socket::is_endpoint` in every language. What exists in moq-net only for -sockets goes with them, except the one private call Kixel's copy needs, which -becomes public. - -## Plan - -Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): ✅ socket -endpoints leave for Kixel -([the epic](/quest/m2/sockets/README.md) has the reasons and the rejected -options). `is_endpoint`, which the unused-API sweep had listed for the FFI, -leaves here instead. - -Delete, as listed at `80ca9787e3`: - -- Rust: `rs/moq-net/src/model/socket.rs` and its `pub mod socket` - (`rs/moq-net/src/model/mod.rs:7`), `rs/moq-net/tests/integration/socket.rs`, - `rs/moq-relay/tests/integration/socket_auth.rs`, and the CHANGELOG and - `doc/lib/rs/moq-net.md` entries. -- JS: `js/net/src/socket.ts`, `socket.test.ts`, `js/net/bench/socket.ts`, - the `Socket` export (`js/net/src/index.ts:39`), `js/net/api/index.api.md`, - and `doc/lib/js/net.md`. -- Bindings: `rs/moq-ffi/src/socket.rs` (and the `MoqSocketEndpoint` entry in - `rs/moq-ffi/uniffi.toml`), `rs/moq-c/src/socket.rs` and its `moq.h` - functions, `py/moq-rs/moq/socket.py` with `tests/test_socket.py` and the - `sockets` bench in `benches/ffi.py`, `swift/Sources/Moq/Socket.swift`, - `kt/moq/.../socket/Socket.kt`, `dart/moq/lib/socket.dart`, - `go/wrapper/socket/`, their tests (`rs/moq-ffi/src/test.rs`, - `rs/moq-c/src/test/parity.rs`), README and docs mentions - (`rs/moq-c/README.md`, `py/moq-rs/README.md`, `doc/lib/py/index.md`), and - the generated Dart file's socket classes. -- Interop: `test/interop/socket.sh`, the `--sockets` lane and `run_sockets` - in `test/interop/interop.sh`, `clients/rust/socket.rs`, - `clients/rust/socket-js.rs`, `clients/js-native/socket.ts`, - `clients/python/sockets.py`, the `interop-socket` and `interop-socket-js` - examples in `rs/moq-cli/Cargo.toml`, and the README's sockets paragraph. -- Docs: `doc/concept/socket.md`, its sidebar entry - (`doc/.vitepress/config.ts:120`), and its line in `doc/concept/index.md`. -- Each package's `MIGRATE.md` entries that describe a socket API change - (`rs/moq-ffi`, `rs/moq-tokio`, `js/net`, `py/moq-rs`, `kt/moq`, `cpp/moq`): - nothing published ever had sockets, so the entries go rather than gain a - removal note. Check `origin/release` for each before deleting it. - -What exists only for sockets, to check before deleting: - -- `broadcast::Producer::reserve_unlisted_track` - (`rs/moq-net/src/model/broadcast.rs:406`), `pub(super)`: a track a peer - who knows its name finds, never announced or listed. Sockets are its one - caller. Kixel's copy is built on moq-net's public API and needs it. - Decided 2026-11-16 (maintainer, kixel.ai's unblock triage): ✅ make - it public, with [Kixel's socket endpoint](https://github.com/kixel-ai/kixel.ai/blob/main/quest/m0/mesh/sockets.md) - as its named consumer (rejected: deleting it, which puts Kixel's session - tracks in track listings). Its doc says unlisted controls discovery, not - authorization, and a moq-net test that outlives the socket tests shows an - accepted reservation is readable by name and absent from track listings. -- `track::Producer::weak` and `TrackWeak` (`rs/moq-net/src/model/track.rs`), - `pub(super)`: the endpoint keeps a weak handle to each open session's - track (`socket.rs:236`) so closing the endpoint aborts its sessions - without holding them open, then aborts what is left before withdrawing - its broadcasts, so subscribers see a cancel rather than an unroutable - route. Kixel's port needs the same cleanup. Holding strong producers is no - substitute: a held producer never auto-closes, which is what the pruning - watches for. Recommended: publish a narrow non-owning handle that reports - whether its track closed and cancels it, rather than `TrackWeak` whole; - its cancel keeps the listing contract `Producer::abort` has (unlist first), - or works only on unlisted tracks. Tests pin that a held handle keeps no - dropped track alive. -- `origin::Consumer::hidden_withheld` and the hold a pre-lite-07 client - session takes on it (`rs/moq-net/src/lite/session.rs:311-315`), which make - a dial fail `Unsupported` while a hop cannot carry hidden broadcasts. - Sockets are its one reader (`socket.rs:442`). Delete it: Kixel's port - dials lite-07 only and needs none. -- Hidden (`.`-named) broadcasts themselves are not socket-only: HLS, the CLI, - and `RouteOptions::hidden()` use them - (`rs/moq-hls/src/server/mod.rs:191`, `doc/concept/identity.md:196`). They - stay. -- `rs/moq-net/src/auth.rs` tests use `.socket` only as a pattern string; - they stay. -- JS keeps nothing for Kixel: it ports only the Rust endpoint, and its two - JS users exchange plain broadcasts instead (decided 2026-11-16, same - triage). The `@moq/net` internals `socket.ts` reads, such as - `hiddenWithheld` and `requestWanted`, go or stay as their other callers - need. - -For the PR: - -- Start by grepping for `socket::`, `Socket.Endpoint`, `MoqSocket`, - `moq_socket_`, `dial_track`, `accept_track`, `.server/`, and `.client/` - across `rs`, `js`, `py`, `swift`, `kt`, `dart`, `go`, `cpp`, `demo`, - `test`, and `doc`; the list above is that grep on the base, and both demos - must already be off it. -- Record the PR's parent commit in the PR body: it is the reference Kixel - ports from. -- Run `just test interop --all` (moq-ffi changes) and `just api-diff`. - -Public API: breaking for `main`-only API in every language; nothing on -`release` has sockets. Adds `reserve_unlisted_track` and a non-owning track cancellation handle. Wire: none -(sockets were a path and track naming convention). - -## Related - -- [moq-net publishes no item without a caller](/quest/m2/unused-api/net.md) - the sweep this keeps `is_endpoint` out of diff --git a/quest/m2/unused-api/README.md b/quest/m2/unused-api/README.md index 32c63dac309..fc342c0eb24 100644 --- a/quest/m2/unused-api/README.md +++ b/quest/m2/unused-api/README.md @@ -31,7 +31,7 @@ reaches them when a binding user needs it, so the sweep mirrors nothing new into moq-ffi. Not part of the sweep, because each has its own quest: socket endpoints -([removed for Kixel](/quest/m2/sockets/README.md)), the public +([removed for Kixel](https://github.com/moq-dev/moq/pull/9029)), the public route API ([the route switch](/quest/m2/multicast/switch.md)). Public API: each child lists what leaves. Wire: none. diff --git a/quest/m3/README.md b/quest/m3/README.md index 9385bd8c7da..bb6314f8acf 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -36,7 +36,7 @@ to [m1](/quest/m1/README.md), since the cut no longer waits on m1. The mesh libraries came back 2026-11-13, below; byte streams moved to Kixel 2026-11-14. The mesh libraries left moq for Kixel on 2026-11-14 (maintainer): moq keeps what a large live-media platform would use. Socket endpoints, the first of -those prerequisites, [leave for Kixel too](/quest/m2/sockets/README.md) +those prerequisites, [leave for Kixel too](https://github.com/moq-dev/moq/pull/9029) (2026-11-16). Decided 2026-11-12 (maintainer, /quest-audit before the cut), from a read of diff --git a/quest/m3/release/demos-deploy.md b/quest/m3/release/demos-deploy.md index 1b93400839f..01cdcc913c0 100644 --- a/quest/m3/release/demos-deploy.md +++ b/quest/m3/release/demos-deploy.md @@ -16,7 +16,7 @@ chosen: an m2 cut gate; deploying now in m0. Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): ✅ the demos go back to their format before sockets, since -[socket endpoints leave moq](/quest/m2/sockets/README.md) (rejected: the +[socket endpoints leave moq](https://github.com/moq-dev/moq/pull/9029) (rejected: the 2026-11-06 plan, which deployed socket grants and lite-07 on every hop for them). This quest was "moq.dev's hosted demos run on sockets" until then. diff --git a/rs/kio/src/producer.rs b/rs/kio/src/producer.rs index 0c16220840e..267527322f9 100644 --- a/rs/kio/src/producer.rs +++ b/rs/kio/src/producer.rs @@ -183,6 +183,11 @@ impl Producer { self.state.poll_closed(waiter) } + /// Whether the channel closed. + pub fn is_closed(&self) -> bool { + self.state.is_closed() + } + /// Whether any consumer handle exists right now. /// /// A point-in-time snapshot with no registration; use diff --git a/rs/moq-c/README.md b/rs/moq-c/README.md index 9d813c19bc4..64639d38090 100644 --- a/rs/moq-c/README.md +++ b/rs/moq-c/README.md @@ -194,21 +194,6 @@ int32_t moq_consume_track_frame(uint32_t frame, moq_frame *dst); int32_t moq_consume_track_frame_free(uint32_t frame); int32_t moq_consume_track_cancel(uint32_t track); -// Sockets: two-way sessions between endpoints sharing an origin -int32_t moq_socket_create(uint32_t origin, const char *prefix, uintptr_t prefix_len, const char *id, uintptr_t id_len); -int32_t moq_socket_is_endpoint(const char *path, uintptr_t path_len); // 1, 0, or a negative code -int32_t moq_socket_close(uint32_t endpoint); -int32_t moq_socket_dial_track(uint32_t endpoint, const char *peer, uintptr_t peer_len, const moq_track_info *info, moq_status_callback on_stream, void *user_data); -int32_t moq_socket_accept_track(uint32_t endpoint, moq_status_callback on_incoming, void *user_data); -int32_t moq_socket_cancel(uint32_t task); -int32_t moq_socket_incoming_info(uint32_t incoming, moq_string *peer, moq_string *session); -int32_t moq_socket_incoming_accept(uint32_t incoming, const moq_track_info *info, moq_status_callback on_stream, void *user_data); -int32_t moq_socket_incoming_reject(uint32_t incoming); -int32_t moq_socket_stream_info(uint32_t stream, moq_string *peer, moq_string *session); -int32_t moq_socket_stream_writer(uint32_t stream); // a raw track handle -int32_t moq_socket_stream_reader(uint32_t stream, moq_status_callback on_frame, void *user_data); // read like moq_consume_track -int32_t moq_socket_stream_free(uint32_t stream); - // Codecs without a broadcast: encode, then publish or store the packets yourself int32_t moq_audio_encoder_create(const moq_audio_encoder_output *output); int32_t moq_audio_encoder_frame_size(uint32_t encoder); diff --git a/rs/moq-c/src/consume.rs b/rs/moq-c/src/consume.rs index 4df7971f972..4ee4c69e90b 100644 --- a/rs/moq-c/src/consume.rs +++ b/rs/moq-c/src/consume.rs @@ -946,32 +946,6 @@ impl Consume { } } - /// Read a socket stream's peer half as a raw track reader. - /// - /// It keeps the reliable, oldest-first subscription the socket opened it with until - /// [`Self::raw_track_update`] changes it. - pub(crate) fn socket_read( - &mut self, - half: moq_net::track::Subscriber, - driver: crate::ffi::Background, - on_frame: OnStatus, - ) -> Result { - let (task, close) = Task::new(on_frame); - let control = RawControl::Live(half.control()); - let id = self.raw_task.insert(RawTaskEntry { task, control })?; - - tokio::spawn(async move { - let res = crate::ffi::run(driver.watch(Self::run_raw(on_frame, half.ordered(), close))).await; - - let entry = State::lock().consume.raw_task.remove(id); - if let Some(entry) = entry { - entry.task.callback.call(res); - } - }); - - Ok(id) - } - pub fn raw_track_update( &mut self, track: Id, diff --git a/rs/moq-c/src/lib.rs b/rs/moq-c/src/lib.rs index fa295f1a871..ec3126526d3 100644 --- a/rs/moq-c/src/lib.rs +++ b/rs/moq-c/src/lib.rs @@ -34,7 +34,6 @@ mod origin; mod publish; mod server; mod session; -mod socket; mod state; mod task; mod video; @@ -49,7 +48,6 @@ pub use encoder::*; pub use error::*; pub use ffi::{moq_follow_callback, moq_state_callback, moq_status_callback}; pub use id::*; -pub use socket::*; pub use video::*; pub(crate) use client::*; diff --git a/rs/moq-c/src/publish.rs b/rs/moq-c/src/publish.rs index 3480e84357f..a93385f908d 100644 --- a/rs/moq-c/src/publish.rs +++ b/rs/moq-c/src/publish.rs @@ -94,9 +94,8 @@ enum Family { Open { /// Handles that may still publish, ended on close. open: HashSet, - /// Finished producers, kept so a late subscriber still finds their tracks, or `None` for - /// a family that drops them on finish. - kept: Option>>, + /// Finished producers, kept so a late subscriber still finds their tracks. + kept: Vec>, }, /// The broadcast closed: its children ended and new ones are refused. Closed, @@ -124,16 +123,7 @@ impl Children { pub(crate) fn new() -> Self { Self(Arc::new(std::sync::Mutex::new(Family::Open { open: HashSet::new(), - kept: Some(Vec::new()), - }))) - } - - /// A family that drops each finished producer instead of keeping it until the close, for - /// children no late subscriber looks up, such as a socket endpoint's streams. - pub(crate) fn unkept() -> Self { - Self(Arc::new(std::sync::Mutex::new(Family::Open { - open: HashSet::new(), - kept: None, + kept: Vec::new(), }))) } @@ -164,31 +154,20 @@ impl Children { } /// Keep `child`'s finished producer until the broadcast ends, or drop it now if it already - /// has or the family keeps none. + /// has. pub(crate) fn keep(&self, child: Child, producer: impl Send + 'static) { let producer: Box = Box::new(producer); let mut family = self.lock(); if let Family::Open { open, kept } = &mut *family { open.remove(&child); - if let Some(kept) = kept { - kept.push(producer); - return; - } + kept.push(producer); + return; } // Dropped outside the lock: the last producer's drop tears its track down. drop(family); drop(producer); } - /// How many children are open and how many finished producers are kept. - #[cfg(test)] - pub(crate) fn counts(&self) -> (usize, usize) { - match &*self.lock() { - Family::Open { open, kept } => (open.len(), kept.as_ref().map_or(0, Vec::len)), - Family::Closed => (0, 0), - } - } - /// Whether the broadcast closed, ending its children. pub(crate) fn is_closed(&self) -> bool { matches!(*self.lock(), Family::Closed) @@ -1212,28 +1191,6 @@ impl Publish { children.insert(&mut self.tracks, raw, Child::Track) } - /// Hand a socket stream's `track` half to C as a raw track handle, a child of the - /// endpoint's `children` like a track is of its broadcast. - pub(crate) fn socket_track(&mut self, track: moq_net::track::Producer, children: &Children) -> Result { - let raw = Kept { - inner: track, - children: children.clone(), - }; - children.insert(&mut self.tracks, raw, Child::Track) - } - - /// End a closed endpoint's open `track` halves, as a broadcast's close ends its tracks, - /// and let go of its finished ones. - pub(crate) fn socket_close(&mut self, children: &Children) { - for child in children.close() { - if let Child::Track(id) = child - && let Some(track) = self.tracks.remove(id) - { - track.abort(moq_net::Error::Unroutable); - } - } - } - /// Append the next group to a raw track, starting at `start`, returning a group producer. pub fn track_group(&mut self, track: Id, start: moq_net::Timestamp) -> Result { let raw = self.tracks.get_mut(track).ok_or(Error::TrackNotFound)?; diff --git a/rs/moq-c/src/socket.rs b/rs/moq-c/src/socket.rs deleted file mode 100644 index a415c3a3d95..00000000000 --- a/rs/moq-c/src/socket.rs +++ /dev/null @@ -1,511 +0,0 @@ -//! Socket endpoints: two-way sessions between endpoints sharing an origin. -//! -//! A session's halves are each a whole track: write with [crate::moq_publish_track_group] and -//! read it like [crate::moq_consume_track]. - -use std::ffi::{c_char, c_void}; -use std::sync::Arc; - -use moq_net::socket::{Endpoint, Incoming, TrackStream}; -use tokio::sync::oneshot; - -use crate::ffi::{self, Background, OnStatus}; -use crate::publish::Children; -use crate::task::Task; -use crate::{Error, Id, NonZeroSlab, State, moq_string, moq_track_info}; - -/// Socket endpoints and the sessions they dial or accept. -#[derive(Default)] -pub struct Socket { - endpoints: NonZeroSlab, - /// Dials, accept loops and accepts in progress. - tasks: NonZeroSlab, - incoming: NonZeroSlab, - streams: NonZeroSlab, -} - -struct EndpointEntry { - inner: Arc, - driver: Background, - /// The endpoint's halves handed to C, ended when it closes. - children: Children, -} - -struct IncomingEntry { - inner: Incoming, - peer: String, - session: String, - driver: Background, - children: Children, -} - -struct StreamEntry { - peer: String, - session: String, - /// This side's half, until C takes it. - writer: Option, - /// The peer's half, until C reads it through [Consume](crate::Consume). - reader: Option, - driver: Background, - children: Children, -} - -impl StreamEntry { - fn new(stream: TrackStream, driver: Background, children: Children) -> Self { - let (peer, session) = (stream.peer().to_string(), stream.session().to_string()); - Self { - peer, - session, - writer: Some(stream.producer), - reader: Some(stream.consumer), - driver, - children, - } - } -} - -/// Deliver `id` and end, or end without one when `close` fires first. -async fn deliver( - callback: OnStatus, - close: oneshot::Receiver<()>, - pending: impl Future>, -) -> Result<(), Error> { - // `biased` so a pending close always wins over a ready stream. - let stream = tokio::select! { - biased; - _ = close => return Ok(()), - res = pending => res?, - }; - let id = State::lock().socket.streams.insert(stream)?; - callback.call(Ok(id)); - Ok(()) -} - -impl Socket { - /// The open and kept counts of an endpoint's children. - #[cfg(test)] - pub(crate) fn endpoint_children(&self, id: Id) -> (usize, usize) { - self.endpoints.get(id).expect("endpoint").children.counts() - } - - fn endpoint(&self, id: Id) -> Result<&EndpointEntry, Error> { - let endpoint = self.endpoints.get(id).ok_or(Error::NotFound)?; - endpoint.driver.check()?; - Ok(endpoint) - } - - fn create(&mut self, origin: &crate::origin::Producer, prefix: &str, id: &str) -> Result { - let inner = Endpoint::new(origin, prefix, id)?; - self.endpoints.insert(EndpointEntry { - inner: Arc::new(inner), - driver: origin.driver.clone(), - children: Children::unkept(), - }) - } - - fn dial( - &mut self, - endpoint: Id, - peer: &str, - info: Option, - callback: OnStatus, - ) -> Result { - let EndpointEntry { - inner, - driver, - children, - } = self.endpoint(endpoint)?; - let (driver, children) = (driver.clone(), children.clone()); - let watch = driver.clone(); - let dialing = inner.dial_track(peer, info)?; - let pending = async move { Ok(StreamEntry::new(dialing.await?, driver, children)) }; - self.tasks.spawn( - callback, - |state| &mut state.socket.tasks, - |close| async move { watch.watch(deliver(callback, close, pending)).await }, - ) - } - - fn accept(&mut self, endpoint: Id, callback: OnStatus) -> Result { - let EndpointEntry { - inner, - driver, - children, - } = self.endpoint(endpoint)?; - let (inner, driver, children) = (inner.clone(), driver.clone(), children.clone()); - let poll = move |waiter: &kio::Waiter| inner.poll_accept_track(waiter); - // The first poll starts listening, so poll once before returning: a dial sent after - // this call then queues for the loop instead of finding nobody listening. - let mut first = match poll(&kio::Waiter::noop()) { - std::task::Poll::Ready(Err(err)) if !matches!(err, moq_net::Error::Closed) => return Err(err.into()), - std::task::Poll::Ready(res) => Some(res), - std::task::Poll::Pending => None, - }; - self.tasks.spawn( - callback, - |state| &mut state.socket.tasks, - |mut close| async move { - let watch = driver.clone(); - let run = async move { - loop { - // `biased` so a pending close always wins over a ready dial, the one the - // first poll buffered included. - let next = tokio::select! { - biased; - _ = &mut close => return Ok(()), - res = async { - match first.take() { - Some(res) => res, - None => kio::wait(&poll).await, - } - } => res, - }; - let incoming = match next { - Ok(incoming) => incoming, - // The endpoint closed: no more dials will arrive. - Err(moq_net::Error::Closed) => return Ok(()), - Err(err) => return Err(err.into()), - }; - let entry = IncomingEntry { - peer: incoming.peer().to_string(), - session: incoming.session().to_string(), - inner: incoming, - driver: driver.clone(), - children: children.clone(), - }; - let id = State::lock().socket.incoming.insert(entry)?; - callback.call(Ok(id)); - } - }; - watch.watch(run).await - }, - ) - } - - fn incoming_accept( - &mut self, - incoming: Id, - info: Option, - callback: OnStatus, - ) -> Result { - self.incoming.get(incoming).ok_or(Error::NotFound)?.driver.check()?; - let IncomingEntry { - inner, - driver, - children, - .. - } = self.incoming.remove(incoming).ok_or(Error::NotFound)?; - let watch = driver.clone(); - let accepting = inner.accept(info); - let pending = async move { Ok(StreamEntry::new(accepting.await?, driver, children)) }; - self.tasks.spawn( - callback, - |state| &mut state.socket.tasks, - |close| async move { watch.watch(deliver(callback, close, pending)).await }, - ) - } - - fn incoming_info( - &self, - incoming: Id, - peer: Option<&mut moq_string>, - session: Option<&mut moq_string>, - ) -> Result<(), Error> { - let entry = self.incoming.get(incoming).ok_or(Error::NotFound)?; - write_ids(&entry.peer, &entry.session, peer, session); - Ok(()) - } - - fn stream_info( - &self, - stream: Id, - peer: Option<&mut moq_string>, - session: Option<&mut moq_string>, - ) -> Result<(), Error> { - let entry = self.streams.get(stream).ok_or(Error::NotFound)?; - write_ids(&entry.peer, &entry.session, peer, session); - Ok(()) - } -} - -fn write_ids(peer: &str, session: &str, peer_dst: Option<&mut moq_string>, session_dst: Option<&mut moq_string>) { - let view = |s: &str| moq_string { - data: s.as_ptr().cast::(), - len: s.len(), - }; - if let Some(dst) = peer_dst { - *dst = view(peer); - } - if let Some(dst) = session_dst { - *dst = view(session); - } -} - -/// Open a socket endpoint named `id` under `prefix` on an origin, announcing it to the peers -/// reachable through that origin. -/// -/// `id` names the endpoint: a well-known name for a server, or a random one for a client. It -/// must be one path segment. Peers find each other under the same `prefix`, so connect the -/// origin to a session (or share it locally) before dialing. Close it with [moq_socket_close]. -/// -/// Returns a positive endpoint handle, or a negative code, such as for an `id` that is not one -/// path segment. -/// -/// # Safety -/// - `prefix` must point at `prefix_len` bytes, and `id` at `id_len` bytes. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_create( - origin: u32, - prefix: *const c_char, - prefix_len: usize, - id: *const c_char, - id_len: usize, -) -> i32 { - ffi::enter(move || { - let origin = ffi::parse_id(origin)?; - let prefix = unsafe { ffi::parse_str(prefix, prefix_len)? }; - let id = unsafe { ffi::parse_str(id, id_len)? }; - let mut state = State::lock(); - let origin = state.origin.get(origin)?.clone(); - state.socket.create(&origin, prefix, id) - }) -} - -/// Whether `path` is a socket endpoint's broadcast, which only routes sessions and carries no -/// media, so a worker that acts on every announced broadcast can skip it. -/// -/// Returns 1 if it is, 0 if not, or a negative code for a `path` that is not a valid path. -/// -/// # Safety -/// - `path` must point at `path_len` bytes. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_is_endpoint(path: *const c_char, path_len: usize) -> i32 { - ffi::enter(move || { - let path = unsafe { ffi::parse_str(path, path_len)? }; - let path = moq_net::Path::new(path).map_err(moq_net::Error::from)?; - Ok(i32::from(moq_net::socket::is_endpoint(&path))) - }) -} - -/// Close a socket endpoint and release its handle. -/// -/// Withdraws the endpoint, ends its accept loops with a terminal `0`, fails its pending dials -/// and accepts, and resets its open sessions, aborting each half still open. Handles -/// already delivered stay valid until released. -/// -/// Returns zero, or a negative code if the endpoint is unknown. -#[unsafe(no_mangle)] -pub extern "C" fn moq_socket_close(endpoint: u32) -> i32 { - ffi::enter(move || { - let endpoint = ffi::parse_id(endpoint)?; - let mut state = State::lock(); - let entry = state.socket.endpoints.remove(endpoint).ok_or(Error::NotFound)?; - entry.inner.close(); - state.publish.socket_close(&entry.children); - Ok(()) - }) -} - -/// Dial the endpoint `peer` in a new session, whose halves are whole tracks. -/// -/// `info` describes this side's half, or NULL to keep every message until read, with no -/// max age. -/// -/// Waits for as long as the peer takes to accept, including for its endpoint to be announced. -/// `on_stream` fires with a positive stream handle once the peer accepts, then exactly once -/// more with a terminal code: `0` (including after [moq_socket_cancel], which gives up and -/// resets the session) or a negative error, `MOQ_ERROR_NOT_FOUND` when the peer rejects it -/// and `MOQ_ERROR_UNSUPPORTED` for a dial to this endpoint itself or across a hop that cannot -/// carry it. Release `user_data` at the terminal callback. -/// -/// Returns a positive task handle, or a negative code. -/// -/// # Safety -/// - `peer` must point at `peer_len` bytes, and `info` be NULL or point at a [moq_track_info]. -/// - `on_stream` must be non-NULL, and `user_data` valid until its terminal callback. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_dial_track( - endpoint: u32, - peer: *const c_char, - peer_len: usize, - info: *const moq_track_info, - on_stream: ffi::moq_status_callback, - user_data: *mut c_void, -) -> i32 { - ffi::enter(move || { - let endpoint = ffi::parse_id(endpoint)?; - let peer = unsafe { ffi::parse_str(peer, peer_len)? }; - let info = unsafe { info.as_ref() }.map(TryInto::try_into).transpose()?; - let callback = unsafe { OnStatus::new(user_data, on_stream)? }; - State::lock().socket.dial(endpoint, peer, info, callback) - }) -} - -/// Accept dials from peers. -/// -/// `on_incoming` fires with a positive incoming handle for each dial, then exactly once more -/// with a terminal code: `0` once the endpoint closed or after [moq_socket_cancel], or a -/// negative error, `MOQ_ERROR_UNSUPPORTED` while no peer can reach the endpoint. Accept each -/// dial with [moq_socket_incoming_accept] or refuse it with [moq_socket_incoming_reject]. Dials -/// queue until delivered, or until their dialer gives up. -/// -/// Returns a positive task handle, or a negative code. -/// -/// # Safety -/// - `on_incoming` must be non-NULL, and `user_data` valid until its terminal callback. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_accept_track( - endpoint: u32, - on_incoming: ffi::moq_status_callback, - user_data: *mut c_void, -) -> i32 { - ffi::enter(move || { - let endpoint = ffi::parse_id(endpoint)?; - let callback = unsafe { OnStatus::new(user_data, on_incoming)? }; - State::lock().socket.accept(endpoint, callback) - }) -} - -/// Stop a dial, an accept loop, or an incoming accept. -/// -/// Its callback still fires once more with a terminal `0`; release `user_data` there. A -/// stopped dial or accept resets its session. Returns zero, or a negative code if it already -/// ended. -#[unsafe(no_mangle)] -pub extern "C" fn moq_socket_cancel(task: u32) -> i32 { - ffi::enter(move || { - let task = ffi::parse_id(task)?; - State::lock().socket.tasks.close(task, Error::NotFound) - }) -} - -/// Read an incoming dial's claimed peer id and session id. -/// -/// Either output may be NULL. The strings borrow the handle until it is accepted or rejected. -/// -/// # Safety -/// - `peer` and `session` must each be NULL or point at a writable [moq_string]. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_incoming_info( - incoming: u32, - peer: *mut moq_string, - session: *mut moq_string, -) -> i32 { - ffi::enter(move || { - let incoming = ffi::parse_id(incoming)?; - let (peer, session) = unsafe { (peer.as_mut(), session.as_mut()) }; - State::lock().socket.incoming_info(incoming, peer, session) - }) -} - -/// Accept an incoming dial, releasing its handle. -/// -/// Verifies the dialer really dialed this session first. `info` describes this side's half, or -/// NULL to keep every message until read. `on_stream` fires like [moq_socket_dial_track]'s, and -/// [moq_socket_cancel] gives up and resets the session. -/// -/// Returns a positive task handle, or a negative code. -/// -/// # Safety -/// - `info` must be NULL or point at a [moq_track_info]. -/// - `on_stream` must be non-NULL, and `user_data` valid until its terminal callback. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_incoming_accept( - incoming: u32, - info: *const moq_track_info, - on_stream: ffi::moq_status_callback, - user_data: *mut c_void, -) -> i32 { - ffi::enter(move || { - let incoming = ffi::parse_id(incoming)?; - let info = unsafe { info.as_ref() }.map(TryInto::try_into).transpose()?; - let callback = unsafe { OnStatus::new(user_data, on_stream)? }; - State::lock().socket.incoming_accept(incoming, info, callback) - }) -} - -/// Refuse an incoming dial as not found, releasing its handle. -#[unsafe(no_mangle)] -pub extern "C" fn moq_socket_incoming_reject(incoming: u32) -> i32 { - ffi::enter(move || { - let incoming = ffi::parse_id(incoming)?; - let entry = State::lock().socket.incoming.remove(incoming).ok_or(Error::NotFound)?; - entry.inner.reject(); - Ok(()) - }) -} - -/// Read a stream's peer endpoint id and session id. -/// -/// Either output may be NULL. The strings borrow the stream handle until [moq_socket_stream_free]. -/// -/// # Safety -/// - `peer` and `session` must each be NULL or point at a writable [moq_string]. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_stream_info(stream: u32, peer: *mut moq_string, session: *mut moq_string) -> i32 { - ffi::enter(move || { - let stream = ffi::parse_id(stream)?; - let (peer, session) = unsafe { (peer.as_mut(), session.as_mut()) }; - State::lock().socket.stream_info(stream, peer, session) - }) -} - -/// Take this side's half of a stream, which outlives the stream handle: a raw track handle, -/// written and ended with the `moq_publish_track_*` calls. -/// -/// Returns the positive handle, or a negative code once taken. -#[unsafe(no_mangle)] -pub extern "C" fn moq_socket_stream_writer(stream: u32) -> i32 { - ffi::enter(move || { - let stream = ffi::parse_id(stream)?; - let mut state = State::lock(); - let entry = state.socket.streams.get_mut(stream).ok_or(Error::NotFound)?; - let children = entry.children.clone(); - let track = entry.writer.take().ok_or(Error::Moq(moq_net::Error::Closed))?; - state.publish.socket_track(track, &children) - }) -} - -/// Read the peer's half of a stream, which outlives the stream handle; callable once. -/// -/// Works like [crate::moq_consume_track]: `on_frame` fires with a positive frame handle for each -/// frame in order, read with [crate::moq_consume_track_frame] and released with -/// [crate::moq_consume_track_frame_free], then exactly once more with a terminal code: `0` once -/// the peer finished (or after [crate::moq_consume_track_cancel]), or a negative error when it -/// reset. It is subscribed reliably and oldest first; [crate::moq_consume_track_update] changes -/// that. -/// -/// Returns a positive track handle, or a negative code once taken. -/// -/// # Safety -/// - `on_frame` must be non-NULL, and `user_data` valid until its terminal callback. -#[unsafe(no_mangle)] -pub unsafe extern "C" fn moq_socket_stream_reader( - stream: u32, - on_frame: ffi::moq_status_callback, - user_data: *mut c_void, -) -> i32 { - ffi::enter(move || { - let stream = ffi::parse_id(stream)?; - let callback = unsafe { OnStatus::new(user_data, on_frame)? }; - let mut state = State::lock(); - let entry = state.socket.streams.get_mut(stream).ok_or(Error::NotFound)?; - let driver = entry.driver.clone(); - let half = entry.reader.take().ok_or(Error::Moq(moq_net::Error::Closed))?; - state.consume.socket_read(half, driver, callback) - }) -} - -/// Release a stream handle. -/// -/// A half not yet taken is dropped: an unread reader stops reading. Taken halves are -/// unaffected. -#[unsafe(no_mangle)] -pub extern "C" fn moq_socket_stream_free(stream: u32) -> i32 { - ffi::enter(move || { - let stream = ffi::parse_id(stream)?; - let entry = State::lock().socket.streams.remove(stream).ok_or(Error::NotFound)?; - drop(entry); - Ok(()) - }) -} diff --git a/rs/moq-c/src/state.rs b/rs/moq-c/src/state.rs index 92743a9486a..90d71f053b5 100644 --- a/rs/moq-c/src/state.rs +++ b/rs/moq-c/src/state.rs @@ -3,7 +3,6 @@ use std::sync::{Arc, LazyLock, Mutex, MutexGuard, PoisonError}; use crate::auth::Auth; use crate::bandwidth::Bandwidth; use crate::encoder::Encoders; -use crate::socket::Socket; use crate::{Consume, Origin, Publish, Server, Session, audio::Audio, video::Video}; pub struct State { @@ -17,7 +16,6 @@ pub struct State { pub bandwidth: Bandwidth, pub auth: Auth, pub encoders: Encoders, - pub socket: Socket, } impl State { @@ -33,7 +31,6 @@ impl State { bandwidth: Bandwidth::default(), auth: Auth::default(), encoders: Encoders::default(), - socket: Socket::default(), } } diff --git a/rs/moq-c/src/test/parity.rs b/rs/moq-c/src/test/parity.rs index 20b8c3cc493..9b6ec1247e5 100644 --- a/rs/moq-c/src/test/parity.rs +++ b/rs/moq-c/src/test/parity.rs @@ -4,261 +4,6 @@ use std::sync::Arc; use super::*; -/// Open the socket endpoint `name` under `room` on `origin`. -fn endpoint(origin: u32, name: &str) -> u32 { - let room = "room"; - id(unsafe { - moq_socket_create( - origin, - room.as_ptr().cast(), - room.len(), - name.as_ptr().cast(), - name.len(), - ) - }) -} - -/// Read a `moq_string` a handle lends. -fn lent(s: moq_string) -> String { - borrowed_string(s.data, s.len).unwrap_or_default() -} - -/// Read the next raw frame delivered to `cb`, releasing it. -fn next_frame(cb: &Callback) -> Vec { - let frame_id = id(cb.recv()); - let mut frame: moq_frame = unsafe { std::mem::zeroed() }; - assert_eq!(unsafe { moq_consume_track_frame(frame_id, &mut frame) }, 0); - let payload = unsafe { std::slice::from_raw_parts(frame.payload, frame.payload_size) }.to_vec(); - assert_eq!(moq_consume_track_frame_free(frame_id), 0); - payload -} - -/// Write `payload` into a raw group of a scale-less track, stamped with the group's key. -fn write_unscaled(group: u32, payload: &[u8]) -> i32 { - let mut key = 0; - assert_eq!(unsafe { moq_publish_group_start(group, &mut key) }, 0); - unsafe { moq_publish_group_frame(group, payload.as_ptr(), payload.len(), &moq_timestamp::unscaled(key)) } -} - -/// Dial `peer` from `endpoint`, reporting to `cb`. -fn dial(endpoint: u32, peer: &str, cb: &Callback) -> i32 { - unsafe { - moq_socket_dial_track( - endpoint, - peer.as_ptr().cast(), - peer.len(), - std::ptr::null(), - Some(channel_callback), - cb.ptr, - ) - } -} - -/// Dial `b` from `a` and accept it, returning (dialed, accepted) stream handles plus the -/// callbacks that must outlive them. -fn connect(a: u32, b: u32) -> (u32, u32, [Callback; 3]) { - let accepts = Callback::new(); - let _accept = id(unsafe { moq_socket_accept_track(b, Some(channel_callback), accepts.ptr) }); - let dialed = Callback::new(); - let _dial = dial(a, "b", &dialed); - - let incoming = id(accepts.recv()); - let (mut claimed, mut session) = (moq_str(""), moq_str("")); - assert_eq!( - unsafe { moq_socket_incoming_info(incoming, &mut claimed, &mut session) }, - 0 - ); - assert_eq!(lent(claimed), "a"); - let accepted = Callback::new(); - let _accepting = - id(unsafe { moq_socket_incoming_accept(incoming, std::ptr::null(), Some(channel_callback), accepted.ptr) }); - assert!( - unsafe { moq_socket_incoming_info(incoming, &mut claimed, &mut session) } < 0, - "accepting releases the incoming handle" - ); - - let client = id(dialed.recv()); - assert_eq!(dialed.recv_terminal(), 0); - let server = id(accepted.recv()); - assert_eq!(accepted.recv_terminal(), 0); - (client, server, [accepts, dialed, accepted]) -} - -#[test] -fn socket_round_trip() { - let origin = id(moq_origin_create()); - let (a, b) = (endpoint(origin, "a"), endpoint(origin, "b")); - let (client, server, [accepts, ..]) = connect(a, b); - - let (mut peer, mut session) = (moq_str(""), moq_str("")); - assert_eq!(unsafe { moq_socket_stream_info(client, &mut peer, &mut session) }, 0); - assert_eq!(lent(peer), "b"); - let client_session = lent(session); - assert_eq!( - unsafe { moq_socket_stream_info(server, &mut peer, std::ptr::null_mut()) }, - 0 - ); - assert_eq!(lent(peer), "a"); - assert_eq!( - unsafe { moq_socket_stream_info(server, std::ptr::null_mut(), &mut session) }, - 0 - ); - assert_eq!( - lent(session), - client_session, - "both sides share the dialer's session id" - ); - - let writer = id(moq_socket_stream_writer(client)); - assert!(moq_socket_stream_writer(client) < 0, "the writer is taken once"); - let frames = Callback::new(); - let _reader = id(unsafe { moq_socket_stream_reader(server, Some(channel_callback), frames.ptr) }); - assert!( - unsafe { moq_socket_stream_reader(server, Some(ignore_callback), std::ptr::null_mut()) } < 0, - "the reader is taken once" - ); - // The halves outlive the stream handles. - assert_eq!(moq_socket_stream_free(server), 0); - - for (key, part) in [b"one".as_slice(), b"two"].into_iter().enumerate() { - let group = id(unsafe { moq_publish_track_group(writer, &moq_timestamp::unscaled(key as u64)) }); - assert_eq!(write_unscaled(group, part), 0); - assert_eq!(moq_publish_group_finish(group), 0); - } - assert_eq!(moq_publish_track_finish(writer), 0); - assert_eq!(next_frame(&frames), b"one"); - assert_eq!(next_frame(&frames), b"two"); - assert_eq!(frames.recv_terminal(), 0, "a finished writer ends the reader cleanly"); - - assert_eq!(moq_socket_stream_free(client), 0); - assert_eq!(moq_socket_close(b), 0); - assert_eq!(accepts.recv_terminal(), 0, "closing the endpoint ends its accept loop"); - assert!(moq_socket_close(b) < 0); - assert_eq!(moq_socket_close(a), 0); - assert_eq!(moq_origin_close(origin), 0); -} - -#[test] -fn socket_track_round_trip_and_endpoint_close() { - let origin = id(moq_origin_create()); - let (a, b) = (endpoint(origin, "a"), endpoint(origin, "b")); - let (client, server, [accepts, ..]) = connect(a, b); - - let track = id(moq_socket_stream_writer(client)); - let frames = Callback::new(); - let _reader = id(unsafe { moq_socket_stream_reader(server, Some(channel_callback), frames.ptr) }); - for (key, part) in [b"first".as_slice(), b"later"].into_iter().enumerate() { - let group = id(unsafe { moq_publish_track_group(track, &moq_timestamp::unscaled(key as u64)) }); - assert_eq!(write_unscaled(group, part), 0); - assert_eq!(moq_publish_group_finish(group), 0); - } - assert_eq!(next_frame(&frames), b"first"); - assert_eq!(next_frame(&frames), b"later"); - - // Closing the endpoint ends its open track halves. - assert_eq!(moq_socket_close(a), 0); - assert!( - moq_publish_track_finish(track) < 0, - "the endpoint's close released the track" - ); - assert!(frames.recv_terminal() < 0, "the peer's half reset"); - - assert_eq!(moq_socket_stream_free(client), 0); - assert_eq!(moq_socket_stream_free(server), 0); - assert_eq!(moq_socket_close(b), 0); - assert_eq!(accepts.recv_terminal(), 0); - assert_eq!(moq_origin_close(origin), 0); -} - -#[test] -fn socket_finished_tracks_are_not_kept_by_the_endpoint() { - let origin = id(moq_origin_create()); - let (a, b) = (endpoint(origin, "a"), endpoint(origin, "b")); - let accepts = Callback::new(); - let _accept = id(unsafe { moq_socket_accept_track(b, Some(channel_callback), accepts.ptr) }); - for round in 0..3u8 { - let dialed = Callback::new(); - let _dial = id(dial(a, "b", &dialed)); - let incoming = id(accepts.recv()); - let accepted = Callback::new(); - let _accepting = - id(unsafe { moq_socket_incoming_accept(incoming, std::ptr::null(), Some(channel_callback), accepted.ptr) }); - let client = id(dialed.recv()); - assert_eq!(dialed.recv_terminal(), 0); - let server = id(accepted.recv()); - assert_eq!(accepted.recv_terminal(), 0); - - let track = id(moq_socket_stream_writer(client)); - let frames = Callback::new(); - let _reader = id(unsafe { moq_socket_stream_reader(server, Some(channel_callback), frames.ptr) }); - let group = id(unsafe { moq_publish_track_group(track, &moq_timestamp::unscaled(0)) }); - assert_eq!(write_unscaled(group, &[round]), 0); - assert_eq!(moq_publish_group_finish(group), 0); - assert_eq!(moq_publish_track_finish(track), 0); - assert_eq!(next_frame(&frames), [round]); - assert_eq!(frames.recv_terminal(), 0, "a finished track ends its reader cleanly"); - assert_eq!(moq_socket_stream_free(client), 0); - assert_eq!(moq_socket_stream_free(server), 0); - assert_eq!( - State::lock().socket.endpoint_children(Id::try_from(a).unwrap()), - (0, 0), - "round {round}: the endpoint neither tracks nor keeps a finished track" - ); - } - - assert_eq!(moq_socket_close(a), 0); - assert_eq!(moq_socket_close(b), 0); - assert_eq!(accepts.recv_terminal(), 0); - assert_eq!(moq_origin_close(origin), 0); -} - -#[test] -fn socket_reject_and_cancel() { - let origin = id(moq_origin_create()); - let (a, b) = (endpoint(origin, "a"), endpoint(origin, "b")); - let accepts = Callback::new(); - let accept = id(unsafe { moq_socket_accept_track(b, Some(channel_callback), accepts.ptr) }); - - let dialed = Callback::new(); - let _dial = id(dial(a, "b", &dialed)); - let incoming = id(accepts.recv()); - assert_eq!(moq_socket_incoming_reject(incoming), 0); - assert!( - moq_socket_incoming_reject(incoming) < 0, - "rejecting releases the handle" - ); - assert!(dialed.recv_terminal() < 0, "a rejected dial fails"); - - // Dialing oneself is refused at once. - let me = "a"; - assert_eq!( - unsafe { - moq_socket_dial_track( - a, - me.as_ptr().cast(), - me.len(), - std::ptr::null(), - Some(ignore_callback), - std::ptr::null_mut(), - ) - }, - MOQ_ERROR_UNSUPPORTED - ); - - // A pending dial gives up when cancelled. - let waiting = Callback::new(); - let pending = id(dial(a, "nobody", &waiting)); - assert_eq!(moq_socket_cancel(pending), 0); - assert_eq!(waiting.recv_terminal(), 0); - - assert_eq!(moq_socket_cancel(accept), 0); - assert_eq!(accepts.recv_terminal(), 0); - assert!(moq_socket_cancel(accept) < 0); - assert_eq!(moq_socket_close(a), 0); - assert_eq!(moq_socket_close(b), 0); - assert_eq!(moq_origin_close(origin), 0); -} - #[test] fn consume_closed_fires_when_the_broadcast_ends() { let origin = id(moq_origin_create()); @@ -682,12 +427,3 @@ fn native_surfaces_are_unsupported_off_apple() { MOQ_ERROR_UNSUPPORTED ); } - -#[test] -fn socket_is_endpoint() { - let is_endpoint = |path: &str| unsafe { moq_socket_is_endpoint(path.as_ptr().cast(), path.len()) }; - assert_eq!(is_endpoint("room/.server/a"), 1); - assert_eq!(is_endpoint("room/.client/a"), 1); - assert_eq!(is_endpoint("room/a"), 0); - assert!(is_endpoint("room//a") < 0); -} diff --git a/rs/moq-cli/Cargo.toml b/rs/moq-cli/Cargo.toml index e6f1ccf3938..aaef371005d 100644 --- a/rs/moq-cli/Cargo.toml +++ b/rs/moq-cli/Cargo.toml @@ -186,10 +186,6 @@ harness = false name = "interop-tail" path = "../../test/interop/clients/rust/tail.rs" -[[example]] -name = "interop-socket-js" -path = "../../test/interop/clients/rust/socket-js.rs" - [[example]] name = "interop-catalog" path = "../../test/interop/clients/rust/catalog.rs" @@ -218,9 +214,5 @@ path = "../../test/interop/clients/rust/hls.rs" name = "interop-audio" path = "../../test/interop/clients/rust/audio.rs" -[[example]] -name = "interop-socket" -path = "../../test/interop/clients/rust/socket.rs" - [lints] workspace = true diff --git a/rs/moq-ffi/src/lib.rs b/rs/moq-ffi/src/lib.rs index 16299f4601e..2e940741fbc 100644 --- a/rs/moq-ffi/src/lib.rs +++ b/rs/moq-ffi/src/lib.rs @@ -26,7 +26,6 @@ pub mod origin; pub mod producer; pub mod server; pub mod session; -pub mod socket; pub mod video; uniffi::setup_scaffolding!("moq"); diff --git a/rs/moq-ffi/src/producer.rs b/rs/moq-ffi/src/producer.rs index 4b4e395db6b..d436d72774a 100644 --- a/rs/moq-ffi/src/producer.rs +++ b/rs/moq-ffi/src/producer.rs @@ -707,10 +707,10 @@ pub struct MoqTrackProducer { /// How far a track binding got. #[derive(Default)] enum TrackPhase { - /// Publishing, as a child of `family` unless it has none, as a socket's track does. + /// Publishing, as a child of `family`. Open { raw: moq_net::track::Producer, - family: Option, + family: Children, }, /// Finished, and kept by its family until the broadcast closes. It may still take groups /// below its end, and abort. @@ -755,19 +755,12 @@ impl Drop for MoqTrackProducer { } impl MoqTrackProducer { - /// Wrap a socket's track without a broadcast family. - pub(crate) fn from_socket(raw: moq_net::track::Producer) -> Self { - Self { - phase: std::sync::Mutex::new(TrackPhase::Open { raw, family: None }), - } - } - /// Wrap `raw` as a child of the broadcast `children` belongs to. fn child(raw: moq_net::track::Producer, children: &Children) -> Result, MoqError> { children.adopt(Arc::new(Self { phase: std::sync::Mutex::new(TrackPhase::Open { raw, - family: Some(children.clone()), + family: children.clone(), }), })) } @@ -788,17 +781,12 @@ impl MoqTrackProducer { let mut phase = std::mem::take(&mut *self.phase.lock().unwrap()); let raw = phase.raw()?.clone(); match (f(raw), phase) { - ( - Ok(writer), - TrackPhase::Open { - family: Some(family), .. - }, - ) => family.adopt(Arc::new(writer)), + (Ok(writer), TrackPhase::Open { family, .. }) => family.adopt(Arc::new(writer)), // A finished track has no family left to join, and ends at its declared end. (Ok(writer), _) => Ok(Arc::new(writer)), (Err(err), phase) => { let family = match &phase { - TrackPhase::Open { family, .. } => family.clone(), + TrackPhase::Open { family, .. } => Some(family.clone()), TrackPhase::Finished(_) | TrackPhase::Closed => None, }; *self.phase.lock().unwrap() = phase; @@ -965,9 +953,7 @@ impl MoqTrackProducer { *phase = TrackPhase::Open { raw, family }; return Err(err.into()); } - if let Some(family) = family { - family.keep(raw.clone()); - } + family.keep(raw.clone()); TrackPhase::Finished(raw) } finished @ TrackPhase::Finished(_) => finished, diff --git a/rs/moq-ffi/src/socket.rs b/rs/moq-ffi/src/socket.rs deleted file mode 100644 index 0c3c778328d..00000000000 --- a/rs/moq-ffi/src/socket.rs +++ /dev/null @@ -1,318 +0,0 @@ -//! Point-to-point socket endpoints and their track sessions. - -use std::sync::Arc; -use std::task::{Poll, ready}; - -use crate::consumer::MoqTrackConsumer; -use crate::error::MoqError; -use crate::ffi::{Background, OneShot, Polled}; -use crate::origin::MoqOriginProducer; -use crate::producer::{MoqTrackInfo, MoqTrackProducer}; - -/// Whether `path` is a socket endpoint's broadcast, which only routes sessions and carries -/// no media, so a worker that acts on every announced broadcast can skip it. -#[uniffi::export] -pub fn moq_socket_is_endpoint(path: String) -> Result { - let path = moq_net::Path::new(&path).map_err(moq_net::Error::from)?; - Ok(moq_net::socket::is_endpoint(&path)) -} - -/// A socket endpoint: its first dial announces it as a client, and its first accept as a -/// server. -#[derive(uniffi::Object)] -pub struct MoqSocketEndpoint { - inner: moq_net::socket::Endpoint, - driver: Background, -} - -#[uniffi::export] -impl MoqSocketEndpoint { - /// The endpoint `id` beneath `prefix`, using `origin` for publishing and finding peers. - /// Nothing is announced until its first dial or accept. - #[uniffi::constructor] - pub fn new(origin: &MoqOriginProducer, prefix: String, id: String) -> Result, MoqError> { - let _guard = crate::ffi::enter(); - let origin = origin.inner(); - origin.driver.check()?; - let inner = moq_net::socket::Endpoint::new(origin, prefix, &id)?; - origin.driver.check()?; - Ok(Arc::new(Self { - inner, - driver: origin.driver.clone(), - })) - } - - /// This endpoint's id. - pub fn id(&self) -> String { - self.inner.id().into() - } - - /// The prefix beneath which this endpoint finds peers. - pub fn prefix(&self) -> String { - self.inner.prefix().to_string() - } - - /// Retract the endpoint's broadcasts and reset its pending dials and sessions. - pub fn close(&self) { - let _guard = crate::ffi::enter(); - self.inner.close(); - } - - /// Dial the server `peer` in a new `track` session; cancelling this call resets only this dial. - pub async fn dial_track( - &self, - peer: String, - info: Option, - ) -> Result, MoqError> { - self.driver - .watch(async { - let stream = self - .inner - .dial_track(&peer, info.map(TryInto::try_into).transpose()?)? - .await?; - Ok(Arc::new(MoqSocketTrackStream::new(stream, self.driver.clone()))) - }) - .await - } - - /// Start accepting `track` dials and await the next one. - pub async fn accept_track(&self) -> Result, MoqError> { - self.driver - .watch(async { - let inner = self.inner.accept_track().await?; - Ok(Arc::new(MoqSocketIncomingTrack { - peer: inner.peer().into(), - session: inner.session().into(), - inner: OneShot::new(inner), - driver: self.driver.clone(), - })) - }) - .await - } -} - -impl Drop for MoqSocketEndpoint { - fn drop(&mut self) { - self.close(); - } -} - -/// An incoming `track` dial, consumed by accepting or rejecting it. -#[derive(uniffi::Object)] -pub struct MoqSocketIncomingTrack { - inner: OneShot, - peer: String, - session: String, - driver: Background, -} - -#[uniffi::export] -impl MoqSocketIncomingTrack { - /// The endpoint claiming this dial; accepting verifies it really dialed. - pub fn peer(&self) -> String { - self.peer.clone() - } - /// The random id of this session. - pub fn session(&self) -> String { - self.session.clone() - } - /// Verify return routability and accept; cancelling resets the dial. - pub async fn accept(&self, info: Option) -> Result, MoqError> { - self.driver.check()?; - let info = info.map(TryInto::try_into).transpose()?; - let (incoming, answering) = self.inner.take()?; - let accepting = Polled::new(incoming.accept(info)).supervised(self.driver.clone()); - let stream = accepting - .wait(|inner, waiter| Poll::Ready(Ok(ready!(inner.poll_ok(waiter))?))) - .await; - answering.settle(stream.map(|stream| Arc::new(MoqSocketTrackStream::new(stream, self.driver.clone())))) - } - /// Refuse the dial as not found. - pub fn reject(&self) -> Result<(), MoqError> { - let _guard = crate::ffi::enter(); - self.driver.check()?; - self.inner.answer(|incoming| { - incoming.reject(); - Ok(()) - }) - } -} - -/// A `track` session exposing its existing producer and subscribed consumer handles. -#[derive(uniffi::Object)] -pub struct MoqSocketTrackStream { - producer: Arc, - consumer: Arc, - peer: String, - session: String, -} - -impl MoqSocketTrackStream { - fn new(inner: moq_net::socket::TrackStream, driver: Background) -> Self { - let peer = inner.peer().into(); - let session = inner.session().into(); - Self { - producer: Arc::new(MoqTrackProducer::from_socket(inner.producer)), - consumer: Arc::new(MoqTrackConsumer::new(inner.consumer, driver)), - peer, - session, - } - } -} - -#[uniffi::export] -impl MoqSocketTrackStream { - /// This side's write half. - pub fn producer(&self) -> Arc { - self.producer.clone() - } - /// The peer's read half, retaining its reliable ascending subscription. - pub fn consumer(&self) -> Arc { - self.consumer.clone() - } - /// The peer endpoint's id. - pub fn peer(&self) -> String { - self.peer.clone() - } - /// The random id of this session. - pub fn session(&self) -> String { - self.session.clone() - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::error::MoqProtocolKind; - use crate::media::{MoqFrame, MoqTimestamp}; - use crate::producer::MoqGroupProducer; - use std::future::Future; - use std::pin::Pin; - use std::task::{Context, Waker}; - - fn pending(future: Pin<&mut impl Future>) { - assert!(future.poll(&mut Context::from_waker(Waker::noop())).is_pending()); - } - - fn endpoints() -> (Arc, Arc) { - let origin = MoqOriginProducer::new(Default::default()); - let a = MoqSocketEndpoint::new(&origin, "room".into(), "a".into()).unwrap(); - let b = MoqSocketEndpoint::new(&origin, "room".into(), "b".into()).unwrap(); - (a, b) - } - - /// `payload` in `group`: socket tracks are scale-less, so a frame carries its group's key. - fn frame(group: &MoqGroupProducer, payload: &[u8]) -> MoqFrame { - MoqFrame { - payload: payload.into(), - timestamp: MoqTimestamp::unscaled(group.start()), - } - } - - async fn connect( - a: &MoqSocketEndpoint, - b: &MoqSocketEndpoint, - ) -> (Arc, Arc) { - let mut incoming = Box::pin(b.accept_track()); - pending(incoming.as_mut()); - let (dialed, accepted) = tokio::join!(a.dial_track(b.id(), None), async { - incoming.await.unwrap().accept(None).await - }); - (dialed.unwrap(), accepted.unwrap()) - } - - #[tokio::test] - async fn track_subscription_survives_stream_release_and_has_no_default_age_limit() { - let (a, b) = endpoints(); - let (client, server) = connect(&a, &b).await; - assert_eq!((client.peer(), server.peer()), ("b".into(), "a".into())); - assert_eq!(client.session(), server.session()); - let producer = client.producer(); - let reverse = server.producer(); - let consumer = server.consumer(); - drop(server); - assert_eq!(consumer.info().max_age, None); - // Create the later group first: the reliable ascending reader must wait for 0. - let later = producer.create_group(1).unwrap(); - later.write_frame(frame(&later, b"later")).unwrap(); - later.finish().unwrap(); - let first = producer.create_group(0).unwrap(); - first.write_frame(frame(&first, b"first")).unwrap(); - first.finish().unwrap(); - producer.finish().unwrap(); - assert_eq!(consumer.read_frame().await.unwrap().unwrap().payload, b"first"); - assert_eq!(consumer.read_frame().await.unwrap().unwrap().payload, b"later"); - assert!(consumer.read_frame().await.unwrap().is_none()); - reverse.finish().unwrap(); - assert!(client.consumer().read_frame().await.unwrap().is_none()); - } - - #[tokio::test] - async fn an_abort_is_not_a_finish_and_does_not_close_another_session() { - let (a, b) = endpoints(); - let (client, server) = connect(&a, &b).await; - let (other, peer) = connect(&a, &b).await; - assert_ne!(client.session(), other.session()); - client.producer().abort(42).unwrap(); - let error = server.consumer().read_frame().await.err().unwrap(); - assert!(matches!(error, MoqError::Protocol { details } if details.kind == MoqProtocolKind::App)); - other - .producer() - .write_frame(MoqFrame { - payload: b"independent".as_slice().into(), - timestamp: MoqTimestamp::unscaled(0), - }) - .unwrap(); - other.producer().finish().unwrap(); - assert_eq!( - peer.consumer().read_frame().await.unwrap().unwrap().payload, - b"independent" - ); - assert!(peer.consumer().read_frame().await.unwrap().is_none()); - } - - #[tokio::test] - async fn cancelled_accept_wait_leaves_the_queue_and_rejection_is_one_shot() { - let (a, b) = endpoints(); - let mut abandoned = Box::pin(b.accept_track()); - pending(abandoned.as_mut()); - drop(abandoned); - let (dialed, ()) = tokio::join!(a.dial_track(b.id(), None), async { - let incoming = b.accept_track().await.unwrap(); - assert_eq!(incoming.peer(), "a"); - incoming.reject().unwrap(); - assert!(matches!(incoming.reject(), Err(MoqError::AlreadyResponded))); - }); - assert!(matches!(dialed, Err(MoqError::NotFound))); - let _ = connect(&a, &b).await; - } - - #[tokio::test] - async fn cancelling_a_dial_resets_its_incoming_but_the_endpoint_can_dial_again() { - let (a, b) = endpoints(); - let mut incoming = Box::pin(b.accept_track()); - pending(incoming.as_mut()); - let mut dial = Box::pin(a.dial_track(b.id(), None)); - pending(dial.as_mut()); - let incoming = tokio::select! { - incoming = incoming => incoming.unwrap(), - _ = &mut dial => panic!("dial resolved without acceptance"), - }; - drop(dial); - assert!(incoming.accept(None).await.is_err()); - let _ = connect(&a, &b).await; - } - - #[tokio::test] - async fn endpoint_close_ends_its_sessions_and_wakes_a_pending_accept() { - let (a, b) = endpoints(); - let (_client, server) = connect(&a, &b).await; - let mut incoming = Box::pin(b.accept_track()); - pending(incoming.as_mut()); - a.close(); - assert!(server.consumer().read_frame().await.is_err()); - b.close(); - assert!(matches!(incoming.await, Err(MoqError::Closed))); - assert!(matches!(b.dial_track(a.id(), None).await, Err(MoqError::Closed))); - } -} diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 5f0bcbbb2b6..f0fa2a158ee 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -10396,13 +10396,3 @@ async fn group_calls_wake_ready_after_unlocking() { let result = parked.result.lock().unwrap().take(); assert!(matches!(result, Some(Err(MoqError::Closed))), "{result:?}"); } - -/// An endpoint's broadcast is told apart from media by path, and a malformed path fails. -#[test] -fn socket_is_endpoint() { - use crate::socket::moq_socket_is_endpoint; - assert!(moq_socket_is_endpoint("room/.server/a".into()).unwrap()); - assert!(moq_socket_is_endpoint("room/.client/a".into()).unwrap()); - assert!(!moq_socket_is_endpoint("room/a".into()).unwrap()); - assert!(moq_socket_is_endpoint("room//a".into()).is_err()); -} diff --git a/rs/moq-ffi/uniffi.toml b/rs/moq-ffi/uniffi.toml index c08ac74f571..4a2333932cc 100644 --- a/rs/moq-ffi/uniffi.toml +++ b/rs/moq-ffi/uniffi.toml @@ -3,7 +3,6 @@ # generated forced end is `end()` so it doesn't collide with it. [bindings.kotlin.rename] "MoqBroadcastProducer.close" = "end" -"MoqSocketEndpoint.close" = "end" # See MoqRoute::instance_id: the C++ generator reserves `instance`, Kotlin does not. "MoqRoute.instance_id" = "instance" "MoqClaim.instance_id" = "instance" diff --git a/rs/moq-net/CHANGELOG.md b/rs/moq-net/CHANGELOG.md index d545d72fa3f..743f8dc0cb3 100644 --- a/rs/moq-net/CHANGELOG.md +++ b/rs/moq-net/CHANGELOG.md @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- `socket`: `Endpoint`, a server, a client, or both, which announces a hidden `/.client/` broadcast at its first dial and `/.server/` at its first accept, so grants decide its roles, dials servers, and accepts clients' dials in `track` sessions (`dial_track`, `accept_track`, `TrackStream`), a track each way with one message per group. Dials and accepts fail `Unsupported` while the origin has a moq-lite client session before 07 whose publish half doesn't opt into hidden broadcasts, since its peer never hears the endpoint; see `doc/concept/socket.md`. +- `broadcast::Producer::reserve_unlisted_track`, a reservation no track listing ever names, read only by a subscriber who knows its name. +- `track::Producer::abort_handle`, a `track::AbortHandle` that aborts the track, unlisting it first, without keeping it open, for an owner that hands the producer out and ends the track on its own shutdown. - `transport::SETUP_RECEIVE_WINDOW`, the connection credit a server offers before it admits a session: enough for the SETUP and one in-band AUTH. - `Server::with_allocator`, the accepting mirror of `Client::with_allocator`: every session the server accepts reserves into one `bandwidth::Allocator` and probes for the sum, instead of each starting an empty registry. diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 2142f90687b..d1a73a640ec 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -16594,15 +16594,14 @@ mod stitch_tests { assert_eq!(frames.len(), 1); } - /// A refused headless group is reported dropped, so a reader in key order, as a - /// `track` socket session reads, moves on to the next group rather than wait on it. + /// A refused headless group is reported dropped, so a reader in key order moves on to the next group rather than wait on it. #[kio::test] async fn a_refused_headless_group_does_not_stall_an_ordered_reader() { let h = Harness::new( Fill::Done, vec![tail_stream(KEY, 2, &[b"tail-2"]), tail_stream(KEY + 1, 0, &[b"next-0"])], ); - // Subscribed before the answer, as the socket does. + // Subscribed before the answer. let mut ordered = h .track .subscribe(Some( diff --git a/rs/moq-net/src/lite/session.rs b/rs/moq-net/src/lite/session.rs index b1149ac05a6..ba837b1a1c7 100644 --- a/rs/moq-net/src/lite/session.rs +++ b/rs/moq-net/src/lite/session.rs @@ -309,11 +309,6 @@ pub fn start(config: Config) -> Result { // subscribe origin issues no ANNOUNCE_PLEASE. let publish = publish.unwrap_or_else(|| origin::Producer::empty().consume().with_stats(stats.clone())); let subscribe = subscribe.unwrap_or_else(|| origin::Producer::empty().with_stats(stats.clone())); - // Before lite-07 the peer can't ask for hidden routes, so a client publishes none of - // them unless its publish half opted in, and a socket endpoint behind it is - // unreachable. A server leaves it to the client, whose origin holds that endpoint. - let withholding = - (client && !version.has_hidden() && !publish.includes_hidden()).then(|| publish.hidden_withheld().hold()); let subscriptions = crate::session::Slots::default().with_stats(publish.stats(), crate::stats::Cap::Subscriptions); let announces = crate::session::Slots::new(limits.announces).with_stats(subscribe.stats(), crate::stats::Cap::Announces); @@ -431,7 +426,6 @@ pub fn start(config: Config) -> Result { publisher, subscriber: SubscriberDriver::new(subscriber, early, client, route_publish), session, - withholding, }; Ok(SessionStart { @@ -467,9 +461,6 @@ pub(crate) struct Driver { /// For the terminal close: the machine's last act reports the outcome to /// the peer through the transport. session: Transport, - /// Counts this session against the publish origin while it withholds that origin's - /// hidden routes from the peer. - withholding: Option, } impl Driver { @@ -520,7 +511,6 @@ impl Driver { } } self.ended = Some(res.clone()); - self.withholding = None; res } diff --git a/rs/moq-net/src/lite/version.rs b/rs/moq-net/src/lite/version.rs index 9db7519e448..dda87c214b1 100644 --- a/rs/moq-net/src/lite/version.rs +++ b/rs/moq-net/src/lite/version.rs @@ -267,15 +267,6 @@ impl Version { self.has_ranges() } - /// Whether ANNOUNCE_REQUEST carries the hidden opt-in, one for each prefix it adds. - /// Added in lite-07; older requests decode as not opted in. - pub(crate) fn has_hidden(self) -> bool { - !matches!( - self, - Self::Lite01 | Self::Lite02 | Self::Lite03 | Self::Lite04 | Self::Lite05 | Self::Lite06 - ) - } - /// Whether ANNOUNCE_START may copy a path head from a live /// announcement on the same stream. Added in lite-07. pub(crate) fn has_announce_compression(self) -> bool { diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index 252ea3c69a3..9f091cef2a1 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -431,8 +431,12 @@ impl Producer { } /// [`Self::reserve_track`] for a track no listing ever names, so only a reader who - /// already knows the name finds it, as a socket session's track. - pub(super) fn reserve_unlisted_track(&self, name: impl Into>) -> Result { + /// already knows the name finds it, such as a per-session track whose name the two + /// sides agreed on. + /// + /// Unlisted controls discovery, not authorization: anyone allowed to subscribe to the + /// broadcast can read the track by name. + pub fn reserve_unlisted_track(&self, name: impl Into>) -> Result { let mut state = self.state.lock(); let request = track::Request::new(self.info.clone(), name.into()).with_stats(self.stats.clone()); state.insert_track(request.weak())?; @@ -513,11 +517,6 @@ impl Producer { self.alive.close(); } - /// `Ready` once the broadcast ended, by [`Self::close`] or the last producer dropping. - pub(super) fn poll_closed(&self, waiter: &kio::Waiter) -> Poll<()> { - self.alive.token.poll_closed(waiter) - } - /// Whether any [`Consumer`] handle exists, which is not itself demand for a track. pub(super) fn is_held(&self) -> bool { self.alive.token.wanted() diff --git a/rs/moq-net/src/model/front.rs b/rs/moq-net/src/model/front.rs index eff9ffce44e..ac11d384dac 100644 --- a/rs/moq-net/src/model/front.rs +++ b/rs/moq-net/src/model/front.rs @@ -1367,10 +1367,9 @@ mod tests { ); } - /// A copy reset before delivering, seen only once its source began closing, such as a - /// session of a closing socket endpoint, is no refusal, yet ends the track with the reset - /// rather than `Unroutable` when nothing replaces the source, or when its replacement - /// refuses. The driver reports it as the copy's end once spliced, or as the answer to + /// A copy reset before delivering, seen only once its source began closing, is no + /// refusal, yet ends the track with the reset rather than `Unroutable` when nothing + /// replaces the source, or when its replacement refuses. The driver reports it as the copy's end once spliced, or as the answer to /// its query when the reset came before the metadata was read. #[test] fn a_closing_source_reset_reports_the_loss() { @@ -1441,7 +1440,7 @@ mod tests { } /// A source that loses a copy mid-serve and then refuses its re-splice, such as a - /// closing socket endpoint, ends the track with the loss, not the refusal. + /// closing publisher, ends the track with the loss, not the refusal. #[test] fn a_refused_resplice_reports_the_loss() { let mut front = serving(remote(1), 100); diff --git a/rs/moq-net/src/model/listing.rs b/rs/moq-net/src/model/listing.rs index 233f50a46e6..4f09e021785 100644 --- a/rs/moq-net/src/model/listing.rs +++ b/rs/moq-net/src/model/listing.rs @@ -1094,6 +1094,68 @@ mod tests { assert_eq!(drain(&mut tracks), ["-a"]); } + /// A reservation that is never listed is still read by anyone who names it. + #[test] + fn an_unlisted_track_is_read_by_name_but_never_listed() { + let broadcast = Info::new().produce(); + let consumer = broadcast.consume(); + let mut tracks = consumer.tracks().unwrap(); + assert_eq!(drain(&mut tracks), ["synced"]); + let request = broadcast.reserve_unlisted_track("a").unwrap(); + let a = request.accept(None); + let mut group = a.append_group(crate::Timestamp::unscaled(0)).unwrap(); + group.write_frame(crate::Timestamp::unscaled(0), vec![7]).unwrap(); + group.finish().unwrap(); + + let waiter = kio::Waiter::noop(); + let Poll::Ready(Ok(mut reader)) = consumer.track("a").unwrap().subscribe(None).poll_ok(&waiter) else { + panic!("the track is read by name"); + }; + let Poll::Ready(Ok(Some(mut group))) = reader.poll_recv_group(&waiter) else { + panic!("expected the group"); + }; + let Poll::Ready(Ok(Some(frame))) = group.poll_read_frame(&waiter) else { + panic!("expected the frame"); + }; + assert_eq!(&frame.payload[..], [7]); + drop(a); + assert_eq!(drain(&mut tracks), NONE); + } + + /// An abort handle ends a listed track the way its producer's abort does: unlisted, with + /// its name free for a new track. + #[test] + fn an_abort_handle_unlists_the_track_it_ends() { + let broadcast = Info::new().produce(); + let a = broadcast.create_track("a", None).unwrap(); + let handle = a.abort_handle(); + let mut tracks = broadcast.consume().tracks().unwrap(); + assert_eq!(drain(&mut tracks), ["+a", "synced"]); + assert!(!handle.is_closed()); + handle.abort(Error::Cancel); + assert!(handle.is_closed()); + assert_eq!(drain(&mut tracks), ["-a"]); + assert!(a.append_group(crate::Timestamp::unscaled(0)).is_err()); + let _again = broadcast.create_track("a", None).unwrap(); + assert_eq!(drain(&mut tracks), ["+a"]); + } + + /// An abort handle keeps no track open: once every producer dropped, the track closed and + /// the handle does nothing. + #[test] + fn an_abort_handle_keeps_no_dropped_track_alive() { + let broadcast = Info::new().produce(); + let a = broadcast.create_track("a", None).unwrap(); + let handle = a.abort_handle(); + let mut tracks = broadcast.consume().tracks().unwrap(); + assert_eq!(drain(&mut tracks), ["+a", "synced"]); + drop(a); + assert!(handle.is_closed()); + assert_eq!(drain(&mut tracks), ["-a"]); + handle.abort(Error::Cancel); + assert_eq!(drain(&mut tracks), NONE); + } + #[test] fn a_staged_track_is_listed_until_dropped_uncommitted() { let broadcast = Info::new().produce(); diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index acfdaa53a9b..5c70dab688b 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -4,7 +4,6 @@ pub mod cache; pub mod datagram; pub mod frame; pub mod group; -pub mod socket; pub mod track; // The origin + announce subsystem shares one implementation (a route table). @@ -54,9 +53,6 @@ pub mod origin { pub use super::follow::Following; #[cfg(all(feature = "async", any(test, feature = "fuzz")))] pub(crate) use super::origin_impl::Footprint; - pub(crate) use super::origin_impl::HiddenWithheld; - #[cfg(feature = "async")] - pub(crate) use super::origin_impl::HiddenWithholding; #[cfg(feature = "async")] // private: capability gap pub(crate) use super::origin_impl::Lookup; diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index ad5ea8079e0..6d69b8f2d18 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -2163,40 +2163,6 @@ fn hides(heads: &[Path], prefix: &Path) -> bool { .any(|head| prefix.strip_prefix(head).is_some_and(|below| below.is_hidden())) } -/// How many sessions withhold an origin's hidden routes from their peer, such as a -/// moq-lite client before 07, whose peer can't ask for them. -#[derive(Clone, Debug, Default)] -pub(crate) struct HiddenWithheld(kio::Shared); - -impl HiddenWithheld { - /// Count a session that withholds them, until the returned guard drops. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub fn hold(&self) -> HiddenWithholding { - *self.0.lock() += 1; - HiddenWithholding(self.clone()) - } - - /// Ready while a session withholds them. - pub fn poll(&self, waiter: &kio::Waiter) -> Poll<()> { - self.0 - .poll(waiter, |count| match **count { - 0 => Poll::Pending, - _ => Poll::Ready(()), - }) - .map(drop) - } -} - -/// A session counted by [`HiddenWithheld::hold`], until dropped. -#[derive(Debug)] -pub(crate) struct HiddenWithholding(HiddenWithheld); - -impl Drop for HiddenWithholding { - fn drop(&mut self) { - *self.0.0.lock() -= 1; - } -} - /// What a [`Routes`] yields: how the route covering a prefix changed, and that route. #[derive(Clone, Debug)] pub struct RouteEvent { @@ -4125,7 +4091,6 @@ pub(crate) struct Footprint { cursors: usize, listeners: usize, withdrawn: usize, - withheld: usize, } #[cfg(all(feature = "async", any(test, feature = "fuzz")))] @@ -4139,7 +4104,6 @@ impl Driver { cursors: shared.cursors.len(), listeners: shared.listeners.len(), withdrawn: shared.withdrawn.len(), - withheld: *shared.hidden_withheld.0.read(), } } } @@ -6391,9 +6355,6 @@ struct OriginState { listeners: Vec, next_listener: u64, - // The sessions withholding this origin's hidden routes from their peer. - hidden_withheld: HiddenWithheld, - // The route layer node this origin's lite-07 sessions share, and the feeder the // driver owns; see [`Consumer::route_node`]. Weak, since the feeder's cursors hold // this state. @@ -6415,7 +6376,6 @@ impl OriginState { closed: false, listeners: Vec::new(), next_listener: 0, - hidden_withheld: HiddenWithheld::default(), route_node: None, } } @@ -7811,11 +7771,6 @@ impl Consumer { self.hidden.include } - /// The sessions withholding this origin's hidden routes from their peer. - pub(crate) fn hidden_withheld(&self) -> HiddenWithheld { - self.shared.read().hidden_withheld.clone() - } - /// The egress stats context this handle was tagged with. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn stats(&self) -> &stats::Session { diff --git a/rs/moq-net/src/model/socket.rs b/rs/moq-net/src/model/socket.rs deleted file mode 100644 index 355fc60f5b7..00000000000 --- a/rs/moq-net/src/model/socket.rs +++ /dev/null @@ -1,1150 +0,0 @@ -//! Point-to-point sessions over an origin, the way TCP connects two hosts. -//! -//! An [`Endpoint`] is a server, a client, or both. As a server it announces the hidden -//! broadcast `/.server/` at its first accept, and peers dial it there. As a -//! client it announces `/.client/` at its first dial, and dials servers. The -//! broadcasts are used only to route sessions, so ordinary grants decide each endpoint's -//! roles. Each connection is a session: two tracks, one in the dialer's client broadcast -//! and one in the acceptor's server broadcast, both named -//! `track///`. Sessions are never announced or listed. -//! -//! Each half of a session is a whole track, so messages are groups, with the track's own -//! priority, delay budget and order. Dial with [`Endpoint::dial_track`], accept with -//! `accept_track` or [`Endpoint::poll_accept_track`], and read and write a [`TrackStream`]. -//! The leading `track` names the protocol; a dial in any other is reset. -//! -//! A dialer `A` picks a random session id `S` and subscribes to `/.server/` for -//! the track `track/A/B/S`. Endpoint `B` surfaces that request as an [`Incoming`] dial -//! from `A`. To accept it, `B` subscribes to `/.client/` -//! for the same track name, and `A` answers only a session it dialed, so the exchange -//! checks that `A` really asked `B`: a dial forged in `A`'s name is reset. Naming both ends -//! binds a session to them, so an endpoint `A` dialed cannot replay the session to a third -//! endpoint `C`: `C` resets a name for another acceptor, and `A` never dialed -//! `track/A/C/S`. Each side then writes its own half and reads the peer's. -//! -//! See `doc/concept/socket.md` for the convention other languages follow. - -use std::collections::VecDeque; -use std::sync::Arc; -use std::task::{Poll, ready}; - -use crate::{Error, Path, SessionError, StreamError}; - -use super::WeakEntry; -use super::origin::{self, RouteOptions}; -use super::track::{self, Delay, Order, Subscription}; - -/// Refuse an id that is not one plain path segment: empty, or holding a `/` or a `\`. -fn check_id(id: &str) -> Result<(), Error> { - if id.is_empty() || id.contains(['/', '\\']) { - return Err(crate::InvalidPattern::InvalidSegment(id.to_string()).into()); - } - Ok(()) -} - -/// What an endpoint's broadcast is for: a server accepts dials, and a client dials. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum Role { - Server, - Client, -} - -impl Role { - /// The segment the role's broadcasts sit under. - fn segment(self) -> &'static str { - match self { - Self::Server => ".server", - Self::Client => ".client", - } - } -} - -/// `//`, the endpoint `id`'s broadcast in `role`. -fn endpoint_path(prefix: &Path, role: Role, id: &str) -> Result { - check_id(id)?; - Ok(prefix.join(&Path::from_static(role.segment()))?.join(&Path::new(id)?)?) -} - -/// Whether `path` is an endpoint's broadcast, which only routes sessions and carries no -/// media, so a worker that acts on every announced broadcast can skip it. -pub fn is_endpoint(path: &Path) -> bool { - let parent = path.parts().nth_back(1); - [Role::Server, Role::Client] - .into_iter() - .any(|role| parent == Some(role.segment())) -} - -/// A fresh session id: 128 random bits as hex, so nobody can guess a session's tracks. Drawn -/// from the ambient generator, the thread's outside a simulated run, so a simulated run -/// replays them. -fn session_id() -> Arc { - use rand::RngExt; - format!("{:032x}", kio::clock::rng().random::()).into() -} - -/// The track name of a session: `///`, the same in -/// both endpoints. -fn track_name(protocol: &str, dialer: &str, acceptor: &str, session: &str) -> String { - format!("{protocol}/{dialer}/{acceptor}/{session}") -} - -/// A session's track name, split. -#[derive(Debug, PartialEq, Eq)] -struct Name<'a> { - protocol: &'a str, - dialer: &'a str, - acceptor: &'a str, - session: &'a str, -} - -/// Split a session's track name, or `None` for a name outside the scheme. -fn parse(name: &str) -> Option> { - let mut segments = name.split('/'); - let [protocol, dialer, acceptor, session] = std::array::from_fn(|_| segments.next().unwrap_or_default()); - if segments.next().is_some() { - return None; - } - for segment in [protocol, dialer, acceptor, session] { - check_id(segment).ok()?; - } - Some(Name { - protocol, - dialer, - acceptor, - session, - }) -} - -/// A `track` session half's default: every message stays until read, rather than only -/// the newest as for an authored track. -fn messages() -> track::Info { - track::Info::default().with_max_age(None) -} - -/// The protocol segment leading every session's track name. -const PROTOCOL: &str = "track"; - -/// What a session's reader asks for: every message from group 0, oldest first, however -/// late it is, so a burst written before the answer is kept. -fn subscription() -> Subscription { - Subscription::default() - .with_order(Order::Ascending) - .with_delay(Delay::default().with_max(None)) - .with_ranges([track::Range { - start: track::Anchor::Time(0), - end: None, - }]) -} - -/// What an endpoint's handles share. -struct Shared { - id: Arc, - prefix: Path, - /// Where this endpoint's broadcasts are announced, at first use. - publisher: origin::Producer, - /// Where the peers' endpoints are requested. - origin: origin::Consumer, - /// The sessions withholding the broadcasts from their peer. - withheld: origin::HiddenWithheld, - state: kio::Lock, -} - -/// A `track` session: each half is a whole track, and each message a group. -/// -/// [`Self::consumer`] starts subscribed reliably and in order: oldest first, with no -/// delay budget, so a late group is waited for rather than skipped. Each hop's cache -/// still reclaims a group nobody read within its idle window (30 s by default), or under -/// memory pressure, except the newest, so a reader that stalls longer can miss groups. -/// Any group the track reports it will never deliver, such as one a moq-transport -/// publisher refused to fetch, is skipped the same way rather than ending the session. -/// Update its subscription, such as with a finite -/// [`Delay`] or [`Order::Descending`], to make it lossy or newest first, and read it in -/// group order with [`track::Subscriber::ordered`]. Groups are written with -/// [`track::Producer::append_group`], each with its own priority on the wire. -/// -/// Finishing [`Self::producer`] closes the session for the peer, and aborting it is an -/// error. The endpoint closing aborts it too. When the peer's half ends with an error, -/// abort this side's half as well, and drop the stream, so both halves end. -pub struct TrackStream { - /// This side's half: the session's track in this endpoint's broadcast. - pub producer: track::Producer, - /// The peer's half: the session's track in the peer's broadcast, subscribed reliably - /// and in order until updated. - pub consumer: track::Subscriber, - peer: Arc, - session: Arc, -} - -impl TrackStream { - /// The peer endpoint's id. - pub fn peer(&self) -> &str { - &self.peer - } - - /// The session's id, which the dialer chose. - pub fn session(&self) -> &str { - &self.session - } -} - -/// An endpoint's handler for the requests to its server broadcast: the dials peers send. -/// -/// A poller takes it out to poll it with no lock held, since taking a request wakes the -/// dialer's subscription, and an inline executor can re-enter the endpoint from that wake. -/// The client broadcast has none, so it answers a request for a session it does not hold -/// with [`Error::NotFound`] at once. -enum Handler { - /// Not made yet, until the first accept makes the server broadcast with it. - None, - /// Waiting for a poller. - Idle(super::broadcast::Requests), - /// Taken by a poller, which puts it back and wakes the pollers that found it taken, - /// so each polls it, and is woken by it, in turn. - Taken(kio::WaiterList), -} - -/// An endpoint's lifecycle. -enum State { - Open(Box), - /// Ended, by [`Endpoint::close`] or one of its broadcasts ending, with why. - Closed(Error), -} - -impl State { - /// The open endpoint, or why it ended. - fn open(&mut self) -> Result<&mut Open, Error> { - match self { - Self::Open(open) => Ok(open), - Self::Closed(err) => Err(err.clone()), - } - } -} - -/// What an open endpoint's pollers share. -struct Open { - /// The route of the broadcast this endpoint dials from, from its first dial on. - client: Option, - /// The route of the broadcast peers dial, from its first accept on. - server: Option, - /// Where the dials peers send arrive. - handler: Handler, - /// Dials from peers, waiting to be accepted. - dials: VecDeque, - /// The accepts waiting for a dial. - waiting: kio::WaiterList, - /// This side's half of every session handed out, aborted when the endpoint ends. - streams: Vec, -} - -impl Open { - fn new() -> Self { - Self { - client: None, - server: None, - handler: Handler::None, - dials: VecDeque::new(), - waiting: kio::WaiterList::new(), - streams: Vec::new(), - } - } - - /// Keep `producer`'s track, so the endpoint ending ends it. - fn hold(&mut self, producer: &track::Producer) { - self.streams.retain(|stream| !stream.is_closed()); - self.streams.push(producer.weak()); - } -} - -impl Shared { - /// This endpoint's broadcast in `role`, hidden until announced. - fn create(&self, role: Role) -> Result { - self.publisher - .route(endpoint_path(&self.prefix, role, &self.id)?, RouteOptions::hidden()) - } - - /// The route of the broadcast this endpoint dials from, announced by its first dial. - /// - /// Made outside the lock, since announcing wakes local consumers, and announced only - /// once it is the endpoint's, so a racing dial's spare is never seen. - fn client(&self) -> Result { - if let Some(route) = &self.state.lock().open()?.client { - return Ok(route.clone()); - } - let route = self.create(Role::Client)?; - let mut state = self.state.lock(); - let open = state.open()?; - if let Some(route) = &open.client { - return Ok(route.clone()); - } - open.client = Some(route.clone()); - drop(state); - route.announce()?; - Ok(route) - } - - /// Make the broadcast peers dial, with its handler, and announce it: the first accept. - /// - /// Called by the poller holding [`Handler::Taken`], so no other makes one. - fn serve(&self) -> Result { - let route = self.create(Role::Server)?; - // Before announcing, so the first dial finds it. - let requests = route.broadcast().requests(); - self.state.lock().open()?.server = Some(route.clone()); - route.announce()?; - Ok(requests) - } - - /// Queue every dial on hand and take the next, registering `waiter` for more, and fail - /// with the endpoint's end once it ended. The first call announces the - /// server broadcast, and fails as [`origin::Producer::route`] does when the origin may - /// not publish it. - /// - /// A request on the server broadcast naming this endpoint as the dialer names no dial - /// to it, since its own dials live in its client broadcast: it is reset with - /// [`Error::NotFound`]. So is a request naming another acceptor, such as a session - /// replayed from the endpoint it was dialed to. A request in another protocol than - /// `track` is reset with [`Error::Unsupported`]. - /// - /// The handler is polled with no lock held (see [`Handler`]), and resets and wakes go - /// out after the lock is released. - fn poll_dial(&self, waiter: &kio::Waiter) -> Poll> { - let taken = { - let mut state = self.state.lock(); - let open = state.open()?; - if let Handler::Taken(waiting) = &mut open.handler { - waiting.register(waiter); - None - } else { - match std::mem::replace(&mut open.handler, Handler::Taken(kio::WaiterList::new())) { - Handler::Idle(requests) => Some(Some(requests)), - // No server broadcast yet: this poller makes it. - Handler::None => Some(None), - Handler::Taken(_) => unreachable!("checked above"), - } - } - }; - let mut requests = match taken { - Some(None) => match self.serve() { - Ok(requests) => Some(requests), - Err(err) => { - // Given back for the next accept to try, waking those that found it taken, - // unless the endpoint ended meanwhile and took it. - let mut state = self.state.lock(); - let waiting = match state.open().map(|open| &mut open.handler) { - Ok(handler @ Handler::Taken(_)) => match std::mem::replace(handler, Handler::None) { - Handler::Taken(waiting) => Some(waiting), - _ => unreachable!("matched above"), - }, - _ => None, - }; - drop(state); - if let Some(mut waiting) = waiting { - waiting.wake(); - } - return Poll::Ready(Err(err)); - } - }, - taken => taken.flatten(), - }; - let mut taken = Vec::new(); - let mut ended = None; - if let Some(requests) = &mut requests { - loop { - match requests.poll_requested_track(waiter) { - Poll::Pending => break, - Poll::Ready(Ok(request)) => taken.push(request), - Poll::Ready(Err(err)) => { - ended = Some(err); - break; - } - } - } - } - - // The server broadcast ended: so does the endpoint, which wakes the pollers that - // found the handler taken. Dropped after, since `end` closes the server broadcast - // first: dropping the last handler rejects the dials still queued with `Dropped`, - // rather than the close's `Unroutable`. - if let Some(err) = ended { - let err = self.end(err); - drop(requests); - for request in taken { - request.reject(Error::Cancel); - } - return Poll::Ready(Err(err)); - } - - let mut resets = Vec::new(); - let mut wakes = Vec::new(); - let mut state = self.state.lock(); - let open = match state.open() { - Ok(open) => open, - // Ended meanwhile, which took the server broadcast and closed it or will: drop the - // handler, and reset the dials it took like the ones the end found queued. - Err(err) => { - drop(state); - drop(requests); - for request in taken { - request.reject(Error::Cancel); - } - return Poll::Ready(Err(err)); - } - }; - if let Some(handler) = requests.take() - && let Handler::Taken(mut waiting) = std::mem::replace(&mut open.handler, Handler::Idle(handler)) - { - wakes.push(waiting.take()); - } - // Dials whose dialer gave up leave the queue before an accept takes one, so it holds - // no more than the dials still open, and an accept gets a live one. - if open.dials.iter().any(|request| !request.demand().is_wanted()) { - let (live, abandoned): (VecDeque<_>, VecDeque<_>) = std::mem::take(&mut open.dials) - .into_iter() - .partition(|request| request.demand().is_wanted()); - open.dials = live; - resets.extend(abandoned.into_iter().map(|request| (request, Error::Cancel))); - } - for request in taken { - // `None` for a name naming no dial to this endpoint: outside the scheme, this - // endpoint's own, or for another acceptor. - let target = match parse(request.name()) { - Some(name) if name.acceptor == &*self.id && name.dialer != &*self.id => Some(name.protocol == PROTOCOL), - _ => None, - }; - match target { - Some(true) => { - open.dials.push_back(request); - wakes.push(open.waiting.take()); - } - Some(false) => resets.push((request, Error::Unsupported)), - None => resets.push((request, Error::NotFound)), - } - } - let next = match open.dials.pop_front() { - Some(request) => Poll::Ready(Ok(request)), - None => { - open.waiting.register(waiter); - Poll::Pending - } - }; - drop(state); - for mut wake in wakes { - wake.wake(); - } - for (request, err) in resets { - // Not the name: a session's name is its secret. - tracing::debug!(%err, "reset a request for no session"); - request.reject(err); - } - next - } - - /// Fail with [`Error::Unsupported`] while a session withholds this endpoint's broadcast - /// from its peer, so no peer beyond it can find it: a moq-lite client session before - /// 07, whose peer can't ask for hidden broadcasts. - fn poll_reachable(&self, waiter: &kio::Waiter) -> Result<(), Error> { - match self.withheld.poll(waiter) { - Poll::Ready(()) => Err(Error::Unsupported), - Poll::Pending => Ok(()), - } - } - - /// Poll for the endpoint's end, registering `waiter` for it. Either broadcast ending - /// ends the endpoint. - fn poll_end(&self, waiter: &kio::Waiter) -> Result<(), Error> { - let routes = { - let mut state = self.state.lock(); - let open = state.open()?; - [open.client.clone(), open.server.clone()] - }; - for route in routes.iter().flatten() { - if route.broadcast().poll_closed(waiter).is_ready() { - return Err(self.end(Error::Closed)); - } - } - Ok(()) - } - - /// End the endpoint with `err`, unless it ended already, and return why it ended. - /// - /// Retracts its broadcasts, resets the dials waiting to be accepted, and resets this - /// side's half of every session it handed out, all outside the lock. - fn end(&self, err: Error) -> Error { - let open = { - let mut state = self.state.lock(); - match &*state { - State::Closed(err) => return err.clone(), - State::Open(_) => match std::mem::replace(&mut *state, State::Closed(err.clone())) { - State::Open(open) => *open, - State::Closed(_) => unreachable!("matched above"), - }, - } - }; - let Open { - client, - server, - handler, - dials, - mut waiting, - streams, - } = open; - - // Reset as the session closing, and before retracting the broadcasts: a peer reading - // through a retracted route sees `Unroutable` instead. - for stream in streams { - stream.abort(Error::Stream(StreamError::Session(SessionError::Cancel))); - } - let routes = [client, server]; - for route in routes.iter().flatten() { - route.broadcast().close(); - } - for request in dials { - request.reject(Error::Cancel); - } - waiting.wake(); - // A poller holding the handler drops it once it sees the end. - if let Handler::Taken(mut waiting) = handler { - waiting.wake(); - } - drop(routes); - err - } -} - -/// A socket endpoint: a server, a client, or both. -/// -/// Its first dial announces `/.client/`, which holds its half of each session -/// it dials, and its first accept announces `/.server/`, where peers dial it and -/// which holds its half of each session it accepts. So a pure client never announces a -/// server broadcast, and the origin's grants decide which roles it may take. -/// -/// It accepts dials from its first accept on, like a TCP listener, and dials queue until -/// accepted. Until then it has no server broadcast, so a dial to it waits as for a peer -/// not online yet. -/// -/// Dropping it, [`Self::close`], or either of its broadcasts ending, retracts its broadcasts, -/// resets the dials waiting to be accepted, and resets this side's half of every session it -/// handed out. The peer's half -/// of a [`TrackStream`] is read through -/// its [`TrackStream::consumer`], which the application owns: drop the streams too, which -/// releases the subscriptions. -/// -/// Every hop between two endpoints must carry hidden broadcasts: moq-lite-07 or -/// moq-transport. While the origin has a moq-lite client session before 07, whose peer -/// never hears the endpoint's broadcast, dials and accepts fail with -/// [`Error::Unsupported`]; a session whose publish half opts into hidden broadcasts -/// ([`origin::Consumer::with_hidden`]) carries it and doesn't count. Nothing tells a -/// dialer that the peer sits behind such a hop, so a dial to it waits as for a peer not -/// announced yet, while the peer's own accepts fail. -pub struct Endpoint { - shared: Arc, -} - -impl Endpoint { - /// The endpoint `id` under `prefix` on `origin`, which also reaches the peers. Nothing - /// is announced until its first dial or accept. - /// - /// `id` names the endpoint: a well-known name for a server, or a random one for a - /// client. It must be one path segment. - pub fn new( - origin: &origin::Producer, - prefix: impl TryInto>, - id: &str, - ) -> Result { - let prefix = crate::path::convert(prefix)?; - for role in [Role::Server, Role::Client] { - endpoint_path(&prefix, role, id)?; - } - let consumer = origin.consume(); - Ok(Self { - shared: Arc::new(Shared { - id: id.into(), - prefix, - publisher: origin.clone(), - withheld: consumer.hidden_withheld(), - origin: consumer, - state: kio::Lock::new(State::Open(Box::new(Open::new()))), - }), - }) - } - - /// This endpoint's id. - pub fn id(&self) -> &str { - &self.shared.id - } - - /// The prefix the endpoint's broadcasts sit under, and where it finds its peers. - pub fn prefix(&self) -> &Path { - &self.shared.prefix - } - - /// Dial the server `peer` in a new session, resolving once the peer accepts it. - /// - /// `info` describes this side's half; `None` keeps every message until read, with no - /// max age. - /// - /// Waits for as long as the peer takes to accept: drop the returned future to give up, - /// which resets the session. That includes waiting for the peer's server broadcast to - /// be announced. Fails with the peer's reset when it rejects the dial, or does not - /// accept, and with [`Error::Unsupported`] for a dial to this endpoint itself or across - /// a hop that can't carry it (see [`Endpoint`]). The first dial announces the client - /// broadcast, and fails as [`origin::Producer::route`] does when the origin may not - /// publish it. - pub fn dial_track(&self, peer: &str, info: impl Into>) -> Result, Error> { - let info = info.into().unwrap_or_else(messages); - let shared = &self.shared; - let path = endpoint_path(&shared.prefix, Role::Server, peer)?; - if peer == &*shared.id { - return Err(Error::Unsupported); - } - let client = shared.client()?; - let session = session_id(); - let name = track_name(PROTOCOL, &shared.id, peer, &session); - // This side's half, waiting for the peer to ask for it, which only the peer can: - // nobody else knows the session. - let answer = client.broadcast().reserve_unlisted_track(name.clone())?; - // Followed rather than requested, so a peer not announced yet is waited for. - let resolving = Box::new(shared.origin.follow(path, None)?); - Ok(kio::Pending::new(Dialing { - shared: shared.clone(), - peer: peer.into(), - session, - name, - info: Some(info), - demand: answer.demand(), - answer: Some(Answer::Reserved(answer)), - step: Step::Resolving(resolving), - })) - } - - /// The next dial from a peer; fails with [`Error::Closed`] once the endpoint closed, - /// and with [`Error::Unsupported`] while no peer can reach it (see [`Endpoint`]). - /// - /// The first call starts accepting dials. They queue until taken, or until their - /// dialer gives up, so keep accepting, and accept or reject each one. The first call - /// also announces the server broadcast, and fails as [`origin::Producer::route`] does - /// when the origin may not publish it. - #[cfg_attr(feature = "async", kio::wrapper)] - pub fn poll_accept_track(&self, waiter: &kio::Waiter) -> Poll> { - let shared = &self.shared; - shared.poll_reachable(waiter)?; - let request = ready!(shared.poll_dial(waiter))?; - let name = parse(request.name()).expect("sorted as a dial"); - Poll::Ready(Ok(Incoming { - shared: shared.clone(), - peer: name.dialer.into(), - session: name.session.into(), - request: Some(request), - })) - } - - /// Close the endpoint; see [`Endpoint`]. - pub fn close(&self) { - self.shared.end(Error::Closed); - } -} - -impl Drop for Endpoint { - fn drop(&mut self) { - self.shared.end(Error::Closed); - } -} - -/// A dial from a peer, from [`Endpoint::poll_accept_track`] (or its async `accept_track`): -/// accept or reject it. -/// -/// Dropping it rejects it with [`Error::Cancel`]. -pub struct Incoming { - shared: Arc, - peer: Arc, - session: Arc, - /// The dialer's request for this side's half; taken by an accept or a reject. - request: Option, -} - -impl Incoming { - /// The id the dialer claims. An accept checks it: the dial connects only if that - /// endpoint really dialed this session to this endpoint. - pub fn peer(&self) -> &str { - &self.peer - } - - /// The session's id, which the dialer chose. - pub fn session(&self) -> &str { - &self.session - } - - /// Reject the dial, resetting the dialer's request with [`Error::NotFound`], as for a - /// session that does not exist. - pub fn reject(mut self) { - if let Some(request) = self.request.take() { - request.reject(Error::NotFound); - } - } - - /// Accept the dial, resolving once the dialer answers, which proves it dialed. - /// - /// `info` describes this side's half, as for [`Endpoint::dial_track`]. Nothing is - /// written before the dialer answers. - pub fn accept(mut self, info: impl Into>) -> kio::Pending { - let info = info.into().unwrap_or_else(messages); - let shared = self.shared.clone(); - let (peer, session) = (self.peer.clone(), self.session.clone()); - let request = self.request.take().expect("taken only by accept, reject or drop"); - // Followed rather than requested: the dialer announced its client broadcast before - // it dialed, but a relay can deliver the dial first. A dial claiming an endpoint - // nobody announces waits for as long as its dialer holds it open. - let step = match endpoint_path(&shared.prefix, Role::Client, &peer) - .and_then(|path| shared.origin.follow(path, None)) - { - Ok(following) => Step::Resolving(Box::new(following)), - Err(err) => Step::Failed(err), - }; - kio::Pending::new(Accepting { - shared: shared.clone(), - name: track_name(PROTOCOL, &peer, &shared.id, &session), - peer, - session, - info: Some(info), - demand: request.demand(), - request: Some(request), - step, - }) - } -} - -impl Drop for Incoming { - fn drop(&mut self) { - if let Some(request) = self.request.take() { - request.reject(Error::Cancel); - } - } -} - -/// How far a dial or an accept got in reading the peer's half. -enum Step { - /// Waiting for the peer's endpoint broadcast to be announced. - Resolving(Box), - /// Subscribed to the peer's half, waiting for the peer to serve it. - Subscribing(track::Subscribing), - /// The peer serves its half. - Subscribed(Box), - /// It failed before it started. - Failed(Error), - /// Handed out, or failed. - Done, -} - -impl Step { - /// Advance until the peer serves its half, subscribing to `track` in its endpoint. - fn poll(&mut self, waiter: &kio::Waiter, track: &str) -> Poll> { - loop { - match self { - Self::Resolving(following) => { - // Only the first call is made here, and it never reports the path offline. - let broadcast = ready!(following.poll_available(waiter))?.expect("the first call is never offline"); - let subscribing = broadcast.track(track)?.subscribe(subscription()).into_inner(); - *self = Self::Subscribing(subscribing); - } - Self::Subscribing(subscribing) => { - let subscriber = ready!(subscribing.poll_ok(waiter))?; - *self = Self::Subscribed(Box::new(subscriber)); - } - Self::Subscribed(_) => return Poll::Ready(Ok(())), - Self::Failed(err) => return Poll::Ready(Err(err.clone())), - Self::Done => return Poll::Ready(Err(Error::Closed)), - } - } - } - - fn take_subscriber(&mut self) -> track::Subscriber { - match std::mem::replace(self, Self::Done) { - Self::Subscribed(subscriber) => *subscriber, - _ => unreachable!("taken once subscribed"), - } - } -} - -/// Hand out a session's stream once its handshake is done, unless the endpoint stopped, -/// in which case this side's half is aborted, outside the lock. -fn open( - shared: &Shared, - producer: track::Producer, - step: &mut Step, - peer: &Arc, - session: &Arc, -) -> Result { - let held = shared.state.lock().open().map(|open| open.hold(&producer)); - if let Err(err) = held { - let _ = producer.abort(err.clone()); - return Err(err); - } - Ok(TrackStream { - producer, - consumer: step.take_subscriber(), - peer: peer.clone(), - session: session.clone(), - }) -} - -/// This side's half of a dial. -enum Answer { - /// Reserved, until the peer asks for it. - Reserved(track::Request), - /// Served, once the peer asked. - Served(track::Producer), -} - -impl Answer { - /// Reset the half for the peer with `err`. - fn reset(self, err: Error) { - match self { - Self::Reserved(request) => request.reject(err), - Self::Served(producer) => { - let _ = producer.abort(err); - } - } - } -} - -/// A dial, from [`Endpoint::dial_track`]: resolves once the peer accepts. -/// -/// Dropping it gives up, resetting the session. -pub struct Dialing { - shared: Arc, - peer: Arc, - session: Arc, - /// The session's track name. - name: String, - info: Option, - /// Whether the peer asked for this side's half, which it does on accept. - demand: track::Demand, - answer: Option, - step: Step, -} - -impl Dialing { - /// Poll for the peer to accept; see [`Endpoint::dial_track`]. - pub fn poll_ok(&mut self, waiter: &kio::Waiter) -> Poll> { - let result = self.poll_inner(waiter); - if let Poll::Ready(Err(err)) = &result { - if let Some(answer) = self.answer.take() { - answer.reset(err.clone()); - } - self.step = Step::Done; - } - result - } - - fn poll_inner(&mut self, waiter: &kio::Waiter) -> Poll> { - self.shared.poll_end(waiter)?; - self.shared.poll_reachable(waiter)?; - - // The peer asks for this side's half when it accepts: serve it, which proves to the - // peer that this endpoint dialed. - if let Some(Answer::Reserved(_)) = &self.answer { - // Polled until it parks, so the next change wakes this. - while let Poll::Ready(changed) = self.demand.poll_wanted_changed(waiter) { - // Ends once the reservation does: the endpoint closed. - changed?; - } - if self.demand.wanted().is_some() - && let Some(Answer::Reserved(request)) = self.answer.take() - { - self.answer = Some(Answer::Served(request.accept(self.info.take()))); - } - } - - ready!(self.step.poll(waiter, &self.name))?; - // The peer serves its half only after asking for this side's, so a conforming peer - // asked already; wait for its request regardless. - let Some(Answer::Served(_)) = &self.answer else { - return Poll::Pending; - }; - let Some(Answer::Served(producer)) = self.answer.take() else { - unreachable!("checked above"); - }; - Poll::Ready(open(&self.shared, producer, &mut self.step, &self.peer, &self.session)) - } -} - -impl kio::Task for Dialing { - type Output = Result; - - fn poll(&mut self, waiter: &kio::Waiter) -> Poll { - self.poll_ok(waiter) - } -} - -impl Drop for Dialing { - fn drop(&mut self) { - // Given up: reset the session for the peer. - if let Some(answer) = self.answer.take() { - answer.reset(Error::Cancel); - } - } -} - -/// An accept, from an [`Incoming`] dial: resolves once the dialer answers. -pub struct Accepting { - shared: Arc, - peer: Arc, - session: Arc, - /// The session's track name. - name: String, - info: Option, - /// The dialer's request for this side's half, served once the dialer answers. - request: Option, - /// Whether the dialer still subscribes to this side's half: the dial stays open while - /// it does. - demand: track::Demand, - step: Step, -} - -impl Accepting { - /// Poll for the dialer to answer, which proves it dialed. - /// - /// Waits for the dialer's endpoint to be announced for as long as the dial stays open, - /// and fails with [`Error::Cancel`] once the dialer gives up, or with - /// [`Error::Unsupported`] while this endpoint can't reach it (see [`Endpoint`]). Fails - /// with the dialer's reset when it never dialed this session, such as a dial forged in - /// its name, and resets the dialer's request with the same error. - pub fn poll_ok(&mut self, waiter: &kio::Waiter) -> Poll> { - let result = self.poll_inner(waiter); - if let Poll::Ready(Err(err)) = &result { - self.step = Step::Done; - if let Some(request) = self.request.take() { - request.reject(err.clone()); - } - } - result - } - - fn poll_inner(&mut self, waiter: &kio::Waiter) -> Poll> { - self.shared.poll_end(waiter)?; - self.shared.poll_reachable(waiter)?; - if self.demand.poll_state(waiter) != track::DemandState::Active { - return Poll::Ready(Err(Error::Cancel)); - } - ready!(self.step.poll(waiter, &self.name))?; - - // Accepted outside the lock, since it wakes the dialer. - let request = self.request.take().ok_or(Error::Closed)?; - let producer = request.accept(self.info.take()); - Poll::Ready(open(&self.shared, producer, &mut self.step, &self.peer, &self.session)) - } -} - -impl kio::Task for Accepting { - type Output = Result; - - fn poll(&mut self, waiter: &kio::Waiter) -> Poll { - self.poll_ok(waiter) - } -} - -impl Drop for Accepting { - fn drop(&mut self) { - // Given up: reset the dialer's request. - if let Some(request) = self.request.take() { - request.reject(Error::Cancel); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn track_names_round_trip() { - let name = track_name("track", "alice", "bob", "0123abcd"); - assert_eq!(name, "track/alice/bob/0123abcd"); - assert_eq!( - parse(&name), - Some(Name { - protocol: "track", - dialer: "alice", - acceptor: "bob", - session: "0123abcd" - }) - ); - } - - #[test] - fn foreign_track_names_are_skipped() { - for name in [ - "command", - "alice/s", - "track/alice/s", - "track/alice/bob/", - "track//bob/s", - "track/alice//s", - "/alice/bob/s", - "track/alice/bob/s/0", - "track/a\\b/bob/s", - "track/alice/bob/s\\", - ] { - assert_eq!(parse(name), None, "{name}"); - } - } - - #[test] - fn an_endpoint_path_holds_one_segment() { - let prefix = Path::from_static("room"); - assert_eq!( - endpoint_path(&prefix, Role::Server, "alice").unwrap().as_str(), - "room/.server/alice" - ); - assert_eq!( - endpoint_path(&prefix, Role::Client, "alice").unwrap().as_str(), - "room/.client/alice" - ); - for role in [Role::Server, Role::Client] { - assert!(is_endpoint(&endpoint_path(&prefix, role, "alice").unwrap())); - assert!(endpoint_path(&prefix, role, "a/b").is_err()); - assert!(endpoint_path(&prefix, role, "").is_err()); - assert!(endpoint_path(&prefix, role, "a\\b").is_err()); - } - for path in [ - "room/alice", - ".server", - "room/.server", - "room/.server/alice/cam", - "room/server/alice", - ] { - assert!(!is_endpoint(&Path::new(path).unwrap()), "{path}"); - } - } - - /// Taking a dial wakes the dialer's subscription, and an inline executor can re-enter - /// the endpoint from that wake, such as to accept again or to close it, so the endpoint - /// polls its handler with no lock held. - #[test] - fn taking_a_dial_wakes_outside_the_lock() { - use std::sync::atomic::{AtomicBool, Ordering}; - use std::task::Wake; - - struct Close { - shared: Arc, - woken: AtomicBool, - } - impl Wake for Close { - fn wake(self: Arc) { - // The handler is out being polled, so this waits for it. - let accept = self.shared.poll_dial(&kio::Waiter::noop()); - assert!(accept.is_pending()); - self.shared.end(Error::Closed); - self.woken.store(true, Ordering::SeqCst); - } - } - - let (done, finished) = std::sync::mpsc::channel(); - let thread = std::thread::spawn(move || { - let (origin, _driver) = origin::Producer::new(origin::Config::new(std::num::NonZeroU64::new(1).unwrap())); - let endpoint = Endpoint::new(&origin, "room", "bob").unwrap(); - let noop = kio::Waiter::noop(); - // Listening, so the dial queues for the handler. - assert!(endpoint.poll_accept_track(&noop).is_pending()); - - let server = endpoint.shared.state.lock().open().unwrap().server.clone().unwrap(); - let broadcast = server.broadcast().consume(); - let dial = broadcast - .track("track/alice/bob/0123456789abcdef0123456789abcdef") - .unwrap() - .subscribe(None) - .into_inner(); - let close = Arc::new(Close { - shared: endpoint.shared.clone(), - woken: AtomicBool::new(false), - }); - let waiter = kio::Waiter::new(std::task::Waker::from(close.clone())); - assert!(dial.poll_ok(&waiter).is_pending()); - - // The wake closes the endpoint while this takes the dial, which it resets. - assert!(matches!( - endpoint.poll_accept_track(&noop), - Poll::Ready(Err(Error::Closed)) - )); - assert!(close.woken.load(Ordering::SeqCst)); - assert!(matches!(dial.poll_ok(&noop), Poll::Ready(Err(_)))); - done.send(()).unwrap(); - }); - let timeout = std::time::Duration::from_secs(10); - if let Err(std::sync::mpsc::RecvTimeoutError::Timeout) = finished.recv_timeout(timeout) { - panic!("deadlocked re-entering the endpoint"); - } - thread.join().unwrap(); - } - - /// A dial whose dialer gave up leaves the queue, so an accept never takes it, and - /// dialing and giving up again never piles up requests. - #[test] - fn abandoned_dials_leave_the_queue() { - let (origin, _driver) = origin::Producer::new(origin::Config::new(std::num::NonZeroU64::new(1).unwrap())); - let endpoint = Endpoint::new(&origin, "room", "bob").unwrap(); - let noop = kio::Waiter::noop(); - assert!(endpoint.poll_accept_track(&noop).is_pending()); - - let server = endpoint.shared.state.lock().open().unwrap().server.clone().unwrap(); - let broadcast = server.broadcast().consume(); - let queued = || endpoint.shared.state.lock().open().unwrap().dials.len(); - let dial = |session: u32| { - let dial = broadcast - .track(&format!("track/alice/bob/{session:032x}")) - .unwrap() - .subscribe(None) - .into_inner(); - assert!(dial.poll_ok(&noop).is_pending()); - dial - }; - let accept = || match endpoint.poll_accept_track(&noop) { - Poll::Ready(Ok(incoming)) => incoming.session().to_string(), - _ => panic!("no dial to accept"), - }; - - // The accept takes both dials on hand and queues the second. - let _first = dial(0); - let mut waiting = dial(1); - assert_eq!(accept(), format!("{:032x}", 0)); - for session in (2..8).step_by(2) { - assert_eq!(queued(), 1, "dial {session}"); - drop(waiting); - let _taken = dial(session); - waiting = dial(session + 1); - assert_eq!(accept(), format!("{session:032x}")); - } - assert_eq!(queued(), 1); - } - - /// Either broadcast ending ends the endpoint as closing it does: the sessions it handed - /// out are reset, and it dials and accepts no more. - #[kio::test] - #[cfg(feature = "async")] - async fn a_broadcast_ending_ends_the_endpoint() { - let (origin, driver) = origin::Producer::new(origin::Config::new(std::num::NonZeroU64::new(1).unwrap())); - let _driver = kio::clock::sim::spawn(crate::time::run(driver)); - let alice = Endpoint::new(&origin, "room", "alice").unwrap(); - let bob = Endpoint::new(&origin, "room", "bob").unwrap(); - let accept = async { bob.accept_track().await.unwrap().accept(None).await.unwrap() }; - let (dialed, accepted) = - futures::join!(async { alice.dial_track("bob", None).unwrap().await.unwrap() }, accept); - - let server = bob.shared.state.lock().open().unwrap().server.clone().unwrap(); - server.broadcast().close(); - assert!(matches!(bob.accept_track().await, Err(Error::Closed))); - assert!( - accepted.producer.append_group(crate::Timestamp::unscaled(0)).is_err(), - "a handed-out half outlived the end" - ); - assert!(matches!(bob.dial_track("alice", None), Err(Error::Closed))); - drop(dialed); - } - - #[test] - fn session_ids_are_128_bits() { - let (a, b) = (session_id(), session_id()); - assert_eq!(a.len(), 32); - assert_ne!(a, b); - } -} diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 200ec629bcb..d023fb2c434 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5160,6 +5160,12 @@ impl Producer { } } + /// A handle that aborts the track without keeping it open, for an owner that hands this + /// producer out and must end the track on its own shutdown. + pub fn abort_handle(&self) -> AbortHandle { + AbortHandle(Arc::downgrade(&self.alive)) + } + /// Create a [`Demand`]: a cloneable, watch-only handle to this track's /// subscriber demand. /// @@ -5633,6 +5639,33 @@ fn clamp_combined(combined: Option, bound: Option) -> Op Some(combined) } +/// Aborts a track without keeping it open, from [`Producer::abort_handle`]. +/// +/// Holding one neither keeps the track alive nor counts as its producer, so a track whose +/// producers all dropped closes as usual and the handle then does nothing. +#[derive(Clone)] +pub struct AbortHandle(std::sync::Weak); + +impl AbortHandle { + /// Whether the track closed: aborted, or every producer gone. + pub fn is_closed(&self) -> bool { + self.0.upgrade().is_none_or(|alive| alive.state.is_closed()) + } + + /// Abort the track with `err`, as [`Producer::abort`] does, unless it already closed. + pub fn abort(&self, err: Error) { + let Some(alive) = self.0.upgrade() else { + return; + }; + // Unlisted first, as `Producer::abort` does, so the name is free only once it left. + alive.unlist(false); + let Ok(state) = alive.state.write() else { + return; + }; + commit_abort(state, err, None).abort(); + } +} + /// A weak reference to a track that doesn't prevent auto-close. #[derive(Clone)] pub(super) struct TrackWeak { @@ -5697,21 +5730,6 @@ impl TrackWeak { true } - /// Abort the track with `err`, as [`Producer::abort`] does, unless it already closed. - /// - /// For an owner that hands a track's [`Producer`] out and must end it on its own - /// shutdown without keeping it alive. Leaves the listing alone: such a track is one - /// served on request, which is never listed. - pub(super) fn abort(&self, err: Error) { - let Some(producer) = self.state.produce() else { - return; - }; - let Ok(state) = producer.write() else { - return; - }; - commit_abort(state, err, None).abort(); - } - /// Whether its producer declared the track's end, which is how its listing ends too. pub(super) fn is_finished(&self) -> bool { self.state.read().outcome.end().is_some() diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 3c6f447668a..94c0937d9bd 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -79,7 +79,6 @@ mod session_panic; mod settled_end_survives_abort; mod setup_negotiation; mod slow_joiner; -mod socket; mod source_errors; mod stream_credit; mod subscribe_priority; diff --git a/rs/moq-net/tests/integration/socket.rs b/rs/moq-net/tests/integration/socket.rs deleted file mode 100644 index 7430ab6bd3d..00000000000 --- a/rs/moq-net/tests/integration/socket.rs +++ /dev/null @@ -1,705 +0,0 @@ -#![cfg(feature = "async")] - -//! Sockets through a relay: endpoints on their own origins, each with a session to a -//! relay, dialing and accepting `track` sessions over moq-lite-07 and moq-transport. - -use std::time::Duration; - -use moq_net::broadcast::TracksEvent; -use moq_net::socket::{Endpoint, TrackStream}; -use moq_net::{Error, StreamError, Version, group, origin, track}; - -use crate::support::AppendNext; -use crate::support::harness::{MockConnectOptions, MockPair, connect_mock, spawn}; - -const TIMEOUT: Duration = Duration::from_secs(10); - -/// Long enough for every hop to settle, in simulated time. -const SETTLE: Duration = Duration::from_secs(1); - -const LITE: &str = "moq-lite-07-wip"; -const IETF: &str = "moq-transport-18"; - -fn origin(hop: u64) -> origin::Producer { - let (origin, driver) = origin::Producer::new(origin::Config::new(std::num::NonZeroU64::new(hop).unwrap())); - spawn(driver); - origin.with_proxy(true) -} - -/// A relay and the clients connected to it. -struct Fixture { - relay: origin::Producer, - next: u64, - sessions: Vec, -} - -impl Fixture { - fn new() -> Self { - Self { - relay: origin(1), - next: 2, - sessions: Vec::new(), - } - } - - /// A client publishing and subscribing through one origin, connected to the relay over - /// `version`. - async fn client(&mut self, version: &str) -> origin::Producer { - let client = self.origin(); - self.connect(&client, version, client.consume()).await; - client - } - - /// An origin for a client, not connected yet. - fn origin(&mut self) -> origin::Producer { - self.next += 1; - origin(self.next - 1) - } - - /// Connect `client` to the relay over `version`, publishing `publish`. - async fn connect(&mut self, client: &origin::Producer, version: &str, publish: origin::Consumer) { - let mut options = MockConnectOptions::new(version.parse::().unwrap()); - options.client_publish = Some(publish); - options.client_subscribe = Some(client.clone()); - options.server_publish = Some(self.relay.consume()); - options.server_subscribe = Some(self.relay.clone()); - // Discovery follows the endpoint's own requests, as it does for an application. - options.client_discovers_all = false; - self.sessions.push(connect_mock(options).await); - } - - /// An endpoint `id` on its own client of the relay. - async fn endpoint(&mut self, version: &str, id: &str) -> Endpoint { - let client = self.client(version).await; - Endpoint::new(&client, "room", id).unwrap() - } -} - -/// Start `endpoint` accepting dials, which announces its server broadcast, for a test -/// that dials it by hand. -fn listen(endpoint: &Endpoint) { - assert!(endpoint.poll_accept_track(&kio::Waiter::noop()).is_pending()); -} - -/// Dial `acceptor` from `dialer` in a session while `acceptor` accepts, returning both -/// ends. -async fn connect_track(dialer: &Endpoint, acceptor: &Endpoint) -> (TrackStream, TrackStream) { - let dial = dialer.dial_track(acceptor.id(), None).unwrap(); - let accept = async { - let incoming = acceptor.accept_track().await?; - assert_eq!(incoming.peer(), dialer.id()); - incoming.accept(None).await - }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("connect timeout"); - (dialed.expect("dial"), accepted.expect("accept")) -} - -/// Write `payload` as one message of a session: one group. -fn send(track: &track::Producer, payload: &'static [u8]) { - let mut group = track.append_next().unwrap(); - group - .write_frame(moq_net::Timestamp::unscaled(group.start), payload) - .unwrap(); - group.finish().unwrap(); -} - -/// Read the next message from a session, or `None` once the peer finished. -async fn recv(reader: &mut track::Ordered) -> Result>, Error> { - kio::clock::timeout(TIMEOUT, async { - let Some(item) = reader.next_group().await? else { - return Ok(None); - }; - let mut group = item.group().expect("a group, not a gap"); - let frame = group.read_frame().await?.expect("a frame"); - Ok(Some(frame.payload.to_vec())) - }) - .await - .expect("read timeout") -} - -/// Messages flow both ways, in order, over every pairing of moq-lite-07 and -/// moq-transport: the handshake uses neither instances nor track listings. -#[kio::test] -async fn track_sessions_carry_messages_both_ways() { - for (dialer, acceptor) in [(LITE, LITE), (LITE, IETF), (IETF, LITE), (IETF, IETF)] { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(dialer, "alice").await; - let bob = fixture.endpoint(acceptor, "bob").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - assert_eq!(dialed.peer(), "bob"); - assert_eq!(accepted.peer(), "alice"); - assert_eq!(dialed.session(), accepted.session()); - let mut reader = accepted.consumer.ordered(); - let mut replies = dialed.consumer.ordered(); - - for message in [b"one", b"two", b"six"] { - send(&dialed.producer, message); - } - for message in [b"one", b"two", b"six"] { - assert_eq!( - recv(&mut reader).await.unwrap().unwrap(), - message, - "{dialer} to {acceptor}" - ); - } - send(&accepted.producer, b"pong"); - assert_eq!(recv(&mut replies).await.unwrap().unwrap(), b"pong"); - - // Finishing a half closes the pipe that way. - let _finished = dialed.producer.finish().unwrap(); - assert_eq!(recv(&mut reader).await.unwrap(), None); - } -} - -/// Aborting a half is an error: the peer's read fails instead of ending. -#[kio::test] -async fn an_aborted_half_fails_the_read() { - for version in [LITE, IETF] { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(version, "alice").await; - let bob = fixture.endpoint(version, "bob").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - let mut reader = accepted.consumer.ordered(); - send(&dialed.producer, b"partial"); - assert_eq!(recv(&mut reader).await.unwrap().unwrap(), b"partial"); - dialed.producer.abort(Error::Cancel).unwrap(); - assert!(recv(&mut reader).await.is_err(), "{version}: an abort read as a finish"); - } -} - -/// Messages are read in sequence even when they are written, and so arrive, out of order. -#[kio::test] -async fn track_messages_are_read_in_key() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - let mut reader = accepted.consumer.ordered(); - - let write = |key: u64, payload: &'static [u8]| { - let mut group = dialed.producer.create_group(group::Info { start: key }).unwrap(); - group - .write_frame(moq_net::Timestamp::unscaled(group.start), payload) - .unwrap(); - group.finish().unwrap(); - }; - write(1, b"second"); - // Let the later message arrive first. A reader with no delay limit waits a second for a - // missing message the publisher may still write, then gives it up, so this stays under it. - assert!(kio::clock::timeout(SETTLE / 2, reader.next_group()).await.is_err()); - write(0, b"first"); - assert_eq!(recv(&mut reader).await.unwrap().unwrap(), b"first"); - assert_eq!(recv(&mut reader).await.unwrap().unwrap(), b"second"); -} - -/// With a finite delay budget, a message that never arrives stops blocking the ones after -/// it once the budget runs out. -#[kio::test] -async fn a_lost_track_message_does_not_block_later_ones() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - let lossy = accepted - .consumer - .subscription() - .with_delay(track::Delay::default().with_max(Duration::from_millis(500))); - accepted.consumer.control().update(lossy).unwrap(); - let mut reader = accepted.consumer.ordered(); - - let write = |key: u64, payload: &'static [u8]| { - let mut group = dialed.producer.create_group(group::Info { start: key }).unwrap(); - group - .write_frame(moq_net::Timestamp::unscaled(group.start), payload) - .unwrap(); - group.finish().unwrap(); - }; - // Message 0 is never written: message 1 waits out the budget, then is read. - write(1, b"one"); - assert_eq!(recv(&mut reader).await.unwrap().unwrap(), b"one"); - write(2, b"two"); - assert_eq!(recv(&mut reader).await.unwrap().unwrap(), b"two"); -} - -/// A pair of endpoints holds several sessions at once, each its own pipe. -#[kio::test] -async fn several_sessions_share_a_pair() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let (first, first_accepted) = connect_track(&alice, &bob).await; - let (second, second_accepted) = connect_track(&alice, &bob).await; - // Either side may dial. - let (third, third_accepted) = connect_track(&bob, &alice).await; - assert_ne!(first.session(), second.session()); - - send(&second.producer, b"second"); - send(&first.producer, b"first"); - send(&third_accepted.producer, b"third"); - assert_eq!( - recv(&mut first_accepted.consumer.ordered()).await.unwrap().unwrap(), - b"first" - ); - assert_eq!( - recv(&mut second_accepted.consumer.ordered()).await.unwrap().unwrap(), - b"second" - ); - assert_eq!(recv(&mut third.consumer.ordered()).await.unwrap().unwrap(), b"third"); -} - -/// Rejecting a dial resets it. -#[kio::test] -async fn a_rejected_dial_fails() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let dial = alice.dial_track("bob", None).unwrap(); - let reject = async { - bob.accept_track().await.unwrap().reject(); - }; - let (dialed, ()) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, reject) }) - .await - .expect("reject timeout"); - assert!(dialed.is_err(), "a rejected dial connected"); -} - -/// A dial in another protocol than `track` is reset and never surfaces as a dial. -#[kio::test] -async fn an_unknown_protocol_is_reset() { - let mut fixture = Fixture::new(); - let bob = fixture.endpoint(LITE, "bob").await; - listen(&bob); - let mallory = fixture.client(LITE).await; - let broadcast = kio::clock::timeout(TIMEOUT, mallory.consume().request("room/.server/bob", None)) - .await - .expect("request timeout") - .expect("bob's endpoint"); - for protocol in ["tcp", "quic"] { - let unknown = broadcast - .track(&format!("{protocol}/mallory/bob/0123456789abcdef0123456789abcdef")) - .unwrap() - .subscribe(None); - let unknown = async { kio::clock::timeout(TIMEOUT, unknown).await }; - let accept = async { kio::clock::timeout(SETTLE, bob.accept_track()).await }; - let (unknown, accepted) = futures::join!(unknown, accept); - assert!(unknown.expect("unknown timeout").is_err(), "{protocol}"); - assert!(accepted.is_err(), "a {protocol} dial surfaced as a dial"); - } -} - -/// A dial waits for its peer's endpoint to be announced, and a dial to itself fails. -#[kio::test] -async fn a_dial_waits_for_its_peer() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - assert!(matches!(alice.dial_track("alice", None), Err(Error::Unsupported))); - let mut dial = alice.dial_track("bob", None).unwrap(); - assert!( - kio::clock::timeout(SETTLE, &mut dial).await.is_err(), - "a dial to nobody resolved" - ); - - let bob = fixture.endpoint(LITE, "bob").await; - let accept = async { bob.accept_track().await?.accept(None).await }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("connect timeout"); - let (dialed, accepted) = (dialed.unwrap(), accepted.unwrap()); - assert_eq!(dialed.session(), accepted.session()); -} - -/// An accept waits for the dialer's endpoint while the dial stays open, since a relay can -/// deliver a dial before the dialer's announcement: carol dials bob before she announces -/// her endpoint, and bob's accept connects once she does. -#[kio::test] -async fn an_accept_waits_for_its_dialer() { - let mut fixture = Fixture::new(); - let bob = fixture.endpoint(LITE, "bob").await; - listen(&bob); - let carol = fixture.client(LITE).await; - let name = "track/carol/bob/0123456789abcdef0123456789abcdef"; - let broadcast = kio::clock::timeout(TIMEOUT, carol.consume().request("room/.server/bob", None)) - .await - .expect("request timeout") - .expect("bob's endpoint"); - let dial = broadcast.track(name).unwrap().subscribe(None); - - let incoming = kio::clock::timeout(TIMEOUT, bob.accept_track()) - .await - .expect("accept timeout") - .unwrap(); - assert_eq!(incoming.peer(), "carol"); - let mut accept = incoming.accept(None); - assert!( - kio::clock::timeout(SETTLE, &mut accept).await.is_err(), - "bob's accept ended before carol announced" - ); - - let endpoint = carol - .route("room/.client/carol", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - let mut requests = endpoint.broadcast().requests(); - endpoint.announce().unwrap(); - let answer = async { - let request = kio::wait(|waiter| requests.poll_requested_track(waiter)).await.unwrap(); - assert_eq!(request.name(), name); - request.accept(None) - }; - let (accepted, _half, dialed) = kio::clock::timeout(TIMEOUT, async { futures::join!(accept, answer, dial) }) - .await - .expect("connect timeout"); - assert_eq!(accepted.expect("accept").peer(), "carol"); - dialed.expect("bob's half"); -} - -/// An accept of a dial claiming an endpoint nobody announces waits only while the dial -/// stays open: the forger dropping it fails the accept. -#[kio::test] -async fn an_accept_ends_with_its_dial() { - let mut fixture = Fixture::new(); - let bob = fixture.endpoint(LITE, "bob").await; - listen(&bob); - let mallory = fixture.client(LITE).await; - let broadcast = kio::clock::timeout(TIMEOUT, mallory.consume().request("room/.server/bob", None)) - .await - .expect("request timeout") - .expect("bob's endpoint"); - let forged = broadcast - .track("track/carol/bob/0123456789abcdef0123456789abcdef") - .unwrap() - .subscribe(None); - let incoming = kio::clock::timeout(TIMEOUT, bob.accept_track()) - .await - .expect("accept timeout") - .unwrap(); - assert_eq!(incoming.peer(), "carol"); - let mut accept = incoming.accept(None); - assert!( - kio::clock::timeout(SETTLE, &mut accept).await.is_err(), - "bob's accept ended while the dial was open" - ); - - drop(forged); - let accepted = kio::clock::timeout(TIMEOUT, accept).await.expect("accept timeout"); - assert!(accepted.is_err(), "bob connected to nobody"); -} - -/// A session is bound to both its ends: an endpoint alice dialed knows the session's id, -/// but replaying it to carol connects neither under bob's name nor under carol's. -#[kio::test] -async fn a_replayed_session_never_connects() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let carol = fixture.endpoint(LITE, "carol").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - let session = accepted.session().to_string(); - - // Replayed from bob's side, which knows the session. - listen(&carol); - let mallory = fixture.client(LITE).await; - let broadcast = kio::clock::timeout(TIMEOUT, mallory.consume().request("room/.server/carol", None)) - .await - .expect("request timeout") - .expect("carol's endpoint"); - - // Under the name alice dialed: carol is not its acceptor, so she resets it unheard. - let replayed = broadcast - .track(&format!("track/alice/bob/{session}")) - .unwrap() - .subscribe(None); - // Carol accepts first, so the replay reaches her listening. - let accept = async { kio::clock::timeout(SETTLE, carol.accept_track()).await }; - let (accepted_by_carol, replayed) = futures::join!(accept, async { kio::clock::timeout(TIMEOUT, replayed).await }); - assert!( - replayed.expect("replay timeout").is_err(), - "carol served a replayed session" - ); - assert!(accepted_by_carol.is_err(), "a session for bob surfaced at carol"); - - // Renamed for carol: alice never dialed it, so she resets carol's accept. - let renamed = broadcast - .track(&format!("track/alice/carol/{session}")) - .unwrap() - .subscribe(None); - let incoming = kio::clock::timeout(TIMEOUT, carol.accept_track()) - .await - .expect("accept timeout") - .unwrap(); - assert_eq!(incoming.peer(), "alice"); - let (accepted_by_carol, renamed) = - kio::clock::timeout(TIMEOUT, async { futures::join!(incoming.accept(None), renamed) }) - .await - .expect("connect timeout"); - assert!(accepted_by_carol.is_err(), "carol connected to a replayed session"); - assert!(renamed.is_err(), "the replayer read carol's half"); - - // The real session is untouched. - send(&dialed.producer, b"still here"); - assert_eq!( - recv(&mut accepted.consumer.ordered()).await.unwrap().unwrap(), - b"still here" - ); -} - -/// A third party asking bob for a session in alice's name surfaces at bob as a dial from -/// alice, but alice never dialed it, so she resets bob's accept, and the forger reads -/// nothing. -#[kio::test] -async fn a_forged_dial_never_connects() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let mallory = fixture.client(LITE).await; - // Alice is a client, dialing elsewhere, so her client broadcast answers bob. - let _elsewhere = alice.dial_track("carol", None).unwrap(); - listen(&bob); - - let broadcast = kio::clock::timeout(TIMEOUT, mallory.consume().request("room/.server/bob", None)) - .await - .expect("request timeout") - .expect("bob's endpoint"); - let forged = broadcast - .track("track/alice/bob/0123456789abcdef0123456789abcdef") - .unwrap() - .subscribe(None); - - let incoming = kio::clock::timeout(TIMEOUT, bob.accept_track()) - .await - .expect("accept timeout") - .unwrap(); - assert_eq!(incoming.peer(), "alice"); - let accepted = kio::clock::timeout(TIMEOUT, incoming.accept(None)) - .await - .expect("accept timeout"); - assert!(accepted.is_err(), "bob connected to a forged dial"); - let forged = kio::clock::timeout(TIMEOUT, forged).await.expect("forged timeout"); - assert!(forged.is_err(), "the forger read bob's half"); - - // Alice never heard of it as a dial either, even accepting. - assert!(kio::clock::timeout(SETTLE, alice.accept_track()).await.is_err()); -} - -/// A dial dropped before the peer accepts resets the accept. -#[kio::test] -async fn a_dropped_dial_resets_the_accept() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let mut dial = alice.dial_track("bob", None).unwrap(); - // Polled until bob hears of it, then dropped. - let incoming = kio::clock::timeout(TIMEOUT, async { - match futures::future::select(std::pin::pin!(bob.accept_track()), &mut dial).await { - futures::future::Either::Left((incoming, _)) => incoming, - futures::future::Either::Right(_) => panic!("the dial resolved unaccepted"), - } - }) - .await - .expect("accept timeout") - .unwrap(); - drop(dial); - let accepted = kio::clock::timeout(TIMEOUT, incoming.accept(None)) - .await - .expect("accept timeout"); - assert!(accepted.is_err(), "bob connected to a dropped dial"); -} - -/// Closing an endpoint resets its sessions for the peer, and fails its pending work. -#[kio::test] -async fn closing_an_endpoint_ends_its_sessions() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - - bob.close(); - // Through the relay, bob's close reaches alice as a SESSION_CLOSED reset, as it would - // on one origin. - let ended = recv(&mut dialed.consumer.ordered()).await; - assert!( - matches!(ended, Err(Error::Stream(StreamError::Session(_)))), - "bob's half outlived his endpoint: {ended:?}" - ); - assert!(accepted.producer.append_group(moq_net::Timestamp::unscaled(0)).is_err()); - assert!(matches!(bob.accept_track().await, Err(Error::Closed))); - assert!(matches!(bob.dial_track("alice", None), Err(Error::Closed))); -} - -/// Session tracks are the secret that keeps third parties out, so no listing names them: -/// a reader allowed everything lists each endpoint's broadcast while a session runs and -/// finds no track. -#[kio::test] -async fn session_tracks_are_never_listed() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - send(&dialed.producer, b"secret"); - assert_eq!( - recv(&mut accepted.consumer.ordered()).await.unwrap().unwrap(), - b"secret" - ); - - let eve = fixture.client(LITE).await; - for id in [".client/alice", ".server/bob"] { - let broadcast = kio::clock::timeout(TIMEOUT, eve.consume().request(format!("room/{id}"), None)) - .await - .expect("request timeout") - .expect("the endpoint"); - let mut tracks = broadcast.tracks().unwrap(); - let event = kio::clock::timeout(TIMEOUT, tracks.next()) - .await - .expect("listing timeout") - .unwrap(); - assert!(matches!(event, Some(TracksEvent::Synced)), "{id} listed {event:?}"); - assert!( - kio::clock::timeout(SETTLE, tracks.next()).await.is_err(), - "{id} listed a track" - ); - } -} - -/// An endpoint announces only the roles it takes: its first dial its client broadcast, -/// and its first accept its server broadcast. So a dial to an endpoint that has only -/// dialed finds no server and waits, until that endpoint accepts. -#[kio::test] -async fn an_endpoint_announces_only_its_roles() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let _elsewhere = bob.dial_track("carol", None).unwrap(); - let mut dial = alice.dial_track("bob", None).unwrap(); - assert!( - kio::clock::timeout(SETTLE, &mut dial).await.is_err(), - "a dial reached an endpoint that only dials" - ); - - let eve = fixture.client(LITE).await; - for (path, announced) in [ - ("room/.client/alice", true), - ("room/.client/bob", true), - ("room/.server/alice", false), - ("room/.server/bob", false), - ] { - let found = kio::clock::timeout(SETTLE, eve.consume().request(path, None)).await; - assert_eq!(matches!(found, Ok(Ok(_))), announced, "{path}"); - } - - let accept = async { bob.accept_track().await?.accept(None).await }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("connect timeout"); - let (dialed, accepted) = (dialed.unwrap(), accepted.unwrap()); - assert_eq!(dialed.session(), accepted.session()); - let found = kio::clock::timeout(TIMEOUT, eve.consume().request("room/.server/bob", None)).await; - assert!(matches!(found, Ok(Ok(_))), "bob's server broadcast"); -} - -/// An acceptor can finish its initial burst before the dialer gets the answer. -/// -/// Over moq-lite only: moq-net endpoints never speak moq-transport to each other, and -/// over it the groups below the answer's Largest come from one range FETCH, which our -/// own publishers refuse past a single group. -#[kio::test] -async fn the_first_burst_survives_a_late_subscribe_answer() { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let dial = alice.dial_track("bob", None).unwrap(); - let accept = async { - let stream = bob.accept_track().await.unwrap().accept(None).await.unwrap(); - for payload in [b"one", b"two", b"six"] { - send(&stream.producer, payload); - } - let _finished = stream.producer.clone().finish().unwrap(); - stream - }; - let (dialed, accepted) = futures::join!(dial, accept); - let dialed = dialed.unwrap(); - let _finished = dialed.producer.clone().finish().unwrap(); - let mut reader = dialed.consumer.ordered(); - for payload in [b"one", b"two", b"six"] { - assert_eq!(recv(&mut reader).await.unwrap().unwrap(), payload); - } - assert_eq!(recv(&mut reader).await.unwrap(), None); - assert_eq!(recv(&mut accepted.consumer.ordered()).await.unwrap(), None); -} - -/// The moq-lite versions before hidden broadcasts. -const OLDER: [&str; 2] = ["moq-lite-06", "moq-lite-03"]; - -/// An endpoint behind an older moq-lite hop, which never carries its hidden broadcast, -/// fails its dials and accepts at once, so its peer never hears a dial it can't answer. -#[kio::test] -async fn an_older_hop_fails_dials_and_accepts() { - for older in OLDER { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(older, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - - let dialed = kio::clock::timeout(SETTLE, alice.dial_track("bob", None).unwrap()).await; - assert!(matches!(dialed, Ok(Err(Error::Unsupported))), "{older} dial"); - let accepted = kio::clock::timeout(SETTLE, alice.accept_track()).await; - assert!(matches!(accepted, Ok(Err(Error::Unsupported))), "{older} accept"); - - // So bob never heard a dial to hold an accept open for. - assert!( - kio::clock::timeout(SETTLE, bob.accept_track()).await.is_err(), - "{older}: bob heard a dial" - ); - } -} - -/// A dial to a peer behind an older moq-lite hop can't tell it from a peer not announced -/// yet, so it waits, while the peer's own accepts fail. -#[kio::test] -async fn a_peer_behind_an_older_hop_fails_its_accepts() { - for older in OLDER { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(older, "bob").await; - - let accepted = kio::clock::timeout(SETTLE, bob.accept_track()).await; - assert!(matches!(accepted, Ok(Err(Error::Unsupported))), "{older} accept"); - let mut dial = alice.dial_track("bob", None).unwrap(); - assert!( - kio::clock::timeout(SETTLE, &mut dial).await.is_err(), - "{older}: a dial behind an older hop resolved" - ); - } -} - -/// A dial already waiting fails once its origin gains an older moq-lite session, as an -/// endpoint whose client reconnects does. -#[kio::test] -async fn an_older_hop_fails_a_waiting_dial() { - let mut fixture = Fixture::new(); - let client = fixture.origin(); - let alice = Endpoint::new(&client, "room", "alice").unwrap(); - let mut dial = alice.dial_track("bob", None).unwrap(); - assert!( - kio::clock::timeout(SETTLE, &mut dial).await.is_err(), - "a dial to nobody resolved" - ); - - fixture.connect(&client, OLDER[0], client.consume()).await; - let dialed = kio::clock::timeout(SETTLE, dial).await; - assert!(matches!(dialed, Ok(Err(Error::Unsupported)))); -} - -/// A client whose publish half opts into hidden broadcasts, as a relay's link to a -/// cluster peer does, still carries its endpoint over an older moq-lite hop. -#[kio::test] -async fn an_older_hop_opted_into_hidden_connects() { - let mut fixture = Fixture::new(); - let client = fixture.origin(); - fixture - .connect(&client, OLDER[0], client.consume().with_hidden(true)) - .await; - let alice = Endpoint::new(&client, "room", "alice").unwrap(); - let bob = fixture.endpoint(LITE, "bob").await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - assert_eq!(dialed.session(), accepted.session()); -} diff --git a/rs/moq-relay/tests/integration/main.rs b/rs/moq-relay/tests/integration/main.rs index 23ef97582d7..c79c629d030 100644 --- a/rs/moq-relay/tests/integration/main.rs +++ b/rs/moq-relay/tests/integration/main.rs @@ -43,7 +43,6 @@ mod session_revalidate; mod shutdown_signal; mod smoke; #[cfg(feature = "test-support")] -mod socket_auth; mod stack; mod tenant_matrix; mod upstream_links; diff --git a/rs/moq-relay/tests/integration/quic_sim.rs b/rs/moq-relay/tests/integration/quic_sim.rs index c31b6c9833f..05a2a7ce014 100644 --- a/rs/moq-relay/tests/integration/quic_sim.rs +++ b/rs/moq-relay/tests/integration/quic_sim.rs @@ -100,20 +100,8 @@ pub(crate) struct Relay { /// Serve `cluster` on `addr` through `auth`: each session the server accepts runs a relay /// [`Connection`] on its own simulator task. pub(crate) async fn serve(network: &Network, addr: SocketAddr, cluster: &cluster::Cluster, auth: &auth::Auth) -> Relay { - serve_versions(network, addr, cluster, auth, Vec::new()).await -} - -/// [`serve`], accepting only `versions`, or every default one when empty. -async fn serve_versions( - network: &Network, - addr: SocketAddr, - cluster: &cluster::Cluster, - auth: &auth::Auth, - versions: Vec, -) -> Relay { let _ = rind_rustls::default_provider().unwrap().install_default(); let mut config = moq_tokio::server::Config::default(); - config.listen.version = versions; config.listen.bind = Some(moq_tokio::listen::Bind::Addr(addr)); config.listen.tls.generate = vec!["localhost".into()]; config.sim = Some(network.host(addr.ip())); @@ -234,41 +222,47 @@ fn lossy(network: &Network, a: SocketAddr, b: SocketAddr) { network.path(b, a, |p| *p = path); } -/// The bytes a socket session carries in one group: past the 32 MB live tail, so a hop that -/// sheds rather than paces loses some +/// The bytes the bulk transfer carries in one group: past the 32 MB live tail, so a hop +/// that sheds rather than paces loses some const BULK: usize = 40 * 1024 * 1024; /// The size of each frame of [`BULK`] const CHUNK: usize = 8 * 1024; -/// The only moq-lite that carries hidden broadcasts, which socket endpoints need at every hop -const LITE_07: &str = "moq-lite-07-wip"; -/// A socket session's group carries a bulk transfer through a relay without loss: the writer -/// waits on `ready` before each frame, and the reader drains at its own pace, slower than -/// the writer, so every hop has to pace its ingest to that reader rather than shed. +/// A group carries a bulk transfer through a relay without loss: the writer waits on `ready` +/// before each frame, and the reader drains at its own pace, slower than the writer, so +/// every hop has to pace its ingest to that reader rather than shed. /// /// A paused stream must hold no more than the credit its session reserves for it, or the /// connection window fills with bytes nobody takes and the transport evicts the stream. #[test] -fn a_socket_session_carries_a_bulk_transfer_losslessly() { +fn a_group_carries_a_bulk_transfer_losslessly() { memory::run(|| async { let network = Network::new(0); let auth = memory::public(&["**"], &[], &[]); let cluster = memory::start(memory::cluster(), moq_tokio::connect::Config::default()).await; - let lite = vec![LITE_07.parse().unwrap()]; - let relay = serve_versions(&network, SocketAddr::new(ip(1), PORT), &cluster, &auth, lite).await; + let relay = serve(&network, SocketAddr::new(ip(1), PORT), &cluster, &auth).await; - let (writer, _writing) = endpoint(&network, PUBLISHER, &relay, "writer"); - let (reader, _reading) = endpoint(&network, SUBSCRIBER, &relay, "reader"); + let origin = moq_tokio::origin::spawn(); + let broadcast = origin + .route("bulk", moq_net::origin::RouteOptions::default()) + .expect("route"); + let mut track = broadcast + .broadcast() + .create_track("data", moq_net::track::Info::default()) + .expect("track"); + broadcast.announce().expect("announce"); + let _publisher = client(&network, PUBLISHER, &relay) + .with_publisher(&origin) + .connect(url(&relay)); - let dial = reader.dial_track(writer.id(), None).unwrap(); - let accept = async { writer.accept_track().await?.accept(None).await }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("connect timeout"); - let (dialed, accepted) = (dialed.expect("dial"), accepted.expect("accept")); + let viewer = moq_tokio::origin::spawn(); + let _subscriber = client(&network, SUBSCRIBER, &relay) + .with_subscriber(viewer.clone()) + .connect(url(&relay)); let write = async { - let mut group = accepted.producer.append_group(moq_net::Timestamp::unscaled(0)).unwrap(); + while track.subscription_changed().await.unwrap().is_none() {} + let mut group = track.append_group(moq_net::Timestamp::unscaled(0)).unwrap(); let info = moq_net::frame::Info { size: CHUNK as u64, timestamp: moq_net::Timestamp::unscaled(0), @@ -282,7 +276,23 @@ fn a_socket_session_carries_a_bulk_transfer_losslessly() { group.finish().unwrap(); }; let read = async { - let mut ordered = dialed.consumer.ordered(); + let remote = loop { + match viewer.consume().request("bulk", None).await { + Ok(remote) => break remote, + // Not announced through the relay yet. + Err(_) => kio::clock::sleep(Duration::from_millis(50)).await, + } + }; + let consumer = remote + .track("data") + .unwrap() + .subscribe( + moq_net::track::Subscription::default() + .with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(600))), + ) + .await + .unwrap(); + let mut ordered = consumer.ordered(); let mut group = ordered .next_group() .await @@ -313,28 +323,3 @@ fn a_socket_session_carries_a_bulk_transfer_losslessly() { assert_eq!(read, BULK); }); } - -/// A socket endpoint `id` on the host at `ip`, publishing and subscribing through `relay`. -fn endpoint( - network: &Network, - ip: IpAddr, - relay: &Relay, - id: &str, -) -> (moq_net::socket::Endpoint, moq_tokio::Connection) { - let origin = moq_tokio::origin::spawn(); - let mut connect = moq_tokio::connect::Config::default(); - connect.bind = Some(SocketAddr::new(ip, CLIENT_PORT)); - connect.tls.fingerprint = relay.fingerprints.clone(); - // Every hop between two socket endpoints carries hidden broadcasts. - connect.version = vec![LITE_07.parse().unwrap()]; - let mut config = moq_tokio::client::Config::default(); - config.connect = connect; - config.sim = Some(network.host(ip)); - let connection = config - .init() - .expect("init client") - .with_publisher(&origin) - .with_subscriber(origin.clone()) - .connect(url(relay)); - (moq_net::socket::Endpoint::new(&origin, "room", id).unwrap(), connection) -} diff --git a/rs/moq-relay/tests/integration/socket_auth.rs b/rs/moq-relay/tests/integration/socket_auth.rs deleted file mode 100644 index 0a146ed340f..00000000000 --- a/rs/moq-relay/tests/integration/socket_auth.rs +++ /dev/null @@ -1,350 +0,0 @@ -//! Sockets through a relay that admits with `moq-auth` tokens, verified by the reference -//! auth server, granting only the prefixes `doc/concept/socket.md` lists. -//! -//! Every token is rooted at the socket prefix `room`. A server's token publishes its own -//! endpoint, `.server/`, and subscribes to every client, `.client/**`; a client's token -//! publishes `.client/` and subscribes to every server, `.server/**`. So a client can't -//! pose as a server or read another client, and the session track names are what keep -//! third parties out of a server's sessions: a forged dial never connects, and no listing -//! gives a session's name away. - -use std::time::Duration; - -use moq_net::broadcast::TracksEvent; -use moq_net::socket::{Endpoint, TrackStream}; -use moq_net::transport::memory; -use moq_relay::{Accepted, Connection}; - -const TIMEOUT: Duration = Duration::from_secs(10); - -/// The socket prefix, and every token's root. -const ROOT: &str = "room"; - -const LITE: &str = "moq-lite-07-wip"; -const IETF: &str = "moq-transport-18"; - -/// A key, the reference auth server verifying tokens it signs, and a relay asking it. -struct Fixture { - key: moq_auth::Key, - cluster: moq_relay::cluster::Cluster, - auth: moq_relay::auth::Auth, - _keys: tempfile::TempDir, - _server: tokio::task::JoinHandle<()>, -} - -impl Fixture { - async fn new() -> Self { - let keys = tempfile::tempdir().expect("tempdir"); - let key = moq_auth::Key::generate(moq_auth::Algorithm::HS256, None).expect("generate key"); - let key_path = keys.path().join("key.jwk"); - key.to_file(&key_path).expect("write key"); - - let mut policy = moq_auth::serve::Policy::default(); - policy.keys = Some(moq_auth::serve::Keys::File(key_path)); - let server = moq_auth::serve::Server::new(policy).expect("auth server"); - let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("bind auth"); - let url = format!("http://{}/", listener.local_addr().unwrap()).parse().unwrap(); - let server = tokio::spawn(async move { - let _ = server.serve(listener).await; - }); - - let mut config = moq_relay::auth::Config::default(); - config.url = Some(url); - let auth = config - .init("test", &moq_tokio::tls::Connect::default(), false) - .expect("auth init"); - Self { - key, - cluster: crate::spawn_cluster(Default::default()), - auth, - _keys: keys, - _server: server, - } - } - - /// Sign a token rooted at [`ROOT`] with these grants. - fn token(&self, publish: &str, subscribe: &str) -> String { - let claims = moq_auth::Claims::default() - .with_root(ROOT) - .with_publish([publish.parse().expect("publish pattern")]) - .with_subscribe([subscribe.parse().expect("subscribe pattern")]); - self.key.sign(&claims).expect("sign") - } - - /// A client at [`ROOT`] presenting `token`, publishing and subscribing through one - /// origin, over `version`. - async fn client(&self, token: &str, version: &str) -> (moq_net::origin::Producer, moq_net::Session) { - let origin = moq_tokio::origin::spawn(); - let url = format!("moql://relay.example.com/{ROOT}?jwt={token}").parse().unwrap(); - let client = moq_net::Client::new() - .with_publisher(origin.consume()) - .with_subscriber(origin.clone()) - .with_url(url); - let version = version.parse().expect("version"); - let session = self.connect(client, version).await.expect("connect"); - (origin, session) - } - - /// Dial the relay over a fresh in-memory link, as `client` speaking `version`. - /// - /// On Tokio rather than kio's simulator, which `crate::memory` uses, since the - /// reference auth server is a real HTTP server. - async fn connect( - &self, - client: moq_net::Client, - version: moq_net::Version, - ) -> Result { - let mut config = memory::Config::default(); - config.protocol = Some(version.alpn().to_string()); - let (client_link, relay_link) = memory::pair(config); - - let (cluster, auth) = (self.cluster.clone(), self.auth.clone()); - tokio::spawn(async move { - let server = moq_net::Server::new().with_versions(version.into()); - let Ok(handshake) = server.handshake(moq_net::time::Instant::now(), relay_link).await else { - return; - }; - let request = Accepted::new(handshake, moq_auth::Transport::Quic); - Connection::new(request, cluster, auth).run().await; - }); - - let (session, driver) = client - .with_versions(version.into()) - .connect(moq_net::time::Instant::now(), client_link) - .await?; - tokio::spawn(moq_net::time::run(driver)); - Ok(session) - } - - /// The endpoint `id` in `role`, holding the grants `doc/concept/socket.md` lists for it: - /// publish its own endpoint, subscribe to every endpoint of the other role. - async fn endpoint( - &self, - role: Role, - id: &str, - version: &str, - ) -> (Endpoint, moq_net::origin::Producer, moq_net::Session) { - let (own, peers) = match role { - Role::Server => (".server", ".client"), - Role::Client => (".client", ".server"), - }; - let (origin, session) = self - .client(&self.token(&format!("{own}/{id}"), &format!("{peers}/**")), version) - .await; - // The session is rooted at the prefix, so the endpoint's prefix is the root. - let endpoint = Endpoint::new(&origin, "", id).expect("endpoint"); - (endpoint, origin, session) - } -} - -/// Which grants an endpoint's token holds. -#[derive(Clone, Copy)] -enum Role { - Server, - Client, -} - -/// Dial `acceptor` from `dialer` in a session while `acceptor` accepts, returning both -/// ends. -async fn connect_track(dialer: &Endpoint, acceptor: &Endpoint) -> (TrackStream, TrackStream) { - let dial = dialer.dial_track(acceptor.id(), None).expect("dial"); - let accept = async { - let incoming = acceptor.accept_track().await?; - assert_eq!(incoming.peer(), dialer.id()); - incoming.accept(None).await - }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("connect timeout"); - (dialed.expect("dial"), accepted.expect("accept")) -} - -/// Write `payload` as one message of a session: one group. -fn send(track: &moq_net::track::Producer, payload: &'static [u8]) { - let mut group = track - .append_group(moq_net::Timestamp::unscaled( - track.largest().map_or(0, |largest| largest.group + 1), - )) - .expect("group"); - group - .write_frame(moq_net::Timestamp::unscaled(group.start), payload) - .expect("frame"); - group.finish().expect("finish"); -} - -/// Read the next message of a session. -async fn recv(reader: &mut moq_net::track::Ordered) -> Vec { - kio::clock::timeout(TIMEOUT, async { - let mut group = reader - .next_group() - .await - .expect("group") - .and_then(moq_net::track::Item::group) - .expect("a group"); - let frame = group.read_frame().await.expect("frame").expect("a frame"); - frame.payload.to_vec() - }) - .await - .expect("read timeout") -} - -/// Wait until `id`, a hidden endpoint, is announced to `origin`, so a request for it -/// never syncs ahead of the announce and reports it unroutable. -async fn announced(origin: &moq_net::origin::Producer, id: &str) { - let mut announced = origin.consume().with_hidden(true).announced(); - let wait = async { - while let Some(event) = announced.next().await { - if let moq_net::announce::Event::Present(announce) = event - && announce.prefix.as_str() == id - { - return; - } - } - panic!("{id}: origin closed"); - }; - kio::clock::timeout(TIMEOUT, wait) - .await - .unwrap_or_else(|_| panic!("{id} was never announced")); -} - -/// The prefix grants admit a dial and its accept, and the session carries data both -/// ways, over moq-lite-07 and across a moq-transport hop. -#[tokio::test] -async fn prefix_grants_admit_a_session() { - for (dialer, acceptor) in [(LITE, LITE), (LITE, IETF), (IETF, LITE)] { - let fixture = Fixture::new().await; - let (alice, _alice_origin, _alice_session) = fixture.endpoint(Role::Client, "alice", dialer).await; - let (bob, _bob_origin, _bob_session) = fixture.endpoint(Role::Server, "bob", acceptor).await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - send(&dialed.producer, b"ping"); - assert_eq!( - recv(&mut accepted.consumer.ordered()).await, - b"ping", - "{dialer} to {acceptor}" - ); - send(&accepted.producer, b"pong"); - assert_eq!( - recv(&mut dialed.consumer.ordered()).await, - b"pong", - "{dialer} to {acceptor}" - ); - } -} - -/// Mallory, a client, may dial every server, so she can ask bob for a session in alice's -/// name. Bob sees a dial from alice, but alice never dialed it and resets his accept, so it -/// never connects and mallory reads nothing. -#[tokio::test] -async fn a_forged_dial_never_connects() { - let fixture = Fixture::new().await; - let (alice, _alice_origin, _alice_session) = fixture.endpoint(Role::Client, "alice", LITE).await; - let (bob, _bob_origin, _bob_session) = fixture.endpoint(Role::Server, "bob", LITE).await; - let (_mallory, mallory_origin, _mallory_session) = fixture.endpoint(Role::Client, "mallory", LITE).await; - // Alice dials elsewhere, so her client endpoint is there to reset the forged accept. - let _elsewhere = alice.dial_track("carol", None).expect("dial"); - - // Bob's first accept announces his server endpoint. - assert!(bob.poll_accept_track(&kio::Waiter::noop()).is_pending()); - announced(&mallory_origin, ".server/bob").await; - - let forge = async { - let broadcast = mallory_origin - .consume() - .request(".server/bob", None) - .await - .expect("bob's endpoint"); - let forged = broadcast - .track("track/alice/bob/0123456789abcdef0123456789abcdef") - .expect("track") - .subscribe(None); - let incoming = bob.accept_track().await.expect("accept"); - assert_eq!(incoming.peer(), "alice"); - let (accepted, forged) = futures::join!(incoming.accept(None), forged); - assert!(accepted.is_err(), "bob connected to a forged dial"); - assert!(forged.is_err(), "mallory read bob's half"); - }; - kio::clock::timeout(TIMEOUT, forge).await.expect("forge timeout"); -} - -/// A client's grant keeps it a client: an accept announces a server endpoint the relay -/// never carries, so no dial reaches it, and another client's endpoint is out of reach. -/// -/// Each absence is read by a session that connects once the relay holds everything it -/// could have carried, so its first sync with the relay would have listed it. -#[tokio::test] -async fn a_client_grant_neither_serves_nor_reads_clients() { - let fixture = Fixture::new().await; - let (alice, _alice_origin, _alice_session) = fixture.endpoint(Role::Client, "alice", LITE).await; - let (_bob, bob_origin, _bob_session) = fixture.endpoint(Role::Server, "bob", LITE).await; - // Bob, a server, reads a client endpoint once the relay carries it. - let carried = |id: &'static str| { - let origin = bob_origin.clone(); - async move { - announced(&origin, id).await; - kio::clock::timeout(TIMEOUT, origin.consume().request(id, None)) - .await - .expect("request timeout") - .unwrap_or_else(|err| panic!("{id}: {err}")); - } - }; - - // Alice dials, which announces her client endpoint. - let _alice_dial = alice.dial_track("carol", None).expect("dial"); - carried(".client/alice").await; - - // Mallory, a client, can't read it. - let (mallory, mallory_origin, _mallory_session) = fixture.endpoint(Role::Client, "mallory", LITE).await; - let read = kio::clock::timeout(TIMEOUT, mallory_origin.consume().request(".client/alice", None)) - .await - .expect("request timeout"); - assert!( - matches!(read, Err(moq_net::Error::Unroutable)), - "mallory read alice's endpoint: {:?}", - read.err() - ); - - // Mallory accepts as a server would, then dials, announcing her client endpoint after - // her server one on the same session: once it is carried, the relay decided both. - assert!(mallory.poll_accept_track(&kio::Waiter::noop()).is_pending()); - let _mallory_dial = mallory.dial_track("bob", None).expect("dial"); - carried(".client/mallory").await; - - // So no dial can reach her. - let (_dave, dave_origin, _dave_session) = fixture.endpoint(Role::Client, "dave", LITE).await; - let served = kio::clock::timeout(TIMEOUT, dave_origin.consume().request(".server/mallory", None)) - .await - .expect("request timeout"); - assert!( - matches!(served, Err(moq_net::Error::Unroutable)), - "the relay carried mallory's server endpoint: {:?}", - served.err() - ); -} - -/// A reader allowed every endpoint lists both ends of a running session and finds no -/// track, so it cannot learn a session's name to read or forge it. -#[tokio::test] -async fn a_reader_of_everything_lists_no_session() { - let fixture = Fixture::new().await; - let (alice, _alice_origin, _alice_session) = fixture.endpoint(Role::Client, "alice", LITE).await; - let (bob, _bob_origin, _bob_session) = fixture.endpoint(Role::Server, "bob", LITE).await; - let (dialed, accepted) = connect_track(&alice, &bob).await; - send(&dialed.producer, b"secret"); - assert_eq!(recv(&mut accepted.consumer.ordered()).await, b"secret"); - - let (eve_origin, _eve_session) = fixture.client(&fixture.token(".client/eve", "**"), LITE).await; - for id in [".client/alice", ".server/bob"] { - let broadcast = kio::clock::timeout(TIMEOUT, eve_origin.consume().request(id, None)) - .await - .expect("request timeout") - .expect("the endpoint"); - let mut tracks = broadcast.tracks().expect("tracks"); - let event = kio::clock::timeout(TIMEOUT, tracks.next()) - .await - .expect("listing timeout") - .expect("listing"); - // The session tracks existed before this listing began, so a `Synced` first - // means it listed none of them. - assert!(matches!(event, Some(TracksEvent::Synced)), "{id} listed {event:?}"); - } -} diff --git a/sh/gh/nightly.sh b/sh/gh/nightly.sh index 9fc3575680a..6073013f824 100755 --- a/sh/gh/nightly.sh +++ b/sh/gh/nightly.sh @@ -177,7 +177,7 @@ case "$1" in done ;; js) - for benchmark in broadcasts requests fills fill-probe forward reader frames drift ledger bandwidth replay varint lite-varint track resume priority subscribe-drop refresh observed compression datagrams socket; do + for benchmark in broadcasts requests fills fill-probe forward reader frames drift ledger bandwidth replay varint lite-varint track resume priority subscribe-drop refresh observed compression datagrams; do javascript "$benchmark" just js bench "$benchmark" done javascript catalog just js catalog-bench diff --git a/swift/Sources/Moq/Socket.swift b/swift/Sources/Moq/Socket.swift deleted file mode 100644 index bbf0a1e43ee..00000000000 --- a/swift/Sources/Moq/Socket.swift +++ /dev/null @@ -1,72 +0,0 @@ -import MoqFFI - -/// Point-to-point sessions between server and client endpoints. -public enum Socket {} - -extension Socket { - /// Whether `path` is an endpoint's broadcast, which only routes sessions and carries no media. - public static func isEndpoint(_ path: String) throws -> Bool { - try moqSocketIsEndpoint(path: path) - } - - /// A server, a client, or both: dials or accepts track sessions. - public final class Endpoint: Sendable { - let ffi: MoqSocketEndpoint - - /// The endpoint `id` beneath `prefix`, using `origin` to find peers. Its first dial - /// announces it as a client, and its first accept as a server. - public init(origin: OriginProducer, prefix: String, id: String) throws { - ffi = try MoqSocketEndpoint(origin: origin.ffi, prefix: prefix, id: id) - } - - /// This endpoint's id. - public var id: String { ffi.id() } - /// The shared endpoint prefix. - public var prefix: String { ffi.prefix() } - /// Retract the endpoint's broadcasts and reset its pending dials and sessions. - public func close() { ffi.close() } - - /// Dial a new track session; cancelling resets this dial. - public func dialTrack(peer: String, info: TrackInfo? = nil) async throws -> TrackStream { - TrackStream(try await ffi.dialTrack(peer: peer, info: info)) - } - /// Await the next track dial. - public func acceptTrack() async throws -> IncomingTrack { - IncomingTrack(try await ffi.acceptTrack()) - } - } - - /// An incoming track dial, consumed by accepting or rejecting it. - public final class IncomingTrack: Sendable { - let ffi: MoqSocketIncomingTrack - init(_ ffi: MoqSocketIncomingTrack) { self.ffi = ffi } - /// The claimed peer; accepting verifies return routability. - public var peer: String { ffi.peer() } - /// The private session id. - public var session: String { ffi.session() } - /// Verify return routability and accept the dial. - public func accept(info: TrackInfo? = nil) async throws -> TrackStream { - TrackStream(try await ffi.accept(info: info)) - } - /// Refuse this dial as not found. - public func reject() throws { try ffi.reject() } - } - - /// A track session's two halves and identities. - public final class TrackStream: Sendable { - /// This side's write half. - public let producer: TrackProducer - /// The peer's read half, retaining its subscription. - public let consumer: TrackConsumer - /// The peer endpoint's id. - public let peer: String - /// The private session id. - public let session: String - init(_ ffi: MoqSocketTrackStream) { - producer = TrackProducer(ffi.producer()) - consumer = TrackConsumer(ffi.consumer()) - peer = ffi.peer() - session = ffi.session() - } - } -} diff --git a/test/interop/README.md b/test/interop/README.md index c54040e5236..fe4be8bca7e 100644 --- a/test/interop/README.md +++ b/test/interop/README.md @@ -52,14 +52,6 @@ Rust CLI swap its channels' tones, and every subscriber must refuse every rendit as the wrong tone, not fail some other way. `interop.sh` is the one place the tones and each publisher's codecs are chosen. -When Python is selected, the in-tree run also connects Python and Rust socket -endpoints through the relay in both dial directions and both `tcp` and `track` -protocols. Each direction checks exact frames, empty writes, ordered groups, -and clean FIN. A stdin drain acknowledgement keeps Rust alive until Python has -read its entire half. The lane runs only in the normal pass, pinned to lite-07: -moq-net endpoints speak moq-lite to each other, never moq-transport. Older lite -hops are outside this lane. - The normal run also checks a finite raw track through the relay. Its publisher announces end 4 before writing four 256 KiB groups; the reader verifies every byte, exactly groups 0 through 3, and a clean end at 4. Rust-to-Rust always runs. Selecting @@ -85,12 +77,6 @@ head with a joining FETCH before draft 22 and a fill from it. `--tail` runs the FETCH round, the late joins, and the five finite-tail lanes. The wire-compat lanes, which swap in released relays and clients, skip them. -Socket lanes cross Rust and native JS in both dial directions under Node and Bun, -using `tcp` and `track` through the relay. They verify both peers' bytes, FIN, RST, -rejection, and separate rooted publish grants. Each pins moq-lite-07-wip; moq-net endpoints use lite to talk to each other, -and older moq-lite hops do not carry hidden endpoints. These lanes -run in the normal in-tree test when native JS is selected. `just test interop --sockets` runs them alone. - On lite-07, Node and Bun also replace tracks in a claimed broadcast through the relay over WebSocket and WebTransport. Pre-connected readers subscribe immediately after each replacement while an existing reader resumes. The case also repeats the diff --git a/test/interop/clients/js-native/socket.ts b/test/interop/clients/js-native/socket.ts deleted file mode 100644 index 0bf2f23bda1..00000000000 --- a/test/interop/clients/js-native/socket.ts +++ /dev/null @@ -1,84 +0,0 @@ -/** Socket convention interop over an authenticated relay, with a harness-owned drain acknowledgment. */ -import assert from "node:assert/strict"; -import { createInterface } from "node:readline"; -import * as Moq from "@moq/net"; -import { install } from "@moq/web-transport"; - -install(); -console.debug = console.error; -console.info = console.error; -const [role, address, prefix, version] = process.argv.slice(2); -assert(address && prefix !== undefined && (role === "dial" || role === "accept") && version); -const origin = new Moq.Origin.Producer(); -const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from(prefix), role === "dial" ? "dialer" : "acceptor"); -const next = role === "accept" ? endpoint.acceptTrack() : undefined; -const connection = await Moq.Connection.connect({ - url: new URL(address), - versions: [version], - websocket: { enabled: false }, - publish: origin.consume(), - consume: origin, -}); -const lines = createInterface({ input: process.stdin }); -const commands = lines[Symbol.asyncIterator](); -// Every Incoming is a claim: a pipelined request can outlive its refused metadata. -// Verify each new incoming once; a failed caller's dial still fails the harness. -async function accepted(): Promise { - for (;;) { - const incoming = await endpoint.acceptTrack(); - try { - return await incoming.accept(); - } catch { - /* The claimed original dial was canceled or no longer answers. */ - } - } -} - -try { - if (role === "accept") process.stdout.write("socket ready\n"); - // Refuse before opening data sessions, so the probe is independent of late - // unverified queries for a completed half. - if (next) { - (await next).reject(); - } else { - await assert.rejects(endpoint.dialTrack("acceptor")); - } - const stream = next ? await accepted() : await endpoint.dialTrack("acceptor"); - assert.equal(stream.peer, role === "dial" ? "acceptor" : "dialer"); - assert.match(stream.session, /^[0-9a-f]{32}$/); - const own = role === "dial" ? 16 : 32; - const peer = role === "dial" ? 32 : 16; - // Exchange each independent message as a request and reply. - const reader = stream.consumer.ordered(); - for (let i = 0; i < 3; i++) { - // Each message is its own group, so its frame carries that group's key. - const timestamp = Moq.Time.Timestamp.unscaled(BigInt(i)); - if (role === "dial") stream.producer.writeFrame({ payload: new Uint8Array([own + i]), timestamp }); - const frame = await reader.readFrame(); - assert.equal(frame?.group, BigInt(i)); - assert.deepEqual(frame?.payload, new Uint8Array([peer + i])); - if (role === "accept") stream.producer.writeFrame({ payload: new Uint8Array([own + i]), timestamp }); - } - stream.producer.finish(); - assert.equal(await reader.readFrame(), undefined); - const second = next ? await accepted() : await endpoint.dialTrack("acceptor"); - const resetReader = second.consumer.ordered(); - if (role === "dial") { - // The first message is at key 0. - second.producer.writeFrame({ payload: new Uint8Array([48]), timestamp: Moq.Time.Timestamp.unscaled(0n) }); - await assert.rejects(resetReader.readFrame()); - } else { - assert.deepEqual((await resetReader.readFrame())?.payload, new Uint8Array([48])); - second.producer.abort(new Moq.Error.Stream(Moq.StreamCode(70))); - } - console.error(`socket verified ${role}`); - if (role === "accept") assert.equal((await commands.next()).value, "socket drained"); -} finally { - lines.close(); - endpoint.close(); - try { - await connection.close(); - } finally { - origin.close(); - } -} diff --git a/test/interop/clients/python/sockets.py b/test/interop/clients/python/sockets.py deleted file mode 100644 index 8017eaeff67..00000000000 --- a/test/interop/clients/python/sockets.py +++ /dev/null @@ -1,92 +0,0 @@ -"""Python/Rust socket interop through a relay, in both directions.""" - -import argparse -import asyncio -import contextlib -from collections.abc import Coroutine -from typing import Any, TypeVar - -import moq -from moq import socket - -PAYLOADS = [b"first", b"", b"last"] -T = TypeVar("T") - - -async def start(coro: Coroutine[Any, Any, T]) -> asyncio.Task[T]: - """Enable an accept queue before another process can dial it.""" - loop = asyncio.get_running_loop() - polled = loop.create_future() - - async def run() -> T: - loop.call_soon(polled.set_result, None) - return await coro - - task = asyncio.create_task(run()) - await polled - return task - - -async def exchange(stream: socket.TrackStream) -> None: - assert stream.peer == "rust" - assert stream.consumer.info().max_age is None - for key in range(3): - group = stream.producer.append_group(moq.Timestamp.unscaled(key)) - for payload in PAYLOADS: - group.write_frame(payload, moq.Timestamp.unscaled(key)) - group.finish() - stream.producer.finish() - received = [(group.start, [frame.payload async for frame in group]) async for group in stream.consumer] - assert received == [(sequence, PAYLOADS) for sequence in range(3)], received - - -async def main(args: argparse.Namespace) -> None: - origin = moq.OriginProducer() - async with moq.Client( - args.url, publish=origin, consume=origin, versions=[args.version], tls=moq.ClientTls(insecure=True) - ): - for role in ("dial", "accept"): - prefix = f"{args.prefix}/{role}" - async with socket.Endpoint(origin, prefix, "python") as endpoint: - incoming = None - if role == "dial": - incoming = await start(endpoint.accept_track()) - peer = await asyncio.create_subprocess_exec( - args.rust, - args.url, - prefix, - args.version, - role, - stdin=asyncio.subprocess.PIPE, - stdout=asyncio.subprocess.PIPE, - ) - assert peer.stdout is not None and peer.stdin is not None - try: - if incoming is not None: - stream = await (await incoming).accept() - else: - assert await peer.stdout.readline() == b"listening\n" - stream = await endpoint.dial_track("rust") - await exchange(stream) - assert await peer.stdout.readline() == b"drained\n" - peer.stdin.write(b"drained\n") - await peer.stdin.drain() - assert await peer.wait() == 0 - print(f"socket, Rust {role}: passed", flush=True) - finally: - if incoming is not None and not incoming.done(): - incoming.cancel() - with contextlib.suppress(asyncio.CancelledError): - await incoming - if peer.returncode is None: - peer.kill() - await peer.wait() - - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("url") - parser.add_argument("prefix") - parser.add_argument("version") - parser.add_argument("rust") - asyncio.run(main(parser.parse_args())) diff --git a/test/interop/clients/rust/socket-js.rs b/test/interop/clients/rust/socket-js.rs deleted file mode 100644 index ab38cd224a5..00000000000 --- a/test/interop/clients/rust/socket-js.rs +++ /dev/null @@ -1,131 +0,0 @@ -//! Socket convention interop through a relay, with a harness-owned drain acknowledgment. -use anyhow::{Context, ensure}; -use tokio::io::AsyncBufReadExt; - -#[tokio::main(flavor = "current_thread")] -async fn main() -> anyhow::Result<()> { - let args: Vec<_> = std::env::args().collect(); - ensure!( - args.len() == 5, - "usage: interop-socket-js dial|accept URL PREFIX VERSION" - ); - let dial = match args[1].as_str() { - "dial" => true, - "accept" => false, - _ => anyhow::bail!("unknown role"), - }; - let mut config = moq_tokio::connect::Config::default(); - config.version.push(args[4].parse().map_err(anyhow::Error::msg)?); - config.websocket.enabled = Some(false); - let client = config.init(Default::default())?.with_reconnect(false); - let origin = moq_tokio::origin::spawn(); - let endpoint = moq_net::socket::Endpoint::new(&origin, args[3].as_str(), if dial { "dialer" } else { "acceptor" })?; - if !dial { - ensure!(endpoint.poll_accept_track(&kio::Waiter::noop()).is_pending()); - } - let connection = client - .clone() - .with_publisher(&origin) - .with_subscriber(origin.clone()) - .connect(args[2].parse::()?) - .established() - .await?; - if !dial { - println!("socket ready"); - } - // Probe refusal before data sessions can generate late queries for closed halves. - if dial { - ensure!( - endpoint.dial_track("acceptor", None)?.await.is_err(), - "rejected dial connected" - ); - } else { - endpoint.accept_track().await?.reject(); - } - let own: u8 = if dial { 16 } else { 32 }; - let peer: u8 = if dial { 32 } else { 16 }; - let mut stream = if dial { - endpoint.dial_track("acceptor", None)?.await? - } else { - accepted(&endpoint).await? - }; - ensure!(stream.peer() == if dial { "acceptor" } else { "dialer" }); - ensure!(stream.session().len() == 32); - let mut reader = stream.consumer.ordered(); - for i in 0..3 { - if dial { - stream.producer.write_frame(key(i), vec![own + i])?; - } - let mut group = reader - .next_group() - .await? - .and_then(moq_net::track::Item::group) - .context("short track half")?; - ensure!(group.start == u64::from(i), "wrong group key"); - let frame = group.read_frame().await?.context("empty track message")?; - ensure!(frame.payload.as_ref() == [peer + i], "wrong track frame"); - ensure!(group.read_frame().await?.is_none(), "extra frame in track message"); - if !dial { - stream.producer.write_frame(key(i), vec![own + i])?; - } - } - let _finished = stream.producer.finish()?; - ensure!(reader.next_group().await?.is_none(), "extra track frame"); - eprintln!("socket data verified {}", args[1]); - - let mut reset = if dial { - endpoint.dial_track("acceptor", None)?.await? - } else { - accepted(&endpoint).await? - }; - let mut reset_reader = reset.consumer.ordered(); - if dial { - reset.producer.write_frame(key(0), vec![48])?; - ensure!(reset_reader.next_group().await.is_err(), "track RST became FIN"); - eprintln!("socket reset verified dial"); - } else { - let mut group = reset_reader - .next_group() - .await? - .and_then(moq_net::track::Item::group) - .context("missing reset signal")?; - ensure!( - group - .read_frame() - .await? - .context("empty reset signal")? - .payload - .as_ref() == [48] - ); - reset.producer.abort(moq_net::Error::App(6))?; - } - eprintln!("socket verified {}", args[1]); - if !dial { - let mut lines = tokio::io::BufReader::new(tokio::io::stdin()).lines(); - ensure!( - lines.next_line().await?.as_deref() == Some("socket drained"), - "peer did not drain" - ); - } - // Held to here so the finished half and its reader outlive the drain. - drop(reader); - endpoint.close(); - let _ = connection.close().await; - client.close().await; - Ok(()) -} - -// An Incoming's claimed original dial can disappear before verification. -async fn accepted(endpoint: &moq_net::socket::Endpoint) -> anyhow::Result { - loop { - let incoming = endpoint.accept_track().await?; - if let Ok(stream) = incoming.accept(None).await { - return Ok(stream); - } - } -} - -/// Message `i`'s key: a session track is scale-less, so each message's group is keyed by its index. -fn key(i: u8) -> moq_net::Timestamp { - moq_net::Timestamp::unscaled(u64::from(i)) -} diff --git a/test/interop/clients/rust/socket.rs b/test/interop/clients/rust/socket.rs deleted file mode 100644 index 060f4f6c818..00000000000 --- a/test/interop/clients/rust/socket.rs +++ /dev/null @@ -1,88 +0,0 @@ -//! Socket interop peer; stdin acknowledges that the other process drained its half. -use anyhow::{Context, ensure}; -use moq_net::socket::Endpoint; -use std::future::Future; -use std::task::Poll; -use tokio::io::AsyncBufReadExt; - -const PAYLOADS: [&[u8]; 3] = [b"first", b"", b"last"]; - -async fn group(group: &mut moq_net::group::Consumer) -> anyhow::Result<()> { - for expected in PAYLOADS { - let frame = group.read_frame().await?.context("short group")?; - ensure!(frame.payload.as_ref() == expected, "wrong frame"); - ensure!(frame.timestamp.scale().is_none(), "unexpected timestamp"); - } - ensure!(group.read_frame().await?.is_none(), "extra frame"); - Ok(()) -} - -#[tokio::main(flavor = "current_thread")] -async fn main() -> anyhow::Result<()> { - let args: Vec<_> = std::env::args().collect(); - ensure!(args.len() == 5, "usage: interop-socket URL PREFIX VERSION dial|accept"); - let mut config = moq_tokio::connect::Config::default(); - config.version.push(args[3].parse().map_err(anyhow::Error::msg)?); - config.websocket.enabled = Some(false); - let client = config.init(Default::default())?.with_reconnect(false); - let origin = moq_tokio::origin::spawn(); - let endpoint = Endpoint::new(&origin, args[2].as_str(), "rust")?; - let session = client - .with_publisher(&origin) - .with_subscriber(origin.clone()) - .connect(args[1].parse::()?) - .established() - .await?; - let dial = args[4] == "dial"; - ensure!(dial || args[4] == "accept", "unknown role"); - let stream = if dial { - endpoint.dial_track("python", None)?.await? - } else { - let mut incoming = Box::pin(endpoint.accept_track()); - std::future::poll_fn(|cx| { - assert!(incoming.as_mut().poll(cx).is_pending()); - Poll::Ready(()) - }) - .await; - println!("listening"); - incoming.await?.accept(None).await? - }; - ensure!(stream.peer() == "python", "wrong peer"); - ensure!(stream.consumer.info().max_age.is_none(), "track has an age limit"); - let producer = stream.producer; - let mut consumer = stream.consumer.ordered(); - // The track carries no timescale, so its groups count up from 0. - for start in 0..3 { - let start = moq_net::Timestamp::unscaled(start); - let mut group = producer.append_group(start)?; - for payload in PAYLOADS { - group.write_frame(start, payload)?; - } - group.finish()?; - } - let _producer = producer.finish()?; - for start in 0..3 { - let mut next = consumer - .next_group() - .await? - .and_then(moq_net::track::Item::group) - .context("short track")?; - ensure!(next.start == start, "wrong group order"); - group(&mut next).await?; - } - ensure!(consumer.next_group().await?.is_none(), "extra group"); - println!("drained"); - ack().await?; - endpoint.close(); - session.close().await?; - Ok(()) -} - -async fn ack() -> anyhow::Result<()> { - let mut lines = tokio::io::BufReader::new(tokio::io::stdin()).lines(); - ensure!( - lines.next_line().await?.as_deref() == Some("drained"), - "peer did not drain" - ); - Ok(()) -} diff --git a/test/interop/interop.sh b/test/interop/interop.sh index fa28a712454..30b6af1260d 100755 --- a/test/interop/interop.sh +++ b/test/interop/interop.sh @@ -76,7 +76,6 @@ MEDIA_CASES="" MEDIA_ENGINES="chromium" MEDIA_PUBLISHER="chromium" TAIL_ONLY=0 -SOCKET_ONLY=0 FINISH_ONLY=0 # The versions the matrix runs on, one pass each. `auto` leaves every client its defaults, # except the clients the harness reads a grant from, which offer $AUTH_VERSION alone. Any other @@ -131,11 +130,6 @@ while [[ $# -gt 0 ]]; do TAIL_ONLY=1 shift ;; - --sockets) - SOCKET_ONLY=1 - TAIL_ONLY=1 - shift - ;; --versions) require_value "$@" VERSIONS="$2" @@ -182,11 +176,6 @@ if [[ -n "$MEDIA_CASES" && "$MEDIA" -ne 1 ]]; then exit 2 fi -if [[ "$SOCKET_ONLY" -eq 1 && "$VERSIONS" != auto ]]; then - echo "error: --sockets pins moq-lite-07-wip; omit --versions" >&2 - exit 2 -fi - # Numeric guards so a fat-fingered --timeout / INTEROP_PORT fails clearly here # instead of surfacing later as a cryptic `timeout` or relay-bind error. [[ "$TIMEOUT" =~ ^[0-9]+(\.[0-9]+)?$ ]] || { @@ -469,9 +458,6 @@ media_publisher() { esac } -# shellcheck source-path=SCRIPTDIR source=socket.sh -source "$INTEROP_DIR/socket.sh" - # Record every case this run will reach as `not run`, before any build, so one the run # never reaches still shows in the report. Mirrors the dispatch at the end of this file. plan_cases() { @@ -484,10 +470,6 @@ plan_cases() { for sub in "${SUB_LIST[@]}"; do record "not run" case="no publisher" publisher=none subscriber="$sub"; done return fi - if [[ "$SOCKET_ONLY" -eq 1 ]]; then - plan_js_sockets - return - fi for VERSION in "${VERSION_LIST[@]}"; do plan_pass; done VERSION=auto } @@ -524,9 +506,6 @@ plan_pass() { done fi [[ "$FINISH_ONLY" -eq 0 ]] || return 0 - if needs python; then - [[ "$VERSION" != auto ]] || record "not run" case="Python Rust sockets" publisher=python subscriber=rust - fi for pub in rust js-native-node js-native-bun; do [[ "$pub" == rust ]] || needs "$pub" || continue for sub in rust js-native-node js-native-bun; do @@ -556,7 +535,6 @@ plan_pass() { fi done [[ "$VERSION" == auto ]] || return 0 - plan_js_sockets for runtime in js-native-node js-native-bun; do needs "$runtime" || continue for version in "${COMPRESS_VERSIONS[@]}"; do @@ -663,7 +641,7 @@ build_relay_cli() { INTEROP_SUB_MOQ="$HARNESS_RUN/bin/moq-sub" fi if [[ "$build_examples" -eq 1 && "$MEDIA" -eq 0 ]]; then - local example args=() examples=(interop-socket interop-tail interop-socket-js interop-catalog interop-audio interop-hls interop-control interop-goaway interop-goaway-client interop-finish) + local example args=() examples=(interop-tail interop-catalog interop-audio interop-hls interop-control interop-goaway interop-goaway-client interop-finish) for example in "${examples[@]}"; do args+=(--example "$example"); done (cd "$WORKSPACE" && cargo build --locked ${flag[@]+"${flag[@]}"} -p moq-cli "${args[@]}") for example in "${examples[@]}"; do @@ -2614,24 +2592,6 @@ run_hls_grid() { } # This checkout's CLI FETCH and finite track tails in this pass, then the lite drop lanes. -# Both roles share this lane; Python acknowledges Rust's drain before teardown. -# Lite only: moq-net endpoints speak moq-lite to each other, never moq-transport. -run_sockets() { - needs python || return 0 - [[ "$VERSION" == auto ]] || return 0 - local version=moq-lite-07-wip log="$LOGS/sockets.log" prefix="sockets-$$-$RANDOM" - if is_broken python || ! timeout -k 3 "$TIMEOUT" "$PY" "$CLIENTS/python/sockets.py" \ - "$(token_url --publish "$prefix/**" --subscribe "$prefix/**")" "$prefix" "$version" \ - "$EXAMPLES/interop-socket" >"$log" 2>&1; then - echo " FAIL Python/Rust sockets ($version): $log" - record fail case="Python Rust sockets" publisher=python subscriber=rust log="$log" - overall=1 - return - fi - echo " PASS Python/Rust sockets ($version), both roles" - record pass case="Python Rust sockets" publisher=python subscriber=rust log="$log" -} - run_tails() { local runtime version transport echo "=== CLI FETCH close ===" @@ -2676,7 +2636,6 @@ run_tails() { done # The compress, compressed catalog, and drop lanes pin their own versions. [[ "$VERSION" == auto ]] || return 0 - run_js_sockets for runtime in js-native-node js-native-bun; do needs "$runtime" || continue run_replacement_case "$runtime" websocket @@ -2720,8 +2679,6 @@ elif [[ "$NEGATIVE" -eq 1 ]]; then # no data), proving the harness can actually report failure. echo "=== negative control: subscribers expect NO data ===" run_round "none" "interop-missing-$$-$RANDOM.hang" "" nothing "$(relay_next_line)" -elif [[ "$SOCKET_ONLY" -eq 1 ]]; then - run_js_sockets else for VERSION in "${VERSION_LIST[@]}"; do LOGS="$HARNESS_RUN" @@ -2735,7 +2692,6 @@ else [[ "$TAIL_ONLY" -eq 1 ]] || run_finishes if [[ "$FINISH_ONLY" -eq 0 ]]; then run_tails - run_sockets fi fi done diff --git a/test/interop/socket.sh b/test/interop/socket.sh deleted file mode 100644 index 265fdf5fd7f..00000000000 --- a/test/interop/socket.sh +++ /dev/null @@ -1,89 +0,0 @@ -# Socket pairs use the relay and signed endpoint grants prepared by interop.sh. -# shellcheck shell=bash -# shellcheck disable=SC2034 # overall is the interop runner's shared verdict. - -# shellcheck disable=SC2329 # called by harness_spawn -run_socket_client() { - local runtime="$1" - shift - case "$runtime" in - rust) timeout -k 3 "$TIMEOUT" "$EXAMPLES/interop-socket-js" "$@" ;; - js-native-node) (cd "$CLIENTS/js-native" && timeout -k 3 "$TIMEOUT" node --import tsx socket.ts "$@") ;; - js-native-bun) (cd "$CLIENTS/js-native" && timeout -k 3 "$TIMEOUT" bun socket.ts "$@") ;; - esac -} - -# shellcheck disable=SC2329 # called by harness_spawn -run_socket_acceptor() { - local events="$1" ack="$2" - shift 2 - run_socket_client "$@" <"$ack" >"$events" 8>&- 9>&- -} - -run_socket_pair() { - local dialer="$1" acceptor="$2" version="$3" started=$SECONDS from pid dial_pid ready - local name="socket-$dialer-$acceptor-$version" prefix="socket-$dialer-$acceptor-$version-$$-$RANDOM" - local events="$LOGS/socket-events" ack="$LOGS/socket-ack" - local cell=(case="socket" publisher="$dialer" subscriber="$acceptor" version="$version" log="$LOGS/$name-dial.log") - if is_broken "$dialer" || is_broken "$acceptor"; then - record fail "${cell[@]}" detail="client unavailable" - overall=1 - return - fi - mkfifo "$events" "$ack" - exec 8<>"$events" 9<>"$ack" - from=$(relay_next_line) - # Grant each peer only its own endpoint and the other role's endpoints. - harness_spawn "$name-accept" "$LOGS/$name-accept.log" run_socket_acceptor "$events" "$ack" "$acceptor" accept \ - "$(token_url --root "$prefix" --publish '.server/acceptor' --subscribe '.client/**')" "$prefix" "$version" - pid="$HARNESS_PID" - if IFS= read -r -t "$TIMEOUT" ready <&8 && [[ "$ready" == 'socket ready' ]]; then - harness_spawn "$name-dial" "$LOGS/$name-dial.log" run_socket_client "$dialer" dial \ - "$(token_url --root "$prefix" --publish '.client/dialer' --subscribe '.server/**')" "$prefix" "$version" - dial_pid="$HARNESS_PID" - harness_wait "$dial_pid" || true - if grep -qx "socket verified dial" "$LOGS/$name-dial.log"; then - printf 'socket drained\n' >&9 - harness_wait "$pid" || true - if grep -qx "socket verified accept" "$LOGS/$name-accept.log" && check_versions "$name" "$from" "$version"; then - echo " PASS socket $dialer -> $acceptor $version" - record pass "${cell[@]}" seconds=$((SECONDS - started)) - exec 8>&- 9>&- - rm "$events" "$ack" - return - fi - fi - fi - echo " FAIL socket $dialer -> $acceptor $version" - record fail "${cell[@]}" seconds=$((SECONDS - started)) detail="socket handshake, payload, or FIN failed" - overall=1 - harness_reap "$pid" - cat "$LOGS/$name-accept.log" - [[ ! -f "$LOGS/$name-dial.log" ]] || cat "$LOGS/$name-dial.log" - exec 8>&- 9>&- - rm "$events" "$ack" -} - -socket_pairs() { - local action="$1" runtime - local version=moq-lite-07-wip - for runtime in js-native-node js-native-bun; do - needs "$runtime" || continue - "$action" rust "$runtime" "$version" - "$action" "$runtime" rust "$version" - done -} - -# shellcheck disable=SC2329 # called by socket_pairs -plan_socket_pair() { - record "not run" case="socket" publisher="$1" subscriber="$2" version="$3" -} - -plan_js_sockets() { - socket_pairs plan_socket_pair -} - -run_js_sockets() { - echo '=== sockets ===' - socket_pairs run_socket_pair -} From 4c5d6b774b62f566f8bad3c465807485e0271db8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 13:31:55 -0800 Subject: [PATCH 3/3] quest: keep the py-client-body-error follow-up, as picked Co-Authored-By: Claude Opus 5.5 --- quest/m0/test-flakes-3/py-client-body-error.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quest/m0/test-flakes-3/py-client-body-error.md b/quest/m0/test-flakes-3/py-client-body-error.md index 1904938e2e4..e87a50e8ba9 100644 --- a/quest/m0/test-flakes-3/py-client-body-error.md +++ b/quest/m0/test-flakes-3/py-client-body-error.md @@ -22,4 +22,6 @@ connect races the server's accept, that is a bug in `py/moq-rs` or moq-ffi, fixed there with a regression test. Only if the trace shows the test reads a session it does not yet hold is the fix in the test, by the epic's rules. +Kept as a follow-up by the maintainer's 2026-11-17 pick of #9029. + Public API: none. Wire: none.