toss
Work with UDP sockets on the Erlang target.
Types
A handle to a socket,
which can be used to send datagrams without specifying a destination.
This type works as a token of proof that
connect has been successfully called at least once.
It is still tied to the underlying socket, and is not unique.
pub type ConnectedSender
Errors that can occur when working with UDP sockets.
For more information on these errors see the Erlang documentation.
pub type Error {
NotOwner
Timeout
Eaddrinuse
Eaddrnotavail
Eafnosupport
Ealready
Econnaborted
Econnrefused
Econnreset
Edestaddrreq
Ehostdown
Ehostunreach
Einprogress
Eisconn
Emsgsize
Enetdown
Enetunreach
Enopkg
Enoprotoopt
Enotconn
Enotty
Enotsock
Eproto
Eprotonosupport
Eprototype
Esocktnosupport
Etimedout
Ewouldblock
Exbadport
Exbadseq
Nxdomain
Eacces
Eagain
Ebadf
Ebadmsg
Ebusy
Edeadlk
Edeadlock
Edquot
Eexist
Efault
Efbig
Eftype
Eintr
Einval
Eio
Eisdir
Eloop
Emfile
Emlink
Emultihop
Enametoolong
Enfile
Enobufs
Enodev
Enolck
Enolink
Enoent
Enomem
Enospc
Enosr
Enostr
Enosys
Enotblk
Enotdir
Enotsup
Enxio
Eopnotsupp
Eoverflow
Eperm
Epipe
Erange
Erofs
Espipe
Esrch
Estale
Etxtbsy
Exdev
}
Constructors
-
NotOwnerSocket not owned by the process trying to use it. This is documented as an error value in the
gen_udpdocumentation, but it’s unclear how to trigger it. -
TimeoutOperation timed out
-
EaddrinuseAddress already in use
-
EaddrnotavailCannot assign requested address
-
EafnosupportAddress family not supported
-
EalreadyOperation already in progress
-
EconnabortedConnection aborted
-
EconnrefusedConnection refused
-
EconnresetConnection reset by peer
-
EdestaddrreqDestination address required
-
EhostdownHost is down
-
EhostunreachNo route to host
-
EinprogressOperation now in progress
-
EisconnSocket is already connected
-
EmsgsizeMessage too long
-
EnetdownNetwork is down
-
EnetunreachNetwork is unreachable
-
EnopkgPackage not installed
-
EnoprotooptProtocol not available
-
EnotconnSocket is not connected
-
EnottyInappropriate ioctl for device
-
EnotsockSocket operation on non-socket
-
EprotoProtocol error
-
EprotonosupportProtocol not supported
-
EprototypeProtocol wrong type for socket
-
EsocktnosupportSocket type not supported
-
EtimedoutConnection timed out
-
EwouldblockOperation would block
-
ExbadportBad port number
-
ExbadseqBad sequence number
-
NxdomainNon-existent domain
-
EaccesPermission denied
-
EagainResource temporarily unavailable
-
EbadfBad file descriptor
-
EbadmsgBad message
-
EbusyDevice or resource busy
-
EdeadlkResource deadlock avoided
-
EdeadlockResource deadlock avoided
-
EdquotDisk quota exceeded
-
EexistFile exists
-
EfaultBad address
-
EfbigFile too large
-
EftypeInappropriate file type or format
-
EintrInterrupted system call
-
EinvalInvalid argument
-
EioInput/output error
-
EisdirIs a directory
-
EloopToo many levels of symbolic links
-
EmfileToo many open files
-
EmlinkToo many links
-
EmultihopMultihop attempted
-
EnametoolongFile name too long
-
EnfileToo many open files in system
-
EnobufsNo buffer space available
-
EnodevNo such device
-
EnolckNo locks available
-
EnolinkLink has been severed
-
EnoentNo such file or directory
-
EnomemOut of memory
-
EnospcNo space left on device
-
EnosrOut of streams resources
-
EnostrDevice not a stream
-
EnosysFunction not implemented
-
EnotblkBlock device required
-
EnotdirNot a directory
-
EnotsupOperation not supported
-
EnxioNo such device or address
-
EopnotsuppOperation not supported on socket
-
EoverflowValue too large for defined data type
-
EpermOperation not permitted
-
EpipeBroken pipe
-
ErangeResult too large
-
ErofsRead-only file system
-
EspipeIllegal seek
-
EsrchNo such process
-
EstaleStale file handle
-
EtxtbsyText file busy
-
ExdevCross-device link
IP address of a peer
pub type IpAddress {
Ipv4Address(Int, Int, Int, Int)
Ipv6Address(Int, Int, Int, Int, Int, Int, Int, Int)
}
Constructors
-
Ipv4Address(Int, Int, Int, Int) -
Ipv6Address(Int, Int, Int, Int, Int, Int, Int, Int)
The set of options used to open a socket.
pub opaque type SocketOptions
Values
pub fn close(socket: Socket) -> Nil
Closes the socket, freeing up any resources it uses. The socket and any associated senders can no longer be used after this.
pub fn connect_to(
socket: Socket,
host: IpAddress,
port port: Int,
) -> Result(ConnectedSender, Error)
Modifies the socket to only receive data from the specified source.
Other messages are discarded on arrival by the OS protocol stack.
Returns a handle to the socket,
which can be used to send data without specifying the destination every time.
Note that multiple calls to connect_to will override any previous calls -
all previously returned senders will also change behaviour.
pub fn connect_to_host(
socket: Socket,
host: String,
port port: Int,
) -> Result(ConnectedSender, Error)
Like connect_to, but uses a host name instead of an IP address.
If you have an IP address as a string,
convert it first using parse_ip, and use connect_to.
pub fn describe_error(error: Error) -> String
Convert an error into a human-readable description
pub fn join_multicast_group(
socket: Socket,
multicast_address: IpAddress,
interface_address: IpAddress,
) -> Result(Nil, Error)
Joins a multicast group on the given local interface.
pub fn leave_multicast_group(
socket: Socket,
multicast_address: IpAddress,
interface_address: IpAddress,
) -> Result(Nil, Error)
Leaves a multicast group on the given local interface.
pub fn local_port(socket: Socket) -> Result(Int, Nil)
Returns the local port, useful if the socket was opened with port 0
pub fn loop_mutlicast(
socket: Socket,
loop: Bool,
) -> Result(Nil, Error)
Sets whether sent multicast packets are looped back to the socket.
pub fn new(port port: Int) -> SocketOptions
Constructs the default options to open a socket with, binding it to the given local port. 0 can be used to let the OS automatically choose a free port.
pub fn open(options: SocketOptions) -> Result(Socket, Error)
Opens a UDP socket. When freshly opened, the socket will receive data from any source.
pub fn prase_ip(address: String) -> Result(IpAddress, Nil)
Tries to parse an IP address from a string.
pub fn receive(
socket: Socket,
max_length max_length: Int,
timeout_milliseconds timeout: Int,
) -> Result(#(Result(IpAddress, Nil), Int, BitArray), Error)
Receives a UDP datagram from the socket. The source address, port, and the datagram are returned on success. The maximum length will affect memory allocation, so it should be selected conservatively. The address will be an error if the sender does not have a socket address, or the Erlang VM doesn’t recognise the address (toss does currently not support local Unix domain sockets).
pub fn receive_forever(
socket: Socket,
max_length max_length: Int,
) -> Result(#(Result(IpAddress, Nil), Int, BitArray), Error)
Receives a UDP datagram from the socket, without a timeout.
See receive for details.
pub fn receive_next_datagram_as_message(
socket: Socket,
) -> Result(Nil, Error)
Switch the socket to active (once) mode, meaning that the next datagram received on the socket will be sent as an Erlang message to the socket owner’s inbox.
This is useful for when you wish to have an OTP actor handle incoming messages,
as using the receive function would result in the actor being
blocked and unable to handle other messages while waiting for the next packet.
Messages will be sent to the process that controls the socket,
which is the process that established the socket with the open function.
In order to continue receiving messages, you will need to call this function again after receiving a message. This is intended to provide backpressure to the OS socket, instead of flooding the inbox on the Erlang side, which could happen if switching to full active mode.
pub fn reuse_address(options: SocketOptions) -> SocketOptions
Allows reusing an already open local address and port
Otherwise, an error will be returned from open,
if a socket on the port is already open.
pub fn select_udp_messages(
selector: process.Selector(a),
mapper: fn(UdpMessage) -> a,
) -> process.Selector(a)
Configure a selector to receive messages from UDP sockets.
You will also need to call
receive_next_datagram_as_message
to use the selector successfully - once initially,
and again after receiving each message.
Note that this will receive messages from all UDP sockets that the process controls, rather than any specific one. If you wish to only handle messages from one socket then use one process per socket.
pub fn send(
sender: ConnectedSender,
data: BitArray,
) -> Result(Nil, Error)
Sends a UDP datagram to the peer of a connected socket.
pub fn send_to(
socket: Socket,
host: IpAddress,
port: Int,
data: BitArray,
) -> Result(Nil, Error)
Sends a UDP datagram to the specified destination by IP address.
pub fn send_to_host(
socket: Socket,
host: String,
port: Int,
data: BitArray,
) -> Result(Nil, Error)
Sends a UDP datagram to the specified destination by hostname.
pub fn use_ipv4(options: SocketOptions) -> SocketOptions
Specifies to open the socket in IPv4 mode. You can not send to IPv6 addresses when opening a socket with this option.
pub fn use_ipv6(options: SocketOptions) -> SocketOptions
Specifies to open the socket in IPv6 mode. On dual stack systems, you can send to IPv4-mapped IPv6 addresses, and respectively datagrams received from IPv4 addresses will have an address in the IPv4-mapped format.
pub fn using_interface(
options: SocketOptions,
ip: IpAddress,
) -> SocketOptions
If the local host has multiple interfaces, specifies which one to use by IP address.