authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-04 03:54:43 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-04 03:54:43 -08:00
logaf7ad60d8723ce5c97bf09d894318c24866261c1
treeeeca3802816722f5b9b9939c30e31bbef336133c
parentf0f0855830c4a62db74f61412172407b2a7dfa47
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add Stream.sendfile()


2 files changed, 14 insertions(+), 0 deletions(-)

net.zig+13
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const builtin = @import("builtin");
33const nio = @import("nio");
4const nfs = @import("nfs");
45const sys_linux = @import("sys-linux");
56
67const os = builtin.target.os.tag;
......@@ -10,6 +11,8 @@ const sys = switch (os) {
1011 else => @compileError("TODO"),
1112};
1213
14pub const off_t = sys.off_t;
15
1316pub const Address = extern union {
1417 any: sys.struct_sockaddr,
1518 in: Ip4Address,
......@@ -133,6 +136,16 @@ pub const Stream = struct {
133136 return sys.shutdown(@intCast(@intFromEnum(s.socket)), how);
134137 }
135138
139 pub fn sendfile(s: Stream, file: nfs.File, offset: off_t, count: ?usize) !void {
140 const count_actual = count orelse (try file.stat()).size;
141 var total: u63 = 0;
142 while (total < count_actual) {
143 const len = try sys.sendfile(@intCast(@intFromEnum(s.socket)), @intFromEnum(file.fd), &(offset + total), count_actual - total);
144 total += @intCast(len);
145 if (len == 0) break;
146 }
147 }
148
136149 pub const ReadError = switch (builtin.target.os.tag) {
137150 .linux => sys.errno.Error,
138151 else => @compileError("TODO"),
zigmod.yml+1
......@@ -6,5 +6,6 @@ description: An alternative network layer
66dependencies:
77 - src: git https://github.com/nektro/zig-nio
88 - src: git https://github.com/nektro/zig-sys-linux
9 - src: git https://github.com/nektro/zig-nfs
910root_dependencies:
1011 - src: git https://github.com/nektro/zig-expect