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 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const nio = @import("nio");3const nio = @import("nio");
4const nfs = @import("nfs");
4const sys_linux = @import("sys-linux");5const sys_linux = @import("sys-linux");
56
6const os = builtin.target.os.tag;7const os = builtin.target.os.tag;
...@@ -10,6 +11,8 @@ const sys = switch (os) {...@@ -10,6 +11,8 @@ const sys = switch (os) {
10 else => @compileError("TODO"),11 else => @compileError("TODO"),
11};12};
1213
14pub const off_t = sys.off_t;
15
13pub const Address = extern union {16pub const Address = extern union {
14 any: sys.struct_sockaddr,17 any: sys.struct_sockaddr,
15 in: Ip4Address,18 in: Ip4Address,
...@@ -133,6 +136,16 @@ pub const Stream = struct {...@@ -133,6 +136,16 @@ pub const Stream = struct {
133 return sys.shutdown(@intCast(@intFromEnum(s.socket)), how);136 return sys.shutdown(@intCast(@intFromEnum(s.socket)), how);
134 }137 }
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
136 pub const ReadError = switch (builtin.target.os.tag) {149 pub const ReadError = switch (builtin.target.os.tag) {
137 .linux => sys.errno.Error,150 .linux => sys.errno.Error,
138 else => @compileError("TODO"),151 else => @compileError("TODO"),
zigmod.yml+1
...@@ -6,5 +6,6 @@ description: An alternative network layer...@@ -6,5 +6,6 @@ description: An alternative network layer
6dependencies:6dependencies:
7 - src: git https://github.com/nektro/zig-nio7 - src: git https://github.com/nektro/zig-nio
8 - src: git https://github.com/nektro/zig-sys-linux8 - src: git https://github.com/nektro/zig-sys-linux
9 - src: git https://github.com/nektro/zig-nfs
9root_dependencies:10root_dependencies:
10 - src: git https://github.com/nektro/zig-expect11 - src: git https://github.com/nektro/zig-expect