diff --git a/Cargo.lock b/Cargo.lock index 45fb70df251..da82825057b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4274,7 +4274,6 @@ dependencies = [ "object_store", "percent-encoding", "pollster", - "rand 0.10.3", "reqwest", "rind-object-store", "rustls", diff --git a/dart/moq/lib/socket.dart b/dart/moq/lib/socket.dart index 366c43d69f2..1da2fbe2ae5 100644 --- a/dart/moq/lib/socket.dart +++ b/dart/moq/lib/socket.dart @@ -7,14 +7,8 @@ import 'package:moq_ffi/moq_ffi.dart'; /// first accept as a server. typedef Endpoint = MoqSocketEndpoint; -/// An incoming TCP dial, consumed by accepting or rejecting it. -typedef IncomingTcp = MoqSocketIncomingTcp; - /// An incoming track dial, consumed by accepting or rejecting it. typedef IncomingTrack = MoqSocketIncomingTrack; -/// A TCP session with group producer and consumer halves. -typedef TcpStream = MoqSocketTcpStream; - /// A track session with producer and reliably subscribed consumer halves. typedef TrackStream = MoqSocketTrackStream; diff --git a/dart/moq_ffi/lib/src/moq.dart b/dart/moq_ffi/lib/src/moq.dart index a4881a969d3..a02faba4b81 100644 --- a/dart/moq_ffi/lib/src/moq.dart +++ b/dart/moq_ffi/lib/src/moq.dart @@ -16280,10 +16280,8 @@ class FfiConverterMoqTokens { } abstract class MoqSocketEndpointInterface { - UniffiRustFuture acceptTcp(); UniffiRustFuture acceptTrack(); void close(); - UniffiRustFuture dialTcp({required String peer}); UniffiRustFuture dialTrack({ required String peer, required MoqTrackInfo? info, @@ -16344,21 +16342,6 @@ class MoqSocketEndpoint implements MoqSocketEndpointInterface { } } - UniffiRustFuture acceptTcp() { - return uniffiRustCallAsync( - () => uniffi_moq_ffi_fn_method_moqsocketendpoint_accept_tcp( - uniffiClonePointer(), - ), - ffi_moq_ffi_rust_future_poll_u64, - ffi_moq_ffi_rust_future_complete_u64, - ffi_moq_ffi_rust_future_cancel_u64, - ffi_moq_ffi_rust_future_free_u64, - (ptr) => - FfiConverterMoqSocketIncomingTcp.lift(Pointer.fromAddress(ptr)), - moqExceptionErrorHandler, - ); - } - UniffiRustFuture acceptTrack() { return uniffiRustCallAsync( () => uniffi_moq_ffi_fn_method_moqsocketendpoint_accept_track( @@ -16384,22 +16367,6 @@ class MoqSocketEndpoint implements MoqSocketEndpointInterface { }, null); } - UniffiRustFuture dialTcp({required String peer}) { - return uniffiRustCallAsync( - () => uniffi_moq_ffi_fn_method_moqsocketendpoint_dial_tcp( - uniffiClonePointer(), - FfiConverterString.lower(peer), - ), - ffi_moq_ffi_rust_future_poll_u64, - ffi_moq_ffi_rust_future_complete_u64, - ffi_moq_ffi_rust_future_cancel_u64, - ffi_moq_ffi_rust_future_free_u64, - (ptr) => - FfiConverterMoqSocketTcpStream.lift(Pointer.fromAddress(ptr)), - moqExceptionErrorHandler, - ); - } - UniffiRustFuture dialTrack({ required String peer, required MoqTrackInfo? info, @@ -16469,125 +16436,6 @@ class FfiConverterMoqSocketEndpoint { } } -abstract class MoqSocketIncomingTcpInterface { - UniffiRustFuture accept(); - String peer(); - void reject(); - String session(); -} - -final _MoqSocketIncomingTcpFinalizer = Finalizer>((ptr) { - rustCall( - (status) => uniffi_moq_ffi_fn_free_moqsocketincomingtcp(ptr, status), - ); -}); - -class MoqSocketIncomingTcp implements MoqSocketIncomingTcpInterface { - late final Pointer _ptr; - bool _isDisposed = false; - MoqSocketIncomingTcp._(this._ptr) { - _MoqSocketIncomingTcpFinalizer.attach(this, _ptr, detach: this); - } - factory MoqSocketIncomingTcp.lift(Pointer ptr) { - return MoqSocketIncomingTcp._(ptr); - } - Pointer uniffiClonePointer() { - if (_isDisposed) { - throw StateError("Object has already been disposed"); - } - return rustCall( - (status) => uniffi_moq_ffi_fn_clone_moqsocketincomingtcp(_ptr, status), - ); - } - - void dispose() { - if (_isDisposed) { - return; - } - final status = calloc(); - _isDisposed = true; - _MoqSocketIncomingTcpFinalizer.detach(this); - try { - uniffi_moq_ffi_fn_free_moqsocketincomingtcp(_ptr, status); - checkCallStatus(NullRustCallStatusErrorHandler(), status); - } finally { - calloc.free(status); - } - } - - UniffiRustFuture accept() { - return uniffiRustCallAsync( - () => uniffi_moq_ffi_fn_method_moqsocketincomingtcp_accept( - uniffiClonePointer(), - ), - ffi_moq_ffi_rust_future_poll_u64, - ffi_moq_ffi_rust_future_complete_u64, - ffi_moq_ffi_rust_future_cancel_u64, - ffi_moq_ffi_rust_future_free_u64, - (ptr) => - FfiConverterMoqSocketTcpStream.lift(Pointer.fromAddress(ptr)), - moqExceptionErrorHandler, - ); - } - - String peer() { - return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqsocketincomingtcp_peer( - uniffiClonePointer(), - status, - ), - FfiConverterString.lift, - null, - ); - } - - void reject() { - return rustCall((status) { - uniffi_moq_ffi_fn_method_moqsocketincomingtcp_reject( - uniffiClonePointer(), - status, - ); - }, moqExceptionErrorHandler); - } - - String session() { - return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqsocketincomingtcp_session( - uniffiClonePointer(), - status, - ), - FfiConverterString.lift, - null, - ); - } -} - -class FfiConverterMoqSocketIncomingTcp { - static MoqSocketIncomingTcp lift(Pointer ptr) { - return MoqSocketIncomingTcp.lift(ptr); - } - - static Pointer lower(MoqSocketIncomingTcp value) { - return value.uniffiClonePointer(); - } - - static int allocationSize(MoqSocketIncomingTcp value) { - return 8; - } - - static LiftRetVal read(Uint8List buf) { - final handle = buf.buffer.asByteData(buf.offsetInBytes).getInt64(0); - final pointer = Pointer.fromAddress(handle); - return LiftRetVal(MoqSocketIncomingTcp.lift(pointer), 8); - } - - static int write(MoqSocketIncomingTcp value, Uint8List buf) { - final handle = lower(value); - buf.buffer.asByteData(buf.offsetInBytes).setInt64(0, handle.address); - return 8; - } -} - abstract class MoqSocketIncomingTrackInterface { UniffiRustFuture accept({required MoqTrackInfo? info}); String peer(); @@ -16708,121 +16556,6 @@ class FfiConverterMoqSocketIncomingTrack { } } -abstract class MoqSocketTcpStreamInterface { - MoqGroupConsumer consumer(); - String peer(); - MoqGroupProducer producer(); - String session(); -} - -final _MoqSocketTcpStreamFinalizer = Finalizer>((ptr) { - rustCall((status) => uniffi_moq_ffi_fn_free_moqsockettcpstream(ptr, status)); -}); - -class MoqSocketTcpStream implements MoqSocketTcpStreamInterface { - late final Pointer _ptr; - bool _isDisposed = false; - MoqSocketTcpStream._(this._ptr) { - _MoqSocketTcpStreamFinalizer.attach(this, _ptr, detach: this); - } - factory MoqSocketTcpStream.lift(Pointer ptr) { - return MoqSocketTcpStream._(ptr); - } - Pointer uniffiClonePointer() { - if (_isDisposed) { - throw StateError("Object has already been disposed"); - } - return rustCall( - (status) => uniffi_moq_ffi_fn_clone_moqsockettcpstream(_ptr, status), - ); - } - - void dispose() { - if (_isDisposed) { - return; - } - final status = calloc(); - _isDisposed = true; - _MoqSocketTcpStreamFinalizer.detach(this); - try { - uniffi_moq_ffi_fn_free_moqsockettcpstream(_ptr, status); - checkCallStatus(NullRustCallStatusErrorHandler(), status); - } finally { - calloc.free(status); - } - } - - MoqGroupConsumer consumer() { - return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqsockettcpstream_consumer( - uniffiClonePointer(), - status, - ), - FfiConverterMoqGroupConsumer.lift, - null, - ); - } - - String peer() { - return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqsockettcpstream_peer( - uniffiClonePointer(), - status, - ), - FfiConverterString.lift, - null, - ); - } - - MoqGroupProducer producer() { - return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqsockettcpstream_producer( - uniffiClonePointer(), - status, - ), - FfiConverterMoqGroupProducer.lift, - null, - ); - } - - String session() { - return rustCallWithLifter( - (status) => uniffi_moq_ffi_fn_method_moqsockettcpstream_session( - uniffiClonePointer(), - status, - ), - FfiConverterString.lift, - null, - ); - } -} - -class FfiConverterMoqSocketTcpStream { - static MoqSocketTcpStream lift(Pointer ptr) { - return MoqSocketTcpStream.lift(ptr); - } - - static Pointer lower(MoqSocketTcpStream value) { - return value.uniffiClonePointer(); - } - - static int allocationSize(MoqSocketTcpStream value) { - return 8; - } - - static LiftRetVal read(Uint8List buf) { - final handle = buf.buffer.asByteData(buf.offsetInBytes).getInt64(0); - final pointer = Pointer.fromAddress(handle); - return LiftRetVal(MoqSocketTcpStream.lift(pointer), 8); - } - - static int write(MoqSocketTcpStream value, Uint8List buf) { - final handle = lower(value); - buf.buffer.asByteData(buf.offsetInBytes).setInt64(0, handle.address); - return 8; - } -} - abstract class MoqSocketTrackStreamInterface { MoqTrackConsumer consumer(); String peer(); @@ -23534,11 +23267,6 @@ external Pointer uniffi_moq_ffi_fn_constructor_moqsocketendpoint_new( Pointer uniffiStatus, ); -@Native Function(Pointer)>(assetId: _uniffiAssetId) -external Pointer uniffi_moq_ffi_fn_method_moqsocketendpoint_accept_tcp( - Pointer ptr, -); - @Native Function(Pointer)>(assetId: _uniffiAssetId) external Pointer uniffi_moq_ffi_fn_method_moqsocketendpoint_accept_track( Pointer ptr, @@ -23552,14 +23280,6 @@ external void uniffi_moq_ffi_fn_method_moqsocketendpoint_close( Pointer uniffiStatus, ); -@Native Function(Pointer, RustBuffer)>( - assetId: _uniffiAssetId, -) -external Pointer uniffi_moq_ffi_fn_method_moqsocketendpoint_dial_tcp( - Pointer ptr, - RustBuffer peer, -); - @Native Function(Pointer, RustBuffer, RustBuffer)>( assetId: _uniffiAssetId, ) @@ -23585,51 +23305,6 @@ external RustBuffer uniffi_moq_ffi_fn_method_moqsocketendpoint_prefix( Pointer uniffiStatus, ); -@Native Function(Pointer, Pointer)>( - assetId: _uniffiAssetId, -) -external Pointer uniffi_moq_ffi_fn_clone_moqsocketincomingtcp( - Pointer handle, - Pointer uniffiStatus, -); - -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external void uniffi_moq_ffi_fn_free_moqsocketincomingtcp( - Pointer handle, - Pointer uniffiStatus, -); - -@Native Function(Pointer)>(assetId: _uniffiAssetId) -external Pointer uniffi_moq_ffi_fn_method_moqsocketincomingtcp_accept( - Pointer ptr, -); - -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external RustBuffer uniffi_moq_ffi_fn_method_moqsocketincomingtcp_peer( - Pointer ptr, - Pointer uniffiStatus, -); - -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external void uniffi_moq_ffi_fn_method_moqsocketincomingtcp_reject( - Pointer ptr, - Pointer uniffiStatus, -); - -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external RustBuffer uniffi_moq_ffi_fn_method_moqsocketincomingtcp_session( - Pointer ptr, - Pointer uniffiStatus, -); - @Native Function(Pointer, Pointer)>( assetId: _uniffiAssetId, ) @@ -23678,54 +23353,6 @@ external RustBuffer uniffi_moq_ffi_fn_method_moqsocketincomingtrack_session( Pointer uniffiStatus, ); -@Native Function(Pointer, Pointer)>( - assetId: _uniffiAssetId, -) -external Pointer uniffi_moq_ffi_fn_clone_moqsockettcpstream( - Pointer handle, - Pointer uniffiStatus, -); - -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external void uniffi_moq_ffi_fn_free_moqsockettcpstream( - Pointer handle, - Pointer uniffiStatus, -); - -@Native Function(Pointer, Pointer)>( - assetId: _uniffiAssetId, -) -external Pointer uniffi_moq_ffi_fn_method_moqsockettcpstream_consumer( - Pointer ptr, - Pointer uniffiStatus, -); - -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external RustBuffer uniffi_moq_ffi_fn_method_moqsockettcpstream_peer( - Pointer ptr, - Pointer uniffiStatus, -); - -@Native Function(Pointer, Pointer)>( - assetId: _uniffiAssetId, -) -external Pointer uniffi_moq_ffi_fn_method_moqsockettcpstream_producer( - Pointer ptr, - Pointer uniffiStatus, -); - -@Native, Pointer)>( - assetId: _uniffiAssetId, -) -external RustBuffer uniffi_moq_ffi_fn_method_moqsockettcpstream_session( - Pointer ptr, - Pointer uniffiStatus, -); - @Native Function(Pointer, Pointer)>( assetId: _uniffiAssetId, ) @@ -25053,18 +24680,12 @@ external int uniffi_moq_ffi_checksum_method_moqtokens_answered(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqtokens_set(); -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsocketendpoint_accept_tcp(); - @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqsocketendpoint_accept_track(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqsocketendpoint_close(); -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsocketendpoint_dial_tcp(); - @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqsocketendpoint_dial_track(); @@ -25074,18 +24695,6 @@ external int uniffi_moq_ffi_checksum_method_moqsocketendpoint_id(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqsocketendpoint_prefix(); -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_accept(); - -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_peer(); - -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_reject(); - -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_session(); - @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqsocketincomingtrack_accept(); @@ -25098,18 +24707,6 @@ external int uniffi_moq_ffi_checksum_method_moqsocketincomingtrack_reject(); @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqsocketincomingtrack_session(); -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsockettcpstream_consumer(); - -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsockettcpstream_peer(); - -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsockettcpstream_producer(); - -@Native(assetId: _uniffiAssetId) -external int uniffi_moq_ffi_checksum_method_moqsockettcpstream_session(); - @Native(assetId: _uniffiAssetId) external int uniffi_moq_ffi_checksum_method_moqsockettrackstream_consumer(); @@ -25987,18 +25584,13 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqtokens_set() != 20110) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqsocketendpoint_accept_tcp() != 55983) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqsocketendpoint_accept_track() != 3641) { + if (uniffi_moq_ffi_checksum_method_moqsocketendpoint_accept_track() != + 21114) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } if (uniffi_moq_ffi_checksum_method_moqsocketendpoint_close() != 18581) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqsocketendpoint_dial_tcp() != 44736) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } if (uniffi_moq_ffi_checksum_method_moqsocketendpoint_dial_track() != 54786) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } @@ -26008,18 +25600,6 @@ void _checkApiChecksums() { if (uniffi_moq_ffi_checksum_method_moqsocketendpoint_prefix() != 7033) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_accept() != 37027) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_peer() != 44709) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_reject() != 1335) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqsocketincomingtcp_session() != 25054) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } if (uniffi_moq_ffi_checksum_method_moqsocketincomingtrack_accept() != 43414) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } @@ -26033,18 +25613,6 @@ void _checkApiChecksums() { 36889) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } - if (uniffi_moq_ffi_checksum_method_moqsockettcpstream_consumer() != 11536) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqsockettcpstream_peer() != 13339) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqsockettcpstream_producer() != 6563) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } - if (uniffi_moq_ffi_checksum_method_moqsockettcpstream_session() != 61735) { - throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); - } if (uniffi_moq_ffi_checksum_method_moqsockettrackstream_consumer() != 2651) { throw UniffiInternalError.panicked("UniFFI API checksum mismatch"); } diff --git a/doc/bin/cli.md b/doc/bin/cli.md index eaa99666881..50b9fe395de 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -38,7 +38,6 @@ install. It needs an ARMv7 CPU, so not a Pi 1, Zero, or Zero W. | `transcode` | | Publish a just-in-time rendition ladder next to a broadcast. | | `announced` | `[prefix]` | Follow the broadcasts announced on a relay. | | `fetch` | `` | Write one group of a track to stdout. | -| `tcp` | `dial `, `accept ` | Forward TCP connections through a relay. | | `auth` | | Generate, sign, and verify relay JWTs. | | `repair` | `mp4 ` | Finish an `export mp4` recording that was cut short. | | `devices` | | List capture sources and their ids. | @@ -51,8 +50,6 @@ moq export [options] moq play [options] moq announced [prefix] [options] moq fetch [options] -moq tcp dial --local [options] -moq tcp accept --local [options] ``` The **MoQ side** goes first and attaches the process to the network: @@ -323,28 +320,6 @@ non-zero when the broadcast or group is not found. [Inspect a relay](/bin/inspect) walks through `announced` and `fetch` next to their `curl` equivalents. -## TCP - -```bash -# On the server's host: accept sessions as `ssh` and connect each to the local SSH server. -moq --connect https://relay.example.com/anon tcp accept ssh --local 127.0.0.1:22 - -# On the client's host: listen locally and dial `ssh` for each connection. -moq --connect https://relay.example.com/anon tcp dial ssh --local 127.0.0.1:2222 -ssh -p 2222 user@127.0.0.1 -``` - -Forwards TCP connections through a relay, so an unmodified client and server -talk without either being reachable from the other. Each connection is a -[socket](/concept/socket) `tcp` session: bytes arrive in order with nothing -lost, a FIN is passed on as a FIN, and a reset as a reset. `tcp accept` connects -to `--local` before it accepts a dial, so a refused connection refuses the dial. -`--prefix` names where the two ends meet under the connection path, and -`--priority` schedules the bytes this end receives against the relay's other -traffic. Every hop must carry hidden broadcasts, so unless `--connect-version` -says otherwise it offers only `moq-lite-07-wip` and moq-transport, and the -relay must accept one of them. - ## Archive Recording through our publishers and relays needs opt-in `moq-lite-07-wip` diff --git a/doc/concept/index.md b/doc/concept/index.md index 1b1884eaca3..05edb3ac896 100644 --- a/doc/concept/index.md +++ b/doc/concept/index.md @@ -38,7 +38,7 @@ 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, as reliable byte pipes or as tracks of messages. +- [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, encryption, 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 index d40e5f107a6..5bb13e11c87 100644 --- a/doc/concept/socket.md +++ b/doc/concept/socket.md @@ -1,6 +1,6 @@ --- title: Sockets -description: Dial and accept point-to-point sessions over MoQ, as reliable byte pipes or as tracks of messages +description: Dial and accept point-to-point sessions over MoQ, each a track of messages each way --- # Sockets @@ -9,9 +9,8 @@ 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)), -in one of two protocols: `tcp`, a reliable, ordered byte -pipe each way, or `track`, a track each way whose groups are messages. Use a +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. @@ -58,23 +57,23 @@ each other. Both role segments start with `.`, so endpoints are 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, whatever their protocol. +session tracks, served on request. ## Sessions -A session between a dialer `A` and an acceptor `B` is named by its protocol `P`, -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, `///`, one in the -dialer's client broadcast and one in the acceptor's server broadcast: +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 | | --- | --- | --- | --- | -| `P/A/B/S` | `/.client/A` | `A` | `B` | -| `P/A/B/S` | `/.server/B` | `B` | `A` | +| `track/A/B/S` | `/.client/A` | `A` | `B` | +| `track/A/B/S` | `/.server/B` | `B` | `A` | -The protocol is `tcp` or `track`; see [Protocols](#protocols). A server resets -a dial whose protocol it doesn't speak, or isn't accepting, as unsupported. +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 @@ -86,16 +85,16 @@ or others. ### Dial -1. `A` picks `S` and holds its own half, `P/A/B/S` in `/.client/A`, +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 `P/A/B/S`, waiting for +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 in protocol `P`, claiming to be from `A`, with session `S`. + an incoming dial, claiming to be from `A`, with session `S`. ### Accept -1. `B` subscribes to `/.client/A` for the track `P/A/B/S`, and holds +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. @@ -110,63 +109,21 @@ or others. ### Reject `B` resets `A`'s subscription, with not found, as for a session that does not -exist. A server resets a dial in a protocol it is not accepting as unsupported. -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 +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. -## Protocols +## Messages Each side writes its own half and reads the peer's. A reader subscribes -reliably and in order: from the half's first frame, oldest first, with no delay -budget, so it waits for a late group rather than skipping it. The protocol -decides what a half holds. - -### `tcp` - -Each half is exactly one group, sequence 0. In MoQ a group travels as one QUIC -stream on every hop, so it is reliable, strictly ordered, and flow controlled, -exactly like one direction of a TCP connection: - -- A frame is a write. The reader reads the frames in the order they were - written, with nothing skipped. -- The reader joins the live group, which is the half's only one, from its - first frame. Over moq-transport, that is a `SUBSCRIBE` plus a joining `FETCH` - (or a fill) for the frames written before its answer. -- Finishing the group is FIN: the reader reads to the end, then sees the end. -- Resetting the group, or its track, is RST: the reader fails with the error. -- A second group on a session track is a protocol error: the reader resets the - session, both halves. - -There are no track-level settings: no priority between messages, no delay -budget, no order, since the newest group is never stale. - -A session can last indefinitely: a group has no size cap, and each copy keeps -only a bounded tail of it. A writer that waits for room before each write (in -Rust, `group::Producer::ready`; in the bindings, `GroupProducer.ready(size)`) runs at most a window ahead of the reader on -every hop, so a slow reader slows the writer, as TCP backpressure does, and -nothing is lost. A writer that doesn't wait sheds the front of its half, and a -reader that falls behind it fails with an eviction error instead of skipping -data. Pacing reaches across native QUIC and qmux hops; see -[unbounded groups](https://github.com/moq-dev/moq/tree/main/quest/m3/unbounded-groups) -for what remains, such as browsers, which read without pacing. - -Use `tcp` for a byte stream or a log of records that must all arrive in order, -such as a proxied TCP connection, a file transfer, or a request and its reply. -Each side's reader subscribes at a priority it picks, which schedules the -peer's half against its other subscriptions on every hop. In Rust, -`moq_tokio::socket::TcpStream` reads and writes a `tcp` session as tokio -`AsyncRead` and `AsyncWrite`, and [`moq tcp`](/bin/cli#tcp) forwards TCP -connections over them. - -### `track` +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 +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 @@ -174,15 +131,14 @@ 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. -The reader's subscription starts reliable and in order, like `tcp`. An -application may make it lossy for messages that go stale, such as positions or -controls, by updating the 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. +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 `track` for independent messages, where a late one should not block the +Use a socket for independent messages, where a late one should not block the next: commands, controls, acknowledgements, or telemetry. ## Closing @@ -204,8 +160,8 @@ 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 `P/A/B/S` as a session for another -acceptor, and `A` never dialed `P/A/C/S`, so it resets `C`'s accept of that. +`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: @@ -261,6 +217,5 @@ planned mesh layer makes ids public keys. ## Later Later protocols are other prefixes on the same endpoint broadcasts, with no -new announcement. A multiplexed `quic` mode, with many streams per session like -QUIC, would name its sessions `quic///` and bring back several -groups or streams per session. +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 3b2b9325c1d..0a8b6e00973 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -113,30 +113,24 @@ const connection = await Moq.Connection.connect({ publish: origin.consume(), consume: origin, }); const endpoint = new Moq.Socket.Endpoint(origin, Moq.Path.from("game"), "viewer"); -const session = await endpoint.dialTcp("server", { signal: abort.signal }); -session.producer.writeFrame({ payload: new TextEncoder().encode("hello") }); -session.producer.finish(); // FIN in this direction; the peer may still reply. -for (let frame = await session.consumer.readFrame(); frame; frame = await session.consumer.readFrame()) { +const session = await endpoint.dialTrack("server", undefined, { signal: abort.signal }); +session.producer.writeFrame({ payload: new TextEncoder().encode("hello") }); // One message, one group. +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.acceptTcp()`, then accepts or rejects that incoming dial. +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. -TCP and track dials have independent accept queues. For independent messages, use -`dialTrack` / `acceptTrack`: the stream exposes a track producer and an already subscribed -consumer in ascending order until updated. It skips groups that never arrive whole, -including refused, dropped, or evicted groups; a cut group is never a complete message. -TCP joins its one group live, recovering the head with a joining FETCH or fill over -moq-transport. Both stream types expose `peer` and -`session`; read and write through their producer and consumer handles. - -JS TCP currently reads without fastest-reader pacing. A reader that falls behind the -group's bounded tail fails with an eviction error, so keeping up is required for lossless -delivery. Finishing a producer is FIN; aborting, closing the endpoint, or releasing a TCP -reader early resets the session. Close endpoints explicitly, and use `AbortSignal` to -cancel pending dials and accepts. Keep session ids secret. +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 9785aa23523..0eef5bf1fa9 100644 --- a/doc/lib/py/index.md +++ b/doc/lib/py/index.md @@ -170,11 +170,12 @@ async def socket_client(): 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_tcp("server") - stream.producer.write_frame(b"command") - stream.producer.finish() # FIN; abort(error_code) sends RST instead. - async for frame in stream.consumer: - print(frame.payload) + stream = await endpoint.dial_track("server") + stream.producer.write_frame(b"command") # One message: a group of one frame. + 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()) ``` @@ -183,16 +184,11 @@ 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_tcp()`, then calls `await incoming.accept()` +A server awaits `endpoint.accept_track()`, then calls `await incoming.accept()` or `incoming.reject()`. Accepting verifies that the claimed peer really dialed. -`TcpStream` exposes group `producer` and `consumer` handles, plus `peer` and -`session`. Keep the session id private. Its bounded cache reports eviction if a -reader falls behind; it never skips bytes. - -Use `dial_track(peer, info=None)` and `accept_track()` for independent groups of -messages. `TrackStream` exposes a track producer and a reliably subscribed, -ascending consumer. Omitted `info` keeps messages without a publisher age limit; -update the consumer's subscription to change delivery. The protocols have -separate accept queues. Cancelling a dial or acceptance resets that dial; +`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 69f2dae3415..7fec6963e28 100644 --- a/doc/lib/rs/moq-net.md +++ b/doc/lib/rs/moq-net.md @@ -22,7 +22,7 @@ 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. A session speaks one of two protocols, each with its own dial and accept. `accept_tcp()` and `accept_track()` yield an `Incoming` dial to `accept` or `reject` (dropping it rejects). `dial_tcp(server, priority)` and a `tcp` dial accepted with `accept(priority)` yield a `socket::TcpStream`, whose reader subscribes to the peer's half at that priority, a `group::Producer` and `group::Consumer` pair carrying one reliable, ordered byte pipe each way (finish is FIN, abort is RST, and `ready` paces the writer to the reader; `moq_tokio::socket::TcpStream` reads and writes it as tokio `AsyncRead` and `AsyncWrite`); `dial_track(server, info)` and an accepted `track` 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). +- **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. The application owns sequence numbers: the broadcast keeps nothing of an ended track, so a producer that replaces it starts at 0. A name always means the same content, so the replacement continues past its predecessor's groups with `create_group` at an explicit sequence. 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. - **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`, which 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. A finished group returns a `group::Finished`, which can be read or aborted to release its cache. diff --git a/go/wrapper/bridge.go b/go/wrapper/bridge.go index 9d8769c457f..be29fbb7566 100644 --- a/go/wrapper/bridge.go +++ b/go/wrapper/bridge.go @@ -9,8 +9,6 @@ import ( // without exporting them. func init() { bridge.OriginProducer = func(v any) *ffi.MoqOriginProducer { return v.(*OriginProducer).inner } - bridge.WrapGroupProducer = func(v *ffi.MoqGroupProducer) any { return &GroupProducer{inner: v} } - bridge.WrapGroupConsumer = func(v *ffi.MoqGroupConsumer) any { return &GroupConsumer{inner: v} } 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)) } diff --git a/go/wrapper/internal/bridge/handles.go b/go/wrapper/internal/bridge/handles.go index 68421803225..e1f3b9dd0c5 100644 --- a/go/wrapper/internal/bridge/handles.go +++ b/go/wrapper/internal/bridge/handles.go @@ -14,10 +14,6 @@ import ( var ( // OriginProducer returns the handle behind a *moq.OriginProducer. OriginProducer func(any) *ffi.MoqOriginProducer - // WrapGroupProducer wraps a socket write half. - WrapGroupProducer func(*ffi.MoqGroupProducer) any - // WrapGroupConsumer wraps a socket read half. - WrapGroupConsumer func(*ffi.MoqGroupConsumer) any // WrapTrackProducer wraps a socket write half. WrapTrackProducer func(*ffi.MoqTrackProducer) any // WrapTrackConsumer retains a socket's subscribed read half. diff --git a/go/wrapper/socket/socket.go b/go/wrapper/socket/socket.go index 3770e129761..92737e7fef5 100644 --- a/go/wrapper/socket/socket.go +++ b/go/wrapper/socket/socket.go @@ -8,7 +8,7 @@ import ( "moq.dev/moq/internal/bridge" ) -// Endpoint is a server, a client, or both, and accepts TCP and track dials separately. +// 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 @@ -30,66 +30,6 @@ 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() } -// DialTCP dials a new tcp session; cancelling resets only this dial. -func (e *Endpoint) DialTCP(ctx context.Context, peer string) (*TCPStream, error) { - inner, err := bridge.CallHandle(ctx, nil, func(ctx context.Context) (*ffi.MoqSocketTcpStream, error) { return e.inner.DialTcp(ctx, peer) }) - if err != nil { - return nil, err - } - return newTCPStream(inner), nil -} - -// AcceptTCP starts listening for tcp and awaits the next dial. -func (e *Endpoint) AcceptTCP(ctx context.Context) (*IncomingTCP, error) { - inner, err := bridge.CallHandle(ctx, nil, e.inner.AcceptTcp) - if err != nil { - return nil, err - } - return &IncomingTCP{inner: inner}, nil -} - -// IncomingTCP is a tcp dial, consumed by accepting or rejecting it. -type IncomingTCP struct{ inner *ffi.MoqSocketIncomingTcp } - -// Peer returns the claimed endpoint; accepting verifies return routability. -func (i *IncomingTCP) Peer() string { return i.inner.Peer() } - -// Session returns the private session id. -func (i *IncomingTCP) Session() string { return i.inner.Session() } - -// Reject refuses the dial as not found. -func (i *IncomingTCP) Reject() error { return i.inner.Reject() } - -// Accept verifies return routability and accepts this dial. -func (i *IncomingTCP) Accept(ctx context.Context) (*TCPStream, error) { - inner, err := bridge.CallHandle(ctx, nil, func(ctx context.Context) (*ffi.MoqSocketTcpStream, error) { return i.inner.Accept(ctx) }) - if err != nil { - return nil, err - } - return newTCPStream(inner), nil -} - -// TCPStream exposes a tcp session's two halves and identities. -type TCPStream struct { - // Producer is this side's write half. - Producer *moq.GroupProducer - // Consumer is the peer's subscribed read half. - Consumer *moq.GroupConsumer - // Peer is the peer endpoint's id. - Peer string - // Session is the private session id. - Session string -} - -func newTCPStream(inner *ffi.MoqSocketTcpStream) *TCPStream { - defer inner.Destroy() - return &TCPStream{ - Producer: bridge.WrapGroupProducer(inner.Producer()).(*moq.GroupProducer), - Consumer: bridge.WrapGroupConsumer(inner.Consumer()).(*moq.GroupConsumer), - Peer: inner.Peer(), Session: inner.Session(), - } -} - // 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) diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index b392d830334..81a90222648 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -602,17 +602,15 @@ export * as Signals from "@moq/signals"; export namespace Socket { export class Endpoint { constructor(origin: Origin.Producer, prefix: Path.Valid, id: string); - acceptTcp(options?: Socket.Options): Promise>; - acceptTrack(options?: Socket.Options): Promise>; + acceptTrack(options?: Socket.Options): Promise; close(): void; get closed(): Signals.GetPromise; - dialTcp(peer: string, options?: Socket.TcpOptions): Promise; dialTrack(peer: string, info?: Track.InfoInit, options?: Socket.Options): Promise; readonly id: string; readonly prefix: Path.Valid; } - export class Incoming { - accept(...args: S extends Socket.TcpStream ? [options?: Socket.TcpOptions] : [info?: Track.InfoInit, options?: Socket.Options]): Promise; + export class Incoming { + accept(info?: Track.InfoInit, options?: Socket.Options): Promise; readonly peer: string; reject(): void; readonly session: string; @@ -620,15 +618,6 @@ export namespace Socket { export interface Options { signal?: AbortSignal; } - export interface TcpOptions extends Socket.Options { - priority?: number; - } - export class TcpStream { - readonly consumer: Group.Consumer; - readonly peer: string; - readonly producer: Group.Producer; - readonly session: string; - } export class TrackStream { readonly consumer: Track.Subscriber; readonly peer: string; diff --git a/js/net/bench/socket.ts b/js/net/bench/socket.ts index caa634a8a3e..fba778c5310 100644 --- a/js/net/bench/socket.ts +++ b/js/net/bench/socket.ts @@ -1,4 +1,4 @@ -/** Per-session socket handshake, frame delivery and teardown swept over endpoints x sessions per endpoint. */ +/** 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"; @@ -7,79 +7,72 @@ import * as Lite from "../src/lite/index.ts"; import { createMockTransportPair } from "../src/mock.ts"; console.debug = () => {}; -console.log("mode,protocol,endpoints,sessions_per_endpoint,open_us,frame_us,close_us"); +console.log("mode,endpoints,sessions_per_endpoint,open_us,frame_us,close_us"); for (const mode of ["local", "lite07"] as const) - for (const protocol of ["tcp", "track"] 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 = protocol === "tcp" ? peer.acceptTcp() : peer.acceptTrack(); - const dialing = - protocol === "tcp" ? dialer.dialTcp(peer.id) : dialer.dialTrack(peer.id); - return Promise.all([dialing, (await incoming).accept()]); + 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, }), - ), - ); - const opened = performance.now(); - await Promise.all( - pairs.map(async ([local, remote]) => { - local.producer.writeFrame({ payload: new Uint8Array(1024) }); - const frame = - remote instanceof Socket.TcpStream - ? await remote.consumer.readFrame() - : await remote.consumer.ordered().readFrame(); - if (frame?.payload.length !== 1024) throw new Error("missing socket payload"); + 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 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), + ), + ); + const opened = performance.now(); + await Promise.all( + pairs.map(async ([local, remote]) => { + local.producer.writeFrame({ payload: new Uint8Array(1024) }); + const frame = await remote.consumer.ordered().readFrame(); + if (frame?.payload.length !== 1024) throw new Error("missing socket payload"); + }), ); - console.log(`${mode},${protocol},${endpoints},${sessions},${median.join(",")}`); + 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 edd36adcf96..79fee8bad8d 100644 --- a/js/net/src/index.ts +++ b/js/net/src/index.ts @@ -35,7 +35,7 @@ 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 TCP and track sessions through an origin. */ +/** 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"; diff --git a/js/net/src/socket.test.ts b/js/net/src/socket.test.ts index 17f37c93c04..edefcc7dbab 100644 --- a/js/net/src/socket.test.ts +++ b/js/net/src/socket.test.ts @@ -4,7 +4,7 @@ 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, ProtocolViolation, StreamCode, StreamError } from "./error.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"; @@ -34,13 +34,6 @@ function setup() { const b = new Socket.Endpoint(origin, Path.from("room"), "bob"); return { origin, a, b }; } -async function tcp() { - const state = setup(); - const incoming = state.b.acceptTcp(); - const dialing = state.a.dialTcp("bob"); - const remote = await (await incoming).accept(); - return { ...state, local: await dialing, remote }; -} async function track() { const state = setup(); const incoming = state.b.acceptTrack(); @@ -61,9 +54,8 @@ async function raw(origin: Origin.Producer, role: Role, id: string, name: string return (await broadcast(origin, role, id)).track(name).subscribe({ order: "ascending", delay: { max: null } }); } -test("Socket namespace exposes endpoint and both stream roles", () => { +test("Socket namespace exposes endpoint and stream", () => { expect(Socket.Endpoint).toBeFunction(); - expect(Socket.TcpStream).toBeFunction(); expect(Socket.TrackStream).toBeFunction(); }); @@ -84,24 +76,6 @@ test("an endpoint closes with its origin before it dials or accepts", async () = expect(new Socket.Endpoint(origin, Path.from("room"), "bob").closed.peek()).toBeNull(); }); -test("TCP carries ordered frames in both directions, with independent FIN", async () => { - const { a, b, local, remote } = await tcp(); - expect(local.peer).toBe("bob"); - expect(remote.peer).toBe("alice"); - expect(local.session).toBe(remote.session); - expect(local.session).toMatch(/^[0-9a-f]{32}$/); - for (let i = 0; i < 3; i++) local.producer.writeFrame(bytes(i)); - local.producer.finish(); - for (let i = 0; i < 3; i++) expect((await remote.consumer.readFrame())?.payload).toEqual(bytes(i).payload); - expect(await remote.consumer.readFrame()).toBeUndefined(); - remote.producer.writeFrame(bytes(9)); - remote.producer.finish(); - expect((await local.consumer.readFrame())?.payload).toEqual(bytes(9).payload); - expect(await local.consumer.readFrame()).toBeUndefined(); - a.close(); - b.close(); -}); - test("a peer route replaced between metadata and opening cannot inherit a socket session", async () => { const clock = time(); const { origin, a } = setup(); @@ -163,6 +137,10 @@ test("a peer route replaced between metadata and opening cannot inherit a socket 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)); @@ -197,24 +175,30 @@ test("a track session skips dropped groups and keeps the next message", async () expect(await reader.readFrame()).toBeUndefined(); }); -test("each protocol has its own accept queue and sessions never reuse an identity", async () => { +test("concurrent sessions between the same endpoints never reuse an identity", async () => { const { a, b } = setup(); - const nextTrack = b.acceptTrack(); - const nextTcp = b.acceptTcp(); - const dialingTcp = a.dialTcp("bob"); - const dialingTrack = a.dialTrack("bob"); - const [incomingTrack, incomingTcp] = await Promise.all([nextTrack, nextTcp]); - expect(incomingTcp.session).not.toBe(incomingTrack.session); - const [remoteTrack, remoteTcp, localTrack, localTcp] = await Promise.all([ - incomingTrack.accept(), - incomingTcp.accept(), - dialingTrack, - dialingTcp, + 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, ]); - localTrack.producer.writeFrame(bytes(1)); - localTcp.producer.writeFrame(bytes(2)); - expect((await remoteTrack.consumer.ordered().readFrame())?.payload).toEqual(bytes(1).payload); - expect((await remoteTcp.consumer.readFrame())?.payload).toEqual(bytes(2).payload); + 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 () => { @@ -235,69 +219,64 @@ test("finished track sessions release their unlisted producers after both halves expect(hooks.trackReleased(remote.producer).peek()).toBeNull(); }); -for (const protocol of ["tcp", "track"] as const) { - test(`late ${protocol} reads retain return-routability checks after the original dial closes`, async () => { - const clock = time(); - const { origin, a, b, local, remote } = await (protocol === "tcp" ? tcp() : track()); - const next = protocol === "tcp" ? b.acceptTcp() : b.acceptTrack(); - void next.catch(() => {}); - const reset = new StreamError(StreamCode(70)); - const oldReader = local instanceof Socket.TcpStream ? local.consumer : local.consumer.ordered(); - remote.producer.abort(reset); +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); - const source = await broadcast(origin, ".server", "bob"); - let outcome: unknown; - const fetched = source - .track(`${protocol}/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 = protocol === "tcp" ? b.acceptTcp() : b.acceptTrack(); - const dialing = protocol === "tcp" ? a.dialTcp("bob") : 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 = - remoteFresh instanceof Socket.TcpStream - ? await remoteFresh.consumer.readFrame() - : 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; - } - }); -} + } 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.dialTcp("alice")).rejects.toBeInstanceOf(Error); + await expect(a.dialTrack("alice")).rejects.toBeInstanceOf(Error); expect(announced(origin, ".client", "alice")).toBeUndefined(); const cancel = new AbortController(); - const dialing = a.dialTcp("bob", { signal: cancel.signal }); + 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", "tcp/bob/alice/not-a-dial"); + const unsupported = await raw(origin, ".client", "alice", "track/bob/alice/not-a-dial"); await expect(unsupported.info()).rejects.toBeInstanceOf(NotFound); unsupported.close(); @@ -305,7 +284,7 @@ test("an endpoint announces only the roles it takes, and its client answers only const clock = time(); await clock.advance(Duration.secs(3600)); expect(announced(origin, ".server", "bob")).toBeUndefined(); - const incoming = b.acceptTcp(); + const incoming = b.acceptTrack(); expect(announced(origin, ".server", "bob")).toBeDefined(); expect(announced(origin, ".client", "bob")).toBeUndefined(); expect((await incoming).peer).toBe("alice"); @@ -313,38 +292,12 @@ test("an endpoint announces only the roles it takes, and its client answers only expect(await failure).toBeInstanceOf(Error); }); -test("a TCP dial and accept each read the peer's half at the priority they ask", async () => { - const { origin, a, b } = setup(); - // Bob's server endpoint, played by hand, sees the priority Alice dials with. - const server = origin.route(Path.from("room", ".server", "bob")); - const dialing = a.dialTcp("bob", { priority: 5 }); - void dialing.catch(() => {}); - const dial = await wireOf(server.broadcast).requested(); - expect(dial?.priority).toBe(5); - dial?.reject(new NotFound("socket session")); - server.close(); - - // Alice's client endpoint, played by hand, sees the priority Bob accepts with. - const client = origin.route(Path.from("room", ".client", "carol")); - const incoming = b.acceptTcp(); - const subscriber = await raw(origin, ".server", "bob", "tcp/carol/bob/0123456789abcdef0123456789abcdef"); - const accepting = (await incoming).accept({ priority: -3 }); - void accepting.catch(() => {}); - const answer = await wireOf(client.broadcast).requested(); - expect(answer?.priority).toBe(-3); - answer?.reject(new NotFound("socket session")); - subscriber.close(); - client.close(); - a.close(); - b.close(); -}); - -test("unknown and unaccepted protocols reset without entering the other queue", async () => { +test("dials in another protocol, tcp included, reset without entering the queue", async () => { const { origin, b } = setup(); const hold = new AbortController(); - const incoming = b.acceptTcp({ signal: hold.signal }); + const incoming = b.acceptTrack({ signal: hold.signal }); const rejected = incoming.catch((error: unknown) => error); - for (const protocol of ["track", "quic"]) { + 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(); @@ -355,26 +308,26 @@ test("unknown and unaccepted protocols reset without entering the other queue", test("reject refuses one dial and leaves another independent session usable", async () => { const { a, b } = setup(); - const incoming = b.acceptTcp(); - const refused = a.dialTcp("bob"); + 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.acceptTcp(); - const dialing = a.dialTcp("bob"); + 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.readFrame())?.payload).toEqual(bytes(42).payload); + 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.acceptTcp(); + const next = b.acceptTrack(); const cancel = new AbortController(); const reason = new Error("gave up"); - const dialing = a.dialTcp("bob", { signal: cancel.signal }); + const dialing = a.dialTrack("bob", undefined, { signal: cancel.signal }); const failure = dialing.catch((error: unknown) => error); const incoming = await next; cancel.abort(reason); @@ -382,13 +335,13 @@ test("cancelled dials leave queues and cannot be accepted later", async () => { 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.dialTcp("bob", { signal: pending.signal }); + 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.acceptTcp(); - const fresh = a.dialTcp("bob"); + 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]); @@ -398,13 +351,13 @@ test("dial and accept cancellation before announcement release their routing hol const clock = time(); const { origin, a, b } = setup(); const cancel = new AbortController(); - const dialing = a.dialTcp("later", { signal: cancel.signal }); + 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.acceptTcp(); - const subscriber = await raw(origin, ".server", "bob", "tcp/absent/bob/never-announced"); + 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)); @@ -415,14 +368,14 @@ test("dial and accept cancellation before announcement release their routing hol test("a dial waits for the peer to announce, without an arbitrary timeout", async () => { const clock = time(); const { origin, a } = setup(); - const dialing = a.dialTcp("later"); + 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.acceptTcp(); + const incoming = await later.acceptTrack(); const remote = await incoming.accept(); const local = await dialing; local.producer.writeFrame(bytes(1)); - expect((await remote.consumer.readFrame())?.payload).toEqual(bytes(1).payload); + 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 () => { @@ -448,10 +401,10 @@ test("accept waits for a delayed dialer announcement only while its original dia 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.dialTcp("carol").catch(() => {}); - for (const name of ["tcp/alice/bob/forged", "tcp/alice/someone-else/replayed"]) { + 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.acceptTcp({ signal: cancel.signal }); + 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); @@ -465,8 +418,8 @@ test("forged and replayed dials cannot pass return routability", async () => { test("session names are absent from listings while reserved and established", async () => { const { origin, a, b } = setup(); - const next = b.acceptTcp(); - const dialing = a.dialTcp("bob"); + const next = b.acceptTrack(); + const dialing = a.dialTrack("bob"); const incoming = await next; const endpoints = [ [".client", "alice"], @@ -487,286 +440,192 @@ test("session names are absent from listings while reserved and established", as expect(remote.session).toBe(incoming.session); }); -test("TCP RST, reader cancellation, and endpoint lifetime reset both halves", async () => { - for (const action of ["abort", "reader", "endpoint"] as const) { - const { a, local, remote } = await tcp(); - const reset = new StreamError(StreamCode(70)); - if (action === "abort") local.producer.abort(reset); - if (action === "reader") local.consumer.close(); - if (action === "endpoint") a.close(); - await expect(remote.consumer.readFrame()).rejects.toBeInstanceOf(Error); +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("track reset and endpoint close fail peers and leave no open accepts", async () => { - const { a, b, local, remote } = await track(); - local.producer.abort(new StreamError(StreamCode(71))); - await expect(remote.consumer.ordered().readFrame()).rejects.toBeInstanceOf(Error); - expect(await remote.producer.closed).toBeInstanceOf(Error); - const pending = b.acceptTcp(); - const failure = pending.catch((error: unknown) => error); - b.close(); - a.close(); - expect(await failure).toBeInstanceOf(Error); -}); - -test("a second TCP group resets the session even after the first FIN", async () => { - for (const initial of [0n, 2n]) { - const { origin, b } = setup(); - const route = origin.route(Path.from("room/.client/raw")); - const name = `tcp/raw/bob/second-${initial}`; - const answer = hooks.reserveUnlistedTrack(route.broadcast, name); - const next = b.acceptTcp(); - const subscriber = await raw(origin, ".server", "bob", name); - const acceptance = (await next).accept(); - // Drive the accept's return subscription before publishing the first half. - const clock = time(); - await clock.advance(Duration.ZERO); - const producer = answer.accept(); - const stream = await acceptance; - const group = producer.createGroup(initial); - group.writeFrame(bytes(1)); - group.finish(); - if (initial === 0n) { - expect((await stream.consumer.readFrame())?.payload).toEqual(bytes(1).payload); - expect(await stream.consumer.readFrame()).toBeUndefined(); - producer.createGroup(1n).finish(); - } - expect(await stream.producer.closed).toBeInstanceOf(ProtocolViolation); - subscriber.close(); - } -}); - -for (const dropped of [false, true]) { - test(`TCP fails when its only group ${dropped ? "is dropped" : "never arrives"}`, async () => { - const { origin, b } = setup(); - const route = origin.route(Path.from("room/.client/raw")); - const name = `tcp/raw/bob/missing-${dropped}`; - const answer = hooks.reserveUnlistedTrack(route.broadcast, name); - const next = b.acceptTcp(); - const subscriber = await raw(origin, ".server", "bob", name); - const acceptance = (await next).accept(); - const clock = time(); - await clock.advance(Duration.ZERO); - const producer = answer.accept(); - const stream = await acceptance; - const reading = stream.consumer.readFrame(); - if (dropped) hooks.dropTrackGroups(producer, 0n, 1n, { error: new NotFound("TCP group unavailable") }); - producer.finish(); - await expect(reading).rejects.toBeInstanceOf(NotFound); - expect(await stream.producer.closed).toBeInstanceOf(NotFound); - subscriber.close(); - }); -} - -test("an empty TCP group still delivers a clean FIN", async () => { - const { local, remote } = await tcp(); - local.producer.finish(); - remote.producer.finish(); - expect(await remote.consumer.readFrame()).toBeUndefined(); - expect(await local.consumer.readFrame()).toBeUndefined(); -}); - -test("unpaced TCP eviction is an error, never a skipped frame or clean FIN", async () => { - const clock = time(); - const { local, remote } = await tcp(); - // The local group's bounded tail is 32 MiB. Every payload is a distinct frame identity. - const payload = new Uint8Array(1024 * 1024); - for (let i = 0; i < 32; i++) { - local.producer.writeFrame({ payload }); - await clock.advance(Duration.ZERO); - } - local.producer.finish(); - await expect(remote.consumer.readFrame()).rejects.toMatchObject({ code: StreamCode.Evicted }); -}); - 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.dialTcp(id)).rejects.toBeInstanceOf(Error); + await expect(a.dialTrack(id)).rejects.toBeInstanceOf(Error); } a.close(); a.close(); - await expect(a.acceptTcp()).rejects.toBeInstanceOf(Error); + await expect(a.acceptTrack()).rejects.toBeInstanceOf(Error); await expect(a.dialTrack("bob")).rejects.toBeInstanceOf(Error); }); -for (const protocol of ["tcp", "track"] as const) { +test(`a socket preserves an early answer burst over ${Lite.ALPN_07_WIP}`, async () => { const alpn = Lite.ALPN_07_WIP; - test(`socket ${protocol} preserves an early answer burst 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 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)); + 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 route = bo.route(Path.from("room/.server/bob"), { hidden: true }); - const requested = wireOf(route.broadcast).requested(); - route.announce(); + 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 returning: Track.Subscriber | undefined; - let drained: Promise | undefined; - const following = bo.follow(Path.from("room/.client/alice")); + let outcome: unknown; + const next = b.acceptTrack(); + const dialing = a.dialTrack("bob"); + const settled = dialing.then( + (stream) => { + outcome = stream; + }, + (error) => { + outcome = error; + }, + ); try { - const dialing = protocol === "tcp" ? a.dialTcp("bob") : 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: protocol === "track" ? [{ start: { absolute: 0n } }] : undefined, - }); - 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 }); - if (protocol === "tcp") { - const group = staged.producer.createGroup(0n); - for (let i = 1; i <= 3; i++) group.writeFrame(bytes(i)); - group.finish(); - } else { - for (let i = 1; i <= 3; i++) staged.producer.writeFrame(bytes(i)); - } - staged.producer.finish(); - staged.commit(); + (await next).reject(); await clock.advance(Duration.ZERO); - const local = await dialing; - const reader = local instanceof Socket.TcpStream ? local.consumer : 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(); + expect(outcome).toBeInstanceOf(Error); } finally { - returning?.close(); - following.close(); a.close(); - route.broadcast.close(); + b.close(); client.abort(); server.abort(); - await drained; + await settled; } }); } for (const alpn of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_18, Ietf.ALPN.DRAFT_20, Ietf.ALPN.DRAFT_22]) { - for (const protocol of ["tcp", "track"] as const) { - test(`socket ${protocol} 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 = protocol === "tcp" ? b.acceptTcp() : b.acceptTrack(); - const dialing = protocol === "tcp" ? a.dialTcp("bob") : 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]) { - for (const protocol of ["tcp", "track"] as const) { - test(`pending socket ${protocol} 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 = protocol === "tcp" ? b.acceptTcp() : b.acceptTrack(); - let outcome: unknown; - const dialing = protocol === "tcp" ? a.dialTcp("bob") : 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 = - remote instanceof Socket.TcpStream - ? await remote.consumer.readFrame() - : await remote.consumer.ordered().readFrame(); - expect(frame?.payload).toEqual(bytes(7).payload); - } finally { - a.close(); - b.close(); - client.abort(); - server.abort(); - await settled; - } - }); - } + 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 @@ -780,7 +639,7 @@ test("a socket endpoint behind a lite-06 client fails its dials and accepts, not 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.dialTcp("bob").then( + const waiting = a.dialTrack("bob").then( () => undefined, (error: unknown) => error, ); @@ -791,7 +650,7 @@ test("a socket endpoint behind a lite-06 client fails its dials and accepts, not acceptConnection({ url, transport: pair.server, publish: bo.consume(), consume: bo }), ]); let heard = false; - const next = b.acceptTcp().then( + const next = b.acceptTrack().then( () => { heard = true; }, @@ -800,9 +659,7 @@ test("a socket endpoint behind a lite-06 client fails its dials and accepts, not try { const unsupported = /unreachable behind a moq-lite hop before 07/; expect(((await waiting) as Error).message).toMatch(unsupported); - await expect(a.dialTcp("bob")).rejects.toThrow(unsupported); await expect(a.dialTrack("bob")).rejects.toThrow(unsupported); - await expect(a.acceptTcp()).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)); @@ -812,7 +669,7 @@ test("a socket endpoint behind a lite-06 client fails its dials and accepts, not client.abort(); await client.closed; let accepted: unknown; - const accepting = a.acceptTcp().then( + const accepting = a.acceptTrack().then( (value) => { accepted = value; }, diff --git a/js/net/src/socket.ts b/js/net/src/socket.ts index 1d059e333fa..08a2ddb42c8 100644 --- a/js/net/src/socket.ts +++ b/js/net/src/socket.ts @@ -1,5 +1,6 @@ /** - * Point-to-point sessions between server and client endpoints. + * 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 @@ -11,8 +12,7 @@ * @module */ import { type Dispose, type GetPromise, type Getter, Once, Signal } from "@moq/signals"; -import { NotFound, ProtocolViolation, StreamCode, StreamError } from "./error.ts"; -import * as Group from "./group.ts"; +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"; @@ -22,15 +22,17 @@ import { isUnrequested as abandoned } from "./util/demand.ts"; import { fromBytes } from "./util/hex.ts"; import { wireOf } from "./wire.ts"; -type Protocol = "tcp" | "track"; -type Stream = TcpStream | TrackStream; +/** 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" }; -const reliable: Track.Subscription = { +// 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. @@ -92,44 +94,14 @@ export interface Options { signal?: AbortSignal; } -/** How a TCP dial or accept reads the peer's half. */ -export interface TcpOptions extends Options { - /** Schedules the peer's half against the origin's other subscriptions, as a track subscription's priority: -128 to 127, 0 by default. */ - priority?: number; -} - -let makeTcp: (parts: Parts, producer: Group.Producer, consumer: Group.Consumer) => TcpStream; let makeTrack: (parts: Parts) => TrackStream; -let makeIncoming: (endpoint: Endpoint, protocol: Protocol, request: Track.Request) => Incoming; +let makeIncoming: (endpoint: Endpoint, request: Track.Request) => Incoming; let accept: ( endpoint: Endpoint, - protocol: Protocol, request: Track.Request, info: Track.InfoInit, - options?: TcpOptions, -) => Promise; - -/** A TCP session's one group each way; finish is FIN, and abort is RST. */ -export class TcpStream { - /** This side's writes, as frames in group zero. */ - readonly producer: Group.Producer; - /** The peer's writes in order; a reader falling behind the unpaced JS tail fails with eviction. */ - readonly consumer: Group.Consumer; - /** The peer endpoint's id. */ - readonly peer: string; - /** The fresh, secret session id chosen by the dialer. */ - readonly session: string; - - private constructor(parts: Parts, producer: Group.Producer, consumer: Group.Consumer) { - this.producer = producer; - this.consumer = consumer; - this.peer = parts.peer; - this.session = parts.session; - } - static { - makeTcp = (parts, producer, consumer) => new TcpStream(parts, producer, consumer); - } -} + options?: Options, +) => Promise; /** A track session whose ascending reader skips unavailable groups, with no default publisher max age. */ export class TrackStream { @@ -161,40 +133,32 @@ interface Parts { } /** An unverified incoming request to accept (checking its original dial is still open) or reject. */ -export class Incoming { +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; - #protocol: Protocol; #request?: Track.Request; - private constructor(endpoint: Endpoint, protocol: Protocol, 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.#protocol = protocol; this.#request = request; } static { - makeIncoming = (endpoint, protocol, request) => new Incoming(endpoint, protocol, request); + makeIncoming = (endpoint, request) => new Incoming(endpoint, request); } - /** Accept while the original dial stays open; track accepts may describe their outgoing half. */ - accept( - ...args: S extends TcpStream ? [options?: TcpOptions] : [info?: Track.InfoInit, options?: Options] - ): Promise { + /** 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()); - const [info, options] = - this.#protocol === "tcp" - ? [{}, args[0] as TcpOptions | undefined] - : [(args[0] as Track.InfoInit) ?? { maxAge: null }, args[1]]; - return accept(this.#endpoint, this.#protocol, request, info, options) as Promise; + return accept(this.#endpoint, request, info, options); } /** Refuse this dial as not found; idempotent. */ @@ -205,7 +169,6 @@ export class Incoming { } class Queue { - listening = false; dials = new Signal(new Map()); close(): void { for (const [request, dispose] of this.dials.peek()) { @@ -217,8 +180,7 @@ class Queue { } /** - * A server, a client, or both: dials servers under the same prefix, and accepts each protocol - * separately. + * 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 @@ -243,7 +205,7 @@ export class Endpoint { #abort = new AbortController(); #origin: Origin.Producer; #routes: Partial> = {}; - #queues = { tcp: new Queue(), track: new Queue() }; + #queue = new Queue(); #sessions = new Set(); #unclose: Dispose[] = []; #withheld: Getter; @@ -271,34 +233,24 @@ export class Endpoint { } static { - accept = (endpoint, protocol, request, info, options) => endpoint.#accept(protocol, request, info, options); + accept = (endpoint, request, info, options) => endpoint.#accept(request, info, options); } - /** Dial the server peer in a TCP session; waits for its announcement and acceptance until cancelled. */ - dialTcp(peer: string, options?: TcpOptions): Promise { - return this.#dial("tcp", peer, {}, options) as Promise; - } - /** Dial the server peer in a track session; omitted info keeps every message with no publisher max age. */ + /** + * 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("track", peer, info, options) as Promise; - } - /** Take the next TCP dial; the first call starts queueing TCP dials until close, and the first accept announces the server. */ - acceptTcp(options?: Options): Promise> { - return this.#next("tcp", options); - } - /** Take the next track dial; its queue is independent of TCP's. */ - acceptTrack(options?: Options): Promise> { - return this.#next("track", options); + return this.#dial(peer, info, options); } - - async #next(protocol: Protocol, options?: Options): Promise> { + /** 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.#queues[protocol]; - queue.listening = true; + const queue = this.#queue; if (!this.#routes.server) void this.#serve(this.#route("server")); for (;;) { await waitFor(queue.dials, (dials) => dials.size > 0, signal); @@ -308,7 +260,7 @@ export class Endpoint { const [request, dispose] = first; dispose(); queue.dials.mutate((dials) => dials.delete(request)); - if (!abandoned(request)) return makeIncoming(this, protocol, request); + if (!abandoned(request)) return makeIncoming(this, request); request.reject(cancelled()); } } finally { @@ -336,12 +288,12 @@ export class Endpoint { request.reject(new NotFound("socket session")); continue; } - if ((name.protocol !== "tcp" && name.protocol !== "track") || !this.#queues[name.protocol].listening) { + 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 queue = this.#queues[name.protocol]; const demand = request.demand(); const prune = () => { if (!abandoned(request)) return; @@ -370,12 +322,10 @@ export class Endpoint { // Subscribe to the session's half in the peer's broadcast, its server or client one by role. async #remote( - protocol: Protocol, role: Role, peer: string, name: string, signal: AbortSignal, - priority: number, hold: (following: Origin.Following) => void, ): Promise<{ consumer: Track.Subscriber; peer: GetPromise }> { const following = this.#origin.follow(endpointPath(this.prefix, role, peer)); @@ -388,11 +338,7 @@ export class Endpoint { const broadcast = await waitFor(following.active, (active) => active !== undefined, pending); if (!broadcast) throw ended(); return { - consumer: broadcast.track(name).subscribe({ - ...reliable, - priority, - ranges: protocol === "track" ? [{ start: { absolute: 0n } }] : undefined, - }), + consumer: broadcast.track(name).subscribe(reliable()), peer: broadcast.closed, }; } finally { @@ -400,14 +346,14 @@ export class Endpoint { } } - async #dial(protocol: Protocol, peer: string, info: Track.InfoInit, options?: TcpOptions): Promise { + 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 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; @@ -415,17 +361,9 @@ export class Endpoint { let stop: Dispose = () => {}; const unreached = this.#reachable(abort); try { - const remote = await this.#remote( - protocol, - "server", - peer, - name, - signal, - options?.priority ?? 0, - (value) => { - following = value; - }, - ); + 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(() => { @@ -434,7 +372,7 @@ export class Endpoint { await untilAborted(Promise.all([serving, consumer.info()]), signal); signal.throwIfAborted(); if (!producer || !following) throw cancelled(); - return this.#open(protocol, { producer, consumer, peer, session }, following, remote.peer); + return this.#open({ producer, consumer, peer, session }, following, remote.peer); } catch (error) { answer.reject(error instanceof Error ? error : cancelled()); consumer?.close(); @@ -447,12 +385,7 @@ export class Endpoint { } } - async #accept( - protocol: Protocol, - request: Track.Request, - info: Track.InfoInit, - options?: TcpOptions, - ): Promise { + async #accept(request: Track.Request, info: Track.InfoInit, options?: Options): Promise { const name = parse(request.name); if (!name) throw cancelled(); const abort = new AbortController(); @@ -470,17 +403,9 @@ export class Endpoint { try { check(); signal.throwIfAborted(); - const remote = await this.#remote( - protocol, - "client", - name.dialer, - request.name, - signal, - options?.priority ?? 0, - (value) => { - following = value; - }, - ); + 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); @@ -488,7 +413,6 @@ export class Endpoint { if (!following) throw cancelled(); hooks.keepRequest(request); return this.#open( - protocol, { producer: request.accept(info), consumer, peer: name.dialer, session: name.session }, following, remote.peer, @@ -506,13 +430,13 @@ export class Endpoint { } } - #open(protocol: Protocol, parts: Parts, following: Origin.Following, peer: GetPromise): Stream { + #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 protocol === "tcp" ? session.tcp() : session.track(); + return session.track(); } catch (error) { session.reset(error instanceof Error ? error : cancelled()); throw error; @@ -529,13 +453,13 @@ export class Endpoint { this.#closed.set(error); for (const unclose of this.#unclose) unclose(); this.#abort.abort(error ?? ended()); - for (const queue of Object.values(this.#queues)) queue.close(); + 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, monitors both halves, and ends the TCP copy on protocol violations. +// Owns the routing hold and monitors both halves, releasing the session once both end. class Session { #parts: Parts; #following: Origin.Following; @@ -543,8 +467,6 @@ class Session { #done: Dispose; #abort = new AbortController(); #stops: Dispose[] = []; - #local?: Group.Producer; - #outgoing?: Group.Producer; #sent = false; #received = false; constructor(parts: Parts, following: Origin.Following, peer: GetPromise, done: Dispose) { @@ -593,67 +515,12 @@ class Session { return makeTrack(this.#parts); } - tcp(): TcpStream { - const { producer, consumer } = this.#parts; - const outgoing = producer.createGroup(0n); - const local = new Group.Producer(0n); - const incoming = local.consume(); - this.#local = local; - this.#outgoing = outgoing; - this.#monitor(); - this.#watch(outgoing.closed, () => { - producer.finish(); - this.#sent = true; - }); - this.#watch(producer.closed, () => {}); - this.#watch(consumer.closed, () => {}); - const check = () => { - if (!local.demand().wanted.peek() && !this.#received) this.reset(cancelled()); - }; - this.#hold(local.demand().wanted.subscribe(check)); - void this.#copy().catch((error) => this.reset(error instanceof Error ? error : cancelled())); - void this.#groups().catch((error) => this.reset(error instanceof Error ? error : cancelled())); - return makeTcp(this.#parts, outgoing, incoming); - } - - async #copy(): Promise { - const ordered = this.#parts.consumer.ordered(); - const group = await ordered.nextGroup(); - // Even an empty TCP half publishes group 0. The ordered cursor skips lost - // groups, so reaching track end without it is loss, not a clean FIN. - if (!group) throw new NotFound("TCP session group"); - try { - if (group.sequence !== 0n) throw new ProtocolViolation("second group on a TCP session"); - for (;;) { - const frame = await group.readFrame(); - if (!frame) break; - this.#local?.writeFrame({ payload: frame.payload }); - } - } finally { - group.close(); - } - if (this.#abort.signal.aborted) return; - this.#received = true; - this.#local?.finish(); - this.#complete(); - } - - async #groups(): Promise { - for (;;) { - const largest = await this.#parts.consumer.largest(); - if (!largest) return; - if (largest.group > 0n) throw new ProtocolViolation("second group on a TCP session"); - } - } - #complete(): void { if (this.#sent && this.#received) this.#close(false); } reset(error: Error): void { if (this.#abort.signal.aborted) return; - this.#outgoing?.abort(error); - this.#local?.abort(error); this.#parts.producer.abort(error); this.#parts.consumer.close(error); this.#close(true); @@ -667,7 +534,7 @@ class Session { // 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.#local) this.#parts.consumer.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 index 449b94b71fa..674bfad869c 100644 --- a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/socket/Socket.kt +++ b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/socket/Socket.kt @@ -6,14 +6,8 @@ package dev.moq.socket */ typealias Endpoint = uniffi.moq.MoqSocketEndpoint -/** An incoming TCP dial, consumed by accepting or rejecting it. */ -typealias IncomingTcp = uniffi.moq.MoqSocketIncomingTcp - /** An incoming track dial, consumed by accepting or rejecting it. */ typealias IncomingTrack = uniffi.moq.MoqSocketIncomingTrack -/** A TCP session with group producer and consumer halves. */ -typealias TcpStream = uniffi.moq.MoqSocketTcpStream - /** A track session with producer and reliably subscribed consumer halves. */ typealias TrackStream = uniffi.moq.MoqSocketTrackStream diff --git a/nix/Cargo.nix b/nix/Cargo.nix index 9332b63e005..df653d7b983 100644 --- a/nix/Cargo.nix +++ b/nix/Cargo.nix @@ -18277,10 +18277,6 @@ rec { packageId = "pollster"; optional = true; } - { - name = "rand"; - packageId = "rand 0.10.3"; - } { name = "reqwest"; packageId = "reqwest"; diff --git a/py/moq-rs/README.md b/py/moq-rs/README.md index 47ec20e9979..a1c87923159 100644 --- a/py/moq-rs/README.md +++ b/py/moq-rs/README.md @@ -259,16 +259,9 @@ 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_tcp(peer)` and `await endpoint.accept_tcp()` use the TCP queue. -- `await endpoint.dial_track(peer, info=None)` and `await endpoint.accept_track()` use the track queue. -- An incoming dial has `peer` and `session`; `await incoming.accept()` verifies - return routability, and `incoming.reject()` refuses it. Track accepts also take `info=`. -- `TcpStream` exposes `producer: GroupProducer`, `consumer: GroupConsumer`, `peer`, - and `session`. Write frames, finish for FIN, and abort for RST. Await - `producer.ready(len(payload))` before each write so a slow reader slows the - writer, as TCP backpressure does. A writer that doesn't wait sheds the front - of its half, and a reader that falls behind fails with eviction rather than - skipping bytes. +- `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 diff --git a/py/moq-rs/benches/ffi.py b/py/moq-rs/benches/ffi.py index 3ddac852d18..9c96b2ef0d9 100644 --- a/py/moq-rs/benches/ffi.py +++ b/py/moq-rs/benches/ffi.py @@ -110,63 +110,57 @@ def call(name: str, fn: Callable[[], object], results: list[dict]) -> None: async def sockets(results: list[dict]) -> None: """Sweep listening endpoints and simultaneous sessions per endpoint separately.""" - for protocol in ("tcp", "track"): - 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.TcpStream | moq.socket.TrackStream) -> None: - if isinstance(stream, moq.socket.TcpStream): - assert [frame.payload async for frame in stream.consumer] == [b"echo"] - else: - 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_tcp() if protocol == "tcp" else 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_tcp(server.id) if protocol == "tcp" else client.dial_track(server.id)) - stream.producer.write_frame(b"echo") - 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/{protocol}/endpoints={endpoints}/sessions={sessions}", - "unit": "sessions/s", - "value": rounds * endpoints * sessions / elapsed, - } + 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") + 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, ) - client.close() - for server in servers: - server.close() + + 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: diff --git a/py/moq-rs/docs/index.md b/py/moq-rs/docs/index.md index 16bd0b3ee6b..b39489453c6 100644 --- a/py/moq-rs/docs/index.md +++ b/py/moq-rs/docs/index.md @@ -281,8 +281,6 @@ publishing and consuming. See the [socket convention](https://doc.moq.dev/concep :nosignatures: Endpoint - IncomingTcp IncomingTrack - TcpStream TrackStream ``` diff --git a/py/moq-rs/moq/socket.py b/py/moq-rs/moq/socket.py index 21cc91ea79c..484902c6cc7 100644 --- a/py/moq-rs/moq/socket.py +++ b/py/moq-rs/moq/socket.py @@ -1,26 +1,24 @@ -"""Point-to-point TCP and track sessions between server and client endpoints.""" +"""Point-to-point track sessions between server and client endpoints.""" from __future__ import annotations from moq_ffi import ( MoqSocketEndpoint, - MoqSocketIncomingTcp, MoqSocketIncomingTrack, - MoqSocketTcpStream, MoqSocketTrackStream, ) from ._records import _track_info from .origin import OriginProducer -from .publish import GroupProducer, TrackProducer -from .subscribe import GroupConsumer, TrackConsumer +from .publish import TrackProducer +from .subscribe import TrackConsumer from .types import TrackInfo -__all__ = ["Endpoint", "IncomingTcp", "IncomingTrack", "TcpStream", "TrackStream"] +__all__ = ["Endpoint", "IncomingTrack", "TrackStream"] class Endpoint: - """A server, a client, or both: dials or accepts independent sessions. + """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. @@ -51,14 +49,6 @@ def close(self) -> None: """Retract the endpoint's broadcasts and reset its pending dials and sessions.""" self._inner.close() - async def dial_tcp(self, peer: str) -> TcpStream: - """Dial the server peer in a new TCP session, waiting until it accepts.""" - return TcpStream(await self._inner.dial_tcp(peer)) - - async def accept_tcp(self) -> IncomingTcp: - """Start listening for TCP and return the next incoming TCP dial.""" - return IncomingTcp(await self._inner.accept_tcp()) - 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))) @@ -68,61 +58,6 @@ async def accept_track(self) -> IncomingTrack: return IncomingTrack(await self._inner.accept_track()) -class IncomingTcp: - """An incoming tcp dial, consumed by accepting or rejecting it.""" - - def __init__(self, inner: MoqSocketIncomingTcp) -> 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) -> TcpStream: - """Accept after verifying that the peer really dialed this session.""" - return TcpStream(await self._inner.accept()) - - def reject(self) -> None: - """Refuse this dial as not found.""" - self._inner.reject() - - -class TcpStream: - """A tcp session's two halves and its peer and session identities.""" - - def __init__(self, inner: MoqSocketTcpStream) -> None: - self._producer = GroupProducer(inner.producer()) - self._consumer = GroupConsumer(inner.consumer()) - self._peer = inner.peer() - self._session = inner.session() - - @property - def producer(self) -> GroupProducer: - """This side's write half.""" - return self._producer - - @property - def consumer(self) -> GroupConsumer: - """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 - - class IncomingTrack: """An incoming track dial, consumed by accepting or rejecting it.""" diff --git a/py/moq-rs/tests/test_socket.py b/py/moq-rs/tests/test_socket.py index ffecc9624f4..e7d065941d0 100644 --- a/py/moq-rs/tests/test_socket.py +++ b/py/moq-rs/tests/test_socket.py @@ -33,50 +33,28 @@ def endpoints() -> tuple[socket.Endpoint, socket.Endpoint]: return socket.Endpoint(origin, "room", "a"), socket.Endpoint(origin, "room", "b") -async def tcp(a: socket.Endpoint, b: socket.Endpoint) -> tuple[socket.TcpStream, socket.TcpStream]: - incoming = await start(b.accept_tcp()) - dial = await start(a.dial_tcp(b.id)) +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_tcp_halves_fin_and_peer_identity(): - a, b = endpoints() - client, server = await tcp(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, reader, consumer = client.producer, client.consumer, server.consumer - # Releasing the stream cannot release either half kept by the application. - del client - gc.collect() - producer.write_frame(b"one") - producer.write_frame(b"") - producer.finish() - del producer - gc.collect() - assert [frame.payload async for frame in consumer] == [b"one", b""] - server.producer.finish() - assert await reader.read_frame() is None - a.close() - b.close() - - async def test_refusal_and_cancelled_wait_leave_endpoint_usable(): a, b = endpoints() - pending = await start(b.accept_tcp()) + pending = await start(b.accept_track()) pending.cancel() with pytest.raises(asyncio.CancelledError): await pending - dial = await start(a.dial_tcp("b")) - incoming = await b.accept_tcp() + 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 tcp(a, b) + client, server = await connect(a, b) assert client.session == server.session a.close() b.close() @@ -84,29 +62,29 @@ async def test_refusal_and_cancelled_wait_leave_endpoint_usable(): async def test_cancelled_dial_releases_the_original_subscription(): a, b = endpoints() - incoming = await start(b.accept_tcp()) - dial = await start(a.dial_tcp("b")) + 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 tcp(a, b) + client, server = await connect(a, b) assert client.session == server.session a.close() b.close() -async def test_tcp_rst_and_endpoint_close_are_errors(): +async def test_abort_and_endpoint_close_are_errors(): a, b = endpoints() - client, server = await tcp(a, b) + 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 tcp(a, b) + client, server = await connect(a, b) a.close() with pytest.raises(moq.Error.Closed): await server.consumer.read_frame() @@ -115,11 +93,10 @@ async def test_tcp_rst_and_endpoint_close_are_errors(): async def test_track_default_retains_ascending_messages_and_subscription(): a, b = endpoints() - incoming = await start(b.accept_track()) - dial = await start(a.dial_track("b")) - server = await (await incoming).accept() - client = await dial - assert (client.peer, server.peer, client.session) == ("b", "a", server.session) + 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() @@ -139,23 +116,17 @@ async def test_track_default_retains_ascending_messages_and_subscription(): b.close() -async def test_accept_queues_are_separate_and_unaccepted_protocol_is_unsupported(): - a, b = endpoints() - tcp_wait = await start(b.accept_tcp()) +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 a.dial_track("b") - track_wait = await start(b.accept_track()) - tcp_dial = await start(a.dial_tcp("b")) - track_dial = await start(a.dial_track("b")) - tcp_offer, track_offer = await tcp_wait, await track_wait - assert tcp_offer.session != track_offer.session - tcp_server = await tcp_offer.accept() - track_server = await track_offer.accept() - tcp_client, track_client = await tcp_dial, await track_dial - assert tcp_client.session == tcp_server.session - assert track_client.session == track_server.session - a.close() - b.close() + 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(): @@ -165,10 +136,10 @@ def test_a_dial_waits_for_a_late_peer_without_a_deadline(): async def late_peer(clock): origin = moq.OriginProducer() async with socket.Endpoint(origin, "room", "a") as a: - dial = await start(a.dial_tcp("b")) + 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_tcp() + incoming = await b.accept_track() accepted = await incoming.accept() connected = await dial assert connected.session == accepted.session @@ -184,14 +155,14 @@ async def test_cancelling_acceptance_drops_the_original_dial_while_proving_routa forged = origin.route("room/.client/forged", hidden=True) returning = forged.broadcast.requests() forged.announce() - incoming = await start(endpoint.accept_tcp()) + incoming = await start(endpoint.accept_track()) broadcast = await origin.consume().request("room/.server/server") - original = await start(broadcast.subscribe_track("tcp/forged/server/0123456789abcdef0123456789abcdef")) + 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("tcp/forged/server/") + assert returned.name.startswith("track/forged/server/") accepting.cancel() with pytest.raises(asyncio.CancelledError): await accepting @@ -203,51 +174,18 @@ async def test_cancelling_acceptance_drops_the_original_dial_while_proving_routa async def test_dropping_the_endpoint_resets_a_live_session(): a, b = endpoints() - client, server = await tcp(a, b) + client, server = await connect(a, b) del a gc.collect() - # Withdrawal and the session's cancellation can reach either reader first; - # the Rust contract guarantees a reset, with no clean EOF or promised cause. + # 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() - b.close() - - -async def test_tcp_ready_paces_a_writer_behind_a_slow_reader(): - a, b = endpoints() - client, server = await tcp(a, b) - mib, frames = 1024 * 1024, 48 - written = read = 0 - ahead = asyncio.Event() - - async def write() -> None: - nonlocal written - for _ in range(frames): - await client.producer.ready(mib) - # Paced, the writer stays a window ahead of the reader, well short of the 32 MiB - # cache. Unpaced, it would shed frames the reader has not taken and evict it. - assert written - read < 32 - client.producer.write_frame(bytes(mib)) - written += 1 - if written == 2: - ahead.set() - client.producer.finish() - - writer = await start(write()) - async for frame in server.consumer: - assert len(frame.payload) == mib - read += 1 - if read == 1: - # Stall one frame in until the writer has run ahead of the reader. - await ahead.wait() - assert read == frames - await writer - server.producer.finish() - a.close() - b.close() if __name__ == "__main__": diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index f91ea550921..700507d4e11 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -245,10 +245,11 @@ subscriber's `yieldNow()`, and is re-enabled by [moq-dev/moq#8357](https://github.com/moq-dev/moq/pull/8357). Added 2026-11-09 (maintainer's standing rule that flakes join this epic): -moq-rs's Python socket test `test_tcp_rst_and_endpoint_close_are_errors`, whose +moq-rs's Python socket test `test_abort_and_endpoint_close_are_errors` (then +`test_tcp_rst_and_endpoint_close_are_errors`, on a `tcp` session), whose reader got `Unroutable` after the endpoint closed during [moq-dev/moq#8293](https://github.com/moq-dev/moq/pull/8293)'s loaded `just check`. -It is quarantined with `pytest.mark.skip` until its child fixes it. +No `pytest.mark.skip` was ever applied, so it still runs until its child fixes it. Added 2026-11-09 (maintainer's standing rule that flakes join this epic): the Go wrapper's `TestRecvGroupCancelKeepsConsumer`, whose `RecvGroup` after a diff --git a/quest/m1/README.md b/quest/m1/README.md index bfc1d6bbb7b..beb15b5c95c 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -36,7 +36,9 @@ Decided 2026-11-12 (maintainer, /quest-audit before the cut): staying first in m3). The 2026-11-03 reason to keep them out of m1 was that the cut would wait on them; since 2026-11-05 the cut waits on m2 alone, and a downstream application builds on them from `main` now. Superseded - 2026-11-13, below. + 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. - ✅ 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 module proxy, since the mirror repository nears 1 GB; and the ashpd bump. diff --git a/quest/m1/stream/README.md b/quest/m1/stream/README.md deleted file mode 100644 index 82ee813ba3d..00000000000 --- a/quest/m1/stream/README.md +++ /dev/null @@ -1,58 +0,0 @@ -# Byte streams over MoQ - -## Goal - -A TCP connection rides MoQ as a lossless, ordered, duplex byte stream through -any relay chain, proxied at the TCP level and never as IP packets. Each hop -runs its own QUIC congestion control and retransmits, so a lossy last mile -recovers locally instead of end to end, and relays schedule streams against -media by the existing priority rules. Rust and JS expose the same surface. - -Non-goals: IP or datagram tunneling, which would throw away the CDN's per-hop -QoS; replaying a stream to a late joiner; fanout byte streams beyond what -fastest-reader pacing gives. - -## Plan - -Decided 2026-10-25 with the maintainer (mesh planning interview): - -- TCP-level proxying over groups, never IP over datagrams. -- A forwarded service is one [socket](/doc/concept/socket.md) endpoint, - and each TCP connection is one session on it (amended 2026-11-03, below), - one group each way; each TCP `read()` becomes one frame, because a frame's length must - be known before it is written and a read has one. It coalesces whatever is - buffered with no added timer (the local TCP socket's own Nagle or - `TCP_NODELAY` still applies). -- The receiver flushes a frame's bytes to its TCP socket as they arrive, never - waiting for the whole frame, unless the mesh's authentication level - (`moq_crypto::sign`) must verify it first; a frame that fails verification ends its - group with a typed error, surfaced as a reset. - Group finish is a FIN (half-close); a group abort with a code is a reset. - The implementer measures and records a frame-size cap. -- Lossless delivery comes from - [fastest-reader pacing](https://github.com/moq-dev/moq/pull/7664), not - from refill: a stream registers no fill handler, because most TCP traffic is - not worth retaining for replay. A reader that falls behind the window ends - `Evicted`, surfaced as a reset. -- Connection setup is generic: a listener accepts connections a dialer - announces under a path prefix, and the API names peers by path. Endpoint - identity, signing, and service names belong to the - [mesh](/quest/m3/mesh/README.md), which builds on this. - -Amended 2026-11-01 with the maintainer (socket planning interview), -2026-11-03 when sockets became endpoints, and 2026-11-04 when the socket's -`tcp` protocol became one group each way: connection setup is the -[socket](/doc/concept/socket.md) `Endpoint`, `dial_tcp`, and -`accept_tcp`, which settle the layout (`/.server/` and -`/.client/`, a `tcp///` track each -way). Each TCP connection is one `tcp` -session, whose halves are a single group each way for the connection's life: -frames are writes, finishing is FIN, resetting is RST, and the writer awaits -`group::Producer::ready` for backpressure. Each session gets its own priority, -pacing, and eviction, and a long-lived connection never sits in an old group -that newer ones crowd out of a track's window. - -This epic owns the end-to-end proof: an unmodified TCP client and server (SSH -and an HTTP bulk transfer) talk through a two-relay chain with loss injected on -one hop, with no corruption, FIN and reset propagated, and memory bounded by -the window. diff --git a/quest/m1/stream/relay-eviction.md b/quest/m1/stream/relay-eviction.md deleted file mode 100644 index fe0d2c3850b..00000000000 --- a/quest/m1/stream/relay-eviction.md +++ /dev/null @@ -1,34 +0,0 @@ -# [M] Lossless relay hops for byte streams - -## Goal - -A `tcp` socket session carries a bulk transfer through a relay without loss, -however fast the writer: every hop paces its ingest to its fastest reader, so -no reader of the session's one group ever ends `Evicted`. - -## Plan - -Found while validating `moq tcp` (the Rust byte streams quest): a 300 MB HTTP -download through `moq tcp dial`, a local `moq-relay`, and `moq tcp accept`, -all on one host, ends with a reset partway in about half the runs. It happens -over both WebTransport and native QUIC (`moql://`), more often with 8 KiB -frames than 256 KiB ones. - -The relay's ingest of the accepting side's half aborts its group with -`evicted` (its subscriber logs `group error err=evicted group=0`) while the -accepting side's publisher only sees the relay cancel, so the eviction is the -relay's own copy shedding a frame its one reader still needed. The endpoints' -local writers wait on `group::Producer::poll_ready`, so they cannot be the -cause. Suspects, in order: ingest skipping its pace when -`transport::boxed::RecvStream::pause` refuses -(`coding::Reader::poll_pace` reads on when it cannot pause), the paused-credit -budget being half the connection window (4 MiB by default) against a 1 MiB -stream window, and a second, faster reader of the relay's copy that pacing -waits on instead. - -Reproduce with a relay config accepting `moq-lite-07-wip`, then -`moq --connect tcp accept web --local ` and -`moq --connect tcp dial web --local 127.0.0.1:28081`, and `curl` a -few hundred MB through `127.0.0.1:28081` in a loop. Add a sim test of the -same shape (a fast local writer, a relay, a reader paced by its consumer) that -fails first, then fix it at the root. diff --git a/quest/m3/README.md b/quest/m3/README.md index 8b91777dfea..bf2918d2af2 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -25,7 +25,7 @@ Dependencies on m1 and m2 remain explicit. An implementation that discovers a new necessary API break brings that decision to the maintainer before proceeding. -Decided 2026-11-03 (maintainer, Kixel's quest audit): [byte streams](/quest/m1/stream/README.md) +Decided 2026-11-03 (maintainer, Kixel's quest audit): byte streams and the [mesh libraries](/quest/m3/mesh/README.md) rank first here, ahead of [release execution](/quest/m3/release/README.md). Kixel's daemon builds on them from `main` through a git submodule, so it needs them merged, not @@ -33,7 +33,7 @@ released. They stay out of m1 and m2 so the cut does not wait on them; each starts as soon as its prerequisites (sockets, fastest-reader pacing, outbound federation) land. Superseded 2026-11-12 (maintainer, /quest-audit): both moved 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 stay in m1. +libraries came back 2026-11-13, below; byte streams moved to Kixel 2026-11-14. Decided 2026-11-12 (maintainer, /quest-audit before the cut), from a read of every quest against the code and the downstream quest trees: diff --git a/quest/m3/mesh/README.md b/quest/m3/mesh/README.md index f1e4ac3e004..059d092a82c 100644 --- a/quest/m3/mesh/README.md +++ b/quest/m3/mesh/README.md @@ -41,7 +41,7 @@ moq library, media included, can point at one. Decided 2026-10-25 with the maintainer (planning interview), still in force: -- Traffic is app-level [byte streams](/quest/m1/stream/README.md) and named +- Traffic is app-level byte streams and named tracks, never IP packets. - The coordinator contract is generic and lives here: enrollment issues a relay URL and a token, and self-hosters run it with `moq auth`. A hosted @@ -108,7 +108,7 @@ the third, and survives a relay restart. Decided 2026-11-12 (maintainer, /quest-audit before the cut): -- ✅ (Superseded 2026-11-13: this epic is back in m3, above.) This epic and [byte streams](/quest/m1/stream/README.md) moved to m1 +- ✅ (Superseded 2026-11-13: this epic is back in m3, above.) This epic and byte streams moved to m1 from the head of m3 (rejected: staying in m3). The cut no longer waits on m1, and a downstream application builds on them from `main` now. - ✅ The epic no longer lists federation and the stream epic under diff --git a/quest/m3/mesh/endpoint.md b/quest/m3/mesh/endpoint.md index 3d87f18e4c8..d22b6922f29 100644 --- a/quest/m3/mesh/endpoint.md +++ b/quest/m3/mesh/endpoint.md @@ -25,7 +25,7 @@ Decided 2026-10-25 (maintainer): iroh-shaped, not drop-in compatible; tickets, `connect` dials with the caller's own endpoint ID, and `accept` reads the remote ID from the dial, which the socket's answer check proves and the grant pins (publish `.server/` and `.client/` only). -- Streams are byte streams (`moq_tokio::socket::TcpStream`) at the protection +- Streams are byte streams at the protection level the service declares: identity reads `moq-net` directly, and authentication, the mesh default, reads through `moq_crypto::sign` against the `PublicKey` parsed from the peer's endpoint id, which names the endpoint diff --git a/quest/m4/stream-js.md b/quest/m4/stream-js.md deleted file mode 100644 index 299885c3f6f..00000000000 --- a/quest/m4/stream-js.md +++ /dev/null @@ -1,26 +0,0 @@ -# [S] JS byte streams - -## Goal - -`@moq/net` dials and accepts the same byte streams as Rust, exposed as a -WHATWG `ReadableStream`/`WritableStream` pair, and interoperates with the Rust -side in both directions. - -## Plan - -Mirror the Rust surface, `moq_tokio::socket::TcpStream`, on `@moq/net`'s socket. `dialTcp` and `accept` already take the -`priority` option Rust's `dial_tcp` and `accept` do. Add -the Rust/JS pair to `just test interop`. - -`@moq/net`'s socket `TcpStream` already exists and the socket interop test -(`test/interop/socket.sh`) runs, so this adds only the WHATWG adapter. The -JS tail is unpaced today (`js/net/src/socket.ts`: a reader falling behind it -fails with eviction), so a slow JS reader is not lossless until JS paces by -its fastest reader. - -Moved to m4 2026-11-12 (maintainer, /quest-audit): no downstream application -waits on JS byte streams, and they are lossless only after JS pacing lands. - -## Required - -- [JS fastest-reader pacing](/quest/m4/js-fastest-reader.md) - a JS reader that falls behind would otherwise end evicted, not lossless diff --git a/quest/m5/ffi-stream.md b/quest/m5/ffi-stream.md deleted file mode 100644 index 2d6e3814605..00000000000 --- a/quest/m5/ffi-stream.md +++ /dev/null @@ -1,32 +0,0 @@ -# [M] Byte streams in moq-ffi - -## Goal - -The Swift and Kotlin bindings dial and accept the same byte streams as Rust -and JS: a duplex stream to a peer by path with ordered reads and writes, -half-close, and reset with a code, interoperating with `@moq/net` in both -directions. Non-goal: a TCP forwarder on the device; the mobile consumer reads -and writes the stream in-process. - -## Plan - -Decided 2026-10-28 with the maintainer (a downstream planning interview): -planned for a downstream application's React Native app, to reach its host -through a native module over these bindings instead of the WebSocket -fallback, so a lossy mobile link keeps QUIC's per-stream recovery. - -No named consumer remains (checked 2026-11-12, /quest-audit): that -application's mobile module now wraps a WebTransport binding rather than -moq-ffi, and carries its traffic in groups rather than byte streams. It stays -parked here until a binding user asks for byte streams. - -Wrap the Rust surface, `moq_tokio::socket::TcpStream`, in `moq-ffi` with the same -path layout and priority knob (`dial_tcp` and `accept` take a `priority`, which -`moq-ffi` and `moq-c` pass as 0 until then), and surface it through the generated Swift and -Kotlin packages. Use an async read and write shape that matches how the -bindings already expose frames; report the choice. Add a Kotlin or Swift -binding test against a JS peer to the interop suite. - -## Required - -- [JS byte streams](/quest/m4/stream-js.md) - the peer the interop test runs against diff --git a/quest/m9/stream-bench.md b/quest/m9/stream-bench.md deleted file mode 100644 index 858df07b442..00000000000 --- a/quest/m9/stream-bench.md +++ /dev/null @@ -1,18 +0,0 @@ -# [S] Stream benchmark - -## Goal - -A reproducible benchmark reports throughput and latency of a byte stream -through a two-relay chain against raw TCP over the same emulated path, with -and without loss on one hop, so the per-hop QoS claim is measured rather than -asserted. - -## Plan - -Reuse the existing bench harness and network shaper; record the numbers in the -quest's PR and on the bench site if it exists by then. A regression is not a -gate here; the result informs the docs and the mesh guide. - -## Required - -- [Lossless relay hops](/quest/m1/stream/relay-eviction.md) - a bulk transfer through a relay otherwise ends evicted diff --git a/rs/moq-boy/src/input.rs b/rs/moq-boy/src/input.rs index c18b71f4aae..b925d443010 100644 --- a/rs/moq-boy/src/input.rs +++ b/rs/moq-boy/src/input.rs @@ -103,11 +103,7 @@ pub async fn handle_viewers( } /// Accept one viewer's dial and relay its commands until the session ends. -async fn run_viewer( - viewer: ViewerId, - incoming: moq_net::socket::Incoming, - cmd_tx: tokio::sync::mpsc::Sender, -) { +async fn run_viewer(viewer: ViewerId, incoming: moq_net::socket::Incoming, cmd_tx: tokio::sync::mpsc::Sender) { let peer = incoming.peer().to_string(); let stream = match incoming.accept(None).await { Ok(stream) => stream, diff --git a/rs/moq-c/README.md b/rs/moq-c/README.md index cb59199348b..fb814007a7b 100644 --- a/rs/moq-c/README.md +++ b/rs/moq-c/README.md @@ -195,16 +195,14 @@ 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_close(uint32_t endpoint); -int32_t moq_socket_dial_tcp(uint32_t endpoint, const char *peer, uintptr_t peer_len, moq_status_callback on_stream, void *user_data); 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_tcp(uint32_t endpoint, moq_status_callback on_incoming, 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 group (tcp) or raw track (track) handle +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); diff --git a/rs/moq-c/src/consume.rs b/rs/moq-c/src/consume.rs index 10cebaac5dc..98fec57641d 100644 --- a/rs/moq-c/src/consume.rs +++ b/rs/moq-c/src/consume.rs @@ -214,8 +214,6 @@ enum RawControl { Pending(Option), /// Subscribed: updates apply at once, so a refusal reaches the caller. Live(moq_net::track::Control), - /// A socket's `tcp` half: one group, with no subscription to update. - Group, } /// Outcome of polling a raw track source alongside its control channels. @@ -936,34 +934,22 @@ impl Consume { } } - /// Read a socket stream's peer half as a raw track reader: `track` for a `track` session, - /// or the one group of a `tcp` session. + /// Read a socket stream's peer half as a raw track reader. /// - /// The `track` half keeps the reliable, oldest-first subscription the socket opened it with - /// until [`Self::raw_track_update`] changes it. + /// 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: crate::socket::Half, + half: moq_net::track::Subscriber, driver: crate::ffi::Background, on_frame: OnStatus, ) -> Result { - let (task, mut close) = Task::new(on_frame); - let control = match &half { - crate::socket::Half::Track(track) => RawControl::Live(track.control()), - crate::socket::Half::Group(_) => RawControl::Group, - }; + 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(async move { - match half { - crate::socket::Half::Track(track) => Self::run_raw(on_frame, (*track).ordered(), close).await, - crate::socket::Half::Group(mut group) => { - Self::run_group(on_frame, &mut group, &mut close).await.map(drop) - } - } - })) - .await; + 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 { @@ -984,9 +970,6 @@ impl Consume { match &mut entry.control { RawControl::Pending(pending) => *pending = Some(subscription), RawControl::Live(control) => Self::update_raw(control, subscription)?, - RawControl::Group => { - return Err(Error::Unsupported("a tcp socket stream has no subscription".into())); - } } Ok(()) } diff --git a/rs/moq-c/src/publish.rs b/rs/moq-c/src/publish.rs index 1d74bba0feb..9e8d294f3c1 100644 --- a/rs/moq-c/src/publish.rs +++ b/rs/moq-c/src/publish.rs @@ -1198,11 +1198,6 @@ impl Publish { children.insert(&mut self.tracks, raw, Child::Track) } - /// Hand a socket stream's `tcp` half to C as a group handle. - pub(crate) fn socket_group(&mut self, group: moq_net::group::Producer) -> Result { - self.insert_group(group) - } - /// 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) { diff --git a/rs/moq-c/src/socket.rs b/rs/moq-c/src/socket.rs index ce01c654f50..159b070f405 100644 --- a/rs/moq-c/src/socket.rs +++ b/rs/moq-c/src/socket.rs @@ -1,13 +1,12 @@ //! Socket endpoints: two-way sessions between endpoints sharing an origin. //! -//! A `tcp` session's halves are each one group: write with [crate::moq_publish_group_frame] and -//! read frames in order. A `track` session's halves are each a whole track: write with -//! [crate::moq_publish_track_group] and read it like [crate::moq_consume_track]. +//! 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, TcpStream, TrackStream}; +use moq_net::socket::{Endpoint, Incoming, TrackStream}; use tokio::sync::oneshot; use crate::ffi::{self, Background, OnStatus}; @@ -28,64 +27,37 @@ pub struct Socket { struct EndpointEntry { inner: Arc, driver: Background, - /// The endpoint's `track` halves handed to C, ended when it closes. + /// The endpoint's halves handed to C, ended when it closes. children: Children, } -enum IncomingKind { - Tcp(Incoming), - Track(Incoming), -} - struct IncomingEntry { - inner: IncomingKind, + inner: Incoming, peer: String, session: String, driver: Background, children: Children, } -/// The peer's half of a stream, read through [Consume](crate::Consume). -pub(crate) enum Half { - Track(Box), - Group(moq_net::group::Consumer), -} - -/// This side's half of a stream, until C takes it. -enum Writer { - Track(moq_net::track::Producer), - Group(moq_net::group::Producer), -} - struct StreamEntry { peer: String, session: String, - writer: Option, - reader: Option, + /// 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 tcp(stream: TcpStream, driver: Background, children: Children) -> Self { - let (peer, session) = (stream.peer().to_string(), stream.session().to_string()); - Self { - peer, - session, - writer: Some(Writer::Group(stream.producer)), - reader: Some(Half::Group(stream.consumer)), - driver, - children, - } - } - - fn track(stream: TrackStream, driver: Background, children: Children) -> Self { + 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(Writer::Track(stream.producer)), - reader: Some(Half::Track(Box::new(stream.consumer))), + writer: Some(stream.producer), + reader: Some(stream.consumer), driver, children, } @@ -135,7 +107,7 @@ impl Socket { &mut self, endpoint: Id, peer: &str, - track: Option>, + info: Option, callback: OnStatus, ) -> Result { let EndpointEntry { @@ -145,16 +117,8 @@ impl Socket { } = self.endpoint(endpoint)?; let (driver, children) = (driver.clone(), children.clone()); let watch = driver.clone(); - let pending: std::pin::Pin> + Send>> = match track { - None => { - let dialing = inner.dial_tcp(peer, 0)?; - Box::pin(async move { Ok(StreamEntry::tcp(dialing.await?, driver, children)) }) - } - Some(info) => { - let dialing = inner.dial_track(peer, info)?; - Box::pin(async move { Ok(StreamEntry::track(dialing.await?, driver, children)) }) - } - }; + 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, @@ -162,17 +126,14 @@ impl Socket { ) } - fn accept(&mut self, endpoint: Id, track: bool, callback: OnStatus) -> Result { + 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| match track { - false => inner.poll_accept_tcp(waiter).map_ok(IncomingKind::Tcp), - true => inner.poll_accept_track(waiter).map_ok(IncomingKind::Track), - }; + 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()) { @@ -205,13 +166,9 @@ impl Socket { Err(moq_net::Error::Closed) => return Ok(()), Err(err) => return Err(err.into()), }; - let (peer, session) = match &incoming { - IncomingKind::Tcp(incoming) => (incoming.peer(), incoming.session()), - IncomingKind::Track(incoming) => (incoming.peer(), incoming.session()), - }; let entry = IncomingEntry { - peer: peer.to_string(), - session: session.to_string(), + peer: incoming.peer().to_string(), + session: incoming.session().to_string(), inner: incoming, driver: driver.clone(), children: children.clone(), @@ -231,11 +188,7 @@ impl Socket { info: Option, callback: OnStatus, ) -> Result { - let entry = self.incoming.get(incoming).ok_or(Error::NotFound)?; - entry.driver.check()?; - if info.is_some() && matches!(entry.inner, IncomingKind::Tcp(_)) { - return Err(Error::InvalidConfig("a tcp session has no track info".into())); - } + self.incoming.get(incoming).ok_or(Error::NotFound)?.driver.check()?; let IncomingEntry { inner, driver, @@ -243,16 +196,8 @@ impl Socket { .. } = self.incoming.remove(incoming).ok_or(Error::NotFound)?; let watch = driver.clone(); - let pending: std::pin::Pin> + Send>> = match inner { - IncomingKind::Tcp(incoming) => { - let accepting = incoming.accept(0); - Box::pin(async move { Ok(StreamEntry::tcp(accepting.await?, driver, children)) }) - } - IncomingKind::Track(incoming) => { - let accepting = incoming.accept(info); - Box::pin(async move { Ok(StreamEntry::track(accepting.await?, driver, children)) }) - } - }; + 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, @@ -303,8 +248,8 @@ fn write_ids(peer: &str, session: &str, peer_dst: Option<&mut moq_string>, sessi /// 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 when the origin may not -/// publish `/.socket/`. +/// 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. @@ -329,7 +274,7 @@ pub unsafe extern "C" fn moq_socket_create( /// 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 `track` half still open. Handles +/// 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. @@ -345,8 +290,10 @@ pub extern "C" fn moq_socket_close(endpoint: u32) -> i32 { }) } -/// Dial the endpoint `peer` in a new `tcp` session: one ordered, reliable byte-message stream -/// each way. +/// 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 @@ -358,31 +305,6 @@ pub extern "C" fn moq_socket_close(endpoint: u32) -> i32 { /// Returns a positive task handle, or a negative code. /// /// # Safety -/// - `peer` must point at `peer_len` bytes. -/// - `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_tcp( - endpoint: u32, - peer: *const c_char, - peer_len: usize, - 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 callback = unsafe { OnStatus::new(user_data, on_stream)? }; - State::lock().socket.dial(endpoint, peer, None, callback) - }) -} - -/// Dial the endpoint `peer` in a new `track` session, whose halves are whole tracks; see -/// [moq_socket_dial_tcp]. -/// -/// `info` describes this side's half, or NULL to keep every message until read, with no -/// max age. -/// -/// # 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)] @@ -399,40 +321,23 @@ pub unsafe extern "C" fn moq_socket_dial_track( 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, Some(info), callback) + State::lock().socket.dial(endpoint, peer, info, callback) }) } -/// Accept `tcp` dials from peers. +/// 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. Run one loop per protocol. +/// 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_tcp( - 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, false, callback) - }) -} - -/// Accept `track` dials from peers; see [moq_socket_accept_tcp]. -/// -/// # 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, @@ -441,7 +346,7 @@ pub unsafe extern "C" fn moq_socket_accept_track( ffi::enter(move || { let endpoint = ffi::parse_id(endpoint)?; let callback = unsafe { OnStatus::new(user_data, on_incoming)? }; - State::lock().socket.accept(endpoint, true, callback) + State::lock().socket.accept(endpoint, callback) }) } @@ -479,10 +384,9 @@ pub unsafe extern "C" fn moq_socket_incoming_info( /// Accept an incoming dial, releasing its handle. /// -/// Verifies the dialer really dialed this session first. `info` describes this side's half of a -/// `track` session, or NULL to keep every message until read; a `tcp` session takes NULL. -/// `on_stream` fires like [moq_socket_dial_tcp]'s, and [moq_socket_cancel] gives up and resets -/// the session. +/// 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. /// @@ -510,10 +414,7 @@ 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)?; - match entry.inner { - IncomingKind::Tcp(incoming) => incoming.reject(), - IncomingKind::Track(incoming) => incoming.reject(), - } + entry.inner.reject(); Ok(()) }) } @@ -533,12 +434,8 @@ pub unsafe extern "C" fn moq_socket_stream_info(stream: u32, peer: *mut moq_stri }) } -/// Take this side's half of a stream, which outlives the stream handle. -/// -/// A `tcp` session's half is a group handle: write with [crate::moq_publish_group_frame], pacing -/// with [crate::moq_publish_group_ready], and end it with [crate::moq_publish_group_finish] (or -/// abort it). Dropping it unfinished resets the session. A `track` session's half is a raw track -/// handle, written and ended with the `moq_publish_track_*` calls. +/// 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)] @@ -548,10 +445,8 @@ pub extern "C" fn moq_socket_stream_writer(stream: u32) -> i32 { let mut state = State::lock(); let entry = state.socket.streams.get_mut(stream).ok_or(Error::NotFound)?; let children = entry.children.clone(); - match entry.writer.take().ok_or(Error::Moq(moq_net::Error::Closed))? { - Writer::Group(group) => state.publish.socket_group(group), - Writer::Track(track) => state.publish.socket_track(track, &children), - } + let track = entry.writer.take().ok_or(Error::Moq(moq_net::Error::Closed))?; + state.publish.socket_track(track, &children) }) } @@ -561,9 +456,8 @@ pub extern "C" fn moq_socket_stream_writer(stream: u32) -> i32 { /// 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. A `track` session's half is subscribed reliably and oldest first; -/// [crate::moq_consume_track_update] changes that, while a `tcp` session's refuses it with -/// `MOQ_ERROR_UNSUPPORTED`. +/// 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. /// @@ -588,8 +482,8 @@ pub unsafe extern "C" fn moq_socket_stream_reader( /// Release a stream handle. /// -/// A half not yet taken is dropped: an unfinished `tcp` writer resets the session, and an -/// unread reader stops reading. Taken halves are unaffected. +/// 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 || { diff --git a/rs/moq-c/src/test/parity.rs b/rs/moq-c/src/test/parity.rs index 3117b74420a..3bd617cc994 100644 --- a/rs/moq-c/src/test/parity.rs +++ b/rs/moq-c/src/test/parity.rs @@ -33,31 +33,27 @@ fn next_frame(cb: &Callback) -> Vec { payload } +/// 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, track: bool) -> (u32, u32, [Callback; 3]) { +fn connect(a: u32, b: u32) -> (u32, u32, [Callback; 3]) { let accepts = Callback::new(); - let _accept = id(unsafe { - match track { - false => moq_socket_accept_tcp(b, Some(channel_callback), accepts.ptr), - true => moq_socket_accept_track(b, Some(channel_callback), accepts.ptr), - } - }); + let _accept = id(unsafe { moq_socket_accept_track(b, Some(channel_callback), accepts.ptr) }); let dialed = Callback::new(); - let peer = "b"; - let _dial = id(unsafe { - match track { - false => moq_socket_dial_tcp(a, peer.as_ptr().cast(), peer.len(), Some(channel_callback), dialed.ptr), - true => moq_socket_dial_track( - a, - peer.as_ptr().cast(), - peer.len(), - std::ptr::null(), - Some(channel_callback), - dialed.ptr, - ), - } - }); + let _dial = dial(a, "b", &dialed); let incoming = id(accepts.recv()); let (mut claimed, mut session) = (moq_str(""), moq_str("")); @@ -82,10 +78,10 @@ fn connect(a: u32, b: u32, track: bool) -> (u32, u32, [Callback; 3]) { } #[test] -fn socket_tcp_round_trip() { +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, false); + 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); @@ -109,32 +105,26 @@ fn socket_tcp_round_trip() { 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) }); + 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" ); - assert_eq!( - unsafe { moq_consume_track_update(reader, std::ptr::null()) }, - MOQ_ERROR_UNSUPPORTED, - "a tcp half has no subscription" - ); - // The halves outlive the stream handles. Take the reverse writer first: dropping it - // unfinished would reset the session. - let reverse = id(moq_socket_stream_writer(server)); + // The halves outlive the stream handles. assert_eq!(moq_socket_stream_free(server), 0); for part in [b"one".as_slice(), b"two"] { + let group = id(moq_publish_track_group(writer)); assert_eq!( - unsafe { moq_publish_group_frame(writer, part.as_ptr(), part.len(), std::ptr::null()) }, + unsafe { moq_publish_group_frame(group, part.as_ptr(), part.len(), std::ptr::null()) }, 0 ); + assert_eq!(moq_publish_group_finish(group), 0); } - assert_eq!(moq_publish_group_finish(writer), 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_publish_group_finish(reverse), 0); assert_eq!(moq_socket_stream_free(client), 0); assert_eq!(moq_socket_close(b), 0); @@ -148,7 +138,7 @@ fn socket_tcp_round_trip() { 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, true); + let (client, server, [accepts, ..]) = connect(a, b); let track = id(moq_socket_stream_writer(client)); let frames = Callback::new(); @@ -185,19 +175,9 @@ fn socket_finished_tracks_are_not_kept_by_the_endpoint() { 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 peer = "b"; for round in 0..3u8 { let dialed = Callback::new(); - let _dial = id(unsafe { - moq_socket_dial_track( - a, - peer.as_ptr().cast(), - peer.len(), - std::ptr::null(), - Some(channel_callback), - dialed.ptr, - ) - }); + let _dial = id(dial(a, "b", &dialed)); let incoming = id(accepts.recv()); let accepted = Callback::new(); let _accepting = @@ -239,12 +219,10 @@ 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_tcp(b, Some(channel_callback), accepts.ptr) }); + let accept = id(unsafe { moq_socket_accept_track(b, Some(channel_callback), accepts.ptr) }); let dialed = Callback::new(); - let peer = "b"; - let _dial = - id(unsafe { moq_socket_dial_tcp(a, peer.as_ptr().cast(), peer.len(), Some(channel_callback), dialed.ptr) }); + let _dial = id(dial(a, "b", &dialed)); let incoming = id(accepts.recv()); assert_eq!(moq_socket_incoming_reject(incoming), 0); assert!( @@ -257,10 +235,11 @@ fn socket_reject_and_cancel() { let me = "a"; assert_eq!( unsafe { - moq_socket_dial_tcp( + moq_socket_dial_track( a, me.as_ptr().cast(), me.len(), + std::ptr::null(), Some(ignore_callback), std::ptr::null_mut(), ) @@ -270,17 +249,8 @@ fn socket_reject_and_cancel() { // A pending dial gives up when cancelled. let waiting = Callback::new(); - let absent = "nobody"; - let dial = id(unsafe { - moq_socket_dial_tcp( - a, - absent.as_ptr().cast(), - absent.len(), - Some(channel_callback), - waiting.ptr, - ) - }); - assert_eq!(moq_socket_cancel(dial), 0); + 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); diff --git a/rs/moq-cli/Cargo.toml b/rs/moq-cli/Cargo.toml index 91bcfb1a219..faaace8a745 100644 --- a/rs/moq-cli/Cargo.toml +++ b/rs/moq-cli/Cargo.toml @@ -140,8 +140,6 @@ moq-webrtc = { workspace = true } object_store = { workspace = true, features = ["fs"] } percent-encoding = { workspace = true } pollster = { workspace = true, optional = true } -# A `tcp dial` endpoint's id, which must stay unguessable. -rand = { workspace = true } reqwest = { workspace = true, features = ["rustls-no-provider", "json"] } rind-object-store = { workspace = true, optional = true } rustls = { version = "0.23", default-features = false, features = ["std"] } diff --git a/rs/moq-cli/src/args.rs b/rs/moq-cli/src/args.rs index ac0dcdf6bd6..0b86611b18d 100644 --- a/rs/moq-cli/src/args.rs +++ b/rs/moq-cli/src/args.rs @@ -434,8 +434,8 @@ impl Invocation { } // Only `import` and `export` share an Origin. The rest own the process: `play` - // drives a window on the main thread, `transcode` builds its own Origin, `fetch`, - // `announced`, and `tcp` open their own session, and `auth` / `repair` / `devices` never touch the network at all. + // drives a window on the main thread, `transcode` builds its own Origin, `fetch` + // and `announced` open their own session, and `auth` / `repair` / `devices` never touch the network at all. if let Some(command) = self.stages.iter().find(|command| !command.is_stageable()) { anyhow::bail!( "`{}` must be the only verb; it can't share a process with another `--` stage", @@ -670,8 +670,6 @@ pub enum Command { Announced(crate::announced::Args), /// Write one group of a track to stdout. Fetch(crate::fetch::Args), - /// Forward TCP connections through a relay, from a local listener to a socket server. - Tcp(crate::tcp::Args), /// Finish a recording that a crash or second signal left unfinished. Repair(crate::repair::Args), /// Play a broadcast in a native window and speaker. @@ -733,7 +731,6 @@ impl Command { Self::Export(_) | Self::Subscribe(_) => "export", Self::Announced(_) => "announced", Self::Fetch(_) => "fetch", - Self::Tcp(_) => "tcp", Self::Repair(_) => "repair", #[cfg(feature = "play")] Self::Play(_) => "play", diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 191ad854204..f2a782fbacf 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -23,7 +23,6 @@ mod rtmp; mod rtsp; mod srt; mod subscribe; -mod tcp; #[cfg(feature = "transcode")] mod transcode; mod web; @@ -369,14 +368,12 @@ async fn run() -> anyhow::Result<()> { } } - // `fetch`, `announced`, and `tcp` only dial, so an ambient listener or cluster setting they + // `fetch` and `announced` only dial, so an ambient listener or cluster setting they // never use is not validated either. if let [Command::Fetch(_)] = stages.as_slice() { cli.dial_only("fetch", &["--broadcast"])?; } else if let [Command::Announced(_)] = stages.as_slice() { cli.dial_only("announced", &[])?; - } else if let [Command::Tcp(_)] = stages.as_slice() { - cli.dial_only("tcp", &[])?; } else { cli.unserved()?; cli.moq.validate()?; @@ -398,7 +395,6 @@ async fn run() -> anyhow::Result<()> { match stages.remove(0) { Command::Fetch(args) => return fetch::run(cli.moq, args, net).await, Command::Announced(args) => return announced::run(cli.moq, args, net).await, - Command::Tcp(args) => return tcp::run(cli.moq, args, net).await, #[cfg(feature = "play")] Command::Play(args) => return run_play(cli.moq, args, net).await, #[cfg(feature = "transcode")] diff --git a/rs/moq-cli/src/tcp.rs b/rs/moq-cli/src/tcp.rs deleted file mode 100644 index 1ea847e96b2..00000000000 --- a/rs/moq-cli/src/tcp.rs +++ /dev/null @@ -1,210 +0,0 @@ -//! `moq tcp`: forward TCP connections through a relay as socket sessions, so an -//! unmodified client reaches an unmodified server. -//! -//! `tcp accept ` is a socket server: each session dialed to it connects to a local -//! TCP address. `tcp dial ` listens on a local TCP address and dials `peer` for -//! each connection it accepts. See `doc/concept/socket.md`. - -use std::net::SocketAddr; - -use crate::args::MoqSide; -use anyhow::Context; -use moq_net::socket::Endpoint; -use moq_tokio::socket::TcpStream; - -/// Forward TCP connections through a relay. -#[derive(usage_rs::Args, Clone)] -#[usage(unknown_flags = "error", args_override_self = false)] -pub struct Args { - /// Which end of the forward this process is. - #[usage(subcommand)] - pub side: Side, -} - -/// Which end of a forward this process is. -#[derive(usage_rs::Subcommands, Clone)] -pub enum Side { - /// Listen on `--local` and dial the socket server `` for each connection. - Dial(Dial), - /// Be the socket server ``, connecting each session dialed to it to `--local`. - Accept(Accept), -} - -/// `tcp dial`: the client end. -#[derive(usage_rs::Args, Clone)] -#[usage(unknown_flags = "error", args_override_self = false)] -pub struct Dial { - /// The socket server to dial, as `tcp accept` names it. - pub peer: String, - - /// The local TCP address to accept connections on, e.g. `127.0.0.1:2222`. - #[usage(long)] - pub local: SocketAddr, - - #[usage(flatten)] - pub socket: Socket, -} - -/// `tcp accept`: the server end. -#[derive(usage_rs::Args, Clone)] -#[usage(unknown_flags = "error", args_override_self = false)] -pub struct Accept { - /// This socket server's id, which `tcp dial` names. One path segment. - pub id: String, - - /// The local TCP address each session connects to, e.g. `127.0.0.1:22`. - #[usage(long)] - pub local: SocketAddr, - - #[usage(flatten)] - pub socket: Socket, -} - -/// What both ends agree on, and how each reads. -#[derive(usage_rs::Args, Clone)] -pub struct Socket { - /// The path under the connection where the socket's endpoints meet; the connection - /// path itself by default. - #[usage(long)] - pub prefix: Option, - - /// How the bytes this end receives are scheduled against the relay's other traffic, - /// as a subscription's priority: -128 to 127, higher first. - #[usage(long, default = "0")] - pub priority: i8, -} - -/// The versions offered when `--connect-version` names none: every one whose sessions -/// carry a socket's hidden broadcasts. -const VERSIONS: [&str; 4] = [ - "moq-lite-07-wip", - "moq-transport-22", - "moq-transport-20", - "moq-transport-18", -]; - -/// Forward TCP connections until the session to the relay ends. -pub async fn run(moq: MoqSide, args: Args, net: crate::Net) -> anyhow::Result<()> { - let url = moq - .client - .url - .clone() - .context("`tcp` dials a relay: pass --connect ")?; - let mut config = moq.client.clone(); - if config.version.is_empty() { - config.version = VERSIONS - .iter() - .map(|version| version.parse().map_err(anyhow::Error::msg)) - .collect::>()?; - } - - let origin = moq_tokio::origin::spawn(); - let client = net - .client(config)? - .with_publisher(origin.consume()) - .with_subscriber(origin.clone()); - let connection = client.connect(url).established().await.context("failed to connect")?; - - let serve = async { - match args.side { - Side::Dial(dial) => run_dial(&origin, dial).await, - Side::Accept(accept) => run_accept(&origin, accept).await, - } - }; - // The connection redials on its own; once it gives up, nothing can reach the peer. - let result = tokio::select! { - result = serve => result, - closed = connection.closed() => match closed { - Ok(()) => Err(anyhow::anyhow!("the connection to the relay closed")), - Err(err) => Err(err).context("the connection to the relay failed"), - }, - }; - let _ = connection.close().await; - client.close().await; - result -} - -async fn run_dial(origin: &moq_net::origin::Producer, args: Dial) -> anyhow::Result<()> { - let id = format!("{:032x}", rand::random::()); - let endpoint = - Endpoint::new(origin, args.socket.prefix.as_deref().unwrap_or_default(), &id).context("invalid --prefix")?; - let listener = tokio::net::TcpListener::bind(args.local) - .await - .with_context(|| format!("failed to listen on {}", args.local))?; - tracing::info!(local = %args.local, peer = %args.peer, "forwarding"); - - let health = moq_tokio::accept::Health::new("tcp dial"); - loop { - let (local, addr) = match listener.accept().await { - Ok(accepted) => { - health.accepted(); - accepted - } - // One connection's failure or a passing fd shortage, not the forward's. - Err(err) => { - if let Some(delay) = health.failed(&err) { - kio::clock::sleep(delay).await; - } - continue; - } - }; - let dial = endpoint - .dial_tcp(&args.peer, args.socket.priority) - .context("failed to dial")?; - tokio::spawn(async move { - let stream = match dial.await { - Ok(stream) => TcpStream::from(stream), - Err(err) => return tracing::warn!(%addr, %err, "dial failed"), - }; - forward(local, stream, addr).await; - }); - } -} - -async fn run_accept(origin: &moq_net::origin::Producer, args: Accept) -> anyhow::Result<()> { - let endpoint = Endpoint::new(origin, args.socket.prefix.as_deref().unwrap_or_default(), &args.id) - .context("invalid --prefix or id")?; - tracing::info!(id = %args.id, local = %args.local, "accepting"); - - loop { - let incoming = endpoint.accept_tcp().await.context("stopped accepting")?; - let (target, priority) = (args.local, args.socket.priority); - tokio::spawn(async move { - // Connected before the dial is accepted, so a refused connection refuses the dial. - let local = match tokio::net::TcpStream::connect(target).await { - Ok(local) => local, - Err(err) => { - tracing::warn!(%target, %err, "failed to connect"); - return incoming.reject(); - } - }; - let stream = match incoming.accept(priority).await { - Ok(stream) => TcpStream::from(stream), - Err(err) => return tracing::warn!(%err, "accept failed"), - }; - forward(local, stream, target).await; - }); - } -} - -/// Copy both ways until both halves end, passing each FIN on as a shutdown. A failure -/// either way resets both: dropping the session resets it, and the local socket closes -/// with a reset. -async fn forward(mut local: tokio::net::TcpStream, mut stream: TcpStream, addr: SocketAddr) { - let _ = local.set_nodelay(true); - match tokio::io::copy_bidirectional_with_sizes( - &mut local, - &mut stream, - moq_tokio::socket::MAX_FRAME, - moq_tokio::socket::MAX_FRAME, - ) - .await - { - Ok((up, down)) => tracing::debug!(%addr, up, down, "closed"), - Err(err) => { - tracing::debug!(%addr, %err, "reset"); - // Closed with a reset rather than a FIN, as the peer's failure was. - let _ = local.set_zero_linger(); - } - } -} diff --git a/rs/moq-ffi/src/producer.rs b/rs/moq-ffi/src/producer.rs index b3333be0182..5f8a0658cfc 100644 --- a/rs/moq-ffi/src/producer.rs +++ b/rs/moq-ffi/src/producer.rs @@ -969,7 +969,7 @@ impl Drop for MoqGroupProducer { } impl MoqGroupProducer { - /// Wrap `group`, for a track's writer or a socket's write half. + /// Wrap `group`, for a track's writer. pub(crate) fn from_inner(group: moq_net::group::Producer) -> Self { Self { sequence: group.sequence, diff --git a/rs/moq-ffi/src/socket.rs b/rs/moq-ffi/src/socket.rs index d02a12f5e8f..74450d0122e 100644 --- a/rs/moq-ffi/src/socket.rs +++ b/rs/moq-ffi/src/socket.rs @@ -1,16 +1,16 @@ -//! Point-to-point socket endpoints and their two protocol-specific streams. +//! Point-to-point socket endpoints and their track sessions. use std::sync::Arc; use std::task::{Poll, ready}; -use crate::consumer::{MoqGroupConsumer, MoqTrackConsumer}; +use crate::consumer::MoqTrackConsumer; use crate::error::MoqError; use crate::ffi::{Background, OneShot, Polled}; use crate::origin::MoqOriginProducer; -use crate::producer::{MoqGroupProducer, MoqTrackInfo, MoqTrackProducer}; +use crate::producer::{MoqTrackInfo, MoqTrackProducer}; /// A socket endpoint: its first dial announces it as a client, and its first accept as a -/// server, which takes TCP and track dials separately. +/// server. #[derive(uniffi::Object)] pub struct MoqSocketEndpoint { inner: moq_net::socket::Endpoint, @@ -50,31 +50,6 @@ impl MoqSocketEndpoint { self.inner.close(); } - /// Dial the server `peer` in a new `tcp` session; cancelling this call resets only this dial. - pub async fn dial_tcp(&self, peer: String) -> Result, MoqError> { - self.driver - .watch(async { - let stream = self.inner.dial_tcp(&peer, 0)?.await?; - Ok(Arc::new(MoqSocketTcpStream::new(stream, self.driver.clone()))) - }) - .await - } - - /// Start accepting `tcp` dials and await the next one, independently of the other protocol. - pub async fn accept_tcp(&self) -> Result, MoqError> { - self.driver - .watch(async { - let inner = self.inner.accept_tcp().await?; - Ok(Arc::new(MoqSocketIncomingTcp { - peer: inner.peer().into(), - session: inner.session().into(), - inner: OneShot::new(inner), - driver: self.driver.clone(), - })) - }) - .await - } - /// Dial the server `peer` in a new `track` session; cancelling this call resets only this dial. pub async fn dial_track( &self, @@ -92,7 +67,7 @@ impl MoqSocketEndpoint { .await } - /// Start accepting `track` dials and await the next one, independently of the other protocol. + /// Start accepting `track` dials and await the next one. pub async fn accept_track(&self) -> Result, MoqError> { self.driver .watch(async { @@ -114,92 +89,10 @@ impl Drop for MoqSocketEndpoint { } } -/// An incoming `tcp` dial, consumed by accepting or rejecting it. -#[derive(uniffi::Object)] -pub struct MoqSocketIncomingTcp { - inner: OneShot>, - peer: String, - session: String, - driver: Background, -} - -#[uniffi::export] -impl MoqSocketIncomingTcp { - /// 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) -> Result, MoqError> { - self.driver.check()?; - let (incoming, answering) = self.inner.take()?; - let accepting = Polled::new(incoming.accept(0)).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(MoqSocketTcpStream::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 `tcp` session exposing its existing producer and subscribed consumer handles. -#[derive(uniffi::Object)] -pub struct MoqSocketTcpStream { - producer: Arc, - consumer: Arc, - peer: String, - session: String, -} - -impl MoqSocketTcpStream { - fn new(inner: moq_net::socket::TcpStream, driver: Background) -> Self { - let peer = inner.peer().into(); - let session = inner.session().into(); - Self { - producer: Arc::new(MoqGroupProducer::from_inner(inner.producer)), - consumer: Arc::new(MoqGroupConsumer::new(inner.consumer, driver)), - peer, - session, - } - } -} - -#[uniffi::export] -impl MoqSocketTcpStream { - /// 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() - } -} - /// An incoming `track` dial, consumed by accepting or rejecting it. #[derive(uniffi::Object)] pub struct MoqSocketIncomingTrack { - inner: OneShot>, + inner: OneShot, peer: String, session: String, driver: Background, @@ -306,39 +199,49 @@ mod tests { } } - async fn tcp(a: &MoqSocketEndpoint, b: &MoqSocketEndpoint) -> (Arc, Arc) { - let mut incoming = Box::pin(b.accept_tcp()); + 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_tcp(b.id()), async { incoming.await.unwrap().accept().await }); + 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 tcp_halves_outlive_the_stream_and_preserve_fin() { + async fn track_subscription_survives_stream_release_and_has_no_default_age_limit() { let (a, b) = endpoints(); - let (client, server) = tcp(&a, &b).await; + 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 reader = client.consumer(); + let reverse = server.producer(); let consumer = server.consumer(); - drop(client); - producer.write_frame(frame(b"one")).unwrap(); - producer.write_frame(frame(b"two")).unwrap(); + drop(server); + assert!(consumer.info().max_age_unlimited); + // Create the later group first: the reliable ascending reader must wait for 0. + let later = producer.create_group(1).unwrap(); + later.write_frame(frame(b"later")).unwrap(); + later.finish().unwrap(); + let first = producer.create_group(0).unwrap(); + first.write_frame(frame(b"first")).unwrap(); + first.finish().unwrap(); producer.finish().unwrap(); - drop(producer); - assert_eq!(consumer.read_frame().await.unwrap().unwrap().payload, b"one"); - assert_eq!(consumer.read_frame().await.unwrap().unwrap().payload, b"two"); + 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()); - server.producer().finish().unwrap(); - assert!(reader.read_frame().await.unwrap().is_none()); + reverse.finish().unwrap(); + assert!(client.consumer().read_frame().await.unwrap().is_none()); } #[tokio::test] - async fn tcp_reset_is_not_fin_and_does_not_close_another_session() { + async fn an_abort_is_not_a_finish_and_does_not_close_another_session() { let (a, b) = endpoints(); - let (client, server) = tcp(&a, &b).await; - let (other, peer) = tcp(&a, &b).await; + 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(); @@ -352,103 +255,48 @@ mod tests { assert!(peer.consumer().read_frame().await.unwrap().is_none()); } - #[tokio::test] - async fn endpoint_close_resets_an_open_tcp_session_as_closed() { - let (a, b) = endpoints(); - let (_client, server) = tcp(&a, &b).await; - a.close(); - let error = server.consumer().read_frame().await.err().unwrap(); - assert!(matches!(error, MoqError::Closed), "{error:?}"); - } - - #[tokio::test] - async fn dropping_an_unfinished_tcp_writer_resets_with_a_deliberate_cause() { - let (a, b) = endpoints(); - let (client, server) = tcp(&a, &b).await; - let consumer = client.consumer(); - // Keep the read half: dropping the write half must cause the reset itself. - drop(client); - let error = server.consumer().read_frame().await.err().unwrap(); - assert!(matches!(error, MoqError::Protocol { details } if details.kind == MoqProtocolKind::Unroutable)); - drop(consumer); - } - #[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_tcp()); + let mut abandoned = Box::pin(b.accept_track()); pending(abandoned.as_mut()); drop(abandoned); - let (dialed, ()) = tokio::join!(a.dial_tcp(b.id()), async { - let incoming = b.accept_tcp().await.unwrap(); + 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 _ = tcp(&a, &b).await; + 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_tcp()); + let mut incoming = Box::pin(b.accept_track()); pending(incoming.as_mut()); - let mut dial = Box::pin(a.dial_tcp(b.id())); + 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().await.is_err()); - let _ = tcp(&a, &b).await; + assert!(incoming.accept(None).await.is_err()); + let _ = connect(&a, &b).await; } #[tokio::test] - async fn track_subscription_survives_stream_release_and_has_no_default_age_limit() { + 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()); - let (client, server) = tokio::join!(a.dial_track(b.id(), None), async { - incoming.await.unwrap().accept(None).await - }); - let (client, server) = (client.unwrap(), server.unwrap()); - assert_eq!(client.session(), server.session()); - let producer = client.producer(); - let reverse = server.producer(); - let consumer = server.consumer(); - drop(server); - assert!(consumer.info().max_age_unlimited); - // Create the later group first: the reliable ascending reader must wait for 0. - let later = producer.create_group(1).unwrap(); - later.write_frame(frame(b"later")).unwrap(); - later.finish().unwrap(); - let first = producer.create_group(0).unwrap(); - first.write_frame(frame(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 protocols_are_independent_and_endpoint_close_wakes_both_queues() { - let (a, b) = endpoints(); - let mut incoming = Box::pin(b.accept_tcp()); - pending(incoming.as_mut()); - tokio::select! { - result = a.dial_track(b.id(), None) => assert!(matches!(result, Err(MoqError::Unsupported))), - _ = &mut incoming => panic!("track dial arrived in the TCP queue"), - } - let mut tracks = Box::pin(b.accept_track()); - pending(tracks.as_mut()); + a.close(); + assert!(server.consumer().read_frame().await.is_err()); b.close(); assert!(matches!(incoming.await, Err(MoqError::Closed))); - assert!(matches!(tracks.await, Err(MoqError::Closed))); - assert!(matches!(b.dial_tcp(a.id()).await, Err(MoqError::Closed))); + assert!(matches!(b.dial_track(a.id(), None).await, Err(MoqError::Closed))); } } diff --git a/rs/moq-net/CHANGELOG.md b/rs/moq-net/CHANGELOG.md index 2ba4c996bec..bc8fcc2d8f2 100644 --- a/rs/moq-net/CHANGELOG.md +++ b/rs/moq-net/CHANGELOG.md @@ -9,7 +9,7 @@ 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 two protocols: `tcp` sessions (`dial_tcp`, `accept_tcp`, `TcpStream`), one group each way as a reliable, ordered byte pipe, and `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`. +- `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`. - `transport::SETUP_RECEIVE_WINDOW`, the connection credit a server offers before it admits a session: enough for the SETUP and one in-band AUTH. ## [0.3.10](https://github.com/moq-dev/moq/compare/moq-net-v0.3.9...moq-net-v0.3.10) - 2026-10-05 diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 008021e654c..c82f85e376b 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -2225,12 +2225,6 @@ pub struct Producer { } impl Producer { - /// A handle that queues work on this origin's [`Driver`] without keeping the origin - /// alive: work queued once the driver is gone is dropped. - pub(super) fn tasks(&self) -> crate::util::TasksWeak { - self.tasks.downgrade() - } - /// Build a producer from a [`Config`] (identity + cache pool) with no scoped /// prefix and no pre-existing broadcasts, paired with the [`Driver`] that runs /// the origin's lifecycle work. diff --git a/rs/moq-net/src/model/socket.rs b/rs/moq-net/src/model/socket.rs index dfd40e7785d..b873a228de8 100644 --- a/rs/moq-net/src/model/socket.rs +++ b/rs/moq-net/src/model/socket.rs @@ -6,31 +6,25 @@ //! 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 -//! `///`. Sessions are never announced or listed. +//! `track///`. Sessions are never announced or listed. //! -//! A session speaks one of two protocols: -//! -//! - `tcp`: one reliable, ordered byte pipe each way, as TCP gives. Each half is a single -//! group. Dial with [`Endpoint::dial_tcp`], accept with `accept_tcp` or -//! [`Endpoint::poll_accept_tcp`], and read and write a [`TcpStream`]. -//! - `track`: each half 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`]. +//! 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 `/A/B/S`. Endpoint `B` surfaces that request as an [`Incoming`] dial -//! from `A` in its protocol's queue. To accept it, `B` subscribes to `/.client/` +//! 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 -//! `/A/C/S`. Each side then writes its own half and reads the peer's. +//! `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::marker::PhantomData; use std::sync::Arc; use std::task::{Poll, ready}; @@ -39,7 +33,6 @@ use crate::{Error, Path, SessionError, StreamError}; use super::WeakEntry; use super::origin::{self, RouteOptions}; use super::track::{self, Delay, Order, Subscription}; -use super::{frame, group}; /// Refuse an id that is not one plain path segment: empty, or holding a `/` or a `\`. fn check_id(id: &str) -> Result<(), Error> { @@ -128,181 +121,29 @@ fn messages() -> track::Info { track::Info::default().with_max_age(None) } -mod sealed { - use super::*; - - /// The protocols an endpoint speaks. - #[derive(Clone, Copy, Debug, PartialEq, Eq)] - pub enum Protocol { - Tcp, - Track, - } - - /// What an endpoint's handles share. - pub struct Shared { - pub(super) id: Arc, - pub(super) prefix: Path, - /// Where this endpoint's broadcasts are announced, at first use. - pub(super) publisher: origin::Producer, - /// Where the peers' endpoints are requested. - pub(super) origin: origin::Consumer, - /// Where the `tcp` sessions' pumps run: the origin's driver. - pub(super) tasks: crate::util::TasksWeak, - /// The sessions withholding the broadcasts from their peer. - pub(super) withheld: origin::HiddenWithheld, - pub(super) state: kio::Lock, - } - - /// A session's two halves, once the handshake is done. - pub struct Parts { - pub shared: Arc, - /// This side's half, served to the peer. - pub producer: track::Producer, - /// The peer's half, subscribed reliably and in order. - pub subscriber: track::Subscriber, - pub peer: Arc, - pub session: Arc, - } - - /// What a protocol does with a session's halves. - pub trait Open: Sized { - /// The protocol's segment, the first of a session's track name. - const PROTOCOL: Protocol; - - /// Make the stream an application reads and writes. - fn open(parts: Parts) -> Self; - } -} - -use sealed::{Parts, Protocol, Shared}; - -impl Protocol { - fn as_str(self) -> &'static str { - match self { - Self::Tcp => "tcp", - Self::Track => "track", - } - } - - fn parse(protocol: &str) -> Option { - match protocol { - "tcp" => Some(Self::Tcp), - "track" => Some(Self::Track), - _ => None, - } - } +/// The protocol segment leading every session's track name. +const PROTOCOL: &str = "track"; - /// What a session's reader asks for: every message, oldest first, however late it is, - /// scheduled at `priority` against the reader's other subscriptions. - fn subscription(self, priority: i8) -> Subscription { - let reliable = Subscription::default() - .with_priority(priority) - .with_order(Order::Ascending) - .with_delay(Delay::default().with_max(None)); - match self { - // The half's only group is the live one, so a live join reads it from its first - // frame, even one written before the answer: over moq-transport, a SUBSCRIBE - // plus a joining FETCH or fill for that group's head. - Self::Tcp => reliable, - // Starts at group 0, so a burst written before the answer is kept. - Self::Track => reliable.with_range(0..), - } - } +/// 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_range(0..) } -/// A session's stream: a [`TcpStream`] or a [`TrackStream`]. Names the protocol a dial, -/// an incoming dial, or an accept is for. -pub trait Stream: sealed::Open {} - -/// A `tcp` session: one reliable, ordered byte pipe each way, as TCP gives. -/// -/// Each half is the session track's only group, which is one QUIC stream on every hop, -/// so it is reliable, ordered and flow controlled like a TCP direction. A frame is a -/// write. Finishing [`Self::producer`] is FIN, and aborting or dropping it is RST; the -/// peer reads the same from [`Self::consumer`]. Reading ends once the peer finished, and -/// fails with its reset. A second group on a peer's half is a protocol error that resets -/// the session. -/// -/// Await [`group::Producer::poll_ready`] (or `ready`) before each write: it waits until the peer reads, so -/// the writer runs at most a window ahead of the reader and nothing is lost. A writer -/// that never waits sheds the front of its half, and a reader that falls behind it fails -/// with [`Error::Evicted`]. -/// -/// Dropping [`Self::consumer`] before the peer finished resets the session, since nobody -/// reads what the peer writes. The endpoint closing resets it too. -pub struct TcpStream { - /// This side's half: the frames written here reach the peer in order. - pub producer: group::Producer, - /// The peer's half: its frames, in order. - pub consumer: group::Consumer, - peer: Arc, - session: Arc, -} - -impl TcpStream { - /// 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 - } -} - -impl Stream for TcpStream {} - -impl sealed::Open for TcpStream { - const PROTOCOL: Protocol = Protocol::Tcp; - - fn open(parts: Parts) -> Self { - let Parts { - shared, - producer: track, - subscriber, - peer, - session, - } = parts; - let (producer, ours) = match track.create_group(group::Info { sequence: 0 }) { - Ok(group) => { - let demand = group.demand(); - (group, Some(demand)) - } - // The track already ended, such as by the endpoint closing: hand out a half - // that reports it. - Err(err) => { - let group = - group::Producer::new(group::Info { sequence: 0 }, track::Info::default(), Default::default()); - let _ = group.handle().abort(err); - (group, None) - } - }; - // The application reads a copy of the peer's half that the pump fills, so the - // session can end it: the peer's own copy is the session's, not this side's. - let local = group::Producer::new(group::Info { sequence: 0 }, track::Info::default(), Default::default()); - let consumer = local.consume(); - let pump = Pump { - shared: shared.clone(), - track: Some(track), - ours, - subscriber: Some(subscriber), - received: false, - remote: None, - local_handle: local.handle(), - local: Some(Local::Between { - group: local, - next: None, - }), - }; - shared.tasks.push(pump); - Self { - producer, - consumer, - peer, - session, - } - } +/// 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. @@ -343,31 +184,6 @@ impl TrackStream { } } -impl Stream for TrackStream {} - -impl sealed::Open for TrackStream { - const PROTOCOL: Protocol = Protocol::Track; - - fn open(parts: Parts) -> Self { - Self { - producer: parts.producer, - consumer: parts.subscriber, - peer: parts.peer, - session: parts.session, - } - } -} - -/// Dials from peers in one protocol, waiting to be accepted. -#[derive(Default)] -struct Queue { - /// Whether the protocol is accepted: from its first accept on. - listening: bool, - dials: VecDeque, - /// The accepts waiting for a dial. - waiting: kio::WaiterList, -} - /// 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 @@ -393,8 +209,10 @@ struct State { server: Option, /// Where the dials peers send arrive. handler: Handler, - tcp: Queue, - track: Queue, + /// 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 closes. streams: Vec, /// Why the endpoint stopped, once it did. @@ -402,13 +220,6 @@ struct State { } impl State { - fn queue(&mut self, protocol: Protocol) -> &mut Queue { - match protocol { - Protocol::Tcp => &mut self.tcp, - Protocol::Track => &mut self.track, - } - } - /// Keep `producer`'s track, so the endpoint closing ends it. fn hold(&mut self, producer: &track::Producer) { self.streams.retain(|stream| !stream.is_closed()); @@ -468,23 +279,22 @@ impl Shared { Ok(requests) } - /// Queue every dial on hand and take the next in `protocol`, registering `waiter` for - /// more, and fail with the endpoint's end once it stopped. The first call announces the + /// Queue every dial on hand and take the next, registering `waiter` for more, and fail + /// with the endpoint's end once it stopped. 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 a protocol nobody accepts - /// is reset with [`Error::Unsupported`]. + /// 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, protocol: Protocol, waiter: &kio::Waiter) -> Poll> { + fn poll_dial(&self, waiter: &kio::Waiter) -> Poll> { let taken = { let mut state = self.state.lock(); - state.queue(protocol).listening = true; if state.end.is_some() { None } else if let Handler::Taken(waiting) = &mut state.handler { @@ -558,52 +368,42 @@ impl Shared { wakes.push(waiting.take()); } } - // Dials whose dialer gave up leave both queues on every poll, which each new dial - // triggers, so a queue holds no more than the dials still open, even in a protocol - // the application stopped accepting. - for protocol in [Protocol::Tcp, Protocol::Track] { - let queue = state.queue(protocol); - if queue.dials.iter().any(|request| !request.demand().is_wanted()) { - let (open, abandoned): (VecDeque<_>, VecDeque<_>) = std::mem::take(&mut queue.dials) - .into_iter() - .partition(|request| request.demand().is_wanted()); - queue.dials = open; - resets.extend(abandoned.into_iter().map(|request| (request, Error::Cancel))); - } + // 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 state.dials.iter().any(|request| !request.demand().is_wanted()) { + let (open, abandoned): (VecDeque<_>, VecDeque<_>) = std::mem::take(&mut state.dials) + .into_iter() + .partition(|request| request.demand().is_wanted()); + state.dials = open; + 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(Protocol::parse(name.protocol)) - } + Some(name) if name.acceptor == &*self.id && name.dialer != &*self.id => Some(name.protocol == PROTOCOL), _ => None, }; match target { // Taken while the endpoint closed: reset like the dials it queued. _ if state.end.is_some() => resets.push((request, Error::Cancel)), - Some(Some(protocol)) if state.queue(protocol).listening => { - let queue = state.queue(protocol); - queue.dials.push_back(request); - wakes.push(queue.waiting.take()); + Some(true) => { + state.dials.push_back(request); + wakes.push(state.waiting.take()); } - Some(_) => resets.push((request, Error::Unsupported)), + Some(false) => resets.push((request, Error::Unsupported)), None => resets.push((request, Error::NotFound)), } } let next = match &state.end { Some(err) => Poll::Ready(Err(err.clone())), - None => { - let queue = state.queue(protocol); - match queue.dials.pop_front() { - Some(request) => Poll::Ready(Ok(request)), - None => { - queue.waiting.register(waiter); - Poll::Pending - } + None => match state.dials.pop_front() { + Some(request) => Poll::Ready(Ok(request)), + None => { + state.waiting.register(waiter); + Poll::Pending } - } + }, }; drop(state); if let Some((handler, server)) = retired { @@ -666,11 +466,8 @@ impl Shared { } Handler::None => None, }; - let mut queued = Vec::new(); - for queue in [&mut state.tcp, &mut state.track] { - queued.extend(queue.dials.drain(..)); - wakes.push(queue.waiting.take()); - } + let queued = std::mem::take(&mut state.dials); + wakes.push(state.waiting.take()); let streams = std::mem::take(&mut state.streams); let routes = [state.client.take(), state.server.take()]; drop(guard); @@ -701,14 +498,13 @@ impl Shared { /// 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 in a protocol from that protocol's first accept on, like a TCP -/// listener: before that, a dial in it is reset with [`Error::Unsupported`], and after it, -/// dials queue until accepted. Until the endpoint's first accept in any protocol, it has no -/// server broadcast, so a dial to it waits as for a peer not online yet. +/// 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, or [`Self::close`], retracts its broadcasts, resets the dials waiting to be -/// accepted, and resets every session it handed out: a [`TcpStream`] entirely, and this -/// side's half of a [`TrackStream`]. The peer's half of a [`TrackStream`] is read through +/// 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. /// @@ -719,8 +515,6 @@ impl Shared { /// ([`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. -/// -/// `tcp` sessions run a task on the origin's driver, which must keep running. pub struct Endpoint { shared: Arc, } @@ -748,13 +542,12 @@ impl Endpoint { publisher: origin.clone(), withheld: consumer.hidden_withheld(), origin: consumer, - tasks: origin.tasks(), state: kio::Lock::new(State { client: None, server: None, handler: Handler::None, - tcp: Queue::default(), - track: Queue::default(), + dials: VecDeque::new(), + waiting: kio::WaiterList::new(), streams: Vec::new(), end: None, }), @@ -772,40 +565,20 @@ impl Endpoint { &self.shared.prefix } - /// Dial the server `peer` in a new `tcp` session, resolving once the peer accepts it. + /// 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 `tcp` sessions, 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. - /// - /// `priority` schedules the peer's half against this origin's other subscriptions on - /// every hop, as a track subscription's does; see [`Subscription::priority`]. - pub fn dial_tcp(&self, peer: &str, priority: i8) -> Result>, Error> { - self.dial(peer, None, priority) - } - - /// Dial the server `peer` in a new `track` session; see [`Self::dial_tcp`]. - /// - /// `info` describes this side's half; `None` keeps every message until read, with no - /// max age. - pub fn dial_track( - &self, - peer: &str, - info: impl Into>, - ) -> Result>, Error> { - self.dial(peer, Some(info.into().unwrap_or_else(messages)), 0) - } - - fn dial( - &self, - peer: &str, - info: Option, - priority: i8, - ) -> Result>, Error> { + /// 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 { @@ -813,7 +586,7 @@ impl Endpoint { } let client = shared.client()?; let session = session_id(); - let name = track_name(S::PROTOCOL.as_str(), &shared.id, peer, &session); + 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())?; @@ -824,48 +597,34 @@ impl Endpoint { peer: peer.into(), session, name, - info, - subscription: S::PROTOCOL.subscription(priority), + info: Some(info), demand: answer.demand(), answer: Some(Answer::Reserved(answer)), step: Step::Resolving(resolving), - stream: PhantomData, })) } - fn poll_accept(&self, waiter: &kio::Waiter) -> Poll, Error>> { + /// 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(S::PROTOCOL, 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), - stream: PhantomData, })) } - /// The next `tcp` 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 `tcp` dials. They queue until taken, or until their - /// dialer gives up, so keep accepting, and accept or reject each one. The endpoint's - /// first accept in either protocol announces its 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_tcp(&self, waiter: &kio::Waiter) -> Poll, Error>> { - self.poll_accept(waiter) - } - - /// The next `track` dial from a peer; see [`Self::poll_accept_tcp`]. - #[cfg_attr(feature = "async", kio::wrapper)] - pub fn poll_accept_track(&self, waiter: &kio::Waiter) -> Poll, Error>> { - self.poll_accept(waiter) - } - /// Close the endpoint; see [`Endpoint`]. pub fn close(&self) { self.shared.close(); @@ -878,21 +637,19 @@ impl Drop for Endpoint { } } -/// A dial from a peer, from [`Endpoint::poll_accept_tcp`] or -/// [`Endpoint::poll_accept_track`] (or their async `accept_tcp` and `accept_track`): accept -/// or reject it. +/// 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 { +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, - stream: PhantomData S>, } -impl Incoming { +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 { @@ -912,7 +669,12 @@ impl Incoming { } } - fn accept_with(mut self, info: Option, priority: i8) -> kio::Pending> { + /// 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"); @@ -927,40 +689,18 @@ impl Incoming { }; kio::Pending::new(Accepting { shared: shared.clone(), - name: track_name(S::PROTOCOL.as_str(), &peer, &shared.id, &session), + name: track_name(PROTOCOL, &peer, &shared.id, &session), peer, session, - info, - subscription: S::PROTOCOL.subscription(priority), + info: Some(info), demand: request.demand(), request: Some(request), step, - stream: PhantomData, }) } } -impl Incoming { - /// Accept the dial, resolving once the dialer answers, which proves it dialed. - /// - /// `priority` schedules the dialer's half, as for [`Endpoint::dial_tcp`]. Nothing is - /// written before the dialer answers. - pub fn accept(self, priority: i8) -> kio::Pending> { - self.accept_with(None, priority) - } -} - -impl Incoming { - /// 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(self, info: impl Into>) -> kio::Pending> { - self.accept_with(Some(info.into().unwrap_or_else(messages)), 0) - } -} - -impl Drop for Incoming { +impl Drop for Incoming { fn drop(&mut self) { if let Some(request) = self.request.take() { request.reject(Error::Cancel); @@ -984,13 +724,13 @@ enum Step { impl Step { /// Advance until the peer serves its half, subscribing to `track` in its endpoint. - fn poll(&mut self, waiter: &kio::Waiter, subscription: &Subscription, track: &str) -> Poll> { + 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.clone()).into_inner(); + let subscribing = broadcast.track(track)?.subscribe(subscription()).into_inner(); *self = Self::Subscribing(subscribing); } Self::Subscribing(subscribing) => { @@ -1014,13 +754,13 @@ impl Step { /// 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: &Arc, +fn open( + shared: &Shared, producer: track::Producer, step: &mut Step, peer: &Arc, session: &Arc, -) -> Result { +) -> Result { let mut state = shared.state.lock(); if let Some(err) = state.end.clone() { drop(state); @@ -1029,13 +769,12 @@ fn open( } state.hold(&producer); drop(state); - Ok(S::open(Parts { - shared: shared.clone(), + Ok(TrackStream { producer, - subscriber: step.take_subscriber(), + consumer: step.take_subscriber(), peer: peer.clone(), session: session.clone(), - })) + }) } /// This side's half of a dial. @@ -1058,29 +797,25 @@ impl Answer { } } -/// A dial, from [`Endpoint::dial_tcp`] or [`Endpoint::dial_track`]: resolves once the peer -/// accepts. +/// A dial, from [`Endpoint::dial_track`]: resolves once the peer accepts. /// /// Dropping it gives up, resetting the session. -pub struct Dialing { +pub struct Dialing { shared: Arc, peer: Arc, session: Arc, /// The session's track name. name: String, info: Option, - /// How this side reads the peer's half. - subscription: Subscription, /// Whether the peer asked for this side's half, which it does on accept. demand: track::Demand, answer: Option, step: Step, - stream: PhantomData S>, } -impl Dialing { - /// Poll for the peer to accept; see [`Endpoint::dial_tcp`]. - pub fn poll_ok(&mut self, waiter: &kio::Waiter) -> Poll> { +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() { @@ -1091,7 +826,7 @@ impl Dialing { result } - fn poll_inner(&mut self, waiter: &kio::Waiter) -> Poll> { + fn poll_inner(&mut self, waiter: &kio::Waiter) -> Poll> { self.shared.poll_end(waiter)?; self.shared.poll_reachable(waiter)?; @@ -1110,7 +845,7 @@ impl Dialing { } } - ready!(self.step.poll(waiter, &self.subscription, &self.name))?; + 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 { @@ -1123,15 +858,15 @@ impl Dialing { } } -impl kio::Task for Dialing { - type Output = Result; +impl kio::Task for Dialing { + type Output = Result; fn poll(&mut self, waiter: &kio::Waiter) -> Poll { self.poll_ok(waiter) } } -impl Drop for Dialing { +impl Drop for Dialing { fn drop(&mut self) { // Given up: reset the session for the peer. if let Some(answer) = self.answer.take() { @@ -1141,25 +876,22 @@ impl Drop for Dialing { } /// An accept, from an [`Incoming`] dial: resolves once the dialer answers. -pub struct Accepting { +pub struct Accepting { shared: Arc, peer: Arc, session: Arc, /// The session's track name. name: String, info: Option, - /// How this side reads the peer's half. - subscription: Subscription, /// 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, - stream: PhantomData S>, } -impl Accepting { +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, @@ -1167,7 +899,7 @@ impl Accepting { /// [`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> { + 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; @@ -1178,13 +910,13 @@ impl Accepting { result } - fn poll_inner(&mut self, waiter: &kio::Waiter) -> Poll> { + 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.subscription, &self.name))?; + 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)?; @@ -1193,15 +925,15 @@ impl Accepting { } } -impl kio::Task for Accepting { - type Output = Result; +impl kio::Task for Accepting { + type Output = Result; fn poll(&mut self, waiter: &kio::Waiter) -> Poll { self.poll_ok(waiter) } } -impl Drop for Accepting { +impl Drop for Accepting { fn drop(&mut self) { // Given up: reset the dialer's request. if let Some(request) = self.request.take() { @@ -1210,242 +942,18 @@ impl Drop for Accepting { } } -/// A `tcp` session's task on the origin's driver. -/// -/// It copies the peer's half into the group the application reads, a frame at a time and -/// paced by that reader, so backpressure reaches the peer. It ends this side's half the -/// way the application ended its group, and resets the session on a second group from the -/// peer, the application dropping its reader, or the endpoint closing. -struct Pump { - shared: Arc, - /// This side's half, held so the application can write only its one group. - track: Option, - /// This side's group, watched for how the application ends it without keeping it open. - ours: Option, - /// The peer's half, watched for its one group, and for a second. - subscriber: Option, - /// Whether the peer's group arrived. - received: bool, - /// The peer's group, from its arrival until it ends. - remote: Option, - /// The application's copy of the peer's group, until it ends. - local: Option, - local_handle: group::Handle, -} - -/// Where a `tcp` session's pump is in copying the peer's group. -enum Local { - /// Between frames, holding the next one read from the peer until the application's - /// reader makes room for it. - Between { - group: group::Producer, - next: Option, - }, - /// Copying a frame as its bytes arrive, so a reader sees them before the frame ends. - Frame { from: frame::Consumer, to: frame::Producer }, -} - -impl Local { - fn abort(self, err: Error) { - let _ = match self { - Self::Between { group, .. } => group.abort(err), - Self::Frame { to, .. } => to.abort(err), - }; - } -} - -impl Pump { - /// Reset everything still open with `err`. - fn reset(&mut self, err: Error) { - if let Some(local) = self.local.take() { - local.abort(err.clone()); - } - if let Some(track) = self.track.take() { - let _ = track.abort(err); - } - self.ours = None; - self.subscriber = None; - self.remote = None; - } - - fn poll_session(&mut self, waiter: &kio::Waiter) -> Result<(), Error> { - self.shared.poll_end(waiter)?; - - // This side's half ends the way the application ended its group. - if let Some(ours) = &self.ours - && let Poll::Ready(outcome) = ours.poll_closed(waiter) - { - self.ours = None; - match outcome { - Ok(()) => { - if let Some(track) = &self.track { - let _ = track.finish_shared(); - } - } - Err(err) => { - if let Some(track) = self.track.take() { - let _ = track.abort(err); - } - } - } - } - - // The peer's half holds one group: a second is a protocol error, even after the - // first finished, for as long as either half is open. - if !self.received - && let Some(subscriber) = &mut self.subscriber - { - match subscriber.poll_recv_group(waiter) { - Poll::Pending => {} - Poll::Ready(Ok(Some(group))) => { - if group.sequence != 0 { - tracing::debug!(sequence = group.sequence, "a second group on a tcp session"); - return Err(Error::ProtocolViolation); - } - self.received = true; - self.remote = Some(group); - } - Poll::Ready(Ok(None)) => self.subscriber = None, - // Ended short of its group: the peer's reset. - Poll::Ready(Err(err)) => return Err(err), - } - } - if self.received - && let Some(subscriber) = &mut self.subscriber - { - // Not `poll_recv_group`: polled on every wake while reading the group it handed - // out, it stalled that group's delivery. - while let Poll::Ready(largest) = subscriber.poll_largest(waiter) { - match largest { - Some(largest) if largest.group > 0 => { - tracing::debug!(sequence = largest.group, "a second group on a tcp session"); - return Err(Error::ProtocolViolation); - } - Some(_) => {} - // The track ended, finished or reset: `poll_largest` reports both - // alike, so ask which, and pass on the peer's reset. - None => match subscriber.poll_finished(waiter) { - Poll::Ready(Ok(_)) => { - self.subscriber = None; - break; - } - Poll::Ready(Err(err)) => return Err(err), - Poll::Pending => break, - }, - } - } - } - - if self.local.is_none() { - return Ok(()); - } - // Nobody reads what the peer writes. - if self.local_handle.poll_unwanted(waiter).is_ready() { - return Err(Error::Cancel); - } - - let Some(remote) = &mut self.remote else { - // Finished without a group: an empty half. - if !self.received - && self.subscriber.is_none() - && let Some(Local::Between { group, .. }) = self.local.take() - { - let _ = group.finish(); - } - return Ok(()); - }; - loop { - match self.local.take().expect("checked above") { - Local::Between { group, next: None } => match remote.poll_next_frame(waiter) { - Poll::Pending => { - self.local = Some(Local::Between { group, next: None }); - return Ok(()); - } - Poll::Ready(Ok(Some(frame))) => { - self.local = Some(Local::Between { - group, - next: Some(frame), - }) - } - Poll::Ready(Ok(None)) => { - group.finish()?; - self.remote = None; - return Ok(()); - } - Poll::Ready(Err(err)) => { - self.local = Some(Local::Between { group, next: None }); - return Err(err); - } - }, - Local::Between { - group, - next: Some(from), - } => { - let info = frame::Info { - size: from.size, - timestamp: None, - }; - if group.poll_ready(waiter, &info).is_pending() { - self.local = Some(Local::Between { - group, - next: Some(from), - }); - return Ok(()); - } - let to = group.create_frame(info)?; - self.local = Some(Local::Frame { from, to }); - } - Local::Frame { mut from, mut to } => match from.poll_read_chunk(waiter) { - Poll::Pending => { - self.local = Some(Local::Frame { from, to }); - return Ok(()); - } - Poll::Ready(Ok(Some(chunk))) => { - let written = to.write(chunk); - self.local = Some(Local::Frame { from, to }); - written?; - } - Poll::Ready(Ok(None)) => { - let group = to.finish()?; - self.local = Some(Local::Between { group, next: None }); - } - Poll::Ready(Err(err)) => { - self.local = Some(Local::Frame { from, to }); - return Err(err); - } - }, - } - } - } -} - -impl kio::Task for Pump { - type Output = (); - - fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { - if let Err(err) = self.poll_session(waiter) { - self.reset(err); - } - // Done once both halves ended; dropping the track then leaves its end in place. - if self.ours.is_none() && self.local.is_none() { - return Poll::Ready(()); - } - Poll::Pending - } -} - #[cfg(test)] mod tests { use super::*; #[test] fn track_names_round_trip() { - let name = track_name("tcp", "alice", "bob", "0123abcd"); - assert_eq!(name, "tcp/alice/bob/0123abcd"); + let name = track_name("track", "alice", "bob", "0123abcd"); + assert_eq!(name, "track/alice/bob/0123abcd"); assert_eq!( parse(&name), Some(Name { - protocol: "tcp", + protocol: "track", dialer: "alice", acceptor: "bob", session: "0123abcd" @@ -1458,14 +966,14 @@ mod tests { for name in [ "command", "alice/s", - "tcp/alice/s", - "tcp/alice/bob/", - "tcp//bob/s", - "tcp/alice//s", + "track/alice/s", + "track/alice/bob/", + "track//bob/s", + "track/alice//s", "/alice/bob/s", - "tcp/alice/bob/s/0", - "tcp/a\\b/bob/s", - "tcp/alice/bob/s\\", + "track/alice/bob/s/0", + "track/a\\b/bob/s", + "track/alice/bob/s\\", ] { assert_eq!(parse(name), None, "{name}"); } @@ -1514,7 +1022,7 @@ mod tests { 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(Protocol::Track, &kio::Waiter::noop()); + let accept = self.shared.poll_dial(&kio::Waiter::noop()); assert!(accept.is_pending()); self.shared.close(); self.woken.store(true, Ordering::SeqCst); @@ -1527,12 +1035,12 @@ mod tests { 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_tcp(&noop).is_pending()); + assert!(endpoint.poll_accept_track(&noop).is_pending()); let server = endpoint.shared.state.lock().server.clone().unwrap(); let broadcast = server.broadcast().consume(); let dial = broadcast - .track("tcp/alice/bob/0123456789abcdef0123456789abcdef") + .track("track/alice/bob/0123456789abcdef0123456789abcdef") .unwrap() .subscribe(None) .into_inner(); @@ -1545,7 +1053,7 @@ mod tests { // The wake closes the endpoint while this takes the dial, which it resets. assert!(matches!( - endpoint.poll_accept_tcp(&noop), + endpoint.poll_accept_track(&noop), Poll::Ready(Err(Error::Closed)) )); assert!(close.woken.load(Ordering::SeqCst)); @@ -1559,20 +1067,18 @@ mod tests { thread.join().unwrap(); } - /// A dial queued in a protocol the application stopped accepting leaves the queue once - /// its dialer gives up, so dialing and giving up again never piles up requests. + /// 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(); - // Accepting both protocols, then only `tcp`. assert!(endpoint.poll_accept_track(&noop).is_pending()); - assert!(endpoint.poll_accept_tcp(&noop).is_pending()); let server = endpoint.shared.state.lock().server.clone().unwrap(); let broadcast = server.broadcast().consume(); - let queued = || endpoint.shared.state.lock().track.dials.len(); + let queued = || endpoint.shared.state.lock().dials.len(); let dial = |session: u32| { let dial = broadcast .track(&format!("track/alice/bob/{session:032x}")) @@ -1582,16 +1088,23 @@ mod tests { assert!(dial.poll_ok(&noop).is_pending()); dial }; - for session in 0..3 { - let dial = dial(session); - assert!(endpoint.poll_accept_tcp(&noop).is_pending()); + 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(dial); + drop(waiting); + let _taken = dial(session); + waiting = dial(session + 1); + assert_eq!(accept(), format!("{session:032x}")); } - let _open = dial(3); - let _also = dial(4); - assert!(endpoint.poll_accept_tcp(&noop).is_pending()); - assert_eq!(queued(), 2); + assert_eq!(queued(), 1); } #[test] diff --git a/rs/moq-net/tests/integration/socket.rs b/rs/moq-net/tests/integration/socket.rs index 04b617386a5..79250dc5367 100644 --- a/rs/moq-net/tests/integration/socket.rs +++ b/rs/moq-net/tests/integration/socket.rs @@ -1,17 +1,13 @@ #![cfg(feature = "async")] //! Sockets through a relay: endpoints on their own origins, each with a session to a -//! relay, dialing and accepting `tcp` and `track` sessions over moq-lite-07 and -//! moq-transport. +//! relay, dialing and accepting `track` sessions over moq-lite-07 and moq-transport. -use std::sync::Arc; -use std::sync::atomic::{AtomicUsize, Ordering}; use std::time::Duration; -use bytes::Bytes; use moq_net::broadcast::TracksEvent; -use moq_net::socket::{Endpoint, TcpStream, TrackStream}; -use moq_net::{Error, StreamError, Version, frame, group, origin, track}; +use moq_net::socket::{Endpoint, TrackStream}; +use moq_net::{Error, StreamError, Version, group, origin, track}; use crate::support::harness::{MockConnectOptions, MockPair, connect_mock, spawn}; @@ -78,30 +74,14 @@ impl Fixture { } } -/// Start `endpoint` accepting `tcp` dials, which announces its server broadcast, for a -/// test that dials it by hand. +/// 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_tcp(&kio::Waiter::noop()).is_pending()); + assert!(endpoint.poll_accept_track(&kio::Waiter::noop()).is_pending()); } -const MB: usize = 1 << 20; - -/// Dial `acceptor` from `dialer` in a `tcp` session while `acceptor` accepts, returning -/// both ends. -async fn connect_tcp(dialer: &Endpoint, acceptor: &Endpoint) -> (TcpStream, TcpStream) { - let dial = dialer.dial_tcp(acceptor.id(), 0).unwrap(); - let accept = async { - let incoming = acceptor.accept_tcp().await?; - assert_eq!(incoming.peer(), dialer.id()); - incoming.accept(0).await - }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("connect timeout"); - (dialed.expect("dial"), accepted.expect("accept")) -} - -/// Dial `acceptor` from `dialer` in a `track` session; see [`connect_tcp`]. +/// 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 { @@ -115,22 +95,14 @@ async fn connect_track(dialer: &Endpoint, acceptor: &Endpoint) -> (TrackStream, (dialed.expect("dial"), accepted.expect("accept")) } -/// Read the next write from a `tcp` session, or `None` once the peer finished. -async fn read(reader: &mut group::Consumer) -> Result>, Error> { - kio::clock::timeout(TIMEOUT, reader.read_frame()) - .await - .expect("read timeout") - .map(|frame| frame.map(|frame| frame.payload.to_vec())) -} - -/// Write `payload` as one message of a `track` session: one group. +/// Write `payload` as one message of a session: one group. fn send(track: &track::Producer, payload: &'static [u8]) { let mut group = track.append_group().unwrap(); group.write_frame(None, payload).unwrap(); group.finish().unwrap(); } -/// Read the next message from a `track` session, or `None` once the peer finished. +/// 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(mut group) = reader.next_group().await? else { @@ -143,400 +115,8 @@ async fn recv(reader: &mut track::Ordered) -> Result>, Error> { .expect("read timeout") } -/// Writes flow both ways, in order, over every pairing of moq-lite-07 and moq-transport, -/// and finishing a half is FIN: the handshake uses neither instances nor track listings. -#[kio::test] -async fn tcp_sessions_carry_writes_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 (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; - assert_eq!(dialed.peer(), "bob"); - assert_eq!(accepted.peer(), "alice"); - assert_eq!(dialed.session(), accepted.session()); - - for write in [b"one", b"two", b"six"] { - dialed.producer.write_frame(None, &write[..]).unwrap(); - } - for write in [b"one", b"two", b"six"] { - assert_eq!( - read(&mut accepted.consumer).await.unwrap().unwrap(), - write, - "{dialer} to {acceptor}" - ); - } - accepted.producer.write_frame(None, &b"pong"[..]).unwrap(); - assert_eq!(read(&mut dialed.consumer).await.unwrap().unwrap(), b"pong"); - - // FIN, each way. - dialed.producer.finish().unwrap(); - assert_eq!( - read(&mut accepted.consumer).await.unwrap(), - None, - "{dialer} to {acceptor}" - ); - accepted.producer.finish().unwrap(); - assert_eq!( - read(&mut dialed.consumer).await.unwrap(), - None, - "{dialer} to {acceptor}" - ); - } -} - -/// A long session is one group of many writes, read in order. -#[kio::test] -async fn a_long_tcp_session_stays_in_order() { - const WRITES: u32 = 2000; - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; - - let write = async { - for i in 0..WRITES { - let payload = Bytes::copy_from_slice(&i.to_be_bytes()); - dialed - .producer - .ready(&frame::Info { - size: 4, - timestamp: None, - }) - .await; - dialed.producer.write_frame(None, payload).unwrap(); - } - dialed.producer.finish().unwrap(); - }; - let read_all = async { - for i in 0..WRITES { - assert_eq!(read(&mut accepted.consumer).await.unwrap().unwrap(), i.to_be_bytes()); - } - assert_eq!(read(&mut accepted.consumer).await.unwrap(), None); - }; - futures::join!(write, read_all); -} - -/// A write's bytes reach the peer's reader as they arrive, before the write ends, so a -/// forwarded TCP connection never waits on a whole frame. -#[kio::test] -async fn a_tcp_write_is_read_before_it_ends() { - 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, mut accepted) = connect_tcp(&alice, &bob).await; - let mut write = dialed - .producer - .create_frame(frame::Info { - size: 8, - timestamp: None, - }) - .unwrap(); - write.write(&b"half"[..]).unwrap(); - - let mut frame = kio::clock::timeout(TIMEOUT, accepted.consumer.next_frame()) - .await - .expect("read timeout") - .unwrap() - .expect("a frame"); - let chunk = kio::clock::timeout(TIMEOUT, frame.read_chunk()) - .await - .expect("{version}: the half never arrived"); - assert_eq!(chunk.unwrap().unwrap(), &b"half"[..], "{version}"); - - write.write(&b"done"[..]).unwrap(); - write.finish().unwrap().finish().unwrap(); - let chunk = kio::clock::timeout(TIMEOUT, frame.read_chunk()) - .await - .expect("read timeout"); - assert_eq!(chunk.unwrap().unwrap(), &b"done"[..], "{version}"); - assert_eq!(read(&mut accepted.consumer).await.unwrap(), None, "{version}"); - } -} - -/// The relay going away, as when it restarts, resets the session for both ends: a read -/// fails with the cause rather than reading as FIN. -#[kio::test] -async fn losing_the_relay_resets_a_tcp_session() { - 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 (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; - dialed.producer.write_frame(None, &b"before"[..]).unwrap(); - assert_eq!(read(&mut accepted.consumer).await.unwrap().unwrap(), b"before"); - - for pair in fixture.sessions.drain(..) { - drop(pair); - } - assert!(read(&mut accepted.consumer).await.is_err(), "{version}: read as FIN"); - assert!(read(&mut dialed.consumer).await.is_err(), "{version}: read as FIN"); - } -} - -/// Aborting a half is RST: the peer's read fails instead of ending. -#[kio::test] -async fn a_reset_tcp_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, mut accepted) = connect_tcp(&alice, &bob).await; - let mut producer = dialed.producer; - producer.write_frame(None, &b"partial"[..]).unwrap(); - assert_eq!(read(&mut accepted.consumer).await.unwrap().unwrap(), b"partial"); - producer.abort(Error::Cancel).unwrap(); - assert!( - read(&mut accepted.consumer).await.is_err(), - "{version}: a reset read as FIN" - ); - } -} - -/// A slow reader paces the writer through every hop, and a session longer than a group's -/// live tail loses nothing: the writer waits instead of running ahead. -#[kio::test] -async fn a_slow_tcp_reader_paces_the_writer() { - // Well past the 32 MiB a group copy holds, and the window every hop may buffer. - const WRITES: usize = 192; - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(LITE, "alice").await; - let bob = fixture.endpoint(LITE, "bob").await; - let (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; - - let written = Arc::new(AtomicUsize::new(0)); - let write = { - let written = written.clone(); - async move { - for i in 0..WRITES { - let payload = Bytes::from(vec![i as u8; MB]); - let info = frame::Info { - size: MB as u64, - timestamp: None, - }; - dialed.producer.ready(&info).await; - dialed.producer.write_frame(None, payload).unwrap(); - written.fetch_add(1, Ordering::SeqCst); - } - dialed.producer.finish().unwrap(); - } - }; - let read_all = async { - // Not reading for a while: the writer stalls well short of the end. - kio::clock::sleep(Duration::from_secs(5)).await; - let ahead = written.load(Ordering::SeqCst); - assert!(ahead < WRITES, "the writer ran {ahead} writes ahead of an idle reader"); - for i in 0..WRITES { - let frame = read(&mut accepted.consumer).await.unwrap().expect("a write"); - assert_eq!(frame.len(), MB); - assert!(frame.iter().all(|byte| *byte == i as u8), "write {i} out of order"); - // Slowly. - kio::clock::sleep(Duration::from_millis(10)).await; - } - assert_eq!(read(&mut accepted.consumer).await.unwrap(), None); - }; - kio::clock::timeout(Duration::from_secs(120), async { futures::join!(write, read_all) }) - .await - .expect("session timeout"); -} - -/// A `tcp` session carol dialed by hand, as a nonconforming implementation would. -struct ByHand { - /// Bob's end. - accepted: TcpStream, - /// Carol's half, which she writes groups to as she likes. - half: track::Producer, - _dialed: track::Subscriber, - _endpoint: origin::Route, - _carol: origin::Producer, -} - -/// Dial bob by hand from carol in a `tcp` session. -async fn dial_by_hand(fixture: &mut Fixture, bob: &Endpoint) -> ByHand { - let carol = fixture.client(LITE).await; - let endpoint = carol - .route("room/.client/carol", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - endpoint.announce().unwrap(); - let mut requests = endpoint.broadcast().requests(); - let name = "tcp/carol/bob/0123456789abcdef0123456789abcdef"; - listen(bob); - 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 accept = async { - let incoming = bob.accept_tcp().await.unwrap(); - incoming.accept(0).await - }; - 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"); - ByHand { - accepted: accepted.expect("accept"), - half, - _dialed: dialed.expect("bob's half"), - _endpoint: endpoint, - _carol: carol, - } -} - -/// A `tcp` dial and its accept each subscribe to the peer's half at the priority they -/// ask, through the relay, which is what schedules that half against other traffic. -#[kio::test] -async fn tcp_halves_are_read_at_the_asked_priority() { - for version in [LITE, IETF] { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(version, "alice").await; - let bob = fixture.endpoint(version, "bob").await; - - // Dave's server endpoint, played by hand, sees the priority alice dials with. - let dave = fixture.client(version).await; - let server = dave - .route("room/.server/dave", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - server.announce().unwrap(); - let mut requests = server.broadcast().requests(); - let mut dial = alice.dial_tcp("dave", 7).unwrap().into_inner(); - let request = kio::clock::timeout( - TIMEOUT, - kio::wait(|waiter| { - assert!(dial.poll_ok(waiter).is_pending()); - requests.poll_requested_track(waiter) - }), - ) - .await - .expect("dial timeout") - .unwrap(); - assert_eq!(request.subscription().unwrap().priority, 7, "{version}"); - - // Carol's client endpoint, played by hand, sees the priority bob accepts with. - let carol = fixture.client(version).await; - let client = carol - .route("room/.client/carol", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - client.announce().unwrap(); - let mut requests = client.broadcast().requests(); - listen(&bob); - 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("tcp/carol/bob/0123456789abcdef0123456789abcdef") - .unwrap() - .subscribe(None); - let incoming = kio::clock::timeout(TIMEOUT, bob.accept_tcp()) - .await - .expect("accept timeout") - .unwrap(); - let mut accept = incoming.accept(-3).into_inner(); - let request = kio::clock::timeout( - TIMEOUT, - kio::wait(|waiter| { - assert!(accept.poll_ok(waiter).is_pending()); - requests.poll_requested_track(waiter) - }), - ) - .await - .expect("accept timeout") - .unwrap(); - assert_eq!(request.subscription().unwrap().priority, -3, "{version}"); - } -} - -/// Write `sequence` as a finished group of one write. -fn write_group(half: &track::Producer, sequence: u64) { - let mut group = half.create_group(group::Info { sequence }).unwrap(); - group.write_frame(None, &b"data"[..]).unwrap(); - group.finish().unwrap(); -} - -/// A peer that writes a second group in a `tcp` session breaks the protocol: the session -/// is reset, both halves. -#[kio::test] -async fn a_second_tcp_group_resets_the_session() { - let mut fixture = Fixture::new(); - let bob = fixture.endpoint(LITE, "bob").await; - // Bound whole, so carol's endpoint and read stay open to the end. - let mut session = dial_by_hand(&mut fixture, &bob).await; - let (accepted, half) = (&mut session.accepted, &session.half); - - write_group(half, 0); - write_group(half, 1); - // Bob reads at most the first group's write, then the reset. - let mut reads = 0; - loop { - match read(&mut accepted.consumer).await { - Ok(Some(_)) => reads += 1, - Ok(None) => panic!("a second group read as FIN"), - Err(_) => break, - } - assert!(reads <= 1, "read past the first group"); - } - // And bob's own half was reset with it. - kio::clock::sleep(SETTLE).await; - assert!(accepted.producer.write_frame(None, &b"late"[..]).is_err()); -} - -/// A second group after the first one finished, and was read to its end, still resets -/// the half that remains open. -#[kio::test] -async fn a_second_tcp_group_after_fin_resets_the_session() { - let mut fixture = Fixture::new(); - let bob = fixture.endpoint(LITE, "bob").await; - // Bound whole, so carol's endpoint and read stay open to the end. - let mut session = dial_by_hand(&mut fixture, &bob).await; - let (accepted, half) = (&mut session.accepted, &session.half); - - write_group(half, 0); - assert_eq!(read(&mut accepted.consumer).await.unwrap().unwrap(), b"data"); - assert_eq!(read(&mut accepted.consumer).await.unwrap(), None); - accepted.producer.write_frame(None, &b"still open"[..]).unwrap(); - - write_group(half, 1); - kio::clock::sleep(SETTLE).await; - assert!(accepted.producer.write_frame(None, &b"late"[..]).is_err()); -} - -/// The peer resetting its half after its group finished, and was read to its end, resets -/// the half that remains open, as a TCP RST after FIN does. Finishing it instead leaves -/// that half open, as a half-close does. -#[kio::test] -async fn a_peer_reset_after_fin_resets_the_session() { - for reset in [true, false] { - let mut fixture = Fixture::new(); - let bob = fixture.endpoint(LITE, "bob").await; - // Bound whole, so carol's endpoint and read stay open to the end. - let session = dial_by_hand(&mut fixture, &bob).await; - let ByHand { mut accepted, half, .. } = session; - - write_group(&half, 0); - assert_eq!(read(&mut accepted.consumer).await.unwrap().unwrap(), b"data"); - assert_eq!(read(&mut accepted.consumer).await.unwrap(), None); - accepted.producer.write_frame(None, &b"still open"[..]).unwrap(); - - if reset { - half.abort(Error::Cancel).unwrap(); - } else { - let _half = half.finish().unwrap(); - } - kio::clock::sleep(SETTLE).await; - let late = accepted.producer.write_frame(None, &b"late"[..]); - assert_eq!(late.is_err(), reset, "reset: {reset}"); - } -} - /// Messages flow both ways, in order, over every pairing of moq-lite-07 and -/// moq-transport. +/// 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)] { @@ -545,6 +125,7 @@ async fn track_sessions_carry_messages_both_ways() { 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(); @@ -568,6 +149,22 @@ async fn track_sessions_carry_messages_both_ways() { } } +/// 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_sequence() { @@ -617,27 +214,30 @@ async fn a_lost_track_message_does_not_block_later_ones() { assert_eq!(recv(&mut reader).await.unwrap().unwrap(), b"two"); } -/// A pair of endpoints holds several sessions at once in either protocol, each its own -/// pipe. +/// 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 (mut first, mut first_accepted) = connect_tcp(&alice, &bob).await; - let (mut second, mut second_accepted) = connect_tcp(&alice, &bob).await; - // Either side may dial, in either protocol. + 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()); - second.producer.write_frame(None, &b"second"[..]).unwrap(); - first.producer.write_frame(None, &b"first"[..]).unwrap(); + send(&second.producer, b"second"); + send(&first.producer, b"first"); send(&third_accepted.producer, b"third"); - assert_eq!(read(&mut first_accepted.consumer).await.unwrap().unwrap(), b"first"); - assert_eq!(read(&mut second_accepted.consumer).await.unwrap().unwrap(), b"second"); + 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"); - first.producer.finish().unwrap(); - second.producer.finish().unwrap(); } /// Rejecting a dial resets it. @@ -646,9 +246,9 @@ 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_tcp("bob", 0).unwrap(); + let dial = alice.dial_track("bob", None).unwrap(); let reject = async { - bob.accept_tcp().await.unwrap().reject(); + bob.accept_track().await.unwrap().reject(); }; let (dialed, ()) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, reject) }) .await @@ -656,36 +256,28 @@ async fn a_rejected_dial_fails() { assert!(dialed.is_err(), "a rejected dial connected"); } -/// A dial in a protocol the peer does not accept is reset, while the peer still accepts -/// the protocol it listens on. +/// A dial in another protocol than `track` is reset and never surfaces as a dial. #[kio::test] -async fn a_protocol_nobody_accepts_is_reset() { +async fn an_unknown_protocol_is_reset() { 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 { kio::clock::timeout(SETTLE, bob.accept_tcp()).await }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("dial timeout"); - assert!(dialed.is_err(), "a track dial reached a tcp listener"); - assert!(accepted.is_err(), "a track dial surfaced as a tcp dial"); - - // An unknown protocol is reset too. + 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 unknown = broadcast - .track("quic/mallory/bob/0123456789abcdef0123456789abcdef") - .unwrap() - .subscribe(None); - let unknown = async { kio::clock::timeout(TIMEOUT, unknown).await }; - let accept = async { kio::clock::timeout(SETTLE, bob.accept_tcp()).await }; - let (unknown, accepted) = futures::join!(unknown, accept); - assert!(unknown.expect("unknown timeout").is_err()); - assert!(accepted.is_err(), "an unknown protocol surfaced as a dial"); + 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. @@ -693,21 +285,20 @@ async fn a_protocol_nobody_accepts_is_reset() { async fn a_dial_waits_for_its_peer() { let mut fixture = Fixture::new(); let alice = fixture.endpoint(LITE, "alice").await; - assert!(matches!(alice.dial_tcp("alice", 0), Err(Error::Unsupported))); - let mut dial = alice.dial_tcp("bob", 0).unwrap(); + 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_tcp().await?.accept(0).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()); - dialed.producer.finish().unwrap(); } /// An accept waits for the dialer's endpoint while the dial stays open, since a relay can @@ -719,19 +310,19 @@ async fn an_accept_waits_for_its_dialer() { let bob = fixture.endpoint(LITE, "bob").await; listen(&bob); let carol = fixture.client(LITE).await; - let name = "tcp/carol/bob/0123456789abcdef0123456789abcdef"; + 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_tcp()) + let incoming = kio::clock::timeout(TIMEOUT, bob.accept_track()) .await .expect("accept timeout") .unwrap(); assert_eq!(incoming.peer(), "carol"); - let mut accept = incoming.accept(0); + let mut accept = incoming.accept(None); assert!( kio::clock::timeout(SETTLE, &mut accept).await.is_err(), "bob's accept ended before carol announced" @@ -767,15 +358,15 @@ async fn an_accept_ends_with_its_dial() { .expect("request timeout") .expect("bob's endpoint"); let forged = broadcast - .track("tcp/carol/bob/0123456789abcdef0123456789abcdef") + .track("track/carol/bob/0123456789abcdef0123456789abcdef") .unwrap() .subscribe(None); - let incoming = kio::clock::timeout(TIMEOUT, bob.accept_tcp()) + let incoming = kio::clock::timeout(TIMEOUT, bob.accept_track()) .await .expect("accept timeout") .unwrap(); assert_eq!(incoming.peer(), "carol"); - let mut accept = incoming.accept(0); + 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" @@ -794,7 +385,7 @@ async fn a_replayed_session_never_connects() { let alice = fixture.endpoint(LITE, "alice").await; let bob = fixture.endpoint(LITE, "bob").await; let carol = fixture.endpoint(LITE, "carol").await; - let (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; + let (dialed, accepted) = connect_track(&alice, &bob).await; let session = accepted.session().to_string(); // Replayed from bob's side, which knows the session. @@ -807,11 +398,11 @@ async fn a_replayed_session_never_connects() { // Under the name alice dialed: carol is not its acceptor, so she resets it unheard. let replayed = broadcast - .track(&format!("tcp/alice/bob/{session}")) + .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_tcp()).await }; + 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(), @@ -821,78 +412,65 @@ async fn a_replayed_session_never_connects() { // Renamed for carol: alice never dialed it, so she resets carol's accept. let renamed = broadcast - .track(&format!("tcp/alice/carol/{session}")) + .track(&format!("track/alice/carol/{session}")) .unwrap() .subscribe(None); - let incoming = kio::clock::timeout(TIMEOUT, carol.accept_tcp()) + 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(0), 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. - dialed.producer.write_frame(None, &b"still here"[..]).unwrap(); - assert_eq!(read(&mut accepted.consumer).await.unwrap().unwrap(), b"still here"); + 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. The same holds in either protocol. +/// nothing. #[kio::test] async fn a_forged_dial_never_connects() { - for protocol in ["tcp", "track"] { - 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_tcp("carol", 0).unwrap(); - listen(&bob); + 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(&format!("{protocol}/alice/bob/0123456789abcdef0123456789abcdef")) - .unwrap() - .subscribe(None); + 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_peer, accepted) = match protocol { - "tcp" => { - let incoming = kio::clock::timeout(TIMEOUT, bob.accept_tcp()) - .await - .expect("accept timeout") - .unwrap(); - let peer = incoming.peer().to_string(); - let accepted = kio::clock::timeout(TIMEOUT, incoming.accept(0)).await; - (peer, accepted.expect("accept timeout").map(drop)) - } - _ => { - let incoming = kio::clock::timeout(TIMEOUT, bob.accept_track()) - .await - .expect("accept timeout") - .unwrap(); - let peer = incoming.peer().to_string(); - let accepted = kio::clock::timeout(TIMEOUT, incoming.accept(None)).await; - (peer, accepted.expect("accept timeout").map(drop)) - } - }; - assert_eq!(incoming_peer, "alice"); - assert!(accepted.is_err(), "bob connected to a forged {protocol} dial"); - let forged = kio::clock::timeout(TIMEOUT, forged).await.expect("forged timeout"); - assert!(forged.is_err(), "the forger read bob's {protocol} half"); - - // Alice never heard of it as a dial either, even accepting. - assert!(kio::clock::timeout(SETTLE, alice.accept_tcp()).await.is_err()); - assert!(kio::clock::timeout(SETTLE, alice.accept_track()).await.is_err()); - } + 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. @@ -901,10 +479,10 @@ 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_tcp("bob", 0).unwrap(); + 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_tcp()), &mut dial).await { + 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"), } @@ -913,7 +491,7 @@ async fn a_dropped_dial_resets_the_accept() { .expect("accept timeout") .unwrap(); drop(dial); - let accepted = kio::clock::timeout(TIMEOUT, incoming.accept(0)) + let accepted = kio::clock::timeout(TIMEOUT, incoming.accept(None)) .await .expect("accept timeout"); assert!(accepted.is_err(), "bob connected to a dropped dial"); @@ -925,30 +503,19 @@ 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 (mut tcp_dialed, mut tcp_accepted) = connect_tcp(&alice, &bob).await; - let (track_dialed, track_accepted) = connect_track(&alice, &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 = read(&mut tcp_dialed.consumer).await; + let ended = recv(&mut dialed.consumer.ordered()).await; assert!( matches!(ended, Err(Error::Stream(StreamError::Session(_)))), - "bob's tcp half outlived his endpoint: {ended:?}" + "bob's half outlived his endpoint: {ended:?}" ); - // Both halves of bob's own tcp session end. - assert!(read(&mut tcp_accepted.consumer).await.is_err()); - assert!(tcp_accepted.producer.write_frame(None, &b"late"[..]).is_err()); - assert!( - recv(&mut track_dialed.consumer.ordered()).await.is_err(), - "bob's track half outlived his endpoint" - ); - assert!(track_accepted.producer.append_group().is_err()); - assert!(matches!(bob.accept_tcp().await, Err(Error::Closed))); + assert!(accepted.producer.append_group().is_err()); assert!(matches!(bob.accept_track().await, Err(Error::Closed))); - assert!(matches!(bob.dial_tcp("alice", 0), Err(Error::Closed))); - // Alice saw bob's half reset, so she reset hers: the session is gone both ways. - assert!(tcp_dialed.producer.finish().is_err()); + 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: @@ -959,13 +526,10 @@ 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 (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; - let (track_dialed, track_accepted) = connect_track(&alice, &bob).await; - dialed.producer.write_frame(None, &b"secret"[..]).unwrap(); - assert_eq!(read(&mut accepted.consumer).await.unwrap().unwrap(), b"secret"); - send(&track_dialed.producer, b"secret"); + let (dialed, accepted) = connect_track(&alice, &bob).await; + send(&dialed.producer, b"secret"); assert_eq!( - recv(&mut track_accepted.consumer.ordered()).await.unwrap().unwrap(), + recv(&mut accepted.consumer.ordered()).await.unwrap().unwrap(), b"secret" ); @@ -986,7 +550,6 @@ async fn session_tracks_are_never_listed() { "{id} listed a track" ); } - dialed.producer.finish().unwrap(); } /// An endpoint announces only the roles it takes: its first dial its client broadcast, @@ -997,8 +560,8 @@ 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_tcp("carol", 0).unwrap(); - let mut dial = alice.dial_tcp("bob", 0).unwrap(); + 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" @@ -1015,13 +578,12 @@ async fn an_endpoint_announces_only_its_roles() { assert_eq!(matches!(found, Ok(Ok(_))), announced, "{path}"); } - let accept = async { bob.accept_tcp().await?.accept(0).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()); - dialed.producer.finish().unwrap(); let found = kio::clock::timeout(TIMEOUT, eve.consume().request("room/.server/bob", None)).await; assert!(matches!(found, Ok(Ok(_))), "bob's server broadcast"); } @@ -1056,33 +618,6 @@ async fn the_first_burst_survives_a_late_subscribe_answer() { assert_eq!(recv(&mut accepted.consumer.ordered()).await.unwrap(), None); } -/// TCP must also recover every frame when FIN overtakes the subscription answer. -#[kio::test] -async fn the_first_tcp_writes_survive_a_late_subscribe_answer() { - for version in [LITE, IETF, "moq-transport-20", "moq-transport-22"] { - let mut fixture = Fixture::new(); - let alice = fixture.endpoint(version, "alice").await; - let bob = fixture.endpoint(version, "bob").await; - let dial = alice.dial_tcp("bob", 0).unwrap(); - let accept = async { - let mut stream = bob.accept_tcp().await.unwrap().accept(0).await.unwrap(); - for payload in [b"first".as_slice(), b"", b"last"] { - stream.producer.write_frame(None, payload).unwrap(); - } - (stream.producer.finish().unwrap(), stream.consumer) - }; - let (dialed, accepted) = futures::join!(dial, accept); - let mut dialed = dialed.unwrap(); - let (_finished, mut accepted) = accepted; - for payload in [b"first".as_slice(), b"", b"last"] { - assert_eq!(read(&mut dialed.consumer).await.unwrap().unwrap(), payload, "{version}"); - } - assert_eq!(read(&mut dialed.consumer).await.unwrap(), None); - dialed.producer.finish().unwrap(); - assert_eq!(read(&mut accepted).await.unwrap(), None); - } -} - /// The moq-lite versions before hidden broadcasts. const OLDER: [&str; 2] = ["moq-lite-06", "moq-lite-03"]; @@ -1095,18 +630,14 @@ async fn an_older_hop_fails_dials_and_accepts() { let alice = fixture.endpoint(older, "alice").await; let bob = fixture.endpoint(LITE, "bob").await; - let dialed = kio::clock::timeout(SETTLE, alice.dial_tcp("bob", 0).unwrap()).await; - assert!(matches!(dialed, Ok(Err(Error::Unsupported))), "{older} tcp dial"); let dialed = kio::clock::timeout(SETTLE, alice.dial_track("bob", None).unwrap()).await; - assert!(matches!(dialed, Ok(Err(Error::Unsupported))), "{older} track dial"); - let accepted = kio::clock::timeout(SETTLE, alice.accept_tcp()).await; - assert!(matches!(accepted, Ok(Err(Error::Unsupported))), "{older} accept"); + 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_tcp()).await.is_err(), + kio::clock::timeout(SETTLE, bob.accept_track()).await.is_err(), "{older}: bob heard a dial" ); } @@ -1121,9 +652,9 @@ async fn a_peer_behind_an_older_hop_fails_its_accepts() { let alice = fixture.endpoint(LITE, "alice").await; let bob = fixture.endpoint(older, "bob").await; - let accepted = kio::clock::timeout(SETTLE, bob.accept_tcp()).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_tcp("bob", 0).unwrap(); + 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" @@ -1138,7 +669,7 @@ 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_tcp("bob", 0).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" @@ -1160,7 +691,6 @@ async fn an_older_hop_opted_into_hidden_connects() { .await; let alice = Endpoint::new(&client, "room", "alice").unwrap(); let bob = fixture.endpoint(LITE, "bob").await; - let (dialed, accepted) = connect_tcp(&alice, &bob).await; + let (dialed, accepted) = connect_track(&alice, &bob).await; assert_eq!(dialed.session(), accepted.session()); - dialed.producer.finish().unwrap(); } diff --git a/rs/moq-relay/tests/integration/socket_auth.rs b/rs/moq-relay/tests/integration/socket_auth.rs index 96b9694039c..cfca8a6af9e 100644 --- a/rs/moq-relay/tests/integration/socket_auth.rs +++ b/rs/moq-relay/tests/integration/socket_auth.rs @@ -11,7 +11,7 @@ use std::time::Duration; use moq_net::broadcast::TracksEvent; -use moq_net::socket::{Endpoint, TcpStream, TrackStream}; +use moq_net::socket::{Endpoint, TrackStream}; use moq_net::transport::memory; use moq_relay::{Accepted, Connection}; @@ -144,22 +144,8 @@ enum Role { Client, } -/// Dial `acceptor` from `dialer` in a `tcp` session while `acceptor` accepts, returning -/// both ends. -async fn connect_tcp(dialer: &Endpoint, acceptor: &Endpoint) -> (TcpStream, TcpStream) { - let dial = dialer.dial_tcp(acceptor.id(), 0).expect("dial"); - let accept = async { - let incoming = acceptor.accept_tcp().await?; - assert_eq!(incoming.peer(), dialer.id()); - incoming.accept(0).await - }; - let (dialed, accepted) = kio::clock::timeout(TIMEOUT, async { futures::join!(dial, accept) }) - .await - .expect("connect timeout"); - (dialed.expect("dial"), accepted.expect("accept")) -} - -/// Dial `acceptor` from `dialer` in a `track` session; see [`connect_tcp`]. +/// 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 { @@ -173,25 +159,14 @@ async fn connect_track(dialer: &Endpoint, acceptor: &Endpoint) -> (TrackStream, (dialed.expect("dial"), accepted.expect("accept")) } -/// Read the next write from a `tcp` session. -async fn read(reader: &mut moq_net::group::Consumer) -> Vec { - kio::clock::timeout(TIMEOUT, reader.read_frame()) - .await - .expect("read timeout") - .expect("frame") - .expect("a frame") - .payload - .to_vec() -} - -/// Write `payload` as one message of a `track` session: one group. +/// 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().expect("group"); group.write_frame(None, payload).expect("frame"); group.finish().expect("finish"); } -/// Read the next message of a `track` session. +/// 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").expect("a group"); @@ -221,22 +196,14 @@ async fn announced(origin: &moq_net::origin::Producer, id: &str) { .unwrap_or_else(|_| panic!("{id} was never announced")); } -/// The prefix grants admit a dial and its accept in either protocol, and the session -/// carries data, over moq-lite-07 and across a moq-transport hop. +/// 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 (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; - dialed.producer.write_frame(None, &b"ping"[..]).expect("write"); - assert_eq!(read(&mut accepted.consumer).await, b"ping", "{dialer} to {acceptor}"); - accepted.producer.write_frame(None, &b"pong"[..]).expect("write"); - assert_eq!(read(&mut dialed.consumer).await, b"pong", "{dialer} to {acceptor}"); - dialed.producer.finish().expect("finish"); - accepted.producer.finish().expect("finish"); - let (dialed, accepted) = connect_track(&alice, &bob).await; send(&dialed.producer, b"ping"); assert_eq!( @@ -244,6 +211,12 @@ async fn prefix_grants_admit_a_session() { b"ping", "{dialer} to {acceptor}" ); + send(&accepted.producer, b"pong"); + assert_eq!( + recv(&mut dialed.consumer.ordered()).await, + b"pong", + "{dialer} to {acceptor}" + ); } } @@ -257,10 +230,10 @@ async fn a_forged_dial_never_connects() { 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_tcp("carol", 0).expect("dial"); + let _elsewhere = alice.dial_track("carol", None).expect("dial"); // Bob's first accept announces his server endpoint. - assert!(bob.poll_accept_tcp(&kio::Waiter::noop()).is_pending()); + assert!(bob.poll_accept_track(&kio::Waiter::noop()).is_pending()); announced(&mallory_origin, ".server/bob").await; let forge = async { @@ -270,12 +243,12 @@ async fn a_forged_dial_never_connects() { .await .expect("bob's endpoint"); let forged = broadcast - .track("tcp/alice/bob/0123456789abcdef0123456789abcdef") + .track("track/alice/bob/0123456789abcdef0123456789abcdef") .expect("track") .subscribe(None); - let incoming = bob.accept_tcp().await.expect("accept"); + let incoming = bob.accept_track().await.expect("accept"); assert_eq!(incoming.peer(), "alice"); - let (accepted, forged) = futures::join!(incoming.accept(0), forged); + 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"); }; @@ -305,7 +278,7 @@ async fn a_client_grant_neither_serves_nor_reads_clients() { }; // Alice dials, which announces her client endpoint. - let _alice_dial = alice.dial_tcp("carol", 0).expect("dial"); + let _alice_dial = alice.dial_track("carol", None).expect("dial"); carried(".client/alice").await; // Mallory, a client, can't read it. @@ -321,8 +294,8 @@ async fn a_client_grant_neither_serves_nor_reads_clients() { // 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_tcp(&kio::Waiter::noop()).is_pending()); - let _mallory_dial = mallory.dial_tcp("bob", 0).expect("dial"); + 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. @@ -344,12 +317,9 @@ 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 (mut dialed, mut accepted) = connect_tcp(&alice, &bob).await; - dialed.producer.write_frame(None, &b"secret"[..]).expect("write"); - assert_eq!(read(&mut accepted.consumer).await, b"secret"); - let (track_dialed, track_accepted) = connect_track(&alice, &bob).await; - send(&track_dialed.producer, b"secret"); - assert_eq!(recv(&mut track_accepted.consumer.ordered()).await, b"secret"); + 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"] { diff --git a/rs/moq-tokio/src/lib.rs b/rs/moq-tokio/src/lib.rs index 8b971b7012b..8ceb8547c62 100644 --- a/rs/moq-tokio/src/lib.rs +++ b/rs/moq-tokio/src/lib.rs @@ -60,8 +60,6 @@ pub mod secret; pub mod server; #[cfg(all(feature = "sim", feature = "net"))] pub mod sim; -#[cfg(feature = "net")] -pub mod socket; #[cfg(feature = "tcp")] pub mod tcp; pub mod tls; diff --git a/rs/moq-tokio/src/socket.rs b/rs/moq-tokio/src/socket.rs deleted file mode 100644 index 783b0dadf38..00000000000 --- a/rs/moq-tokio/src/socket.rs +++ /dev/null @@ -1,345 +0,0 @@ -//! Socket sessions as tokio I/O. -//! -//! [`TcpStream`] reads and writes a [`moq_net::socket::TcpStream`] through -//! [`AsyncRead`] and [`AsyncWrite`], with TCP's semantics: bytes arrive in order with -//! nothing lost, [`AsyncWriteExt::shutdown`](tokio::io::AsyncWriteExt::shutdown) is FIN, -//! and an error is a reset. So it drops into anything that forwards a TCP connection, -//! such as [`tokio::io::copy_bidirectional`]. - -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll, ready}; - -use bytes::Bytes; -use moq_net::{frame, group}; -use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; - -/// The most a write puts in one frame. -/// -/// Each frame costs a header and a cache slot on every hop, so a write past this splits -/// into several. Through a local relay, 64 KiB to 256 KiB frames moved 300 MB fastest, -/// and 1 MiB frames were slower. -pub const MAX_FRAME: usize = 64 * 1024; - -/// A `tcp` socket session as a byte stream: [`AsyncRead`] reads the peer's half, and -/// [`AsyncWrite`] writes this side's. -/// -/// A read hands out a frame's bytes as they arrive, before the frame ends, and returns 0 -/// once the peer shut down its half. A write waits until the peer's reader is at most a -/// window behind, so a slow reader slows the writer and nothing is lost, and becomes one -/// frame of at most [`MAX_FRAME`] bytes. -/// [`shutdown`](tokio::io::AsyncWriteExt::shutdown) finishes this side's half, which the -/// peer reads as the end, and reading on stays open. -/// -/// Every failure is a reset of the whole session, read and written as an -/// [`io::ErrorKind::ConnectionReset`] carrying the [`moq_net::Error`] that caused it: the -/// peer's [`Self::abort`], a reader falling behind its window -/// ([`moq_net::Error::Evicted`]), or the session to the relay ending. Dropping the stream -/// resets it too, unless both halves already ended. -pub struct TcpStream { - /// This side's half. - writer: Write, - reader: group::Consumer, - /// The peer's frame being read. - frame: Option, - /// What a read got of a chunk but had no room for. - chunk: Bytes, - /// Whether the peer's half ended. - eof: bool, - peer: String, - session: String, - read: kio::Park, - write: kio::Park, -} - -impl TcpStream { - /// The peer endpoint's id. - pub fn peer(&self) -> &str { - &self.peer - } - - /// The session's id, which the dialer chose. Keep it secret: anyone who knows it can - /// read the session. - pub fn session(&self) -> &str { - &self.session - } - - /// Reset the session with `err`, which the peer reads as its error, such as a - /// [`moq_net::Error::App`] code. - pub fn abort(mut self, err: moq_net::Error) { - match std::mem::replace(&mut self.writer, Write::Shut) { - Write::Open(writer) => { - let _ = writer.abort(err); - } - // Shut down already: the peer reads its end, and dropping the reader resets - // the session. - _ => tracing::debug!(%err, "aborted a tcp session after its shutdown"), - } - } -} - -impl From for TcpStream { - fn from(stream: moq_net::socket::TcpStream) -> Self { - let (peer, session) = (stream.peer().to_owned(), stream.session().to_owned()); - Self::new(stream.producer, stream.consumer, peer, session) - } -} - -impl TcpStream { - fn new(writer: group::Producer, reader: group::Consumer, peer: String, session: String) -> Self { - Self { - writer: Write::Open(writer), - reader, - frame: None, - chunk: Bytes::new(), - eof: false, - peer, - session, - read: kio::Park::default(), - write: kio::Park::default(), - } - } -} - -/// Where this side's half is. -enum Write { - Open(group::Producer), - /// Finished, or aborted by [`TcpStream::abort`]. - Shut, - /// Finishing it failed, which every later write and shutdown reports again. - Failed(moq_net::Error), -} - -impl Drop for TcpStream { - fn drop(&mut self) { - // Reset deliberately: an unfinished half dropped silently reads as a bug. - if let Write::Open(writer) = std::mem::replace(&mut self.writer, Write::Shut) { - let _ = writer.abort(moq_net::Error::Cancel); - } - } -} - -fn reset(err: moq_net::Error) -> io::Error { - io::Error::new(io::ErrorKind::ConnectionReset, err) -} - -impl AsyncRead for TcpStream { - fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll> { - let this = self.get_mut(); - let waiter = this.read.hold(cx); - loop { - if buf.remaining() == 0 { - return Poll::Ready(Ok(())); - } - if !this.chunk.is_empty() { - let n = this.chunk.len().min(buf.remaining()); - buf.put_slice(&this.chunk.split_to(n)); - return Poll::Ready(Ok(())); - } - match &mut this.frame { - Some(frame) => match ready!(frame.poll_read_chunk(waiter)).map_err(reset)? { - Some(chunk) => this.chunk = chunk, - None => this.frame = None, - }, - None if this.eof => return Poll::Ready(Ok(())), - None => match ready!(this.reader.poll_next_frame(waiter)).map_err(reset)? { - Some(frame) => this.frame = Some(frame), - None => this.eof = true, - }, - } - } - } -} - -impl AsyncWrite for TcpStream { - fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll> { - let this = self.get_mut(); - let writer = match &mut this.writer { - Write::Open(writer) => writer, - Write::Shut => return Poll::Ready(Err(io::ErrorKind::BrokenPipe.into())), - Write::Failed(err) => return Poll::Ready(Err(reset(err.clone()))), - }; - if buf.is_empty() { - return Poll::Ready(Ok(0)); - } - let size = buf.len().min(MAX_FRAME); - let info = frame::Info { - size: size as u64, - timestamp: None, - }; - ready!(writer.poll_ready(this.write.hold(cx), &info)); - writer - .write_frame(None, Bytes::copy_from_slice(&buf[..size])) - .map_err(reset)?; - Poll::Ready(Ok(size)) - } - - fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - // A written frame is already on its way. - Poll::Ready(Ok(())) - } - - fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - let this = self.get_mut(); - this.writer = match std::mem::replace(&mut this.writer, Write::Shut) { - Write::Open(writer) => match writer.finish() { - Ok(_) => Write::Shut, - Err(err) => Write::Failed(err), - }, - other => other, - }; - match &this.writer { - Write::Failed(err) => Poll::Ready(Err(reset(err.clone()))), - _ => Poll::Ready(Ok(())), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - - /// A group with its own broadcast and track, which outlive the test. - fn group() -> group::Producer { - let broadcast = moq_net::broadcast::Producer::new(Default::default()); - let track = broadcast.create_track("half", None).unwrap(); - let group = track.create_group(group::Info { sequence: 0 }).unwrap(); - std::mem::forget((broadcast, track)); - group - } - - /// Two ends joined by a group each way, as a session's pump joins them. - fn pair() -> (TcpStream, TcpStream) { - let (ab, ba) = (group(), group()); - let (a_reads, b_reads) = (ba.consume(), ab.consume()); - ( - TcpStream::new(ab, a_reads, "b".into(), "s".into()), - TcpStream::new(ba, b_reads, "a".into(), "s".into()), - ) - } - - /// The `moq_net::Error` a reset carries. - fn cause(err: &io::Error) -> &moq_net::Error { - assert_eq!(err.kind(), io::ErrorKind::ConnectionReset, "{err}"); - err.get_ref() - .and_then(|err| err.downcast_ref()) - .expect("a moq_net::Error") - } - - fn poll_read(stream: &mut TcpStream, buf: &mut [u8]) -> Poll> { - let mut buf = ReadBuf::new(buf); - let mut cx = Context::from_waker(std::task::Waker::noop()); - Pin::new(stream) - .poll_read(&mut cx, &mut buf) - .map_ok(|()| buf.filled().len()) - } - - #[kio::test] - async fn a_frame_is_read_before_it_ends() { - let writer = group(); - let mut b = TcpStream::new(group(), writer.consume(), "a".into(), "s".into()); - let mut frame = writer - .create_frame(frame::Info { - size: 10, - timestamp: None, - }) - .unwrap(); - frame.write(Bytes::from_static(b"hell")).unwrap(); - let mut buf = [0; 16]; - assert!(matches!(poll_read(&mut b, &mut buf), Poll::Ready(Ok(4)))); - assert_eq!(&buf[..4], b"hell"); - assert!(poll_read(&mut b, &mut buf).is_pending()); - frame.write(Bytes::from_static(b"o, you")).unwrap(); - frame.finish().unwrap().finish().unwrap(); - let mut rest = Vec::new(); - b.read_to_end(&mut rest).await.unwrap(); - assert_eq!(rest, b"o, you"); - } - - #[kio::test] - async fn bytes_round_trip_and_large_writes_split() { - let (mut a, mut b) = pair(); - let sent: Vec = (0..3 * MAX_FRAME + 17).map(|i| i as u8).collect(); - assert_eq!(a.write(&sent).await.unwrap(), MAX_FRAME); - a.write_all(&sent[MAX_FRAME..]).await.unwrap(); - a.shutdown().await.unwrap(); - let mut got = Vec::new(); - b.read_to_end(&mut got).await.unwrap(); - assert_eq!(got, sent); - assert_eq!(b.reader.frame_count(), 4); - } - - /// Shutting down one half is FIN: the peer reads to the end, and its own half stays open - /// both ways. - #[kio::test] - async fn shutdown_closes_one_half() { - for first in [true, false] { - let (a, b) = pair(); - let (mut closer, mut other) = if first { (a, b) } else { (b, a) }; - closer.write_all(b"bye").await.unwrap(); - closer.shutdown().await.unwrap(); - assert_eq!(closer.write(b"x").await.unwrap_err().kind(), io::ErrorKind::BrokenPipe); - let mut got = Vec::new(); - other.read_to_end(&mut got).await.unwrap(); - assert_eq!(got, b"bye"); - - other.write_all(b"still here").await.unwrap(); - other.shutdown().await.unwrap(); - let mut got = Vec::new(); - closer.read_to_end(&mut got).await.unwrap(); - assert_eq!(got, b"still here"); - } - } - - /// An abort is a reset carrying its code, which like TCP's may discard bytes still in - /// flight. - #[kio::test] - async fn abort_resets_with_its_code() { - for first in [true, false] { - let (a, b) = pair(); - let (mut aborter, mut other) = if first { (a, b) } else { (b, a) }; - aborter.write_all(b"partial").await.unwrap(); - aborter.abort(moq_net::Error::App(7)); - let err = other.read_to_end(&mut Vec::new()).await.unwrap_err(); - assert!(matches!(cause(&err), moq_net::Error::App(7)), "{err}"); - } - } - - /// Dropping a stream before shutting it down resets the peer, as a cancellation. - #[kio::test] - async fn dropping_resets_as_cancelled() { - let (a, mut b) = pair(); - drop(a); - let err = b.read(&mut [0; 4]).await.unwrap_err(); - assert!(matches!(cause(&err), moq_net::Error::Cancel), "{err}"); - } - - /// A shutdown that fails keeps failing, rather than reading as done the second time. - #[kio::test] - async fn a_failed_shutdown_stays_failed() { - let broadcast = moq_net::broadcast::Producer::new(Default::default()); - let track = broadcast.create_track("half", None).unwrap(); - let writer = track.create_group(group::Info { sequence: 0 }).unwrap(); - let mut a = TcpStream::new(writer, group().consume(), "b".into(), "s".into()); - // The track ending under the group fails its finish. - track.abort(moq_net::Error::App(3)).unwrap(); - for _ in 0..2 { - assert!(a.shutdown().await.is_err()); - } - assert!(a.write(b"x").await.is_err()); - } - - /// A reader that fell behind its window is reset, never handed a gap or an end. - #[kio::test] - async fn eviction_is_a_reset() { - let (mut a, mut b) = pair(); - // What a session's pump does when the peer's half shed a frame its reader needed. - let Write::Open(writer) = std::mem::replace(&mut a.writer, Write::Shut) else { - unreachable!("open"); - }; - writer.abort(moq_net::Error::Evicted).unwrap(); - let err = b.read(&mut [0; 4]).await.unwrap_err(); - assert!(matches!(cause(&err), moq_net::Error::Evicted), "{err}"); - } -} diff --git a/swift/Sources/Moq/Socket.swift b/swift/Sources/Moq/Socket.swift index 7dec5e5cd21..73f5e748ece 100644 --- a/swift/Sources/Moq/Socket.swift +++ b/swift/Sources/Moq/Socket.swift @@ -4,7 +4,7 @@ import MoqFFI public enum Socket {} extension Socket { - /// A server, a client, or both: dials or accepts independent TCP and track sessions. + /// A server, a client, or both: dials or accepts track sessions. public final class Endpoint: Sendable { let ffi: MoqSocketEndpoint @@ -21,59 +21,16 @@ extension Socket { /// Retract the endpoint's broadcasts and reset its pending dials and sessions. public func close() { ffi.close() } - /// Dial a new tcp session; cancelling resets this dial. - public func dialTcp(peer: String) async throws -> TcpStream { - TcpStream(try await ffi.dialTcp(peer: peer)) - } - /// Await the next tcp dial, independently of the other protocol. - public func acceptTcp() async throws -> IncomingTcp { - IncomingTcp(try await ffi.acceptTcp()) - } - /// 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, independently of the other protocol. + /// Await the next track dial. public func acceptTrack() async throws -> IncomingTrack { IncomingTrack(try await ffi.acceptTrack()) } } - /// An incoming tcp dial, consumed by accepting or rejecting it. - public final class IncomingTcp: Sendable { - let ffi: MoqSocketIncomingTcp - init(_ ffi: MoqSocketIncomingTcp) { 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() async throws -> TcpStream { - TcpStream(try await ffi.accept()) - } - /// Refuse this dial as not found. - public func reject() throws { try ffi.reject() } - } - - /// A tcp session's two halves and identities. - public final class TcpStream: Sendable { - /// This side's write half. - public let producer: GroupProducer - /// The peer's read half, retaining its subscription. - public let consumer: GroupConsumer - /// The peer endpoint's id. - public let peer: String - /// The private session id. - public let session: String - init(_ ffi: MoqSocketTcpStream) { - producer = GroupProducer(ffi.producer()) - consumer = GroupConsumer(ffi.consumer()) - peer = ffi.peer() - session = ffi.session() - } - } - /// An incoming track dial, consumed by accepting or rejecting it. public final class IncomingTrack: Sendable { let ffi: MoqSocketIncomingTrack diff --git a/test/interop/clients/js-native/socket.ts b/test/interop/clients/js-native/socket.ts index 8266dc48f4e..c7d12d34fb2 100644 --- a/test/interop/clients/js-native/socket.ts +++ b/test/interop/clients/js-native/socket.ts @@ -7,17 +7,11 @@ import { install } from "@moq/web-transport"; install(); console.debug = console.error; console.info = console.error; -const [role, address, prefix, protocol, version] = process.argv.slice(2); -assert( - address && - prefix !== undefined && - (role === "dial" || role === "accept") && - (protocol === "tcp" || protocol === "track") && - version, -); +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" ? (protocol === "tcp" ? endpoint.acceptTcp() : endpoint.acceptTrack()) : undefined; +const next = role === "accept" ? endpoint.acceptTrack() : undefined; const connection = await Moq.Connection.connect({ url: new URL(address), versions: [version], @@ -29,10 +23,9 @@ 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 { +async function accepted(): Promise { for (;;) { - const incoming: Moq.Socket.Incoming | Moq.Socket.Incoming = - await (protocol === "tcp" ? endpoint.acceptTcp() : endpoint.acceptTrack()); + const incoming = await endpoint.acceptTrack(); try { return await incoming.accept(); } catch { @@ -48,43 +41,26 @@ try { if (next) { (await next).reject(); } else { - await assert.rejects(protocol === "tcp" ? endpoint.dialTcp("acceptor") : endpoint.dialTrack("acceptor")); + await assert.rejects(endpoint.dialTrack("acceptor")); } - const stream = next - ? await accepted() - : protocol === "tcp" - ? await endpoint.dialTcp("acceptor") - : await 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; - if (stream instanceof Moq.Socket.TcpStream) { - // Exchange a request and reply before FIN, then drain both independent halves. - for (let i = 0; i < 3; i++) stream.producer.writeFrame({ payload: new Uint8Array([own + i]) }); - stream.producer.finish(); - for (let i = 0; i < 3; i++) - assert.deepEqual((await stream.consumer.readFrame())?.payload, new Uint8Array([peer + i])); - assert.equal(await stream.consumer.readFrame(), undefined); - } else { - // Exchange each independent message as a request and reply. - const reader = stream.consumer.ordered(); - for (let i = 0; i < 3; i++) { - if (role === "dial") stream.producer.writeFrame({ payload: new Uint8Array([own + i]) }); - 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]) }); - } - stream.producer.finish(); - assert.equal(await reader.readFrame(), undefined); + // Exchange each independent message as a request and reply. + const reader = stream.consumer.ordered(); + for (let i = 0; i < 3; i++) { + if (role === "dial") stream.producer.writeFrame({ payload: new Uint8Array([own + i]) }); + 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]) }); } - const second = next - ? await accepted() - : protocol === "tcp" - ? await endpoint.dialTcp("acceptor") - : await endpoint.dialTrack("acceptor"); - const resetReader = second instanceof Moq.Socket.TcpStream ? second.consumer : second.consumer.ordered(); + 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") { second.producer.writeFrame({ payload: new Uint8Array([48]) }); await assert.rejects(resetReader.readFrame()); @@ -92,7 +68,7 @@ try { assert.deepEqual((await resetReader.readFrame())?.payload, new Uint8Array([48])); second.producer.abort(new Moq.Error.Stream(Moq.StreamCode(70))); } - console.error(`socket verified ${protocol} ${role}`); + console.error(`socket verified ${role}`); if (role === "accept") assert.equal((await commands.next()).value, "socket drained"); } finally { lines.close(); diff --git a/test/interop/clients/python/sockets.py b/test/interop/clients/python/sockets.py index fc9a121e469..5241ba0d9e1 100644 --- a/test/interop/clients/python/sockets.py +++ b/test/interop/clients/python/sockets.py @@ -1,4 +1,4 @@ -"""Python/Rust socket interop through a relay, in both directions and protocols.""" +"""Python/Rust socket interop through a relay, in both directions.""" import argparse import asyncio @@ -27,23 +27,17 @@ async def run() -> T: return task -async def exchange(stream: socket.TcpStream | socket.TrackStream) -> None: +async def exchange(stream: socket.TrackStream) -> None: assert stream.peer == "rust" - if isinstance(stream, socket.TcpStream): + assert stream.consumer.info().max_age is None + for _ in range(3): + group = stream.producer.append_group() for payload in PAYLOADS: - stream.producer.write_frame(payload) - stream.producer.finish() - assert [frame.payload async for frame in stream.consumer] == PAYLOADS - else: - assert stream.consumer.info().max_age is None - for _ in range(3): - group = stream.producer.append_group() - for payload in PAYLOADS: - group.write_frame(payload) - group.finish() - stream.producer.finish() - received = [(group.sequence, [frame.payload async for frame in group]) async for group in stream.consumer] - assert received == [(sequence, PAYLOADS) for sequence in range(3)], received + group.write_frame(payload) + group.finish() + stream.producer.finish() + received = [(group.sequence, [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: @@ -51,46 +45,42 @@ async def main(args: argparse.Namespace) -> None: async with moq.Client( args.url, publish=origin, consume=origin, versions=[args.version], tls=moq.ClientTls(insecure=True) ): - for protocol in ("tcp", "track"): - for role in ("dial", "accept"): - prefix = f"{args.prefix}/{protocol}/{role}" - async with socket.Endpoint(origin, prefix, "python") as endpoint: - incoming = None - if role == "dial": - incoming = await start(endpoint.accept_tcp() if protocol == "tcp" else endpoint.accept_track()) - peer = await asyncio.create_subprocess_exec( - args.rust, - args.url, - prefix, - args.version, - protocol, - 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_tcp("rust") if protocol == "tcp" else 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 {protocol}, 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() + 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__": diff --git a/test/interop/clients/rust/socket-js.rs b/test/interop/clients/rust/socket-js.rs index d7162fe7b1d..aa960e7d1d7 100644 --- a/test/interop/clients/rust/socket-js.rs +++ b/test/interop/clients/rust/socket-js.rs @@ -6,32 +6,22 @@ use tokio::io::AsyncBufReadExt; async fn main() -> anyhow::Result<()> { let args: Vec<_> = std::env::args().collect(); ensure!( - args.len() == 6, - "usage: interop-socket-js dial|accept URL PREFIX tcp|track VERSION" + 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 tcp = match args[4].as_str() { - "tcp" => true, - "track" => false, - _ => anyhow::bail!("unknown protocol"), - }; let mut config = moq_tokio::connect::Config::default(); - config.version.push(args[5].parse().map_err(anyhow::Error::msg)?); + 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 { - let waiter = kio::Waiter::noop(); - if tcp { - ensure!(endpoint.poll_accept_tcp(&waiter).is_pending()); - } else { - ensure!(endpoint.poll_accept_track(&waiter).is_pending()); - } + ensure!(endpoint.poll_accept_track(&kio::Waiter::noop()).is_pending()); } let connection = client .clone() @@ -45,116 +35,63 @@ async fn main() -> anyhow::Result<()> { } // Probe refusal before data sessions can generate late queries for closed halves. if dial { - if tcp { - ensure!( - endpoint.dial_tcp("acceptor", 0)?.await.is_err(), - "rejected dial connected" - ); - } else { - ensure!( - endpoint.dial_track("acceptor", None)?.await.is_err(), - "rejected dial connected" - ); - } - } else if tcp { - endpoint.accept_tcp().await?.reject(); + 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 _hold: Box = if tcp { - let mut stream = if dial { - endpoint.dial_tcp("acceptor", 0)?.await? - } else { - accepted_tcp(&endpoint).await? - }; - ensure!(stream.peer() == if dial { "acceptor" } else { "dialer" }); - ensure!(stream.session().len() == 32); - for i in 0..3 { + 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(None, vec![own + i])?; } - let finished = stream.producer.finish()?; - for i in 0..3 { - let frame = stream.consumer.read_frame().await?.context("short TCP half")?; - ensure!(frame.payload.as_ref() == [peer + i], "wrong TCP frame"); + let mut group = reader.next_group().await?.context("short track half")?; + ensure!(group.sequence == u64::from(i), "wrong group sequence"); + 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(None, vec![own + i])?; } - ensure!(stream.consumer.read_frame().await?.is_none(), "extra TCP frame"); - Box::new((finished, stream.consumer)) + } + 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 { - let mut stream = if dial { - endpoint.dial_track("acceptor", None)?.await? - } else { - accepted_track(&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(None, vec![own + i])?; - } - let mut group = reader.next_group().await?.context("short track half")?; - ensure!(group.sequence == u64::from(i), "wrong group sequence"); - 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(None, vec![own + i])?; - } - } - let finished = stream.producer.finish()?; - ensure!(reader.next_group().await?.is_none(), "extra track frame"); - Box::new((finished, reader)) + accepted(&endpoint).await? }; - eprintln!("socket data verified {} {}", args[4], args[1]); - if tcp { - let mut reset = if dial { - endpoint.dial_tcp("acceptor", 0)?.await? - } else { - accepted_tcp(&endpoint).await? - }; - if dial { - reset.producer.write_frame(None, vec![48])?; - ensure!(reset.consumer.read_frame().await.is_err(), "TCP RST became FIN"); - eprintln!("socket reset verified tcp dial"); - } else { - ensure!( - reset - .consumer - .read_frame() - .await? - .context("missing reset signal")? - .payload - .as_ref() == [48] - ); - reset.producer.abort(moq_net::Error::App(6))?; - } + let mut reset_reader = reset.consumer.ordered(); + if dial { + reset.producer.write_frame(None, vec![48])?; + ensure!(reset_reader.next_group().await.is_err(), "track RST became FIN"); + eprintln!("socket reset verified dial"); } else { - let mut reset = if dial { - endpoint.dial_track("acceptor", None)?.await? - } else { - accepted_track(&endpoint).await? - }; - let mut reader = reset.consumer.ordered(); - if dial { - reset.producer.write_frame(None, vec![48])?; - ensure!(reader.next_group().await.is_err(), "track RST became FIN"); - eprintln!("socket reset verified track dial"); - } else { - let mut group = reader.next_group().await?.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))?; - } + let mut group = reset_reader.next_group().await?.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[4], args[1]); + eprintln!("socket verified {}", args[1]); if !dial { let mut lines = tokio::io::BufReader::new(tokio::io::stdin()).lines(); ensure!( @@ -162,6 +99,8 @@ async fn main() -> anyhow::Result<()> { "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; @@ -169,16 +108,7 @@ async fn main() -> anyhow::Result<()> { } // An Incoming's claimed original dial can disappear before verification. -async fn accepted_tcp(endpoint: &moq_net::socket::Endpoint) -> anyhow::Result { - loop { - let incoming = endpoint.accept_tcp().await?; - if let Ok(stream) = incoming.accept(0).await { - return Ok(stream); - } - } -} - -async fn accepted_track(endpoint: &moq_net::socket::Endpoint) -> anyhow::Result { +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 { diff --git a/test/interop/clients/rust/socket.rs b/test/interop/clients/rust/socket.rs index 3fa3302265f..1ccb48fbac2 100644 --- a/test/interop/clients/rust/socket.rs +++ b/test/interop/clients/rust/socket.rs @@ -20,10 +20,7 @@ async fn group(group: &mut moq_net::group::Consumer) -> anyhow::Result<()> { #[tokio::main(flavor = "current_thread")] async fn main() -> anyhow::Result<()> { let args: Vec<_> = std::env::args().collect(); - ensure!( - args.len() == 6, - "usage: interop-socket URL PREFIX VERSION tcp|track dial|accept" - ); + 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); @@ -36,67 +33,40 @@ async fn main() -> anyhow::Result<()> { .connect(args[1].parse::()?) .established() .await?; - let dial = args[5] == "dial"; - ensure!(dial || args[5] == "accept", "unknown role"); - match args[4].as_str() { - "tcp" => { - let mut stream = if dial { - endpoint.dial_tcp("python", 0)?.await? - } else { - let mut incoming = Box::pin(endpoint.accept_tcp()); - std::future::poll_fn(|cx| { - assert!(incoming.as_mut().poll(cx).is_pending()); - Poll::Ready(()) - }) - .await; - println!("listening"); - incoming.await?.accept(0).await? - }; - ensure!(stream.peer() == "python", "wrong peer"); - for payload in PAYLOADS { - stream.producer.write_frame(None, payload)?; - } - stream.producer.finish()?; - group(&mut stream.consumer).await?; - println!("drained"); - ack().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(); + for _ in 0..3 { + let mut group = producer.append_group()?; + for payload in PAYLOADS { + group.write_frame(None, payload)?; } - "track" => { - 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(); - for _ in 0..3 { - let mut group = producer.append_group()?; - for payload in PAYLOADS { - group.write_frame(None, payload)?; - } - group.finish()?; - } - let _producer = producer.finish()?; - for sequence in 0..3 { - let mut next = consumer.next_group().await?.context("short track")?; - ensure!(next.sequence == sequence, "wrong group order"); - group(&mut next).await?; - } - ensure!(consumer.next_group().await?.is_none(), "extra group"); - println!("drained"); - ack().await?; - } - _ => anyhow::bail!("unknown protocol"), + group.finish()?; + } + let _producer = producer.finish()?; + for sequence in 0..3 { + let mut next = consumer.next_group().await?.context("short track")?; + ensure!(next.sequence == sequence, "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(()) diff --git a/test/interop/interop.sh b/test/interop/interop.sh index 85d842df828..fbd959d6539 100755 --- a/test/interop/interop.sh +++ b/test/interop/interop.sh @@ -2597,7 +2597,7 @@ run_hls_grid() { } # This checkout's CLI FETCH and finite track tails in this pass, then the lite drop lanes. -# Both roles and protocols share this lane; Python acknowledges Rust's drain before teardown. +# 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 @@ -2611,7 +2611,7 @@ run_sockets() { overall=1 return fi - echo " PASS Python/Rust sockets ($version), both roles and protocols" + echo " PASS Python/Rust sockets ($version), both roles" record pass case="Python Rust sockets" publisher=python subscriber=rust log="$log" } diff --git a/test/interop/socket.sh b/test/interop/socket.sh index 98edff2f4ad..265fdf5fd7f 100644 --- a/test/interop/socket.sh +++ b/test/interop/socket.sh @@ -21,10 +21,10 @@ run_socket_acceptor() { } run_socket_pair() { - local dialer="$1" acceptor="$2" protocol="$3" version="$4" started=$SECONDS from pid dial_pid ready - local name="socket-$protocol-$dialer-$acceptor-$version" prefix="socket-$protocol-$dialer-$acceptor-$version-$$-$RANDOM" + 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 $protocol" publisher="$dialer" subscriber="$acceptor" version="$version" log="$LOGS/$name-dial.log") + 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 @@ -35,18 +35,18 @@ run_socket_pair() { 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" "$protocol" "$version" + "$(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" "$protocol" "$version" + "$(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 $protocol dial" "$LOGS/$name-dial.log"; then + 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 $protocol accept" "$LOGS/$name-accept.log" && check_versions "$name" "$from" "$version"; then - echo " PASS socket $protocol $dialer -> $acceptor $version" + 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" @@ -54,7 +54,7 @@ run_socket_pair() { fi fi fi - echo " FAIL socket $protocol $dialer -> $acceptor $version" + echo " FAIL socket $dialer -> $acceptor $version" record fail "${cell[@]}" seconds=$((SECONDS - started)) detail="socket handshake, payload, or FIN failed" overall=1 harness_reap "$pid" @@ -65,20 +65,18 @@ run_socket_pair() { } socket_pairs() { - local action="$1" runtime protocol + local action="$1" runtime local version=moq-lite-07-wip for runtime in js-native-node js-native-bun; do needs "$runtime" || continue - for protocol in tcp track; do - "$action" rust "$runtime" "$protocol" "$version" - "$action" "$runtime" rust "$protocol" "$version" - done + "$action" rust "$runtime" "$version" + "$action" "$runtime" rust "$version" done } # shellcheck disable=SC2329 # called by socket_pairs plan_socket_pair() { - record "not run" case="socket $3" publisher="$1" subscriber="$2" version="$4" + record "not run" case="socket" publisher="$1" subscriber="$2" version="$3" } plan_js_sockets() {