How to use non-blocked sockets without new Io (0.16.+)

Same days ago I already asked the same question

I got an answer:

std.posix layer of abstractions is gone, but the lower layer (obviously) isn’t going anywhere…

I tried to understand current status using as reference Threaded Io

pub fn io(t: *Threaded) Io {
    return .{
        .userdata = t,
        .vtable = &.{
            --------------
            .netConnectIp = switch (native_os) {
                .windows => netConnectIpWindows,
                else => netConnectIpPosix,
            },

```

Chain of functions (for linux)

netConectIpPosix calls

  • openSocketPosix - socket creation
  • posixConnect - connect to server

Both functions don’t use Io staff and may be great candidates for Io-independent working with sockets.

Two small problems:

  • they are part of Io/Threaded.zig
  • not public

Slightly different pattern have net Read/Write/.. functions

fn netReadPosix(userdata: ?*anyopaque, fd: net.Socket.Handle, data: [][]u8) net.Stream.Reader.Error!usize {
    if (!have_networking) return error.NetworkDown;
    const t: *Threaded = @ptrCast(@alignCast(userdata));
    _ = t;

    code for read (t is not used ofc) 
    .....................................................

In order to work with non-blocked sockets without IO, low-level functions

should be public and placed somewhere , and I am not sure regarding Io/Threaded.zig

Is it the way to use non-blocked sockets without new Io (0.16.+)?

1 Like

+1 to this request. I also would love to continue using Threaded without the Io wrapper. Several of my applications will stick to Threaded forever, they are CPU-bound (scientific computations) and do not require asynchronicity. I’m currently thinking about moving to libc instead, but if Threaded is exposed as public, that would be a much better approach.

1 Like

I think more parts of std.Io.Threaded will get public as someone works on std.Io.Kqueue, because it will be needed there.

I’m kind of future-proofing my own Zig upgrade path with zio. I also depend on very low level I/O in parts of my system and don’t want to use std.Io.Threaded for that. Currently, it’s already possible to use zio.net and zio.fs without async context, and there is also zio.os with various wrappers for syscalls/libc that makes it possible to use POSIX directly across platforms.

1 Like

fun fact - Io/Threaded does not support non-blocked sockets at all.

So an answer

You should to use non-blocked sockets without new Io(0.16.+) at least Io/Threaded :joy:

If you want to do non-blocking sockets purely with std, your best bet is using std.c and ignoring Windows.

yes, but my main problem that i am going to add Windows implementation