aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorfrmdstryr <frmdstryr@protonmail.com>2019-11-18 08:45:25 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-11-19 01:06:04 +0000
commitaa4e92f3b3dc4b349eeabf589f8b9047e7342f03 (patch)
treefbf901edba030c004fa71eba75c6874cb38be95b /lib
parented956b581283824da1f7d39b953f4d716928eee2 (diff)
downloadzig-aa4e92f3b3dc4b349eeabf589f8b9047e7342f03.tar.gz
zig-aa4e92f3b3dc4b349eeabf589f8b9047e7342f03.zip
Make StreamServer return address of accecpted client
Diffstat (limited to 'lib')
-rw-r--r--lib/std/net.zig14
-rw-r--r--lib/std/net/test.zig4
2 files changed, 13 insertions, 5 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig
index 531b8ce797..f4cd09482b 100644
--- a/lib/std/net.zig
+++ b/lib/std/net.zig
@@ -1360,14 +1360,22 @@ pub const StreamServer = struct {
BlockedByFirewall,
} || os.UnexpectedError;
- /// If this function succeeds, the returned `fs.File` is a caller-managed resource.
- pub fn accept(self: *StreamServer) AcceptError!fs.File {
+ pub const Connection = struct {
+ file: fs.File,
+ address: Address
+ };
+
+ /// If this function succeeds, the returned `Connection` is a caller-managed resource.
+ pub fn accept(self: *StreamServer) AcceptError!Connection {
const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
const accept_flags = nonblock | os.SOCK_CLOEXEC;
var accepted_addr: Address = undefined;
var adr_len: os.socklen_t = @sizeOf(Address);
if (os.accept4(self.sockfd.?, &accepted_addr.any, &adr_len, accept_flags)) |fd| {
- return fs.File.openHandle(fd);
+ return Connection{
+ .file = fs.File.openHandle(fd),
+ .address = accepted_addr,
+ };
} else |err| switch (err) {
// We only give SOCK_NONBLOCK when I/O mode is async, in which case this error
// is handled by os.accept4.
diff --git a/lib/std/net/test.zig b/lib/std/net/test.zig
index 49dbdbb218..6400b022c6 100644
--- a/lib/std/net/test.zig
+++ b/lib/std/net/test.zig
@@ -115,8 +115,8 @@ fn testClient(addr: net.Address) anyerror!void {
}
fn testServer(server: *net.StreamServer) anyerror!void {
- var client_file = try server.accept();
+ var client = try server.accept();
- const stream = &client_file.outStream().stream;
+ const stream = &client.file.outStream().stream;
try stream.print("hello from server\n");
}