Class UdpTransport

java.lang.Object
com.trdp.network.UdpTransport
All Implemented Interfaces:
AutoCloseable

public class UdpTransport extends Object implements 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 Details

    • UdpTransport

      public UdpTransport() throws IOException
      Throws:
      IOException
    • UdpTransport

      public UdpTransport(int port) throws IOException
      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 (via StandardSocketOptions) and unicast IP_TTL (via JNA setsockopt through NativeSocketOptions).

      Parameters:
      port - the UDP port to bind to (0 for ephemeral)
      bindAddress - the local address to bind to, or null for wildcard
      ttl - the IP time-to-live for both multicast and unicast outgoing packets
      trafficClass - the IP traffic class byte (use qosToTrafficClass(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 for DatagramSocket.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

      public void joinMulticastGroup(InetAddress group) throws IOException
      Throws:
      IOException
    • joinMulticastGroup

      public void joinMulticastGroup(InetAddress group, NetworkInterface networkInterface) throws IOException
      Throws:
      IOException
    • send

      public void send(byte[] data, InetAddress address, int port) throws IOException
      Throws:
      IOException
    • receive

      public int receive(byte[] buffer, int timeoutMs) throws IOException
      Throws:
      IOException
    • receiveWithSource

      public ReceivedPacket receiveWithSource(byte[] buffer, int timeoutMs) throws IOException
      Throws:
      IOException
    • getLocalPort

      public int getLocalPort()
    • getTimeToLive

      public int getTimeToLive() throws IOException
      Returns the IP multicast time-to-live value configured on this channel.
      Throws:
      IOException - if the socket option cannot be read
    • getTrafficClass

      public int getTrafficClass() throws SocketException
      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 unicast IP_TTL value configured on this channel via JNA getsockopt(IPPROTO_IP, IP_TTL).
      Returns:
      the current unicast TTL value, or -1 if unavailable
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable