UDPSocket¶
Creates a UDP socket that can be used for sending and receiving UDP messages.
The following examples create:
- an echo server that listens for connections and returns whatever message it receives
- a client that connects to the server, sends a message, and prints the message it receives in response
The server is implemented like this:
use "net"
class MyUDPNotify is UDPNotify
fun ref received(
sock: UDPSocket ref,
data: Array[U8] iso,
from: NetAddress)
=>
sock.write(consume data, from)
fun ref not_listening(sock: UDPSocket ref) =>
None
actor Main
new create(env: Env) =>
UDPSocket(UDPAuth(env.root),
MyUDPNotify, "", "8989")
The client is implemented like this:
use "net"
class MyUDPNotify is UDPNotify
let _out: OutStream
let _destination: NetAddress
new create(
out: OutStream,
destination: NetAddress)
=>
_out = out
_destination = destination
fun ref listening(sock: UDPSocket ref) =>
sock.write("hello world", _destination)
fun ref received(
sock: UDPSocket ref,
data: Array[U8] iso,
from: NetAddress)
=>
_out.print("GOT:" + String.from_array(consume data))
sock.dispose()
fun ref not_listening(sock: UDPSocket ref) =>
None
actor Main
new create(env: Env) =>
try
let destination =
DNS.ip4(DNSAuth(env.root), "localhost", "8989")(0)?
UDPSocket(UDPAuth(env.root),
recover MyUDPNotify(env.out, consume destination) end)
end
Implements¶
- AsioEventNotify tag
Constructors¶
create¶
Listens for both IPv4 and IPv6 datagrams.
new tag create(
auth: UDPAuth val,
notify: UDPNotify iso,
host: String val = "",
service: String val = "0",
size: USize val = 1024)
: UDPSocket tag^
Parameters¶
- auth: UDPAuth val
- notify: UDPNotify iso
- host: String val = ""
- service: String val = "0"
- size: USize val = 1024
Returns¶
- UDPSocket tag^
ip4¶
Listens for IPv4 datagrams.
new tag ip4(
auth: UDPAuth val,
notify: UDPNotify iso,
host: String val = "",
service: String val = "0",
size: USize val = 1024)
: UDPSocket tag^
Parameters¶
- auth: UDPAuth val
- notify: UDPNotify iso
- host: String val = ""
- service: String val = "0"
- size: USize val = 1024
Returns¶
- UDPSocket tag^
ip6¶
Listens for IPv6 datagrams.
new tag ip6(
auth: UDPAuth val,
notify: UDPNotify iso,
host: String val = "",
service: String val = "0",
size: USize val = 1024)
: UDPSocket tag^
Parameters¶
- auth: UDPAuth val
- notify: UDPNotify iso
- host: String val = ""
- service: String val = "0"
- size: USize val = 1024
Returns¶
- UDPSocket tag^
Public Behaviours¶
write¶
Write a single sequence of bytes.
Parameters¶
- data: (String val | Array[U8 val] val)
- to: NetAddress val
writev¶
Write a sequence of sequences of bytes.
Parameters¶
- data: ByteSeqIter val
- to: NetAddress val
set_notify¶
Change the notifier.
Parameters¶
- notify: UDPNotify iso
set_broadcast¶
Enable or disable broadcasting from this socket.
Parameters¶
- state: Bool val
set_multicast_interface¶
By default, the OS will choose which address is used to send packets bound for multicast addresses. This can be used to force a specific interface. To revert to allowing the OS to choose, call with an empty string.
Parameters¶
- from: String val = ""
set_multicast_loopback¶
By default, packets sent to a multicast address will be received by the sending system if it has subscribed to that address. Disabling loopback prevents this.
Parameters¶
- loopback: Bool val
set_multicast_ttl¶
Set the TTL for multicast sends. Defaults to 1.
Parameters¶
- ttl: U8 val
multicast_join¶
Add a multicast group. This can be limited to packets arriving on a specific interface.
Parameters¶
multicast_leave¶
Drop a multicast group. This can be limited to packets arriving on a specific interface. No attempt is made to check that this socket has previously added this group.
Parameters¶
dispose¶
Stop listening.
Public Functions¶
local_address¶
Return the bound IP address.
Returns¶
- NetAddress val
getsockopt¶
General wrapper for UDP sockets to the getsockopt(2)
system call.
The caller must provide an array that is pre-allocated to be at least as large as the largest data structure that the kernel may return for the requested option.
In case of system call success, this function returns the 2-tuple:
1. The integer 0
.
2. An Array[U8]
of data returned by the system call's void *
4th argument. Its size is specified by the kernel via the
system call's sockopt_len_t *
5th argument.
In case of system call failure, this function returns the 2-tuple:
1. The value of errno
.
2. An undefined value that must be ignored.
Usage example:
// listening() is a callback function for class UDPNotify
fun ref listening(sock: UDPSocket ref) =>
match sock.getsockopt(OSSockOpt.sol_socket(), OSSockOpt.so_rcvbuf(), 4)
| (0, let gbytes: Array[U8] iso) =>
try
let br = Reader.create().>append(consume gbytes)
ifdef littleendian then
let buffer_size = br.u32_le()?
else
let buffer_size = br.u32_be()?
end
end
| (let errno: U32, _) =>
// System call failed
end
fun ref getsockopt(
level: I32 val,
option_name: I32 val,
option_max_size: USize val = 4)
: (U32 val , Array[U8 val] iso^)
Parameters¶
Returns¶
getsockopt_u32¶
Wrapper for UDP sockets to the getsockopt(2)
system call where
the kernel's returned option value is a C uint32_t
type / Pony
type U32
.
In case of system call success, this function returns the 2-tuple:
1. The integer 0
.
2. The *option_value
returned by the kernel converted to a Pony U32
.
In case of system call failure, this function returns the 2-tuple:
1. The value of errno
.
2. An undefined value that must be ignored.
Parameters¶
Returns¶
setsockopt¶
General wrapper for UDP sockets to the setsockopt(2)
system call.
The caller is responsible for the correct size and byte contents of
the option
array for the requested level
and option_name
,
including using the appropriate CPU endian byte order.
This function returns 0
on success, else the value of errno
on
failure.
Usage example:
// listening() is a callback function for class UDPNotify
fun ref listening(sock: UDPSocket ref) =>
let sb = Writer
sb.u32_le(7744) // Our desired socket buffer size
let sbytes = Array[U8]
for bs in sb.done().values() do
sbytes.append(bs)
end
match sock.setsockopt(OSSockOpt.sol_socket(), OSSockOpt.so_rcvbuf(), sbytes)
| 0 =>
// System call was successful
| let errno: U32 =>
// System call failed
end
Parameters¶
Returns¶
- U32 val
setsockopt_u32¶
Wrapper for UDP sockets to the setsockopt(2)
system call where
the kernel expects an option value of a C uint32_t
type / Pony
type U32
.
This function returns 0
on success, else the value of errno
on
failure.
Parameters¶
Returns¶
- U32 val
get_so_error¶
Wrapper for the FFI call getsockopt(fd, SOL_SOCKET, SO_ERROR, ...)
Returns¶
get_so_rcvbuf¶
Wrapper for the FFI call getsockopt(fd, SOL_SOCKET, SO_RCVBUF, ...)
Returns¶
get_so_sndbuf¶
Wrapper for the FFI call getsockopt(fd, SOL_SOCKET, SO_SNDBUF, ...)
Returns¶
set_ip_multicast_loop¶
Wrapper for the FFI call setsockopt(fd, SOL_SOCKET, IP_MULTICAST_LOOP, ...)
Parameters¶
- loopback: Bool val
Returns¶
- U32 val
set_ip_multicast_ttl¶
Wrapper for the FFI call setsockopt(fd, SOL_SOCKET, IP_MULTICAST_TTL, ...)
Parameters¶
- ttl: U8 val
Returns¶
- U32 val
set_so_broadcast¶
Wrapper for the FFI call setsockopt(fd, SOL_SOCKET, SO_BROADCAST, ...)
Parameters¶
- state: Bool val
Returns¶
- U32 val
set_so_rcvbuf¶
Wrapper for the FFI call setsockopt(fd, SOL_SOCKET, SO_RCVBUF, ...)
Parameters¶
- bufsize: U32 val
Returns¶
- U32 val
set_so_sndbuf¶
Wrapper for the FFI call setsockopt(fd, SOL_SOCKET, SO_SNDBUF, ...)
Parameters¶
- bufsize: U32 val
Returns¶
- U32 val