UDP v0.1.0

Source
Datagram sockets and the datagrams they receive.

Datagram

pub struct Datagram {
    pub length: uint;
    pub source: SocketAddress;
}

A datagram received, and where it came from. Copy.

Source

Fields

NameTypeDescription
lengthuintHow many bytes of it the buffer holds.
sourceSocketAddressWho sent it.

UdpSocket

pub struct UdpSocket {
    // private fields
}

A UDP socket. Not copied.

Source

Associated functions

Bind

pub func Bind(address: SocketAddress) -> UdpSocket ! IoError

A socket bound to address. Port zero lets the system choose a free port, which LocalAddress then gives.

NameTypeDescription
addressSocketAddressthe address to receive at

Returns: the socket, or fails with AddressInUse or AddressUnavailable

Source

Methods

SendTo

pub func SendTo(
    self: &UdpSocket,
    bytes: char8[..],
    target: SocketAddress
) -> uint ! IoError

Sends bytes as one datagram to target.

NameTypeDescription
byteschar8[..]the datagram
targetSocketAddresswhere to send it

Returns: how many bytes were sent

Source

ReceiveFrom

pub func ReceiveFrom(
    self: &UdpSocket,
    into: var char8[..]
) -> Datagram ! IoError

Waits for a datagram and puts it in into.

NameTypeDescription
intovar char8[..]where to put it

Returns: its length and its sender

Source

Connect

pub func Connect(
    self: &UdpSocket,
    peer: SocketAddress
) -> ! IoError

Sends to and receives from peer only, so Send and Receive need no address.

NameTypeDescription
peerSocketAddressthe one address to talk to

Returns: nothing, or fails when the system refused

Source

Send

pub func Send(
    self: &UdpSocket,
    bytes: char8[..]
) -> uint ! IoError

Sends bytes as one datagram to the connected peer.

NameTypeDescription
byteschar8[..]the datagram

Returns: how many bytes were sent

Source

Receive

pub func Receive(
    self: &UdpSocket,
    into: var char8[..]
) -> uint ! IoError

Waits for a datagram from the connected peer and puts it in into.

NameTypeDescription
intovar char8[..]where to put it

Returns: its length

Source

LocalAddress

pub func LocalAddress(self: &UdpSocket) -> SocketAddress ! IoError

The address the socket is bound to.

Returns: the address

Source

SetReadTimeout

pub func SetReadTimeout(
    self: &UdpSocket,
    timeout: Duration?
) -> ! IoError

How long a receive may wait before failing with TimedOut, or none to wait without a limit.

NameTypeDescription
timeoutDuration?the limit

Returns: nothing, or fails when the system refused

Source

SetNonBlocking

pub func SetNonBlocking(
    self: &UdpSocket,
    enabled: bool
) -> ! IoError

Makes calls on the socket fail with WouldBlock when they would wait, or wait again. A non-blocking socket is meant for a Poller, which says when a call would not wait.

NameTypeDescription
enabledboolwhether calls fail rather than wait

Returns: success when the mode is set

Source

PollSource

pub func PollSource(self: &UdpSocket) -> PollSource

The socket as a Poller watches it, valid while the socket is open.

Returns: the source to register

Source

Destructor

~UdpSocket

func ~UdpSocket(self: &var UdpSocket)

Source