java.lang.Object
net.i2p.router.transport.udp.UDPEndpoint
All Implemented Interfaces:
SocketListener

class UDPEndpointextends Object implements SocketListener
Coordinate the low-level datagram socket, creating and managing the UDPSender and UDPReceiver.
  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _log

      private final Log _log
      Logger for the UDP endpoint.
    • _listenPort

      private int _listenPort
      The port this endpoint listens on.
    • _transport

      private final UDPTransport _transport
      The transport that owns this endpoint.
    • _sender

      private UDPSender _sender
      The thread sending the packets.
    • _receiver

      private UDPReceiver _receiver
      The thread receiving the packets.
    • _socket

      private DatagramSocket _socket
      The socket read and written by the endpoint.
    • _bindAddress

      private final InetAddress _bindAddress
      The address the socket is bound to.
    • _isIPv4

      private final boolean _isIPv4
      True for the IPv4 endpoint.
    • _isIPv6

      private final boolean _isIPv6
      True for the IPv6 endpoint.
    • _counter

      private static final AtomicInteger _counter
      Sequence number of the created endpoints.
    • MIN_SOCKET_BUFFER

      private static final int MIN_SOCKET_BUFFER
      Smallest socket buffer size asked for.
      See Also:
    • MAX_PORT_RETRIES

      private static final int MAX_PORT_RETRIES
      Times a different port is tried before failing.
      See Also:
  • Constructor Details

    • UDPEndpoint

      public UDPEndpoint(RouterContext ctx, UDPTransport transport, int listenPort, InetAddress bindAddress)
      Record the settings a later startup() binds the UDP socket with. No socket is opened here, so the address family flags are decided from bindAddress alone; a null address means a dual-stack wildcard.
      Parameters:
      ctx - router context supplying the clock and property source
      transport - may be null for unit testing ONLY
      listenPort - -1 or the requested port, may not be honored
      bindAddress - null ok
  • Method Details

    • startup

      public void startup() throws SocketException
      Caller should call getListenPort() after this to get the actual bound port and determine success .

      Can be restarted.

      Throws:
      SocketException - if no port could be bound to _bindAddress
    • shutdown

      public void shutdown()
      Shut down the sender, receiver, and socket.
    • setListenPort

      public void setListenPort(int newPort)
      The listen port for the next socket bind.
      Parameters:
      newPort - the port to request at the next bind, or below 1 to pick randomly
    • getSocket

      private DatagramSocket getSocket()
      Open socket using requested port in _listenPort and bind host in _bindAddress. If _listenPort <= 0, or requested port is busy, repeatedly try a new random port.
      Returns:
      null on failure Sets _listenPort to actual port or -1 on failure
    • getListenPort

      public int getListenPort()
      Call after startup() to get the actual port, or -1 on startup failure.
      Returns:
      the port the socket was actually bound to, or -1 if no bind succeeded
    • getSender

      public UDPSender getSender()
      The UDP sender.
      Returns:
      the UDPSender thread pool that transmits outbound packets
    • send

      public void send(UDPPacket packet)
      Add the packet to the outbound queue to be sent ASAP (as allowed by the bandwidth limiter) BLOCKING if queue is full.
      Parameters:
      packet - the packet to hand to the sender thread for transmission
    • receive

      public UDPPacket receive()
      Blocking call to receive the next inbound UDP packet from any peer.

      UNIT TESTING ONLY. Direct from the socket. In normal operation, UDPReceiver thread injects to PacketHandler queue.

      Returns:
      null if we have shut down, or on failure
    • clearOutbound

      public void clearOutbound()
      Clear outbound queue, probably in preparation for sending destroy() to everybody.
    • isIPv4

      public boolean isIPv4()
      Whether this endpoint was configured for IPv4.
      Returns:
      true for wildcard too
    • isIPv6

      public boolean isIPv6()
      Whether this endpoint was configured for IPv6.
      Returns:
      true for wildcard too
    • fail

      public void fail()
      Shut down the endpoint and tell the transport it failed.
      Specified by:
      fail in interface SocketListener
    • toString

      public String toString()
      Endpoint summary for log messages.
      Overrides:
      toString in class Object