diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:51:07 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 09:54:56 -0300 |
| commit | 10cff7a2194aa7d7f4b65180e51522b4518fa601 (patch) | |
| tree | a102e01163f0a5089dc01c8bad3899a84a649cdb /src/replica.zig | |
| parent | 073032a573c8559e0807a15771cd369bb3ac0742 (diff) | |
| download | pardes-10cff7a2194aa7d7f4b65180e51522b4518fa601.tar.gz pardes-10cff7a2194aa7d7f4b65180e51522b4518fa601.zip | |
replica: a session's socket writes and refusals are daemon's writeAll and refuseReplica (its words clamped as the daemon's were), where replica.zig kept copies of writeAll, nosignal and a refusal of its own
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/replica.zig')
| -rw-r--r-- | src/replica.zig | 34 |
1 files changed, 9 insertions, 25 deletions
diff --git a/src/replica.zig b/src/replica.zig index 16ecc184..4ddb3bb4 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -28,6 +28,8 @@ const tape = @import("tape.zig"); const rollback = @import("rollback.zig"); const walk = @import("walk.zig"); const wire = @import("wire.zig"); +const daemon = @import("daemon.zig"); +const writeAll = daemon.writeAll; const screen_state = @import("screen_state.zig"); const c_heap = @import("c_heap"); const terminal = pardes.panes.terminal; @@ -1212,7 +1214,6 @@ fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { const libc = std.c; const log = std.log.scoped(.replica); -const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; /// What a client may leave unread, past its join's dump, before it is cut /// off: minutes of typing, a screenful of a flood's deltas many times over. const lag_max: usize = 64 << 20; @@ -1233,17 +1234,6 @@ fn gmtoff() i32 { return if (localtime_r(&secs, &tm) != null) @intCast(tm.gmtoff) else 0; } -fn writeAll(fd: c_int, bytes: []const u8) bool { - var at: usize = 0; - while (at < bytes.len) { - const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal); - if (n < 0 and libc.errno(n) == .INTR) continue; - if (n <= 0) return false; - at += @intCast(n); - } - return true; -} - /// `bytes` as dump pieces (1 MiB at most each). fn writePieces(fd: c_int, bytes: []const u8) bool { var at: usize = 0; @@ -1762,7 +1752,7 @@ pub const Peers = struct { pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; /// A connection to join, and the view it joins again for, if it does. - pub const Joining = @import("daemon.zig").Joining; + pub const Joining = daemon.Joining; pub const Msg = struct { peer: *Peer, msg: []u8 }; pub fn count(ps: *const Peers) usize { @@ -1837,7 +1827,7 @@ pub const Peers = struct { for (ps.taken.items) |note| switch (note) { .join => |j| { ps.held.append(ps.gpa, j) catch { - refuse(j.fd, "out of memory"); + daemon.refuseReplica(j.fd, "out of memory"); continue; }; ps.joins(core); @@ -1908,12 +1898,6 @@ pub const Peers = struct { } } - fn refuse(fd: c_int, why: []const u8) void { - var out: [600]u8 = undefined; - if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} - _ = libc.close(fd); - } - /// The clients waiting to join, joined, once the texts the session /// never digested have their words (`walk.unhashed`): those are hashed /// on a thread of their own, pinned meanwhile, and the joins wait a @@ -1959,11 +1943,11 @@ pub const Peers = struct { } const slot = for (&ps.list) |*s| { if (s.* == null) break s; - } else return refuse(fd, "that session has as many clients as it takes"); + } else return daemon.refuseReplica(fd, "that session has as many clients as it takes"); live_peers = ps; if (!ps.recording) { // A session recording to a file (PARDES_RECORD) has its tape. - if (tape.on != null) return refuse(fd, "that session records a tape of its own (PARDES_RECORD)"); + if (tape.on != null) return daemon.refuseReplica(fd, "that session records a tape of its own (PARDES_RECORD)"); ps.t = tape.memory(ps.gpa); ps.t.unit_digests = true; // Terminals cross as screen state: a unit carries no pty bytes, @@ -1986,7 +1970,7 @@ pub const Peers = struct { const k = (if (back) joining.view else core.openView()) orelse { core.unattended = ps.count() == 0 and ps.alone_unattended; if (ps.count() == 0) ps.stop(core); - return refuse(fd, "that session has as many views as it takes"); + return daemon.refuseReplica(fd, "that session has as many views as it takes"); }; // Its own process draws it. core.undrawn |= @as(u8, 1) << @intCast(k); @@ -2003,7 +1987,7 @@ pub const Peers = struct { ps.unit(0, ps.seq); if (ps.count() == 0) ps.stop(core); var why: [96]u8 = undefined; - return refuse(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped"); + return daemon.refuseReplica(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped"); }; var whole = bytes.len; for (blobs.items) |b| { @@ -2019,7 +2003,7 @@ pub const Peers = struct { for (blobs.items) |b| pardes.memory.unpin(b.bytes); blobs.deinit(core.gpa); core.gpa.free(bytes); - return refuse(fd, "out of memory"); + return daemon.refuseReplica(fd, "out of memory"); }; peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; ps.next_id +%= 1; |
