structstd.net.Server[src]

Fields

listen_address: Address
stream: std.net.Stream

Error Sets

Error SetAcceptError[src]

Errors

anyerror means the error set is known only at runtime.

BlockedByFirewall

Firewall rules forbid connection.

ConnectionAborted
ConnectionResetByPeer

An incoming connection was indicated, but was subsequently terminated by the remote peer prior to accepting the call.

FileDescriptorNotASocket

The file descriptor sockfd does not refer to a socket.

NetworkSubsystemFailed

The network subsystem has failed.

OperationNotSupported

The referenced socket is not a type that supports connection-oriented service.

ProcessFdQuotaExceeded

The per-process limit on the number of open file descriptors has been reached.

ProtocolFailure
SocketNotListening

Socket is not listening for new connections.

SystemFdQuotaExceeded

The system-wide limit on the total number of open files has been reached.

SystemResources

Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.

Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

WouldBlock

This error occurs when no global event loop is configured, and accepting from the socket would block.

Source Code

Source code
pub const AcceptError = error{
    ConnectionAborted,

    /// The file descriptor sockfd does not refer to a socket.
    FileDescriptorNotASocket,

    /// The per-process limit on the number of open file descriptors has been reached.
    ProcessFdQuotaExceeded,

    /// The system-wide limit on the total number of open files has been reached.
    SystemFdQuotaExceeded,

    /// Not enough free memory.  This often means that the memory allocation  is  limited
    /// by the socket buffer limits, not by the system memory.
    SystemResources,

    /// Socket is not listening for new connections.
    SocketNotListening,

    ProtocolFailure,

    /// Firewall rules forbid connection.
    BlockedByFirewall,

    /// This error occurs when no global event loop is configured,
    /// and accepting from the socket would block.
    WouldBlock,

    /// An incoming connection was indicated, but was subsequently terminated by the
    /// remote peer prior to accepting the call.
    ConnectionResetByPeer,

    /// The network subsystem has failed.
    NetworkSubsystemFailed,

    /// The referenced socket is not a type that supports connection-oriented service.
    OperationNotSupported,
} || UnexpectedError

Functions

Functiondeinit[src]

pub fn deinit(s: *Server) void

Parameters

s: *Server

Source Code

Source code
pub fn deinit(s: *Server) void {
    s.stream.close();
    s.* = undefined;
}

Functionaccept[src]

pub fn accept(s: *Server) AcceptError!Connection

Blocks until a client connects to the server. The returned Connection has an open stream.

Parameters

s: *Server

Source Code

Source code
pub fn accept(s: *Server) AcceptError!Connection {
    var accepted_addr: Address = undefined;
    var addr_len: posix.socklen_t = @sizeOf(Address);
    const fd = try posix.accept(s.stream.handle, &accepted_addr.any, &addr_len, posix.SOCK.CLOEXEC);
    return .{
        .stream = .{ .handle = fd },
        .address = accepted_addr,
    };
}

Source Code

Source code
pub const Server = struct {
    listen_address: Address,
    stream: std.net.Stream,

    pub const Connection = struct {
        stream: std.net.Stream,
        address: Address,
    };

    pub fn deinit(s: *Server) void {
        s.stream.close();
        s.* = undefined;
    }

    pub const AcceptError = posix.AcceptError;

    /// Blocks until a client connects to the server. The returned `Connection` has
    /// an open stream.
    pub fn accept(s: *Server) AcceptError!Connection {
        var accepted_addr: Address = undefined;
        var addr_len: posix.socklen_t = @sizeOf(Address);
        const fd = try posix.accept(s.stream.handle, &accepted_addr.any, &addr_len, posix.SOCK.CLOEXEC);
        return .{
            .stream = .{ .handle = fd },
            .address = accepted_addr,
        };
    }
}