// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! A `transport.Session` backed by a real QUIC connection. //! //! The whole of what this file does is forward: `quic_io.Session` already //! offers accepting, opening, reading and writing streams, and //! `transport.Session` is those operations with the QUIC taken out of their //! names. What it buys is that the HTTP/3 driver above it names none of this //! -- no `quic_io`, no socket, no datagram -- so the driver can be handed a //! scripted session in a test, the way the HTTP/1.1 driver is handed a //! scripted byte stream. const std = @import("std"); const Io = std.Io; const quic = @import("quic"); const quic_io = @import("quic_io"); const http = @import("http"); const transport = http.transport; /// Wraps one `quic_io.Session` as a `transport.Session`. pub fn session(s: *quic_io.Session) transport.Session { return .{ .context = s, .vtable = &vtable }; } const vtable: transport.Session.VTable = .{ .waitEstablished = waitEstablished, .accept = accept, .openUnidirectional = openUnidirectional, .openBidirectional = openBidirectional, .streamIn = streamIn, .writeOut = writeOut, .finish = finish, .finishWith = finishWith, .resetStream = resetStream, .stopSending = stopSending, .close = close, }; fn cast(context: ?*anyopaque) *quic_io.Session { return @ptrCast(@alignCast(context.?)); } fn waitEstablished(context: ?*anyopaque) bool { return cast(context).waitEstablished(); } fn accept(context: ?*anyopaque) ?quic.stream.Id { const stream = cast(context).accept() orelse return null; return stream.id; } fn openUnidirectional(context: ?*anyopaque) transport.Session.OpenError!quic.stream.Id { const stream = cast(context).openUnidirectional() catch return error.StreamUnavailable; return stream.id; } fn openBidirectional(context: ?*anyopaque) transport.Session.OpenError!quic.stream.Id { const stream = cast(context).openBidirectional() catch return error.StreamUnavailable; return stream.id; } fn streamIn( context: ?*anyopaque, id: quic.stream.Id, w: *Io.Writer, limit: Io.Limit, ) Io.Reader.StreamError!usize { const s = cast(context); // `quic_io.Stream.Reader` holds a buffer so that it can be an // `Io.Reader`; here the caller above the seam already has one, so the // reader is built around nothing and used only for the one call that // moves octets. Nothing is buffered in it between calls. var stream: quic_io.Stream = .{ .session = s, .id = id }; var reader = stream.reader(&.{}); return reader.interface.vtable.stream(&reader.interface, w, limit); } fn writeOut(context: ?*anyopaque, id: quic.stream.Id, bytes: []const u8) Io.Writer.Error!void { return cast(context).writeStream(id, bytes) catch error.WriteFailed; } fn finish(context: ?*anyopaque, id: quic.stream.Id) Io.Writer.Error!void { const s = cast(context); var stream: quic_io.Stream = .{ .session = s, .id = id }; var writer = stream.writer(&.{}); return writer.finish(); } /// The last octets and the FIN together, which is one packet rather than two /// and is what every other QUIC implementation puts on the wire. fn finishWith(context: ?*anyopaque, id: quic.stream.Id, bytes: []const u8) Io.Writer.Error!void { return cast(context).writeStreamFinished(id, bytes) catch error.WriteFailed; } fn resetStream(context: ?*anyopaque, id: quic.stream.Id, code: u64) void { // A reset the session cannot send is a session that has already gone, and // there is nothing left to tell the peer. cast(context).resetStream(id, code) catch {}; } fn stopSending(context: ?*anyopaque, id: quic.stream.Id, code: u64) void { cast(context).stopSending(id, code) catch {}; } fn close(context: ?*anyopaque, code: u64, why: []const u8) void { cast(context).closeApplication(code, why); }