Skip to main content

Module relay

Module relay 

Source
Expand description

Generic transport-relay protocol — one pure server-side state machine for TCP and UDP, native and browser.

The pure model is generic over the target T a service resolves to: a SocketAddr natively, a WebTransport Url (string) in the browser. The same step, state, duplicate-Open rejection and owner-rejection serve both — only the interpreter differs (native NativeRelay over OS sockets, browser WtRelay over WebTransport). This is the code realization of “TCP/UDP/native/browser are one relay”.

Every session is identified by the owner-scoped key (from, namespace, session, initiator) (SessionKey). from is the authenticated sender (owner rejection: a peer can only name keys whose from is itself), and initiator records which end opened it — so a session a peer opened never collides with one we opened that got the same id (bidirectional-open safety). A frame’s from_opener flips to our initiator.

The reducer is the sole authority over the session set: Data/Shutdown/Close emit an effect only for a session in sessions (the engine never adjudicates liveness).

  S = (services : Name ⇀ T,  sessions : ℘ SessionKey,  next : ℕ)
  k = (from, namespace, session, init)        init = Remote if from_opener else Local
  step (Command(Register n t))                ↦ (S[services∪{n↦t}], ε)
  step (Command(Accepted tok peer svc))       ↦ (S[sessions∪{kₗ}, next+1], [OpenAccepted tok kₗ svc])
                                                  where kₗ=(peer,ns,next,Local)   ← core mints the id
  step (Command(Untrack k))                   ↦ (S[sessions∖{k}], ε)
  step (Frame(from, Open s n)) | k∈sessions   ↦ (S, ε)                            (duplicate)
                               | n∈services    ↦ (S∪{k}, [Connect k t kind])
                               | otherwise     ↦ (S, [SendClose s])
  step (Frame(from, Data s b)) | k∈sessions   ↦ (S, [Write k b])   else (S, ε)
  step (Frame(from, Close s))  | k∈sessions   ↦ (S∖{k}, [Close k]) else (S, ε)

Structs§

Relay
Transport relay protocol (server side), generic over the service target T.
RelayHandle
Client-facing handle to the relay extension’s live engine: open local tunnels and register local services. This is the relay extension’s own surface — the relay owns its engine and installs itself (install), so nothing about it leaks into the generic Provider (the same way SNARK registers itself). Cloneable; every clone drives the same shared engine and pure Relay state. Holds the two per-namespace scoped capabilities (tcp / udp); each method picks one and can only act within it, so the handle cannot address an arbitrary namespace even internally.
RelayState
Relay state: the service registry and the set of open (server-side, remote-opened) sessions. The live OS/WebTransport resources are the interpreter’s engine table; this is the protocol’s view used for owner-rejection and duplicate-Open rejection.

Enums§

RelayCommand
A local control command, re-injected by the provider (provenance = self; never sent by peers). Generic over the service target T.
RelayEffect
The relay’s own effect algebra (interpreted by NativeRelay / WtRelay).
RelayEvent
The relay’s typed input: a self-injected RelayCommand or an authenticated peer Frame. The from == me split is resolved in Relay::decode.

Constants§

TCP
Namespace for the TCP relay.
UDP
Namespace for the UDP relay.