authorgravatar for git@l4.pmLuna <git@l4.pm> 2019-11-09 12:40:56-03:00
committergravatar for git@l4.pmLuna <git@l4.pm> 2019-11-09 12:40:56-03:00
logf4d8dc278b312fc3eccf33a37cfe89c7c012d6fd
treeea67864a065dd81003610fdc5f6dca573e10ad16
parentc2325053a86f4350e20ea3b476c7efeb942d8438

rename TcpServer -> StreamServer

- add AF_UNIX support to getOsSockLen

2 files changed, 17 insertions(+), 10 deletions(-)

lib/std/net.zig+15-8
......@@ -46,6 +46,9 @@ pub const Address = extern union {
4646 .path = undefined,
4747 };
4848
49 // this enables us to have the proper length of the socket in getOsSockLen
50 mem.zero(&sock_addr.path);
51
4952 if (path.len > sock_addr.path.len) return error.NameTooLong;
5053 mem.copy(u8, &sock_addr.path, path);
5154
......@@ -344,6 +347,10 @@ pub const Address = extern union {
344347 switch (self.any.family) {
345348 os.AF_INET => return @sizeOf(os.sockaddr_in),
346349 os.AF_INET6 => return @sizeOf(os.sockaddr_in6),
350 os.AF_UNIX => blk: {
351 const path_len = std.mem.len(self.un.path.len);
352 break :blk @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);
353 },
347354 else => unreachable,
348355 }
349356 }
......@@ -1264,7 +1271,7 @@ fn dnsParseCallback(ctx: dpc_ctx, rr: u8, data: []const u8, packet: []const u8)
12641271 }
12651272}
12661273
1267pub const TcpServer = struct {
1274pub const StreamServer = struct {
12681275 /// Copied from `Options` on `init`.
12691276 kernel_backlog: u32,
12701277
......@@ -1282,21 +1289,21 @@ pub const TcpServer = struct {
12821289
12831290 /// After this call succeeds, resources have been acquired and must
12841291 /// be released with `deinit`.
1285 pub fn init(options: Options) TcpServer {
1286 return TcpServer{
1292 pub fn init(options: Options) StreamServer {
1293 return StreamServer{
12871294 .sockfd = null,
12881295 .kernel_backlog = options.kernel_backlog,
12891296 .listen_address = undefined,
12901297 };
12911298 }
12921299
1293 /// Release all resources. The `TcpServer` memory becomes `undefined`.
1294 pub fn deinit(self: *TcpServer) void {
1300 /// Release all resources. The `StreamServer` memory becomes `undefined`.
1301 pub fn deinit(self: *StreamServer) void {
12951302 self.close();
12961303 self.* = undefined;
12971304 }
12981305
1299 pub fn listen(self: *TcpServer, address: Address) !void {
1306 pub fn listen(self: *StreamServer, address: Address) !void {
13001307 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
13011308 const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock;
13021309 const sockfd = try os.socket(os.AF_INET, sock_flags, os.IPPROTO_TCP);
......@@ -1315,7 +1322,7 @@ pub const TcpServer = struct {
13151322 /// Stop listening. It is still necessary to call `deinit` after stopping listening.
13161323 /// Calling `deinit` will automatically call `close`. It is safe to call `close` when
13171324 /// not listening.
1318 pub fn close(self: *TcpServer) void {
1325 pub fn close(self: *StreamServer) void {
13191326 if (self.sockfd) |fd| {
13201327 os.close(fd);
13211328 self.sockfd = null;
......@@ -1343,7 +1350,7 @@ pub const TcpServer = struct {
13431350 } || os.UnexpectedError;
13441351
13451352 /// If this function succeeds, the returned `fs.File` is a caller-managed resource.
1346 pub fn accept(self: *TcpServer) AcceptError!fs.File {
1353 pub fn accept(self: *StreamServer) AcceptError!fs.File {
13471354 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
13481355 const accept_flags = nonblock | os.SOCK_CLOEXEC;
13491356 var accepted_addr: Address = undefined;
lib/std/net/test.zig+2-2
......@@ -93,7 +93,7 @@ test "listen on a port, send bytes, receive bytes" {
9393 // TODO doing this at comptime crashed the compiler
9494 const localhost = net.Address.parseIp("127.0.0.1", 0);
9595
96 var server = net.TcpServer.init(net.TcpServer.Options{});
96 var server = net.StreamServer.init(net.StreamServer.Options{});
9797 defer server.deinit();
9898 try server.listen(localhost);
9999
......@@ -114,7 +114,7 @@ fn testClient(addr: net.Address) anyerror!void {
114114 testing.expect(mem.eql(u8, msg, "hello from server\n"));
115115}
116116
117fn testServer(server: *net.TcpServer) anyerror!void {
117fn testServer(server: *net.StreamServer) anyerror!void {
118118 var client_file = try server.accept();
119119
120120 const stream = &client_file.outStream().stream;