moonbitlang/core/async/socket does not have a README file

    InvalidAddr

    Failed to parse an address string

    ResolveHostnameError

    Error message from failure in resolving a network hostname.

    ResolveHostnameError::to_string

    fn ResolveHostnameError::to_string(self : ResolveHostnameError) -> String

    Addr

    pub struct Addr {
    ip : IpAddr
    port : UInt16
    } derive(Compare, Eq, Hash)

    Type for a network address containing both IP and port
    impl Show for Addr

    Addr::Addr

    fn Addr::Addr(ip : IpAddr, port : UInt16) -> Addr

    Create a network address from IP + port

    Addr::compare

    fn Addr::compare(Addr, Addr) -> Int
    automatically derived

    Addr::equal

    fn Addr::equal(Addr, Addr) -> Bool
    automatically derived

    Addr::hash

    fn Addr::hash(self : Addr) -> Int
    automatically derived

    Addr::parse

    fn Addr::parse(src : StringView) -> Addr raise

    Parse a string into IPv4 or IPv6 address with port, format should be ip:port for IPv4 or [ip]:port for IPv6

    Addr::resolve

    async fn Addr::resolve(host : StringView, port~ : UInt16, protocol? : IpProtocolPreference) -> Addr

    Resolve a IPv4 or IPv6 address by hostname.

    By default, Addr::resolve return the first available address. Preference on IPv4 v.s. IPv6 can be configured via protocol. See IpProtocolPreference for available options.

    Literal IP address would be returned directly without DNS resolution attempt. Literal IPv6 address may contain optional bracket.

    Addr::to_string

    fn Addr::to_string(self : Addr) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    IpAddr

    pub enum IpAddr {
    Ipv4(UInt)
    Ipv6(Bytes, scope_id~ : UInt)
    } derive(Compare, Eq, Hash)

    Type for IP address
    impl Show for IpAddr

    IpAddr::IpAddr

    #alias(parse)
    fn IpAddr::IpAddr(src : StringView) -> IpAddr raise

    Parse a string into IPv4 or IPv6 address. IPv6 address may contain a optional bracket.

    IpAddr::compare

    fn IpAddr::compare(IpAddr, IpAddr) -> Int
    automatically derived

    IpAddr::equal

    fn IpAddr::equal(IpAddr, IpAddr) -> Bool
    automatically derived

    IpAddr::hash

    fn IpAddr::hash(self : IpAddr) -> Int
    automatically derived

    IpAddr::is_multicast

    fn IpAddr::is_multicast(ip : IpAddr) -> Bool

    Check if an IP address is a multicast address

    IpAddr::to_string

    fn IpAddr::to_string(self : IpAddr) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    IpAddr::v4

    fn IpAddr::v4(ip : UInt) -> IpAddr

    Create a IPv4 address without port

    IpAddr::v6

    fn IpAddr::v6(ip : Bytes, scope_id? : UInt) -> IpAddr

    Create a IPv6 address without port, with a optional scope ID/zone index. The address part must be exactly 16 bytes/128 bits long.

    IpProtocolPreference

    pub(all) enum IpProtocolPreference {
    OnlyV4
    OnlyV6
    FavorV4
    FavorV6
    NoPreference
    } derive(
    Debug
    )

    IPv4/IPv6 preference when resolving a host name

    Tcp

    type Tcp

    A Tcp connection
    impl BinaryReader for Tcp
    impl BinaryWriter for Tcp

    Tcp::close

    fn Tcp::close(self : Tcp) -> Unit

    Close a TCP connection, release associated resource

    Tcp::connect

    async fn Tcp::connect(addr : Addr) -> Tcp

    Make connection to a remote address using connect(2) system call.

    Tcp::connect_to_host

    async fn Tcp::connect_to_host(host : StringView, port~ : UInt16, protocol? : IpProtocolPreference) -> Tcp

    Connect to a remote host using the happy eyeball algorithm. Preference on IPv4 v.s. IPv6 can be configured via protocol:

    • if protocol is NoPreference (the default behavior), the first successful connection will be returned, regardless of the protocol.
    • if protocol is FavorV4, an IPv6 connection will only be returned if no IPv4 address is available
    • if protocol is OnlyV4, connect_to_host will fail if no IPv4 address is available
    • if protocol is FavorV6, an IPv6 connection will only be returned if no IPv4 address is available
    • if protocol is OnlyV6, connect_to_host will fail if no IPv4 address is available

    Literal IP address would be used for connection directly without DNS resolution attempt. Literal IPv6 address may contain optional bracket.

    Tcp::drop_all

    async fn Tcp::drop_all(self : Tcp) -> Int64 raise
    ReaderError

    Drop all remaining content from a binary reader, until EOF is reached.

    Tcp::drop_exactly

    async fn Tcp::drop_exactly(self : Tcp, len : Int) -> Unit raise
    ReaderError

    reader.drop_exactly(len) drops exactly len bytes from the reader. drop_exactly waits until enough bytes are available. If EOF is reached before enough data is dropped, UnexpectedEof will be raised.

    Tcp::enable_keepalive

    fn Tcp::enable_keepalive(self : Tcp, idle_before_keep_alive? : Int, keep_alive_count? : Int, keep_alive_interval? : Int) -> Unit raise

    Enable TCP keep alive on the socket.

    idle_before_keep_alive is the duration of idle time in seconds to wait before sending the first keep alive probe.

    keep_alive_count is the number of keep alive probe to try before closing the connection.

    keep_alive_interval is the time in seconds between two keep alive probes.

    Tcp::fd

    Get the underlying file descriptor/Windows HANDLE of a TCP connection

    Tcp::local_addr

    fn Tcp::local_addr(self : Tcp) -> Addr

    Get the local address of a TCP connection

    Tcp::peek

    async fn Tcp::peek(self : Tcp) -> BytesView? raise
    ReaderError

    reader.peek() fetches and returns a chunk of data from the reader. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    The returned chunk will contain at least one byte. If EOF is reached before any data is fetched, None will be returned.

    reader.peek() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek() is cancelled, no data will be lost.

    Tcp::peek_at_least

    async fn Tcp::peek_at_least(self : Tcp, min_len : Int) -> BytesView raise
    ReaderError

    reader.peek_at_least(min_len) fetches and returns a chunk containing at least min_len bytes without consuming it. The returned chunk may be larger than min_len.

    The returned view is valid only until another operation is invoked on the same reader. Use .to_owned() to keep the data persistently.

    If EOF is reached before enough data is fetched, UnexpectedEof will be raised. Any shorter trailing data remains buffered and can be fetched by a later reader operation.

    reader.peek_at_least() is cancellation safe as long as the underlying reader implementation is cancellation safe. When it is cancelled, no data will be lost.

    Tcp::peek_exactly

    async fn Tcp::peek_exactly(self : Tcp, len : Int) -> BytesView raise
    ReaderError

    reader.peek_exactly(len) fetches exactly len bytes from the reader. It will wait until enough number of bytes is available. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek_exactly() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    If EOF is reached before enough data is read, UnexpectedEof will be raised. In this case remaining data remain buffered, and can be fetched by other reader operations later.

    reader.peek_exactly() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek_exactly() is cancelled, no data will be lost.

    Tcp::peek_until

    async fn Tcp::peek_until(self : Tcp, pattern : BytesView, include_delimiter? : Bool) -> BytesView? raise
    ReaderError

    reader.peek_until(pat, include_delimiter? : Bool = false) fetches content from reader until the byte string pat is found in the reader stream. The returned data will not get consumed from the reader: other reader API can still fetch them later.

    The chunk returned by reader.peek_until() is a copy-free slice of the reader's internal buffer, and its content will be overwritten by subsequent reader operations. So caller of peek MUST NOT use the returned slice after any other BinaryReader operation is invoked on the same reader. If the data need to be kept persistent, copy it via .to_owned().

    If pat is found in the input stream, content before that occurrence is returned. If include_delimiter is true, pat itself is included at the end of the result. The returned data may be empty in this case if include_delimiter is false.

    EOF is implicitly treated as a delimiter by reader.peek_until(). If pat is not found in the input stream when EOF is reached, trailing data before EOF will be returned (or None if there is no trailing data). In this case, the returned data contains no trailing delimiter even if include_delimiter=true. This can be used to detect the presence of trailing delimiter in the input stream.

    reader.peek_until() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.peek_until() is cancelled, no data will be lost.

    Tcp::read

    async fn Tcp::read(self : Tcp) -> Bytes? raise
    ReaderError

    reader.read() fetches and returns a chunk of data from the reader. It will return as fast as possible when new data become available.

    The returned chunk will contain at least one byte. If EOF is reached before any data is fetched, None will be returned.

    reader.read() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.read() is cancelled, no data will be lost.

    Tcp::read_all

    async fn Tcp::read_all(self : Tcp) -> Bytes raise
    ReaderError

    Read all remaining content from a binary reader, until EOF is reached.

    Tcp::read_at_least

    async fn Tcp::read_at_least(self : Tcp, min_len : Int) -> Bytes raise
    ReaderError

    reader.read_at_least(min_len) fetches and returns an owned chunk containing at least min_len bytes. The returned chunk may be larger than min_len.

    If EOF is reached before enough data is fetched, UnexpectedEof will be raised. Any shorter trailing data remains buffered and can be fetched through another reader operation later.

    reader.read_at_least() is cancellation safe as long as the underlying reader implementation is cancellation safe. When it is cancelled, no data will be lost.

    Tcp::read_exactly

    async fn Tcp::read_exactly(self : Tcp, len : Int) -> Bytes raise
    ReaderError

    reader.read_exactly(len) reads exactly len bytes from the reader. It will wait until enough number of bytes is available. If EOF is reached before enough data is read, UnexpectedEof will be raised.

    Tcp::read_into

    async fn Tcp::read_into(self : Tcp, dst : MutArrayView[Byte]) -> Int raise
    ReaderError

    reader.read_into(dst) reads at most dst.length() bytes from the reader into dst. The number of bytes actually read is returned. Note that the return value may be smaller than dst.length() if not enough data is immediately available. If EOF is reached and no more data is available, zero will be returned.

    Tcp::read_until

    async fn Tcp::read_until(self : Tcp, pattern : BytesView, include_delimiter? : Bool) -> Bytes? raise
    ReaderError

    reader.read_until(pat, include_delimiter? : Bool = false) reads content from reader until the byte string pat is found in the reader stream.

    If pat is found in the input stream, content before that occurrence is returned and the delimiter is consumed. If include_delimiter is true, pat itself is included at the end of the result. The returned data may be empty in this case if include_delimiter is false.

    EOF is implicitly treated as a delimiter by reader.read_until(). If pat is not found in the input stream when EOF is reached, trailing data before EOF will be returned (or None if there is no trailing data). In this case, the returned data contains no trailing delimiter even if include_delimiter=true. This can be used to detect the presence of trailing delimiter in the input stream.

    reader.read_until() is cancellation safe as long as the underlying reader implementation is cancellation safe. When reader.read_until() is cancelled, no data will be lost.

    Tcp::remote_addr

    fn Tcp::remote_addr(self : Tcp) -> Addr

    Get the remote peer's address of a TCP connection

    Tcp::write

    async fn Tcp::write(self : Tcp, data : BytesView) -> Unit raise
    WriterError

    writer.write(data) writes a chunk of data into the writer. write will ensure that all content of data has been written before returning. However, write only guarantees that all data is committed to the underlying writer object; it does not guarantee writer-specific properties, such as written data being flushed to persistent storage for a file stream.

    Tcp::write_reader

    async fn Tcp::write_reader(self : Tcp, reader : &
    BinaryReader
    ) -> Unit

    writer.write_reader(reader) continuously reads from input stream reader and writes all its content to writer until EOF is reached.

    write_reader only raises two kinds of error: @io.ReaderError from the reader, or @io.WriterError from the writer.

    TcpServer

    type TcpServer

    A TCP server that can accept new connections

    TcpServer::TcpServer

    #alias(new, deprecated="`new` is deprecated, use `TcpServer` instead")
    async fn TcpServer::TcpServer(addr : Addr, dual_stack? : Bool, reuse_addr? : Bool, reuse_port_lb? : Bool) -> TcpServer

    Create a TCP server that is bound to addr, listening for incoming connections.

    If addr is the IPv6 wildcard address [::] and dual_stack is true (true by default), the server will work in dual stack mode, accepting connections from both IPv4 clients and IPv6 clients. The address of IPv4 clients are represented via IPv4-mapped IPv6 address.

    If addr is not [::], dual_stack is ignored.

    If the port of addr is zero, the server will be bound to a random port, assigned by the operating system. The actual listen address can be retrieved via .local_addr().

    If reuse_addr is true (false by default), the SO_REUSEADDR option will be enabled on the server, allowing binding to addr even if there are still dead-ish TIME_WAIT sockets on that address.

    On Windows, TIME_WAIT sockets are always ignored, so the behavior is always reuse_addr=true and reuse_addr will be ignored.

    On MacOS (BSD), reuse_addr has the extra side effect that a server binding to a specific address may "steal" the specific interface from a previous wildcard bind (0.0.0.0), and similarly a server binding to a wildcard address can bind to the remaining interfaces if a bind to a specific interface already exists. This side effect cannot be isolated, so reuse_addr should be used with this in mind.

    If reuse_port_lb is true (false by default), SO_REUSEPORT is enabled so several sockets — typically one per worker process — can bind the same address at once while the kernel load-balances incoming connections across them. This is the usual basis for a multi-process server: a supervisor execs N workers, each binds the same address with this option, and the kernel spreads connections, so no socket needs to be shared between processes.

    The load-balancing semantic is Linux-only. SO_REUSEPORT also exists on the BSDs and MacOS, but there it does not evenly distribute connections — which socket accepts a given connection is unspecified — so reuse_port_lb takes effect only on Linux, including Wasm hosted by Moonrun on Linux, and is ignored everywhere else (BSD, MacOS, Windows).

    TcpServer::accept

    async fn TcpServer::accept(self : TcpServer) -> Tcp

    Accept a new connection on a listening TCP server. The address of the peer is available via the returned connection's .remote_addr() method.

    TcpServer::close

    fn TcpServer::close(self : TcpServer) -> Unit

    Close a TCP server, release associated resource

    TcpServer::fd

    Get the underlying file descriptor/Windows HANDLE of a TCP server

    TcpServer::local_addr

    fn TcpServer::local_addr(self : TcpServer) -> Addr

    Get the actual listen address of a TCP server

    TcpServer::run_forever

    async fn TcpServer::run_forever(self : TcpServer, f : async (Tcp) -> Unit, allow_failure? : Bool, max_connections? : Int) -> Unit

    Start the main loop of a TCP server, keep listening for new connections and handle connections using the callback f. f will be supplied the new connection. The address of the client is available via conn.remote_addr().

    The client connection will be closed automatically after f exits, so f must not close the client connection. The server will be automatically closed if run_forever fails.

    If allow_failure is true (true by default), failure in f will be silently ignored.

    If max_connections is present, at most max_connections clients are allowed in parallel. New clients will only get handled after a previous client terminates.

    UdpClient

    type UdpClient

    A UDP client "connected" to a remote UDP server.

    UDP is a connectionless protocol, here "connected" means the client always send packet to and receive packet from the specified remote server.

    UDP does not really have a clear client/server distinction, especially when multicast is involved. The essence of UdpClient is a UDP socket without a fixed/well-known port.

    UdpClient::UdpClient

    #alias(new, deprecated="`new` is deprecated, use `UdpClient` instead")
    async fn UdpClient::UdpClient(addr : Addr) -> UdpClient

    Create a new UDP client connected to server at addr. The client always send packet to and receive packet from the specified remote server.

    If addr is a multicast address, connect will not be called under the hood, and the client may receive packets from arbitrary peer. However, since the port of the client is random, the client effectively can only receive packets from multicast servers that have received packets from this client.

    UdpClient::close

    fn UdpClient::close(self : UdpClient) -> Unit

    Close a UDP client, release associated resource

    UdpClient::connect

    fn UdpClient::connect(self : UdpClient, addr : Addr) -> Unit raise

    Connect the client to a remote address, subsequently the client will send packets to this address, and only receive packets from this address.

    Unicast UDP clients are connected automatically, so connect is mainly useful for turning a multicast client into a unicast one. The typical workflow is to create the client as a multicast client, send multicast packets, and wait for response from servers listening on the multicast address. Once a responding server is found, use connect to turn the client into a unicast one, and start point-to-point communication with the discovered server.

    UdpClient::fd

    Get the underlying file descriptor/Windows HANDLE of a UDP client

    UdpClient::local_addr

    fn UdpClient::local_addr(self : UdpClient) -> Addr

    Get the local address of a UDP client

    UdpClient::recv

    async fn UdpClient::recv(self : UdpClient, max_len? : Int) -> Bytes

    Receive packet from a UDP client. max_len can be used to specify max length of the UDP packet. If max_len is not specified, the maximum UDP packet size of 65535 bytes is used.

    UDP is a datagram based protocol, and recv will always receive exactly one UDP packet.

    At most one task can read from a UDP client at any time. To allow multiple reader, use a worker task for reading and use @async.Queue to distribute the data.

    UdpClient::recv_into

    async fn UdpClient::recv_into(self : UdpClient, dst : MutArrayView[Byte]) -> Int

    Receive packet from a UDP client. At most dst.length() bytes of data will be written to dst. The number of received bytes will be returned.

    UDP is a datagram based protocol, and recv_into will always receive exactly one UDP packet.

    At most one task can read from a UDP client at any time. To allow multiple reader, use a worker task for reading and use @async.Queue to distribute the data.

    UdpClient::recv_into_with_addr

    async fn UdpClient::recv_into_with_addr(self : UdpClient, dst : MutArrayView[Byte]) -> (Int, Addr)

    Similar to recv_into, but additionally return the address of the packet's sender. Unicast UdpClient can only receive traffic from the target server, so this is mainly useful for multicast clients.

    UdpClient::recv_with_addr

    async fn UdpClient::recv_with_addr(self : UdpClient, max_len? : Int) -> (Bytes, Addr)

    Similar to recv, but additionally return the address of the packet's sender. Unicast UdpClient can only receive traffic from the target server, so this is mainly useful for multicast clients.

    UdpClient::send

    async fn UdpClient::send(self : UdpClient, data : BytesView) -> Unit

    Send data through a UDP client.

    UDP is a datagram based protocol, every call of send will send exactly one packet.

    At most one task can write to a UDP client at any time. To allow multiple writers, use a worker task for reading and use @async.Queue to gather the data.

    UdpClient::set_multicast_interface

    fn UdpClient::set_multicast_interface(self : UdpClient, addr : IpAddr) -> Unit raise

    Set the outgoing interface used for sending multicast packets with this client.

    This function is IPv4-only. For IPv6, use set_multicast_interface_v6 instead.

    UdpClient::set_multicast_interface_v6

    fn UdpClient::set_multicast_interface_v6(self : UdpClient, interface : String) -> Unit raise

    Set the outgoing interface used for sending multicast packets with this client. The interface is provided via IPv6 zone suffix index. It can either be a decimal string for a raw interface index, or a network interface name.

    This function is IPv6-only. For IPv4, use set_multicast_interface instead.

    UdpClient::set_multicast_loopback

    fn UdpClient::set_multicast_loopback(self : UdpClient, loopback : Bool) -> Unit raise

    Set the multicast loopback option for this client. On Windows: make sure multicast packets sent from local machine is not received by this client. On other systems: make sure multicast packets sent by this client are not routed back to local machine

    Note that the loopback setting does not apply for localhost traffic.

    UdpClient::set_multicast_ttl

    fn UdpClient::set_multicast_ttl(self : UdpClient, ttl : Int) -> Unit raise

    Set the multicast TTL (or hop limit for IPv6) for this client. ttl=0 means packet never goes out of local machine. ttl=1 means packet never goes out of local subnet.

    UdpServer

    type UdpServer

    A UDP server bound to a listen address and receive packets from clients.

    UDP does not really have a clear client/server distinction, especially when multicast is involved. The essence of UdpServer is a UDP socket with a fixed/well-known port.

    UdpServer::UdpServer

    #alias(new, deprecated="`new` is deprecated, use `UdpServer` instead")
    async fn UdpServer::UdpServer(addr : Addr, dual_stack? : Bool, reuse_port_lb? : Bool) -> UdpServer

    Create a UDP server listening at addr.

    If addr is the IPv6 wildcard address [::] and dual_stack is true (true by default), the server will work in dual stack mode, receiving packets from both IPv4 peers and IPv6 peers. The address of IPv4 peers are represented via IPv4-mapped IPv6 address.

    If addr is not [::], dual_stack is ignored.

    If the port of addr is zero, the server will be bound to a random port, assigned by the operating system. The actual listen address can be retrieved via .local_addr().

    The server will only receive packets whose destination address matches addr. So if you want to receive multicast traffic, the server should be bound to 0.0.0.0 or [::]. DO NOT bind the server to a multicast address: this is not supported on Windows. For multicast-only servers, use UdpServer::multicast() instead.

    If reuse_port_lb is true (false by default), SO_REUSEPORT is enabled so several sockets — typically one per worker process — can bind the same address at once while the kernel load-balances incoming datagrams across them, keeping datagrams from one peer address on one socket while the set of bound sockets stays the same.

    As with TcpServer, the load-balancing semantic is Linux-only, so reuse_port_lb takes effect only on Linux, including Wasm hosted by Moonrun on Linux, and is ignored everywhere else (BSD, MacOS, Windows).

    UdpServer::close

    fn UdpServer::close(self : UdpServer) -> Unit

    Close a UDP server, release associated resource

    UdpServer::fd

    Get the underlying file descriptor/Windows HANDLE of a UDP server

    UdpServer::join_multicast_group

    fn UdpServer::join_multicast_group(self : UdpServer, multi_addr : IpAddr, interface_addr? : IpAddr) -> Unit raise

    Let the server join multicast group at address multi_addr, via interface interface_addr. interface_addr defaults to a random interface chosen by the OS.

    Note that join_multicast_group is mainly about IGMP related stuff, it does not serve as a packet filter. A UDP server can receive all packets whose destination match the server's address. Some consquence of this:

    • if the server is bound to a specific interface, it will not receive any multicast traffic, because multicast traffic has special destination IP

    • multicast-only server created by UdpServer::multicast cannot join multiple multicast groups with different IP address.

    This function is IPv4 only. For IPv6 multicast, use join_multicast_group_v6 instead.

    UdpServer::join_multicast_group_v6

    fn UdpServer::join_multicast_group_v6(self : UdpServer, multi_addr : IpAddr, interface? : String) -> Unit raise

    Let the server join multicast group at address multi_addr, via interface interface. interface, if provided, should use IPv6 zone suffix syntax, meaning it can either be a decimal string for raw interface index, or a network interface name. interface defaults to "0", meaning a default output interface chosen by the OS. For interface-local/link-local multicast packets, the interface must be explicitly specified.

    Note that join_multicast_group is mainly about IGMP related stuff, it does not serve as a packet filter. A UDP server can receive all packets whose destination match the server's address. Some consquence of this:

    • if the server is bound to a specific interface, it will not receive any multicast traffic, because multicast traffic has special destination IP

    This function is IPv6 only. For IPv4 multicast, use join_multicast_group instead.

    UdpServer::local_addr

    fn UdpServer::local_addr(self : UdpServer) -> Addr

    Get the local address of a UDP server

    UdpServer::multicast

    async fn UdpServer::multicast(multi_addr : Addr, interface_addr? : IpAddr) -> UdpServer

    Create a multicast-only UDP server listening for multicast packets towards multi_addr. The server will only receive packets towards multi_addr, no unicast traffic will be received.

    Multicast only servers created via UdpServer::multicast are shared. There can be multiple such server listening on the same address at the same time.

    The server will automatically join the IP multicast group at multi_addr, using interface interface_addr (defaults to a random interface chosen by the OS). Only the IP part of interface_addr matters, the port is irrelevant.

    Due to OS limitation, currently every multicast-only server can listen for only one multicast IP. Calling join_multicast_group on server created by UdpServer::multicast with different address simply does not work. Users can join the same IP with a different interface using join_multicast_group, though.

    Currently this function is IPv4 only. There is no simple way to implement multicast-only IPv6 socket on Linux/MacOS.

    UdpServer::recv

    async fn UdpServer::recv(self : UdpServer, max_len? : Int) -> (Bytes, Addr)

    Receive packet from a UDP server, and obtain the source address of the packet. max_len can be used to specify max length of the UDP packet. If max_len is not specified, the maximum UDP packet size of 65535 bytes is used. The received packet and the source address of the packet will be returned.

    UDP is a datagram based protocol, and recv will always receive exactly one UDP packet.

    At most one task can read from a UDP server at any time. To allow multiple reader, use a worker task for reading and use @async.Queue to distribute the data.

    UdpServer::recv_into

    async fn UdpServer::recv_into(self : UdpServer, dst : MutArrayView[Byte]) -> (Int, Addr)

    Receive packet from a UDP server, and obtain the source address of the packet. At most dst.length() bytes of data will be written to dst. The number of received bytes and the source address of the packet will be returned.

    UDP is a datagram based protocol, and recv_into will always receive exactly one UDP packet.

    At most one task can read from a UDP server at any time. To allow multiple reader, use a worker task for reading and use @async.Queue to distribute the data.

    UdpServer::send

    async fn UdpServer::send(self : UdpServer, data : BytesView, addr : Addr) -> Unit

    Send a packet to addr through a UDP server.

    UDP is a datagram based protocol, every call of send will send exactly one packet.

    At most one task can write to a UDP server at any time. To allow multiple writers, use a worker task for reading and use @async.Queue to gather the data.

    UdpServer::set_multicast_interface

    fn UdpServer::set_multicast_interface(self : UdpServer, addr : IpAddr) -> Unit raise

    Set the outgoing interface used for sending multicast packets with this server.

    This function is IPv4-only. For IPv6, use set_multicast_interface_v6 instead.

    UdpServer::set_multicast_interface_v6

    fn UdpServer::set_multicast_interface_v6(self : UdpServer, interface : String) -> Unit raise

    Set the outgoing interface used for sending multicast packets with this client. The interface is provided via IPv6 zone suffix index. It can either be a decimal string for a raw interface index, or a network interface name.

    This function is IPv6-only. For IPv4, use set_multicast_interface instead.

    UdpServer::set_multicast_loopback

    fn UdpServer::set_multicast_loopback(self : UdpServer, loopback : Bool) -> Unit raise

    Set the multicast loopback option for this server. On Windows: make sure multicast packets sent from local machine is not received by this server. On other systems: make sure multicast packets sent by this server are not routed back to local machine

    Note that the loopback setting does not apply for localhost traffic.

    UdpServer::set_multicast_ttl

    fn UdpServer::set_multicast_ttl(self : UdpServer, ttl : Int) -> Unit raise

    Set the multicast TTL (or hop limit for IPv6) for this client. ttl=0 means packet never goes out of local machine. ttl=1 means packet never goes out of local subnet.