diff options
Diffstat (limited to 'src/daemon.zig')
| -rw-r--r-- | src/daemon.zig | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/daemon.zig b/src/daemon.zig index 18b22f5c..93cc8fa1 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -434,7 +434,9 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no } } -fn writeAll(fd: c_int, bytes: []const u8) bool { +/// `bytes` sent whole on a socket, never raising SIGPIPE: false when the +/// peer is gone. +pub 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); @@ -445,10 +447,12 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { return true; } -/// A replica refused by a session that takes none (a window's, so far). +/// A replica's connection refused in words, and closed: by the daemon at +/// its hello, or by its session. A fresh connection's buffer is empty, so +/// this never blocks. pub fn refuseReplica(fd: c_int, why: []const u8) void { var out: [600]u8 = undefined; - if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} + if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(fd, bytes) else |_| {} _ = libc.close(fd); } @@ -972,10 +976,7 @@ const Daemon = struct { } fn refuse(r: *Replica, why: []const u8) void { - var out: [600]u8 = undefined; - // A fresh connection's buffer is empty: this never blocks. - if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(r.fd, bytes) else |_| {} - _ = libc.close(r.fd); + refuseReplica(r.fd, why); r.* = .{}; } |
