UDP v0.1.0
Datagram
pub struct Datagram {
pub length: uint;
pub source: SocketAddress;
}
A datagram received, and where it came from. Copy.
Fields
| Name | Type | Description |
|---|---|---|
length | uint | How many bytes of it the buffer holds. |
source | SocketAddress | Who sent it. |
UdpSocket
pub struct UdpSocket {
// private fields
}
A UDP socket. Not copied.
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.
| Name | Type | Description |
|---|---|---|
address | SocketAddress | the address to receive at |
Returns: the socket, or fails with AddressInUse or AddressUnavailable
Methods
SendTo
pub func SendTo(
self: &UdpSocket,
bytes: char8[..],
target: SocketAddress
) -> uint ! IoError
Sends bytes as one datagram to target.
| Name | Type | Description |
|---|---|---|
bytes | char8[..] | the datagram |
target | SocketAddress | where to send it |
Returns: how many bytes were sent
ReceiveFrom
pub func ReceiveFrom(
self: &UdpSocket,
into: var char8[..]
) -> Datagram ! IoError
Waits for a datagram and puts it in into.
| Name | Type | Description |
|---|---|---|
into | var char8[..] | where to put it |
Returns: its length and its sender
Connect
pub func Connect(
self: &UdpSocket,
peer: SocketAddress
) -> ! IoError
Sends to and receives from peer only, so Send and Receive need no address.
| Name | Type | Description |
|---|---|---|
peer | SocketAddress | the one address to talk to |
Returns: nothing, or fails when the system refused
Send
pub func Send(
self: &UdpSocket,
bytes: char8[..]
) -> uint ! IoError
Sends bytes as one datagram to the connected peer.
| Name | Type | Description |
|---|---|---|
bytes | char8[..] | the datagram |
Returns: how many bytes were sent
Receive
pub func Receive(
self: &UdpSocket,
into: var char8[..]
) -> uint ! IoError
Waits for a datagram from the connected peer and puts it in into.
| Name | Type | Description |
|---|---|---|
into | var char8[..] | where to put it |
Returns: its length
LocalAddress
pub func LocalAddress(self: &UdpSocket) -> SocketAddress ! IoError
The address the socket is bound to.
Returns: the address
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.
| Name | Type | Description |
|---|---|---|
timeout | Duration? | the limit |
Returns: nothing, or fails when the system refused
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.
| Name | Type | Description |
|---|---|---|
enabled | bool | whether calls fail rather than wait |
Returns: success when the mode is set
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
Destructor
~UdpSocket
func ~UdpSocket(self: &var UdpSocket)