summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig64
1 files changed, 48 insertions, 16 deletions
diff --git a/core/replica.zig b/core/replica.zig
index bf665a50..e1eacf6c 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -1887,6 +1887,16 @@ pub const Peer = struct {
peer.cond.signal(io);
}
+ /// Its last message: queued, and its writer ends once it is written.
+ fn last(peer: *Peer, words: []const u8) void {
+ const io = peer.peers.io;
+ peer.send(&.{words});
+ peer.lock.lockUncancelable(io);
+ peer.closing = true;
+ peer.cond.signal(io);
+ peer.lock.unlock(io);
+ }
+
fn writeLoop(peer: *Peer) void {
const io = peer.peers.io;
defer peer.done.store(true, .release);
@@ -2404,14 +2414,14 @@ pub const Peers = struct {
if (ps.count() == 0) return;
var out: [600]u8 = undefined;
const words = wire.encodeServer(&out, .{ .refused = why }) catch return;
- const io = ps.io;
- for (ps.list) |slot| if (slot) |peer| {
- peer.send(&.{words});
- peer.lock.lockUncancelable(io);
- peer.closing = true;
- peer.cond.signal(io);
- peer.lock.unlock(io);
- };
+ for (ps.list) |slot| if (slot) |peer| peer.last(words);
+ ps.flushed200();
+ // Each joins again for its view, kept in the dump (views.away).
+ ps.drop(core, true);
+ }
+
+ /// What each client was sent goes first, for up to 200 ms.
+ fn flushed200(ps: *Peers) void {
const until = nowNs() + 200 * std.time.ns_per_ms;
while (nowNs() < until) {
const busy = for (ps.list) |slot| {
@@ -2421,8 +2431,6 @@ pub const Peers = struct {
const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
}
- // Each joins again for its view, kept in the dump (views.away).
- ps.drop(core, true);
}
/// A join's dump and pins let go: written, or its client gone.
@@ -2445,16 +2453,26 @@ pub const Peers = struct {
daemon.clients(pids[0..n]);
}
- /// Detach typed in client view `k`'s: its link ends (its reader says
- /// it is gone, and the next boundary lets it go).
+ /// Detach typed in client view `k`'s: told it left the session running
+ /// (`left`), its last word; the client goes, and its reader says so.
pub fn detach(ps: *Peers, k: u8) bool {
for (ps.list) |slot| if (slot) |peer| if (peer.view == k) {
- _ = libc.shutdown(peer.fd, 2);
+ var out: [wire.max_name + 8]u8 = undefined;
+ peer.last(wire.encodeServer(&out, .{ .left = ps.name[0..@min(ps.name.len, wire.max_name)] }) catch unreachable);
return true;
};
return false;
}
+ /// The session ends with `code`: every client told so (`ended`), its
+ /// last word, before the links are let go (`dropAll`).
+ pub fn ended(ps: *Peers, code: u8) void {
+ var out: [16]u8 = undefined;
+ const words = wire.encodeServer(&out, .{ .ended = code }) catch unreachable;
+ for (ps.list) |slot| if (slot) |peer| peer.last(words);
+ ps.flushed200();
+ }
+
fn stop(ps: *Peers, core: *Pardes) void {
if (!ps.recording) return;
pardes.panes.resizeDeferred(core);
@@ -2919,7 +2937,7 @@ pub fn bye(fd: c_int, again: bool) void {
/// What the session streams a client after its dump, as its frontend's loop
/// takes it: a unit's and a terminal message's bytes and a refusal's words
/// on the C heap, the loop's to free.
-pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8 };
+pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8, ended: u8, left };
/// The session's stream off `fd`, each message handed to `post` until the
/// link ends (a refusal ends it too); `post` says whether it took it (one
@@ -2934,13 +2952,16 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime
.blob => .{ .blob = std.heap.c_allocator.dupe(u8, payload) catch return false },
.check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check },
.refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false },
+ .ended => .{ .ended = (wire.decodeServer(tag, payload) catch return false).ended },
+ .left => .left,
.pong, .welcome, .dump, .blobs, .zstd => return true,
};
if (!post(x, msg)) switch (msg) {
.unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b),
- .checked => {},
+ .checked, .ended, .left => {},
};
- return msg != .refused;
+ // A last word ends the stream.
+ return msg == .unit or msg == .screen or msg == .checked or msg == .blob;
}
};
rest.run(gpa, fd, ctx, Take.take);
@@ -3078,6 +3099,9 @@ pub const Link = struct {
visible: u32 = 0,
/// What the session said as it let this client go (the C heap's).
refused: ?[]u8 = null,
+ /// Its last word: it ended, with this code; or this client left it.
+ ended: ?u8 = null,
+ left: bool = false,
/// A line over its frame, until its next key (`notify`): why it joined
/// again. The client's own, never the session's core.
notice_buf: [240]u8 = undefined,
@@ -3196,6 +3220,14 @@ pub const Link = struct {
c.refused = words;
c.quit = true;
},
+ .ended => |code| {
+ c.ended = code;
+ c.quit = true;
+ },
+ .left => {
+ c.left = true;
+ c.quit = true;
+ },
}
if (c.r.diverged != null) c.quit = true;
}