Skip to content

A plan for std.net #7194

@LemonBoy

Description

@LemonBoy

In #7124 a few people chimed in to ask for something more in std.net.
This ticket is meant to organize all the requests/ideas and hopefully turn them into code by the time 0.8 rolls around.

Now that network streams (net.Stream) are decoupled from files we can finally start adding more socket-specific APIs and reduce the amount of footguns linked to the use of sockets (did I really call stat on a socket? sure I did).

The Stream type is a thi(iiiiiiiii)n layer over the OS socket type, there's no guarantee socket handles are valid file handles and vice-versa (eg. that's the case for Winsocket handles) and there's no knowledge of the transport stream type (TCP, UDP, Unix...).
The latter is still up for discussion but the two alternatives I thought of, having separate *Stream type or having a tagged union type, are IMO not so nice from an API ergonomic point of view.
The Stream type is also stateless, it has no knowledge of the connection state, to keep the bookkeeping overhead as small as possible and avoid getting that piece of info out of sync with the kernel.

Right now the idea is to add:

  • More constructors (the Host variant takes a string, the suffix-less one a address-port pair):
    • connectTcpHost (replaces tcpConnectToHost)
    • connectTcp (replaces tcpConnectToAddress)
    • connectUdp (complements connectTcp)
      • Do we need a connectUdpHost ?
    • connectUnixHost (complements connectTcpHost)
    • Support connection timeouts in blocking mode!
    • The tentative prototype is:
       fn (destination: <[]const u8|Address>, options: struct {  
           reuse_port: bool = false,
           reuse_address: bool = false,
           timeout: ?<some type> = null,
       })
  • More property getters/setters:
    • nodelay
    • keepalive
    • linger
    • nonblocking (?)
    • Read/write timeouts (SO_RCVTIMEO and SO_SNDTIMEO)
  • A way to query the local address (getsockname)
    • fn getLocalAddress(self) Address
  • A way to query the remote address (socket_getpeername)
    • fn getRemoteAddress(self) Address
  • A nice wrapper around shutdown()
    • fn shutdown(enum { read, write, both })

Open questions:

  • UDP streams want recvmsg/sendmsg and/or recvfrom/sendto)
  • Some operations make sense only for some socket types (see point above), can we really get away with a single stream type?
  • Peek operation. Is that useful?

cc @Aransentin @frmdstryr @MasterQ32

Metadata

Metadata

Assignees

No one assigned

    Labels

    standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions