public abstract class AbstractUdpListener extends Object
DatagramSocket
and runs a thread
(thread
) which perpetually reads from it.
When a datagram from an unknown source is received, it is parsed as a STUN
Binding Request, and if it has a USERNAME attribute, its ufrag is extracted.
At this point, an implementing class may choose to create a mapping for
the remote address of the datagram, which will be used for further packets
from this address.Modifier and Type | Class and Description |
---|---|
protected class |
AbstractUdpListener.Buffer
Represents a buffer for the purposes of SinglePortUdpHarvester.
|
protected class |
AbstractUdpListener.MySocket
Implements a DatagramSocket for the purposes of a specific
MyCandidate.
|
Modifier and Type | Field and Description |
---|---|
private static int |
BUFFER_SIZE
The size for newly allocated Buffer instances.
|
private boolean |
close
Triggers the termination of the threads of this instance.
|
protected TransportAddress |
localAddress
The local address that this harvester is bound to.
|
private static Logger |
logger
Our class logger.
|
private ArrayBlockingQueue<AbstractUdpListener.Buffer> |
pool
A pool of Buffer instances used to avoid creating of new java
objects.
|
private static int |
POOL_SIZE
The number of Buffer instances to keep in
pool . |
static String |
SO_RCVBUF_PNAME
The name of the property which controls the size of the receive buffer
which
SinglePortUdpHarvester will request for the sockets that
it creates. |
private DatagramSocket |
socket
The "main" socket that this harvester reads from.
|
private Map<SocketAddress,AbstractUdpListener.MySocket> |
sockets
The map which keeps the known remote addresses and their associated
candidateSockets.
|
private Thread |
thread
The thread reading from
socket . |
Modifier | Constructor and Description |
---|---|
protected |
AbstractUdpListener(TransportAddress localAddress)
Initializes a new SinglePortUdpHarvester instance which is to
bind on the specified local address.
|
Modifier and Type | Method and Description |
---|---|
protected AbstractUdpListener.MySocket |
addSocket(InetSocketAddress remoteAddress)
Creates a new
AbstractUdpListener.MySocket instance and associates it with the given
remote address. |
void |
close()
Triggers the termination of the threads of this instance.
|
static List<TransportAddress> |
getAllowedAddresses(int port)
Returns the list of
TransportAddress es, one for each allowed IP
address found on each allowed network interface, with the given port. |
private AbstractUdpListener.Buffer |
getFreeBuffer()
Gets an unused Buffer instance, creating it if necessary.
|
(package private) static String |
getUfrag(byte[] buf,
int off,
int len)
Tries to parse the bytes in buf at offset off (and
length len) as a STUN Binding Request message.
|
protected abstract void |
maybeAcceptNewSession(AbstractUdpListener.Buffer buf,
InetSocketAddress remoteAddress,
String ufrag)
Handles the reception of a STUN Binding Request with a valid USERNAME
attribute, from a "new" remote address (one which is not in
sockets ). |
private void |
runInHarvesterThread()
Perpetually reads datagrams from
socket and handles them
accordingly. |
public static final String SO_RCVBUF_PNAME
SinglePortUdpHarvester
will request for the sockets that
it creates.private static final Logger logger
private static final int BUFFER_SIZE
private static final int POOL_SIZE
pool
.private final Map<SocketAddress,AbstractUdpListener.MySocket> sockets
thread
is the only thread which adds new entries, while
other threads remove entries when candidates are freed.private final ArrayBlockingQueue<AbstractUdpListener.Buffer> pool
protected final TransportAddress localAddress
private final DatagramSocket socket
private boolean close
protected AbstractUdpListener(TransportAddress localAddress) throws IOException
localAddress
- the address to bind to.IOException
- if initialization fails.public static List<TransportAddress> getAllowedAddresses(int port)
TransportAddress
es, one for each allowed IP
address found on each allowed network interface, with the given port.port
- the UDP port number.static String getUfrag(byte[] buf, int off, int len)
buf
- the bytes.off
- the offset.len
- the length.public void close()
private void runInHarvesterThread()
socket
and handles them
accordingly.
It is important that this blocks are little as possible (except on
socket.receive(), of course), because it could potentially delay the
reception of both ICE and media packets for the whole application.protected abstract void maybeAcceptNewSession(AbstractUdpListener.Buffer buf, InetSocketAddress remoteAddress, String ufrag)
sockets
).
Implementations may choose to e.g. create a socket and pass it to their
ICE stack.
Note that this is meant to only be executed by
AbstractUdpListener
's read thread, and should not be called from
implementing classes.buf
- the UDP payload of the first datagram received on the newly
accepted socket.remoteAddress
- the remote address from which the datagram was
received.ufrag
- the local ICE username fragment of the received STUN Binding
Request.private AbstractUdpListener.Buffer getFreeBuffer()
protected AbstractUdpListener.MySocket addSocket(InetSocketAddress remoteAddress) throws SocketException
AbstractUdpListener.MySocket
instance and associates it with the given
remote address. Returns the created instance.
Note that this is meant to only execute in AbstractUdpListener
's
read thread.remoteAddress
- the remote address with which to associate the new
socket instance.SocketException
Copyright © 2018. All rights reserved.