Skip to content
CryoCryo home
Stdlibnet::socket

udp

import std::net::socket::udp; · source

Types

UdpSocket

type struct UdpSocket {
    fd: i32;

    static bind(local: SocketAddr) -> Result<UdpSocket, IoError>;
    static unbound() -> Result<UdpSocket, IoError>;
    static from_fd(sock: i32) -> UdpSocket;
    raw_fd(&this) -> i32;
    connect(mut &this, remote: SocketAddr) -> Result<(), IoError>;
    local_addr(&this) -> Result<SocketAddr, IoError>;
    set_broadcast(mut &this, on: boolean) -> Result<(), IoError>;
    set_nonblocking(mut &this, on: boolean) -> Result<(), IoError>;
}

bind(local) or unbound() creates a datagram socket, in non-blocking mode. connect(remote) fixes a default peer, which is what enables UdpSend / UdpRecv over the addressed UdpSendTo / UdpRecvFrom. It closes its descriptor through an inherent drop.

Properties

PropertyType
fdi32

Constructors

static bind(local: SocketAddr) -> Result<UdpSocket, IoError>;

Open a datagram socket and bind it to local. Use SocketAddr::any_v4(port) to receive on every interface or localhost_v4(port) for loopback. Pass port 0 to let the kernel pick an ephemeral port, then read it back with local_addr.

static unbound() -> Result<UdpSocket, IoError>;

Open an unbound datagram socket. The kernel assigns a local address lazily on the first send / connect. Handy for a pure client that only ever talks to one peer.

static from_fd(sock: i32) -> UdpSocket;

Wrap a raw file descriptor. The caller transfers ownership; drop will close it. Used by the futures below to leave a closed placeholder behind while one of them owns the real socket.

Methods

raw_fd(&this) -> i32;

Borrow the underlying file descriptor without transferring ownership. The UdpSocket still closes it on drop.

connect(mut &this, remote: SocketAddr) -> Result<(), IoError>;

Pin a default peer. After this, UdpSend/UdpRecv talk to remote and the kernel filters incoming datagrams to that source. Does not exchange packets - UDP connect is local bookkeeping only, so it cannot block and stays a plain call.

local_addr(&this) -> Result<SocketAddr, IoError>;

The local address the socket is bound to. After bind(.., 0) this reports the kernel-assigned ephemeral port - the only way to learn it.

set_broadcast(mut &this, on: boolean) -> Result<(), IoError>;

Allow sending to the broadcast address. Off by default; the kernel rejects broadcast sends without it (EACCES).

set_nonblocking(mut &this, on: boolean) -> Result<(), IoError>;

Put the socket in non-blocking mode, where a send or receive reports WouldBlock instead of sleeping the calling thread. bind and unbound already do this; it is exposed for a descriptor adopted through from_fd, which arrives in an unknown mode.

The futures

The same ownership handshake as TCP applies: the socket and buffer go in through start and come back out through take_* on the outcome.

UdpSendTo and UdpSend

type struct UdpSendTo {
    socket: UdpSocket;
    buf:    Array<u8>;
    dest:   SocketAddr;
    rt:     Reactor*;

    static start(socket: UdpSocket, buf: Array<u8>, dest: SocketAddr) -> UdpSendTo;
}
type struct UdpSend {
    socket: UdpSocket;
    buf:    Array<u8>;
    rt:     Reactor*;

    static start(socket: UdpSocket, buf: Array<u8>) -> UdpSend;
}

One datagram out — to an explicit destination, or to the connected peer. Both complete with a UdpIo.

UdpSendTo
Properties
PropertyType
socketUdpSocket
bufArray<u8>
destSocketAddr
rtReactor*
Constructors

####### static start(socket: UdpSocket, buf: Array<u8>, dest: SocketAddr) -> UdpSendTo;

Begin a send of every byte in buf to dest - socket and buffer both owned by this future until it completes.

UdpSend
Properties
PropertyType
socketUdpSocket
bufArray<u8>
rtReactor*
Constructors

####### static start(socket: UdpSocket, buf: Array<u8>) -> UdpSend;

Begin a send of every byte in buf on the socket's pinned peer.

UdpRecvFrom and UdpRecv

type struct UdpRecvFrom {
    socket: UdpSocket;
    buf:    Array<u8>;
    rt:     Reactor*;

    static start(socket: UdpSocket, buf: Array<u8>) -> UdpRecvFrom;
}
type struct UdpRecv {
    socket: UdpSocket;
    buf:    Array<u8>;
    rt:     Reactor*;

    static start(socket: UdpSocket, buf: Array<u8>) -> UdpRecv;
}

One datagram in. UdpRecvFrom completes with a UdpReceived, which carries the sender's address beside the byte count; UdpRecv on a connected socket completes with a UdpIo.

UdpRecvFrom
Properties
PropertyType
socketUdpSocket
bufArray<u8>
rtReactor*
Constructors

####### static start(socket: UdpSocket, buf: Array<u8>) -> UdpRecvFrom;

Begin a receive on socket, filling buf - both owned by this future until it completes.

UdpRecv
Properties
PropertyType
socketUdpSocket
bufArray<u8>
rtReactor*
Constructors

####### static start(socket: UdpSocket, buf: Array<u8>) -> UdpRecv;

Begin a receive on socket, filling buf.

The outcomes

type struct UdpIo {
    socket: UdpSocket;
    buf:    Array<u8>;
    result: Result<u64, IoError>;

    take_socket(mut &this) -> UdpSocket;
    take_buf(mut &this) -> Array<u8>;
    outcome(&this) -> Result<u64, IoError>;
}
type struct UdpReceived {
    socket: UdpSocket;
    buf:    Array<u8>;
    result: Result<(u64, SocketAddr), IoError>;

    take_socket(mut &this) -> UdpSocket;
    take_buf(mut &this) -> Array<u8>;
    outcome(&this) -> Result<(u64, SocketAddr), IoError>;
}

UdpIo

Properties
PropertyType
socketUdpSocket
bufArray<u8>
resultResult<u64, IoError>
Methods
take_socket(mut &this) -> UdpSocket;

Take the socket back, so the next operation can be started on it. Calling it twice yields a closed socket the second time.

take_buf(mut &this) -> Array<u8>;

Take the buffer back. After a receive it holds exactly the bytes of the datagram that arrived; after a send it is untouched, so the caller still has what it sent. Calling it twice yields an empty buffer.

outcome(&this) -> Result<u64, IoError>;

How many bytes moved, or why none did.

UdpReceived

Properties
PropertyType
socketUdpSocket
bufArray<u8>
resultResult<(u64, SocketAddr), IoError>
Methods
take_socket(mut &this) -> UdpSocket;
take_buf(mut &this) -> Array<u8>;
outcome(&this) -> Result<(u64, SocketAddr), IoError>;

The byte count and the sender, or why no datagram arrived.

Trait implementations

UdpSendTo

implement trait Future for struct UdpSendTo
implement trait Drop for struct UdpSendTo

UdpSend

implement trait Future for struct UdpSend
implement trait Drop for struct UdpSend

UdpRecvFrom

implement trait Future for struct UdpRecvFrom
implement trait Drop for struct UdpRecvFrom

UdpRecv

implement trait Future for struct UdpRecv
implement trait Drop for struct UdpRecv

UdpSocket closes its descriptor through an inherent drop.