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 {...@@ -1290,6 +1290,9 @@ pub const StreamServer = struct {
1290 /// If more than this many connections pool in the kernel, clients will start1290 /// If more than this many connections pool in the kernel, clients will start
1291 /// seeing "Connection refused".1291 /// seeing "Connection refused".
1292 kernel_backlog: u32 = 128,1292 kernel_backlog: u32 = 128,
1293
1294 /// Enable SO_REUSEADDR on the socket.
1295 reuse_address: bool = false,
1293 };1296 };
12941297
1295 /// After this call succeeds, resources have been acquired and must1298 /// After this call succeeds, resources have been acquired and must
...@@ -1320,6 +1323,19 @@ pub const StreamServer = struct {...@@ -1320,6 +1323,19 @@ pub const StreamServer = struct {
1320 self.sockfd = null;1323 self.sockfd = null;
1321 }1324 }
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
1323 var socklen = address.getOsSockLen();1339 var socklen = address.getOsSockLen();
1324 try os.bind(sockfd, &address.any, socklen);1340 try os.bind(sockfd, &address.any, socklen);
1325 try os.listen(sockfd, self.kernel_backlog);1341 try os.listen(sockfd, self.kernel_backlog);