From 609f31d2e31c1a461ec01e5ac3311b7cdd5a2a54 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 12 Mar 2026 20:09:16 -0700 Subject: [PATCH] add setSendTimeout and setRecvTimeout to Stream --- net.zig | 17 +++++++++++++++++ zigmod.yml | 1 + 2 files changed, 18 insertions(+) diff --git a/net.zig b/net.zig index 039e3a72a62681061ada43eccee58bbcdb9a8e04..326077278cba3e5eaa4346b251dc18f17f50518e 100644 --- a/net.zig +++ b/net.zig @@ -2,6 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); const nio = @import("nio"); const nfs = @import("nfs"); +const time = @import("time"); const sys_linux = @import("sys-linux"); const os = builtin.target.os.tag; @@ -188,6 +189,22 @@ pub const Stream = struct { .state = @ptrFromInt(@intFromEnum(s.socket)), }; } + + pub fn setSendTimeout(s: Stream, timeout_us: u31) !void { + const timeval: sys.struct_timeval = .{ + .sec = timeout_us / time.us_per_s, + .usec = timeout_us % time.us_per_s, + }; + return sys.setsockopt(@intCast(@intFromEnum(s.socket)), sys.SOL.SOCKET, sys.SO.SNDTIMEO, @ptrCast((&timeval)[0..1])); + } + + pub fn setRecvTimeout(s: Stream, timeout_us: u31) !void { + const timeval: sys.struct_timeval = .{ + .sec = timeout_us / time.us_per_s, + .usec = timeout_us % time.us_per_s, + }; + return sys.setsockopt(@intCast(@intFromEnum(s.socket)), sys.SOL.SOCKET, sys.SO.RCVTIMEO, @ptrCast((&timeval)[0..1])); + } }; pub const Server = struct { diff --git a/zigmod.yml b/zigmod.yml index 7f06360393aba49b90fd5ecd2027156c4a592bf4..749b06d5fbf34e3d3d99bab31e9e30238f16a370 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -7,5 +7,6 @@ dependencies: - src: git https://github.com/nektro/zig-nio - src: git https://github.com/nektro/zig-sys-linux - src: git https://github.com/nektro/zig-nfs + - src: git https://github.com/nektro/zig-time root_dependencies: - src: git https://github.com/nektro/zig-expect -- 2.54.0