diff options
Diffstat (limited to 'src/detached/wire.zig')
| -rw-r--r-- | src/detached/wire.zig | 155 |
1 files changed, 119 insertions, 36 deletions
diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 20d75274..097cbee7 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -1,13 +1,41 @@ -//! The framed stream a remote client will speak (docs/next-daemon.md, -//! phase 5): fixed-width little-endian fields and explicit tags, the hello -//! and its version, and the input Events. Native struct layout never reaches -//! the wire. The cell frames that went the other way, and the frontends that -//! drew them, went with phase 3. +//! The framed stream a replica speaks (docs/next-daemon.md, phase 5), on +//! the daemon's `pardes-replica.sock` and then its session's: fixed-width +//! little-endian fields and explicit tags, the hello and its version, a +//! ping, and the input Events. Native struct layout never reaches the wire. +//! The cell frames that went the other way, and the frontends that drew +//! them, went with phase 3. const std = @import("std"); +const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); +const config = @import("pardes_config"); -/// 13: the frames, and every message a session sent, are gone (phase 3). -pub const version: u16 = 13; +/// 14: the hello names a session and carries the semantics hash, and a +/// ping is answered (phase 5's groundwork); 13: the frames went (phase 3). +pub const version: u16 = 14; + +/// What makes two cores step differently, hashed: a replica's hello carries +/// its own, and one of another build is refused. The protocol, the release +/// and commit, the platform and build options, every dependency's content +/// hash (grammars, ghostty, Unicode) and the walker codec's assumptions +/// (pointer width, byte order). Not the OS: a Linux client of a FreeBSD +/// session is the main case, and what differs there is the host's, taped. +/// ponytail: an unstamped build (Debug with -Dplatform, every test build) +/// has no commit, so two dev trees of one release are told apart only by +/// their options and dependencies; -Dstamp-commit stamps one. +pub const semantics: u64 = blk: { + @setEvalBranchQuota(1_000_000); + break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint( + "wire {d}\npardes {s} {s}\n{t} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}", + .{ + version, config.version, config.commit orelse "unstamped", + config.platform, config.syntax_highlighting, config.syntax_zig_grammar, + config.syntax_minimal_grammars, config.syntax_full_grammars, config.mupdf, + config.theme_animation, config.workspace_tag, config.embed_sources, + config.esp32p4_cols, config.esp32p4_rows, @sizeOf(usize), + builtin.cpu.arch.endian(), config.deps, + }, + )); +}; /// EndOfStream: the message ends early; WriteFailed: no room for it. /// ReadFailed is in only because std.Io.Reader says so: a fixed one never @@ -23,6 +51,7 @@ pub const header_len = 5; // tag:u8, payload length:u32le pub const ClientTag = enum(u8) { hello = 0x01, bye = 0x02, + ping = 0x03, key = 0x10, mouse = 0x11, @@ -59,12 +88,50 @@ const KindTag = enum(u8) { press = 0x00, release = 0x01, motion = 0x02, drag = 0 /// when the rest of the layout is the part that changed. pub const Hello = struct { version: u16 = version, - /// This client's grid. Never zero: a client not sized yet must not - /// collapse a shared session (getCols). - cols: u16, - rows: u16, + /// The client's `semantics`: the daemon refuses it unless the session's + /// is the same. + semantics: u64 = semantics, + /// Which session (its 9P name, as `--attach=` names it). + session: []const u8, }; +/// A session's name on the wire, as the daemon keeps it. +pub const max_name = 128; +/// The longest hello there is, header and all. +pub const hello_max = header_len + 2 + 8 + 2 + max_name; + +/// Daemon or session -> client. +pub const ServerTag = enum(u8) { + /// The hello is accepted and the connection is the session's now: its pid + /// and name. + welcome = 0x01, + /// Refused, said in words; the connection ends. + refused = 0x02, + /// A ping's value back. + pong = 0x03, +}; + +pub const ServerMsg = union(ServerTag) { + welcome: struct { pid: u32, session: []const u8 }, + refused: []const u8, + pong: u64, +}; + +pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { + var w: std.Io.Writer = .fixed(out); + const at = try beginMessage(&w, @backingInt(std.meta.activeTag(msg))); + switch (msg) { + .welcome => |h| { + try w.writeInt(u32, h.pid, .little); + try putSlice16(&w, h.session); + }, + .refused => |words| try putSlice16(&w, words), + .pong => |v| try w.writeInt(u64, v, .little), + } + try finishMessage(&w, at); + return w.buffered(); +} + /// `Hello.version` alone, read out of the still-undecoded payload. /// /// Separate from `decodeClient` because of the guarantee the fixed offset above @@ -88,6 +155,8 @@ pub fn helloVersion(payload: []const u8) Error!u16 { pub const ClientMsg = union(enum) { hello: Hello, bye, + /// A round trip's probe, answered `pong` with the same value. + ping: u64, event: pardes.Event, }; @@ -291,10 +360,12 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { switch (msg) { .hello => |h| { try w.writeInt(u16, h.version, .little); - try w.writeInt(u16, h.cols, .little); - try w.writeInt(u16, h.rows, .little); + try w.writeInt(u64, h.semantics, .little); + if (h.session.len > max_name) return error.Overlong; + try putSlice16(&w, h.session); }, .bye => {}, + .ping => |v| try w.writeInt(u64, v, .little), .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -352,10 +423,15 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { const msg: ClientMsg = switch (std.enums.fromInt(ClientTag, tag) orelse return error.BadTag) { .hello => .{ .hello = .{ .version = try r.takeInt(u16, .little), - .cols = try getCols(&r), - .rows = try getRows(&r), + .semantics = try r.takeInt(u64, .little), + .session = blk: { + const n = try r.takeInt(u16, .little); + if (n > max_name) return error.BadValue; + break :blk try r.take(n); + }, } }, .bye => .bye, + .ping => .{ .ping = try r.takeInt(u64, .little) }, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -415,7 +491,8 @@ const msg_slack = header_len + 64; /// once instead of guessing and retrying. pub fn clientBound(msg: ClientMsg) usize { return msg_slack + switch (msg) { - .hello, .bye => 0, + .hello => |h| h.session.len, + .bye, .ping => 0, .event => |ev| switch (ev) { .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0, .key => |k| k.text.len, @@ -458,12 +535,21 @@ test "detached wire: every Event variant round-trips" { try testing.expectEqual(@as(u8, 0x1d), @backingInt(ClientTag.pointer_leave)); { - const got = try roundClient(&buf, .{ .hello = .{ .cols = 80, .rows = 24 } }); + const got = try roundClient(&buf, .{ .hello = .{ .session = "work" } }); try testing.expectEqual(version, got.hello.version); - try testing.expectEqual(@as(u16, 80), got.hello.cols); - try testing.expectEqual(@as(u16, 24), got.hello.rows); + try testing.expectEqual(semantics, got.hello.semantics); + try testing.expectEqualStrings("work", got.hello.session); } try testing.expectEqual(ClientMsg.bye, try roundClient(&buf, .bye)); + try testing.expectEqual(@as(u64, 0xfeed_beef_0000_0001), (try roundClient(&buf, .{ .ping = 0xfeed_beef_0000_0001 })).ping); + { + // The other way: framed the same, its tag the ServerTag's number. + const bytes = try encodeServer(&buf, .{ .welcome = .{ .pid = 42, .session = "work" } }); + const f = (try framed(bytes)).?; + try testing.expectEqual(@as(u8, 0x01), f.tag); + try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k' }, f.payload); + try testing.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag); + } { const key: pardes.Key = .{ .cp = pardes.Key.page_down, .text = "ü", .ctrl = true, .alt = false, .shift = true }; @@ -530,20 +616,19 @@ test "detached wire: a session is never told to do a frontend's remembering" { // Adding a name here is meant to be an ARGUMENT, not a formality, and the // bar is one sentence: A HUMAN DID IT. A keystroke, a click, a pinch, a // window resized, a paste, a command line executed, a pointer leaving the - // window, the window's focus — plus the two session words that say who is speaking. A pty's + // window, the window's focus — plus the session words that say who is speaking, time the link and end it. A pty's // output, a worker's answer, a watched file's new bytes and an animation // tick all fail that bar the same way: nobody did them, a machine reported // them, and the machine that reports them is the one already holding the // core. const allowed = [_][]const u8{ - // Who this connection is, and that it is finished. - "hello", "bye", + // Who this connection is, its round trip, and that it is finished. + "hello", "bye", "ping", // ...and everything a person can do to a window. - "key", "mouse", - "resize", "paste", - "command", "pdf_scroll", - "pinch", "touch_scroll", - "pointer_leave", "focus", + "key", "mouse", "resize", + "paste", "command", "pdf_scroll", + "pinch", "touch_scroll", "pointer_leave", + "focus", }; // One: the tag set is exactly that, named rather than counted, so @@ -663,18 +748,16 @@ test "detached wire: values a field cannot mean are refused" { @backingInt(ClientTag.pdf_scroll), &.{pardes.MAX_PANES}, )); - // A zero-column grid would collapse a shared session; an over-wide one is - // past what this protocol carries. - try testing.expectError(error.BadValue, decodeClient( - @backingInt(ClientTag.hello), - &.{ 1, 0, 0, 0, 24, 0 }, - )); + // A session name longer than the daemon keeps one: refused, never cut + // to another session's name. { - var hello: [6]u8 = undefined; + var hello: [12 + max_name + 1]u8 = @splat('x'); std.mem.writeInt(u16, hello[0..2], version, .little); - std.mem.writeInt(u16, hello[2..4], max_cols + 1, .little); - std.mem.writeInt(u16, hello[4..6], 24, .little); + std.mem.writeInt(u16, hello[10..12], max_name + 1, .little); try testing.expectError(error.BadValue, decodeClient(@backingInt(ClientTag.hello), &hello)); + std.mem.writeInt(u16, hello[10..12], max_name, .little); + try testing.expectEqual(@as(usize, max_name), (try decodeClient(@backingInt(ClientTag.hello), hello[0 .. hello.len - 1])).hello.session.len); + try testing.expect(header_len + hello.len - 1 == hello_max); } // A NaN scroll distance. `pinch` multiplies into a zoom the pane keeps. { |
