Skip to content
CryoCryo home
Stdlibnet::socket

udp

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

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>;
    drop(mut &this) -> void;
}

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.

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.

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.

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>;
}

Trait implementations

implement trait Future for struct UdpSendTo

implement trait Drop for struct UdpSendTo

implement trait Future for struct UdpSend

implement trait Drop for struct UdpSend

implement trait Future for struct UdpRecvFrom

implement trait Drop for struct UdpRecvFrom

implement trait Future for struct UdpRecv

implement trait Drop for struct UdpRecv

UdpSocket closes its descriptor through an inherent drop.