summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig115
1 files changed, 106 insertions, 9 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 5c965b2b..7bf0574f 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -292,6 +292,7 @@ pub const Screens = struct {
enc: screen_state.Encoder,
tracks: [pardes.MAX_PANES]?Tracked = @splat(null),
peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .empty,
+ free_peers: std.ArrayListUnmanaged(u16) = .empty,
out: std.ArrayListUnmanaged(u8) = .empty,
/// Nanoseconds: what each flush spent encoding, for every peer.
encode_ns: u64 = 0,
@@ -311,6 +312,7 @@ pub const Screens = struct {
s.gpa.destroy(p);
}
s.peers.deinit(s.gpa);
+ s.free_peers.deinit(s.gpa);
s.out.deinit(s.gpa);
s.enc.deinit();
}
@@ -323,12 +325,21 @@ pub const Screens = struct {
/// A new peer, with nothing yet: its first delta of each terminal
/// starts it afresh.
pub fn addPeer(s: *Screens) !u16 {
+ if (s.free_peers.pop()) |again| return again;
const p = try s.gpa.create([pardes.MAX_PANES]PeerOf);
p.* = @splat(.{});
try s.peers.append(s.gpa, p);
return @intCast(s.peers.items.len - 1);
}
+ /// Peer `peer` gone: what it was sent forgotten, its number free for
+ /// the next `addPeer` (a socket's clients come and go).
+ pub fn dropPeer(s: *Screens, peer: u16) void {
+ for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa);
+ s.peers.items[peer].* = @splat(.{});
+ s.free_peers.append(s.gpa, peer) catch {};
+ }
+
/// What `peer` is owed of every terminal, each a message onto `out`
/// (owned): nothing for one it has as it is, or with `bound` in flight.
pub fn flush(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void {
@@ -558,6 +569,8 @@ pub const Replica = struct {
const r = try gpa.create(Replica);
r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true };
r.t.headless = true;
+ // Its terminals are the session's deltas (Peers sends them).
+ r.t.screen_state = true;
errdefer {
tape.release(&r.t);
r.epoch.deinit();
@@ -572,6 +585,10 @@ pub const Replica = struct {
o.pdf_allocator = o.pdf_allocator orelse std.heap.c_allocator;
// The session's store and Recent are the authority's.
o.store_path = null;
+ // Its emulators off the epoch, as `terminal.create` makes a screen
+ // replica's: deltas write them while a copy is held.
+ pardes.dump.terminal_gpa = terminal.emulator_gpa;
+ defer pardes.dump.terminal_gpa = null;
const c = try pardes.dump.load(epoch, o, bytes, null);
// What loading queued (a watch, a theme asked for) is the
// authority's errand, done there already: the session was dumped
@@ -878,7 +895,7 @@ pub const Replica = struct {
/// Whether its core loaded from the dump is the session as it was
/// dumped, by the welcome's `digest`.
pub fn loaded(r: *Replica, want: u64) bool {
- return tape.digestOf(r.core()) == want;
+ return r.digest() == want;
}
/// Its own view drawn and presented through `present`, between steps
@@ -1109,6 +1126,8 @@ pub const Peer = struct {
fd: c_int,
id: u16,
view: u8,
+ /// Its number in the session's `Screens`.
+ sid: u16 = 0,
/// Inputs read (each one's seq, as the client numbered them).
seq: u32 = 0,
reader: ?std.Thread = null,
@@ -1181,9 +1200,13 @@ pub const Peer = struct {
peer.send(&.{pong});
return true;
},
- // ponytail: rterm's acks (src/screen_state.zig) go to its
- // session state once terminals cross as screens.
- .screen_ack => return true,
+ // Its word about a terminal (an ack, rows asked, a reset), to
+ // the session's Screens at the next boundary.
+ .screen_ack => {
+ const msg = peer.peers.gpa.dupe(u8, payload) catch return false;
+ peer.peers.post(.{ .screen = .{ .peer = peer, .msg = msg } });
+ return true;
+ },
.check => {
peer.peers.post(.{ .check = peer });
return true;
@@ -1241,8 +1264,13 @@ pub const Peers = struct {
boundary_ns: std.ArrayListUnmanaged(u64) = .empty,
cut: u32 = 0,
head: [unit_head]u8 = undefined,
+ /// Its clients' terminals as screen state (src/screen_state.zig), while
+ /// it records: what each was sent.
+ screens: Screens = undefined,
+ msgs: std.ArrayListUnmanaged([]u8) = .empty,
- pub const Note = union(enum) { join: c_int, input: struct { peer: *Peer, msg: []u8 }, gone: *Peer, check: *Peer };
+ pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer };
+ pub const Msg = struct { peer: *Peer, msg: []u8 };
pub fn count(ps: *const Peers) usize {
var n: usize = 0;
@@ -1256,7 +1284,7 @@ pub const Peers = struct {
ps.lock.unlock(ps.io);
switch (note) {
.join => |fd| _ = libc.close(fd),
- .input => |in| ps.gpa.free(in.msg),
+ .input, .screen => |in| ps.gpa.free(in.msg),
.gone, .check => {},
}
return;
@@ -1298,8 +1326,12 @@ pub const Peers = struct {
const t0 = nowNs();
defer if (ps.recording) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {};
if (ps.recording) {
+ // The rows the pump's steps read, then its unit, then what the
+ // terminals came to (the next steps read that).
+ ps.screensOut(core, .fill);
ps.seq += 1;
ps.unit(0, ps.seq);
+ ps.screensOut(core, .flush);
}
if (!ps.mail.load(.acquire)) return;
ps.lock.lockUncancelable(ps.io);
@@ -1312,6 +1344,13 @@ pub const Peers = struct {
ps.input(core, in.peer, in.msg);
ps.gpa.free(in.msg);
},
+ .screen => |in| {
+ defer ps.gpa.free(in.msg);
+ const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue;
+ defer ps.gpa.free(reply);
+ const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len);
+ in.peer.send(&.{ &head, reply });
+ },
.gone => |peer| ps.leave(core, peer),
.check => |peer| {
var out: [16]u8 = undefined;
@@ -1325,12 +1364,41 @@ pub const Peers = struct {
const in = parseInput(msg) orelse return;
const t0 = nowNs();
defer ps.input_ns.append(ps.gpa, nowNs() - t0) catch {};
+ // The terminals as this step reads them, then the step, the rows
+ // it read, its unit (part B terminal notes' order).
+ ps.screensOut(core, .flush);
core.toView(peer.view);
core.update(in.event);
core.step();
+ ps.screensOut(core, .fill);
ps.unit(peer.id, in.seq);
}
+ const ScreensOut = enum { flush, fill };
+
+ /// What each client is owed of the terminals (`Screens.flush`), or the
+ /// rows a step read that it lacks (`Screens.fill`, then `filled`), each
+ /// a screen message queued for it.
+ fn screensOut(ps: *Peers, core: *Pardes, comptime what: ScreensOut) void {
+ for (ps.list) |slot| if (slot) |peer| ps.screensTo(core, peer, what);
+ if (what == .fill) ps.screens.filled(core);
+ }
+
+ fn screensTo(ps: *Peers, core: *Pardes, peer: *Peer, comptime what: ScreensOut) void {
+ defer {
+ for (ps.msgs.items) |m| ps.gpa.free(m);
+ ps.msgs.clearRetainingCapacity();
+ }
+ (switch (what) {
+ .flush => ps.screens.flush(core, peer.sid, &ps.msgs),
+ .fill => ps.screens.fill(core, peer.sid, &ps.msgs),
+ }) catch return;
+ for (ps.msgs.items) |m| {
+ const head = wire.header(@backingInt(wire.ServerTag.screen), m.len);
+ peer.send(&.{ &head, m });
+ }
+ }
+
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 |_| {}
@@ -1348,6 +1416,11 @@ pub const Peers = struct {
if (tape.on != null) return refuse(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,
+ // and its digest is a word (PARDES_REPLICA_TRACE: one a field).
+ ps.t.screen_state = true;
+ ps.t.whole_digest = std.c.getenv("PARDES_REPLICA_TRACE") == null;
+ ps.screens = .init(ps.gpa);
tape.on = &ps.t;
ps.recording = true;
}
@@ -1402,6 +1475,11 @@ pub const Peers = struct {
return;
}
slot.* = peer;
+ // Its terminals afresh, after its dump and before any unit.
+ if (ps.screens.addPeer()) |sid| {
+ peer.sid = sid;
+ ps.screensTo(core, peer, .flush);
+ } else |_| _ = libc.shutdown(fd, 2);
log.info("client {d} joined '{s}' as view {d}: a {d} KB dump in {d} us", .{ peer.id, ps.name, k, bytes.len >> 10, (nowNs() - t0) / 1000 });
if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq });
}
@@ -1411,6 +1489,7 @@ pub const Peers = struct {
slot.* = null;
};
log.info("client {d} left '{s}'", .{ peer.id, ps.name });
+ ps.screens.dropPeer(peer.sid);
core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
core.closeView(peer.view);
if (ps.count() == 0) core.unattended = ps.alone_unattended;
@@ -1438,7 +1517,7 @@ pub const Peers = struct {
var i: usize = 0;
while (i < ps.inbox.items.len) {
const mine = switch (ps.inbox.items[i]) {
- .input => |in| in.peer == peer,
+ .input, .screen => |in| in.peer == peer,
.gone, .check => |p| p == peer,
.join => false,
};
@@ -1446,7 +1525,10 @@ pub const Peers = struct {
i += 1;
continue;
}
- if (ps.inbox.items[i] == .input) ps.gpa.free(ps.inbox.items[i].input.msg);
+ switch (ps.inbox.items[i]) {
+ .input, .screen => |in| ps.gpa.free(in.msg),
+ else => {},
+ }
_ = ps.inbox.orderedRemove(i);
}
ps.lock.unlock(io);
@@ -1465,6 +1547,7 @@ pub const Peers = struct {
fn stop(ps: *Peers, core: *Pardes) void {
if (!ps.recording) return;
+ ps.screens.deinit(core);
if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real;
tape.on = null;
tape.release(&ps.t);
@@ -1478,13 +1561,14 @@ pub const Peers = struct {
slot.* = null;
core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
core.closeView(peer.view);
+ ps.screens.dropPeer(peer.sid);
ps.letGo(peer);
};
ps.stop(core);
ps.lock.lockUncancelable(ps.io);
for (ps.inbox.items) |note| switch (note) {
.join => |fd| _ = libc.close(fd),
- .input => |in| ps.gpa.free(in.msg),
+ .input, .screen => |in| ps.gpa.free(in.msg),
.gone, .check => {},
};
ps.inbox.clearRetainingCapacity();
@@ -1498,6 +1582,7 @@ pub const Peers = struct {
ps.taken.deinit(ps.gpa);
ps.input_ns.deinit(ps.gpa);
ps.boundary_ns.deinit(ps.gpa);
+ ps.msgs.deinit(ps.gpa);
}
fn writeStats(ps: *Peers, path: []const u8) void {
@@ -1627,6 +1712,18 @@ pub fn sendInput(fd: c_int, msg: []const u8) bool {
return writeAll(fd, msg[head_len..]);
}
+/// A message `Replica.screen` or `screenRequest` made, to the session.
+pub fn sendScreen(fd: c_int, msg: []const u8) bool {
+ var buf: [64]u8 = undefined;
+ if (wire.header_len + msg.len > buf.len) {
+ const head = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len);
+ return writeAll(fd, &head) and writeAll(fd, msg);
+ }
+ buf[0..wire.header_len].* = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len);
+ @memcpy(buf[wire.header_len..][0..msg.len], msg);
+ return writeAll(fd, buf[0 .. wire.header_len + msg.len]);
+}
+
/// Asks the session's digest, answered in stream order.
pub fn askCheck(fd: c_int) bool {
var out: [16]u8 = undefined;