Merge pull request #122 from bgrozev/multiplexing-fixes-and-features2

Multiplexing fixes and features
This commit is contained in:
bgrozev 2017-07-19 14:29:15 -05:00 committed by GitHub
commit c2f08ceca2
4 changed files with 154 additions and 8 deletions

View File

@ -36,12 +36,23 @@ public class DTLSDatagramFilter
*/
public static boolean isDTLS(DatagramPacket p)
{
int len = p.getLength();
return p != null && isDTLS(p.getData(), p.getOffset(), p.getLength());
}
/**
* Determines whether the buffer represented by {@code data}, {@code off}
* and {@code len} looks like a DTLS packet.
*
* @param data the array that contains the data.
* @param off the offset.
* @param len the length.
* @return {@code true} if the buffer looks like a DTLS packet; otherwise,
* {@code false}.
*/
public static boolean isDTLS(byte[] data, int off, int len)
{
if (len > 0)
{
byte[] data = p.getData();
int off = p.getOffset();
int fb = data[off] & 0xff;
return 19 < fb && fb < 64;

View File

@ -119,7 +119,10 @@ public class MultiplexedDatagramSocket
{
multiplexing.close(this);
super.close();
// We intentionally do not call super.close(), because it eventually
// delegates to #multiplexing. We don't want to close #multiplexing
// just yet, because it may have other filtered sockets attached to it
// (or it needs to be kept open for other reasons)
}
/**

View File

@ -99,8 +99,9 @@ public class MultiplexingDatagramSocket
/**
* The list of <tt>DatagramPacket</tt>s to be received through this
* <tt>DatagramSocket</tt> i.e. not accepted by the <tt>DatagramFilter</tt>s
* of {@link #sockets} at the time of the reading from the network.
* <tt>DatagramSocket</tt> i.e. not accepted by the list of
* {@link MultiplexedDatagramSocket} of this instance at the time of the
* reading from the network.
*/
private final List<DatagramPacket> received
= new SocketReceiveBuffer()
@ -125,6 +126,15 @@ public class MultiplexingDatagramSocket
*/
private int soTimeout = 0;
/**
* Whether this socket should be kept open even when all of its
* {@link MultiplexedDatagramSocket} are closed (if the value is
* {@code true}), or it should be closed when the last of its
* {@link MultiplexedDatagramSocket} is closed (if the value is
* {@code false}).
*/
private final boolean persistent;
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to any available
@ -138,6 +148,25 @@ public class MultiplexingDatagramSocket
public MultiplexingDatagramSocket()
throws SocketException
{
this(false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to any available
* port on the local host machine. The socket will be bound to the wildcard
* address, an IP address chosen by the kernel.
*
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @see DatagramSocket#DatagramSocket()
*/
public MultiplexingDatagramSocket(boolean persistent)
throws SocketException
{
this.persistent = persistent;
}
/**
@ -152,8 +181,30 @@ public class MultiplexingDatagramSocket
*/
public MultiplexingDatagramSocket(DatagramSocket delegate)
throws SocketException
{
this(delegate, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering on a specific
* <tt>DatagramSocket</tt>.
*
* @param delegate the <tt>DatagramSocket</tt> on which
* <tt>DatagramPacket</tt> filtering is to be enabled by the new instance
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if anything goes wrong while initializing the new
* instance
*/
public MultiplexingDatagramSocket(
DatagramSocket delegate,
boolean persistent)
throws SocketException
{
super(delegate);
this.persistent = persistent;
}
/**
@ -169,8 +220,29 @@ public class MultiplexingDatagramSocket
*/
public MultiplexingDatagramSocket(int port)
throws SocketException
{
this(port, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering and binds it to the specified
* port on the local host machine. The socket will be bound to the wildcard
* address, an IP address chosen by the kernel.
*
* @param port the port to bind the new socket to
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int)
*/
public MultiplexingDatagramSocket(int port, boolean persistent)
throws SocketException
{
super(port);
this.persistent = persistent;
}
/**
@ -188,8 +260,34 @@ public class MultiplexingDatagramSocket
*/
public MultiplexingDatagramSocket(int port, InetAddress laddr)
throws SocketException
{
this(port, laddr, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
* address. The local port must be between 0 and 65535 inclusive. If the IP
* address is 0.0.0.0, the socket will be bound to the wildcard address, an
* IP address chosen by the kernel.
*
* @param port the local port to bind the new socket to
* @param laddr the local address to bind the new socket to
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int, InetAddress)
*/
public MultiplexingDatagramSocket(
int port,
InetAddress laddr,
boolean persistent)
throws SocketException
{
super(port, laddr);
this.persistent = persistent;
}
/**
@ -209,8 +307,35 @@ public class MultiplexingDatagramSocket
*/
public MultiplexingDatagramSocket(SocketAddress bindaddr)
throws SocketException
{
this(bindaddr, false);
}
/**
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
* to enable <tt>DatagramPacket</tt> filtering, bound to the specified local
* socket address.
* <p>
* If the specified local socket address is <tt>null</tt>, creates an
* unbound socket.
* </p>
*
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
* unbound socket
* @param persistent whether this socket should be kept open after all of
* its {@link MultiplexedDatagramSocket}s are closed.
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(SocketAddress)
*/
public MultiplexingDatagramSocket(
SocketAddress bindaddr,
boolean persistent)
throws SocketException
{
super(bindaddr);
this.persistent = persistent;
}
/**
@ -221,7 +346,11 @@ public class MultiplexingDatagramSocket
*/
void close(MultiplexedDatagramSocket multiplexed)
{
multiplexingXXXSocketSupport.close(multiplexed);
if (!multiplexingXXXSocketSupport.close(multiplexed)
&& !persistent)
{
close();
}
}
/**

View File

@ -279,12 +279,15 @@ abstract class MultiplexingXXXSocketSupport
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>.
*
* @param multiplexed the <tt>MultiplexedDatagramSocket</tt> to close
* @return {@code true} if there are remaining filtered sockets.
*/
void close(MultiplexedXXXSocketT multiplexed)
boolean close(MultiplexedXXXSocketT multiplexed)
{
synchronized (sockets)
{
sockets.remove(multiplexed);
return !sockets.isEmpty();
}
}