java.lang.Object
net.i2p.router.transport.udp.UDPEndpoint
- All Implemented Interfaces:
SocketListener
Coordinate the low-level datagram socket, creating and managing the UDPSender and UDPReceiver.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final InetAddressThe address the socket is bound to.private final RouterContextThe router context.private static final AtomicIntegerSequence number of the created endpoints.private final booleanTrue for the IPv4 endpoint.private final booleanTrue for the IPv6 endpoint.private intThe port this endpoint listens on.private final LogLogger for the UDP endpoint.private UDPReceiverThe thread receiving the packets.private UDPSenderThe thread sending the packets.private DatagramSocketThe socket read and written by the endpoint.private final UDPTransportThe transport that owns this endpoint.private static final intTimes a different port is tried before failing.private static final intSmallest socket buffer size asked for.Constructor Summary
ConstructorsConstructorDescriptionUDPEndpoint(RouterContext ctx, UDPTransport transport, int listenPort, InetAddress bindAddress) Record the settings a laterstartup()binds the UDP socket with.Method Summary
Modifier and TypeMethodDescriptionvoidClear outbound queue, probably in preparation for sending destroy() to everybody.voidfail()Shut down the endpoint and tell the transport it failed.intCall afterstartup()to get the actual port, or -1 on startup failure.The UDP sender.private DatagramSocketOpen socket using requested port in _listenPort and bind host in _bindAddress.booleanisIPv4()Whether this endpoint was configured for IPv4.booleanisIPv6()Whether this endpoint was configured for IPv6.receive()Blocking call to receive the next inbound UDP packet from any peer.voidAdd the packet to the outbound queue to be sent ASAP (as allowed by the bandwidth limiter) BLOCKING if queue is full.voidsetListenPort(int newPort) The listen port for the next socket bind.voidshutdown()Shut down the sender, receiver, and socket.voidstartup()Caller should callgetListenPort()after this to get the actual bound port and determine success .toString()Endpoint summary for log messages.
Field Details
_context
The router context._log
Logger for the UDP endpoint._listenPort
private int _listenPortThe port this endpoint listens on._transport
The transport that owns this endpoint._sender
The thread sending the packets._receiver
The thread receiving the packets._socket
The socket read and written by the endpoint._bindAddress
The address the socket is bound to._isIPv4
private final boolean _isIPv4True for the IPv4 endpoint._isIPv6
private final boolean _isIPv6True for the IPv6 endpoint._counter
Sequence number of the created endpoints.MIN_SOCKET_BUFFER
private static final int MIN_SOCKET_BUFFERSmallest socket buffer size asked for.- See Also:
MAX_PORT_RETRIES
private static final int MAX_PORT_RETRIESTimes 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 laterstartup()binds the UDP socket with. No socket is opened here, so the address family flags are decided frombindAddressalone; a null address means a dual-stack wildcard.- Parameters:
ctx- router context supplying the clock and property sourcetransport- may be null for unit testing ONLYlistenPort- -1 or the requested port, may not be honoredbindAddress- null ok
Method Details
startup
Caller should callgetListenPort()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
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 afterstartup()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
The UDP sender.- Returns:
- the UDPSender thread pool that transmits outbound packets
send
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
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:
failin interfaceSocketListener
toString