Class WasiDatagramSocket

  • All Implemented Interfaces:
    WasiSocketOpt, WasiIO, java.io.Closeable, java.lang.AutoCloseable

    public class WasiDatagramSocket
    extends java.lang.Object
    implements WasiSocketOpt, WasiIO
    WASI datagram (UDP) socket backed by MulticastSocket.

    Uses MulticastSocket (a subclass of DatagramSocket) so that multicast operations (join/leave group, set TTL, set outgoing interface) are available when requested via setsockopt.

    Unlike WasiSocket (stream/TCP), datagrams are connectionless: each send/receive carries its own destination/source address.

    • Constructor Summary

      Constructors 
      Constructor Description
      WasiDatagramSocket()
      Creates an unbound datagram socket.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void bind​(java.net.InetSocketAddress address)
      Bind this socket to a local address and port.
      boolean canReceive​(byte[] peekBuffer)
      Non-blocking check for pending datagrams — used by the Selector for poll_oneoff FD_READ readiness on UDP sockets.
      void close()  
      boolean getBroadcast()
      Get the SO_BROADCAST socket option.
      java.net.InetSocketAddress getLastSender()  
      java.net.SocketAddress getLocalSocketAddress()  
      int getReceiveBufferSize()
      Get the receive buffer size (SO_RCVBUF).
      int getSendBufferSize()
      Get the send buffer size (SO_SNDBUF).
      FileType getType()
      Gets the WasiIO file descriptor type.
      boolean isBound()  
      boolean isNonBlocking()  
      void joinGroup​(java.net.InetAddress group)
      Join a multicast group (IP_ADD_MEMBERSHIP).
      void joinGroup​(java.net.InetAddress group, int interfaceIndex)
      Join a multicast group on a specific interface (IPV6_JOIN_GROUP).
      void leaveGroup​(java.net.InetAddress group)
      Leave a multicast group (IP_DROP_MEMBERSHIP).
      void leaveGroup​(java.net.InetAddress group, int interfaceIndex)
      Leave a multicast group on a specific interface (IPV6_LEAVE_GROUP).
      int receiveFrom​(byte[] data, int offset, int length)
      Receive a datagram.
      int sendTo​(byte[] data, int offset, int length, java.net.InetSocketAddress destination)
      Send a datagram to the specified destination.
      void setBroadcast​(boolean on)
      Enable/disable the SO_BROADCAST socket option.
      void setMulticastInterface​(java.net.InetAddress iface)
      Set the outgoing multicast interface (IP_MULTICAST_IF).
      void setMulticastLoopback​(boolean enable)
      Enable or disable multicast loopback (IP_MULTICAST_LOOP).
      void setMulticastNetworkInterface​(int interfaceIndex)
      Set the outgoing multicast interface by index (IPV6_MULTICAST_IF).
      void setMulticastTTL​(int ttl)
      Set the multicast time-to-live / hop limit (IP_MULTICAST_TTL).
      void setNonBlocking​(boolean on)
      Enable/disable the non blocking state on this WasiIO
      void setReceiveBufferSize​(int size)
      Set the receive buffer size (SO_RCVBUF).
      void setReuseAddress​(boolean on)
      Enable/disable the reuse address socket option.
      void setSendBufferSize​(int size)
      Set the send buffer size (SO_SNDBUF).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WasiDatagramSocket

        public WasiDatagramSocket()
                           throws java.io.IOException
        Creates an unbound datagram socket.

        The underlying MulticastSocket is created immediately so that socket options (buffer sizes, reuse-address, etc.) can be applied before binding without buffering them locally.

        Throws:
        java.io.IOException
    • Method Detail

      • bind

        public void bind​(java.net.InetSocketAddress address)
                  throws java.io.IOException
        Bind this socket to a local address and port.
        Throws:
        java.io.IOException
      • sendTo

        public int sendTo​(byte[] data,
                          int offset,
                          int length,
                          java.net.InetSocketAddress destination)
                   throws java.io.IOException
        Send a datagram to the specified destination.

        If the socket has not been explicitly bound, the OS auto-binds it to an ephemeral port on the first send.

        Returns:
        the number of bytes sent.
        Throws:
        java.io.IOException
      • receiveFrom

        public int receiveFrom​(byte[] data,
                               int offset,
                               int length)
                        throws java.io.IOException
        Receive a datagram. The sender's address is stored in lastSender.

        If a packet was stashed by canReceive(byte[]), it is consumed first without hitting the network again.

        Returns:
        the number of bytes received, or 0 if non-blocking and no data available.
        Throws:
        java.io.IOException
      • canReceive

        public boolean canReceive​(byte[] peekBuffer)
                           throws java.io.IOException
        Non-blocking check for pending datagrams — used by the Selector for poll_oneoff FD_READ readiness on UDP sockets.

        Follows the same destructive-peek pattern as WasiServerSocket.canAccept(): temporarily sets a short timeout, attempts a receive, and stashes any packet for the next receiveFrom(byte[], int, int) call. If a packet is already stashed, returns true immediately without hitting the network.

        Parameters:
        peekBuffer - caller-provided reusable buffer (the Selector keeps one per UDP socket to avoid GC pressure).
        Returns:
        true if a datagram is available (already stashed or just received).
        Throws:
        java.io.IOException
      • getLastSender

        @Nullable
        public java.net.InetSocketAddress getLastSender()
        Returns:
        the address of the last received datagram's sender, or null.
      • getLocalSocketAddress

        @Nullable
        public java.net.SocketAddress getLocalSocketAddress()
      • isBound

        public boolean isBound()
      • getType

        public FileType getType()
        Description copied from interface: WasiIO
        Gets the WasiIO file descriptor type.
        Specified by:
        getType in interface WasiIO
        Returns:
        the WasiIO file descriptor type.
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • setNonBlocking

        public void setNonBlocking​(boolean on)
                            throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Enable/disable the non blocking state on this WasiIO
        Specified by:
        setNonBlocking in interface WasiSocketOpt
        Parameters:
        on - whether to enable or disable the non-blocking socket option
        Throws:
        java.io.IOException - if an I/O error occurs.
      • isNonBlocking

        public boolean isNonBlocking()
      • setBroadcast

        public void setBroadcast​(boolean on)
                          throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Enable/disable the SO_BROADCAST socket option. Only meaningful for datagram sockets; stream sockets should ignore this call.
        Specified by:
        setBroadcast in interface WasiSocketOpt
        Parameters:
        on - whether to enable or disable broadcast
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getBroadcast

        public boolean getBroadcast()
                             throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Get the SO_BROADCAST socket option. Only meaningful for datagram sockets; stream and server sockets always return false.
        Specified by:
        getBroadcast in interface WasiSocketOpt
        Returns:
        whether broadcast is enabled
        Throws:
        java.io.IOException - if an I/O error occurs.
      • setReuseAddress

        public void setReuseAddress​(boolean on)
                             throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Enable/disable the reuse address socket option.
        Specified by:
        setReuseAddress in interface WasiSocketOpt
        Parameters:
        on - whether to enable or disable the reuse address socket option
        Throws:
        java.io.IOException - if an I/O error occurs.
      • setReceiveBufferSize

        public void setReceiveBufferSize​(int size)
                                  throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Set the receive buffer size (SO_RCVBUF).
        Specified by:
        setReceiveBufferSize in interface WasiSocketOpt
        Parameters:
        size - the buffer size in bytes
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getReceiveBufferSize

        public int getReceiveBufferSize()
                                 throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Get the receive buffer size (SO_RCVBUF).
        Specified by:
        getReceiveBufferSize in interface WasiSocketOpt
        Returns:
        the buffer size in bytes
        Throws:
        java.io.IOException - if an I/O error occurs.
      • setSendBufferSize

        public void setSendBufferSize​(int size)
                               throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Set the send buffer size (SO_SNDBUF).
        Specified by:
        setSendBufferSize in interface WasiSocketOpt
        Parameters:
        size - the buffer size in bytes
        Throws:
        java.io.IOException - if an I/O error occurs.
      • getSendBufferSize

        public int getSendBufferSize()
                              throws java.io.IOException
        Description copied from interface: WasiSocketOpt
        Get the send buffer size (SO_SNDBUF).
        Specified by:
        getSendBufferSize in interface WasiSocketOpt
        Returns:
        the buffer size in bytes
        Throws:
        java.io.IOException - if an I/O error occurs.
      • setMulticastInterface

        public void setMulticastInterface​(java.net.InetAddress iface)
                                   throws java.io.IOException
        Set the outgoing multicast interface (IP_MULTICAST_IF).
        Parameters:
        iface - the IPv4 address of the local interface to use.
        Throws:
        java.io.IOException
      • setMulticastTTL

        public void setMulticastTTL​(int ttl)
                             throws java.io.IOException
        Set the multicast time-to-live / hop limit (IP_MULTICAST_TTL).
        Parameters:
        ttl - hop limit (0–255).
        Throws:
        java.io.IOException
      • setMulticastLoopback

        public void setMulticastLoopback​(boolean enable)
                                  throws java.io.IOException
        Enable or disable multicast loopback (IP_MULTICAST_LOOP).

        Note: Java's MulticastSocket.setLoopbackMode(boolean) parameter semantics are inverted — true means disable loopback.

        Parameters:
        enable - true to enable loopback, false to disable.
        Throws:
        java.io.IOException
      • joinGroup

        public void joinGroup​(java.net.InetAddress group)
                       throws java.io.IOException
        Join a multicast group (IP_ADD_MEMBERSHIP).
        Parameters:
        group - the multicast group address to join.
        Throws:
        java.io.IOException
      • leaveGroup

        public void leaveGroup​(java.net.InetAddress group)
                        throws java.io.IOException
        Leave a multicast group (IP_DROP_MEMBERSHIP).
        Parameters:
        group - the multicast group address to leave.
        Throws:
        java.io.IOException
      • setMulticastNetworkInterface

        public void setMulticastNetworkInterface​(int interfaceIndex)
                                          throws java.io.IOException
        Set the outgoing multicast interface by index (IPV6_MULTICAST_IF).
        Parameters:
        interfaceIndex - the OS interface index (0 = default).
        Throws:
        java.io.IOException
      • joinGroup

        public void joinGroup​(java.net.InetAddress group,
                              int interfaceIndex)
                       throws java.io.IOException
        Join a multicast group on a specific interface (IPV6_JOIN_GROUP).
        Parameters:
        group - the multicast group address.
        interfaceIndex - the OS interface index (0 = default).
        Throws:
        java.io.IOException
      • leaveGroup

        public void leaveGroup​(java.net.InetAddress group,
                               int interfaceIndex)
                        throws java.io.IOException
        Leave a multicast group on a specific interface (IPV6_LEAVE_GROUP).
        Parameters:
        group - the multicast group address.
        interfaceIndex - the OS interface index (0 = default).
        Throws:
        java.io.IOException