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