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 MultiplexingDatagramSocket instance which is
* to enable DatagramPacket filtering and binds it to any available
@@ -138,6 +148,25 @@ public class MultiplexingDatagramSocket
public MultiplexingDatagramSocket()
throws SocketException
{
+ this(false);
+ }
+
+ /**
+ * Initializes a new MultiplexingDatagramSocket instance which is
+ * to enable DatagramPacket 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 MultiplexingDatagramSocket instance which is
+ * to enable DatagramPacket filtering on a specific
+ * DatagramSocket.
+ *
+ * @param delegate the DatagramSocket on which
+ * DatagramPacket 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 MultiplexingDatagramSocket instance which is
+ * to enable DatagramPacket 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 MultiplexingDatagramSocket instance which is
+ * to enable DatagramPacket 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 MultiplexingDatagramSocket instance which is
+ * to enable DatagramPacket filtering, bound to the specified local
+ * socket address.
+ *
+ * If the specified local socket address is null, creates an
+ * unbound socket.
+ *
+ *
+ * @param bindaddr local socket address to bind, or null 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();
+ }
}
/**
diff --git a/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java b/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java
index e079c20..958cf90 100644
--- a/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java
+++ b/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java
@@ -279,12 +279,15 @@ abstract class MultiplexingXXXSocketSupport
* DatagramPackets away from this DatagramSocket.
*
* @param multiplexed the MultiplexedDatagramSocket 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();
}
}