Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.net.StreamServer.accept cannot handle SO_(SND|RCV)TIMEO sockopts #7142

Open
stef opened this issue Nov 17, 2020 · 1 comment
Open

std.net.StreamServer.accept cannot handle SO_(SND|RCV)TIMEO sockopts #7142

stef opened this issue Nov 17, 2020 · 1 comment
Labels
standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@stef
Copy link
Contributor

stef commented Nov 17, 2020

i'm running into this line in my code: https://github.com/ziglang/zig/blob/master/lib/std/net.zig#L1701 - accept() getting a WouldBlock error, since i did not set std.io.is_async, but what i did was to do a setsockopt for SO_(SND|RCV)TIMEO which sets a timeout on the socket...

i don't really want to set the socket to nonblocking. i just want to regularly return if there is no incoming connection, but i'm ok to have a few seconds timeouts on that...

i lifted the code from 0.6.0 into my own sources and modified it to look like this:

pub fn accept(self: *net.StreamServer) !net.StreamServer.Connection {
    const accept_flags = os.SOCK_CLOEXEC;
    var accepted_addr: net.Address = undefined;
    var adr_len: os.socklen_t = @sizeOf(net.Address);
    if (os.accept4(self.sockfd.?, &accepted_addr.any, &adr_len, accept_flags)) |fd| {
        return net.StreamServer.Connection{
            .file = fs.File{
                .handle = fd,
                .io_mode = std.io.mode,
            },
            .address = accepted_addr,
        };
    } else |err| return err;
}
@andrewrk andrewrk added the standard library This issue involves writing Zig code for the standard library. label Nov 30, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 30, 2020
@frmdstryr
Copy link
Contributor

Related #7194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
3 participants