Class UdpTransport
java.lang.Object
com.trdp.network.UdpTransport
- All Implemented Interfaces:
AutoCloseable
DatagramChannel-based UDP transport with multicast group management
and configurable socket options (bind address, TTL, QoS).
Sets both multicast TTL (StandardSocketOptions.IP_MULTICAST_TTL) and
unicast TTL (IP_TTL via JNA setsockopt through
NativeSocketOptions). Unicast TTL requires JVM flag
--add-opens java.base/sun.nio.ch=ALL-UNNAMED for native fd extraction;
without it, unicast packets use the OS default TTL.
-
Constructor Summary
ConstructorsConstructorDescriptionUdpTransport(int port) UdpTransport(int port, InetAddress bindAddress, int ttl, int trafficClass) Creates a UDP transport with custom socket options. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()intintReturns the IP multicast time-to-live value configured on this channel.intReturns the IP traffic class byte configured on this socket.intReturns the unicastIP_TTLvalue configured on this channel via JNAgetsockopt(IPPROTO_IP, IP_TTL).voidjoinMulticastGroup(InetAddress group) voidjoinMulticastGroup(InetAddress group, NetworkInterface networkInterface) static intqosToTrafficClass(int qos) Converts a QoS value (IP Precedence 0..7) to the traffic class byte forDatagramSocket.setTrafficClass(int).intreceive(byte[] buffer, int timeoutMs) receiveWithSource(byte[] buffer, int timeoutMs) voidsend(byte[] data, InetAddress address, int port)
-
Constructor Details
-
UdpTransport
- Throws:
IOException
-
UdpTransport
- Throws:
IOException
-
UdpTransport
public UdpTransport(int port, InetAddress bindAddress, int ttl, int trafficClass) throws IOException Creates a UDP transport with custom socket options.Sets both
IP_MULTICAST_TTL(viaStandardSocketOptions) and unicastIP_TTL(via JNAsetsockoptthroughNativeSocketOptions).- Parameters:
port- the UDP port to bind to (0 for ephemeral)bindAddress- the local address to bind to, ornullfor wildcardttl- the IP time-to-live for both multicast and unicast outgoing packetstrafficClass- the IP traffic class byte (useqosToTrafficClass(int)to convert from QoS)- Throws:
IOException- if socket creation fails
-
-
Method Details
-
qosToTrafficClass
public static int qosToTrafficClass(int qos) Converts a QoS value (IP Precedence 0..7) to the traffic class byte forDatagramSocket.setTrafficClass(int).IP Precedence occupies bits 7-5 of the TOS/DSCP byte.
- Parameters:
qos- QoS value (0..7)- Returns:
- traffic class byte with IP Precedence in bits 7-5
-
joinMulticastGroup
- Throws:
IOException
-
joinMulticastGroup
public void joinMulticastGroup(InetAddress group, NetworkInterface networkInterface) throws IOException - Throws:
IOException
-
send
- Throws:
IOException
-
receive
- Throws:
IOException
-
receiveWithSource
- Throws:
IOException
-
getLocalPort
public int getLocalPort() -
getTimeToLive
Returns the IP multicast time-to-live value configured on this channel.- Throws:
IOException- if the socket option cannot be read
-
getTrafficClass
Returns the IP traffic class byte configured on this socket.- Throws:
SocketException- if the socket option cannot be read
-
getUnicastTtl
public int getUnicastTtl()Returns the unicastIP_TTLvalue configured on this channel via JNAgetsockopt(IPPROTO_IP, IP_TTL).- Returns:
- the current unicast TTL value, or
-1if unavailable
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-