authorgravatar for git@l4.pmLuna <git@l4.pm> 2019-11-28 22:19:42-03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-30 19:26:29-05:00
log631eb6783d1dbc64f031426436ab685008c31ca8
treead25a1ccb42800f1c814399f1e811bab027a4c90
parent73e535e1125d76bcd4e85123defea8b76412ab09
signature Commit is signed but in an unrecognized format.

add StreamServer.Options.reuse_address

this uses a bad direct interface with std.os.linux, this should add setsockopt to std.os.

1 files changed, 16 insertions(+), 0 deletions(-)

lib/std/net.zig+16
......@@ -1290,6 +1290,9 @@ pub const StreamServer = struct {
12901290 /// If more than this many connections pool in the kernel, clients will start
12911291 /// seeing "Connection refused".
12921292 kernel_backlog: u32 = 128,
1293
1294 /// Enable SO_REUSEADDR on the socket.
1295 reuse_address: bool = false,
12931296 };
12941297
12951298 /// After this call succeeds, resources have been acquired and must
......@@ -1320,6 +1323,19 @@ pub const StreamServer = struct {
13201323 self.sockfd = null;
13211324 }
13221325
1326 // TODO proper interface with errors in std.os
1327 var optval: c_int = 1;
1328
1329 if (self.options.reuse_address) {
1330 _ = os.linux.setsockopt(
1331 server.sockfd.?,
1332 os.linux.SOL_SOCKET,
1333 os.linux.SO_REUSEADDR,
1334 @ptrCast([*]const u8, &optval),
1335 @sizeOf(c_int),
1336 );
1337 }
1338
13231339 var socklen = address.getOsSockLen();
13241340 try os.bind(sockfd, &address.any, socklen);
13251341 try os.listen(sockfd, self.kernel_backlog);