Class WasiDatagramSocket
- java.lang.Object
-
- ej.wasi.io.socket.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 byMulticastSocket.Uses
MulticastSocket(a subclass ofDatagramSocket) so that multicast operations (join/leave group, set TTL, set outgoing interface) are available when requested viasetsockopt.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 voidbind(java.net.InetSocketAddress address)Bind this socket to a local address and port.booleancanReceive(byte[] peekBuffer)Non-blocking check for pending datagrams — used by the Selector forpoll_oneoffFD_READ readiness on UDP sockets.voidclose()booleangetBroadcast()Get the SO_BROADCAST socket option.java.net.InetSocketAddressgetLastSender()java.net.SocketAddressgetLocalSocketAddress()intgetReceiveBufferSize()Get the receive buffer size (SO_RCVBUF).intgetSendBufferSize()Get the send buffer size (SO_SNDBUF).FileTypegetType()Gets the WasiIO file descriptor type.booleanisBound()booleanisNonBlocking()voidjoinGroup(java.net.InetAddress group)Join a multicast group (IP_ADD_MEMBERSHIP).voidjoinGroup(java.net.InetAddress group, int interfaceIndex)Join a multicast group on a specific interface (IPV6_JOIN_GROUP).voidleaveGroup(java.net.InetAddress group)Leave a multicast group (IP_DROP_MEMBERSHIP).voidleaveGroup(java.net.InetAddress group, int interfaceIndex)Leave a multicast group on a specific interface (IPV6_LEAVE_GROUP).intreceiveFrom(byte[] data, int offset, int length)Receive a datagram.intsendTo(byte[] data, int offset, int length, java.net.InetSocketAddress destination)Send a datagram to the specified destination.voidsetBroadcast(boolean on)Enable/disable the SO_BROADCAST socket option.voidsetMulticastInterface(java.net.InetAddress iface)Set the outgoing multicast interface (IP_MULTICAST_IF).voidsetMulticastLoopback(boolean enable)Enable or disable multicast loopback (IP_MULTICAST_LOOP).voidsetMulticastNetworkInterface(int interfaceIndex)Set the outgoing multicast interface by index (IPV6_MULTICAST_IF).voidsetMulticastTTL(int ttl)Set the multicast time-to-live / hop limit (IP_MULTICAST_TTL).voidsetNonBlocking(boolean on)Enable/disable the non blocking state on this WasiIOvoidsetReceiveBufferSize(int size)Set the receive buffer size (SO_RCVBUF).voidsetReuseAddress(boolean on)Enable/disable the reuse address socket option.voidsetSendBufferSize(int size)Set the send buffer size (SO_SNDBUF).
-
-
-
Constructor Detail
-
WasiDatagramSocket
public WasiDatagramSocket() throws java.io.IOExceptionCreates an unbound datagram socket.The underlying
MulticastSocketis 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.IOExceptionBind 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.IOExceptionSend 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.IOExceptionReceive a datagram. The sender's address is stored inlastSender.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.IOExceptionNon-blocking check for pending datagrams — used by the Selector forpoll_oneoffFD_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 nextreceiveFrom(byte[], int, int)call. If a packet is already stashed, returnstrueimmediately without hitting the network.- Parameters:
peekBuffer- caller-provided reusable buffer (the Selector keeps one per UDP socket to avoid GC pressure).- Returns:
trueif 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:WasiIOGets the WasiIO file descriptor type.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
setNonBlocking
public void setNonBlocking(boolean on) throws java.io.IOExceptionDescription copied from interface:WasiSocketOptEnable/disable the non blocking state on this WasiIO- Specified by:
setNonBlockingin interfaceWasiSocketOpt- 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.IOExceptionDescription copied from interface:WasiSocketOptEnable/disable the SO_BROADCAST socket option. Only meaningful for datagram sockets; stream sockets should ignore this call.- Specified by:
setBroadcastin interfaceWasiSocketOpt- 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.IOExceptionDescription copied from interface:WasiSocketOptGet the SO_BROADCAST socket option. Only meaningful for datagram sockets; stream and server sockets always returnfalse.- Specified by:
getBroadcastin interfaceWasiSocketOpt- Returns:
- whether broadcast is enabled
- Throws:
java.io.IOException- if an I/O error occurs.
-
setReuseAddress
public void setReuseAddress(boolean on) throws java.io.IOExceptionDescription copied from interface:WasiSocketOptEnable/disable the reuse address socket option.- Specified by:
setReuseAddressin interfaceWasiSocketOpt- 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.IOExceptionDescription copied from interface:WasiSocketOptSet the receive buffer size (SO_RCVBUF).- Specified by:
setReceiveBufferSizein interfaceWasiSocketOpt- Parameters:
size- the buffer size in bytes- Throws:
java.io.IOException- if an I/O error occurs.
-
getReceiveBufferSize
public int getReceiveBufferSize() throws java.io.IOExceptionDescription copied from interface:WasiSocketOptGet the receive buffer size (SO_RCVBUF).- Specified by:
getReceiveBufferSizein interfaceWasiSocketOpt- 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.IOExceptionDescription copied from interface:WasiSocketOptSet the send buffer size (SO_SNDBUF).- Specified by:
setSendBufferSizein interfaceWasiSocketOpt- Parameters:
size- the buffer size in bytes- Throws:
java.io.IOException- if an I/O error occurs.
-
getSendBufferSize
public int getSendBufferSize() throws java.io.IOExceptionDescription copied from interface:WasiSocketOptGet the send buffer size (SO_SNDBUF).- Specified by:
getSendBufferSizein interfaceWasiSocketOpt- 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.IOExceptionSet 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.IOExceptionSet 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.IOExceptionEnable or disable multicast loopback (IP_MULTICAST_LOOP).Note: Java's
MulticastSocket.setLoopbackMode(boolean)parameter semantics are inverted —truemeans disable loopback.- Parameters:
enable-trueto enable loopback,falseto disable.- Throws:
java.io.IOException
-
joinGroup
public void joinGroup(java.net.InetAddress group) throws java.io.IOExceptionJoin 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.IOExceptionLeave 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.IOExceptionSet 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.IOExceptionJoin 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.IOExceptionLeave 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
-
-