feat: Closes all socket containers when the the merging socket closes.

This commit is contained in:
Boris Grozev 2016-11-29 16:29:45 -06:00
parent 67f301eebc
commit ff6eeb343e

View File

@ -112,16 +112,22 @@ public class MergingDatagramSocket
{ {
return; return;
} }
// TODO: close all containers
closed = true; closed = true;
// XXX do we want to risk obtaining the lock here, or should we just // XXX do we want to risk obtaining the lock here, or should we just
// let any thread in find out about the close after it's next timeout? // let any thread in find out about the close after it's next timeout?
synchronized (receiveLock) synchronized (receiveLock)
{ {
receiveLock.notifyAll(); receiveLock.notifyAll();
} }
synchronized (socketContainersSyncRoot)
{
for (SocketContainer container : socketContainers)
{
doRemove(container.getSocket());
}
}
} }
/** /**
@ -334,10 +340,8 @@ public class MergingDatagramSocket
} }
if (socketContainer != null) if (socketContainer != null)
{ {
// Stop the reading thread. // We just removed it from the merging socket, so use remove=false
// TODO: do we want to interrupt the thread here? This will not socketContainer.close(false);
// help unless we set a soTimeout > 0
socketContainer.closed = true;
} }
} }
@ -665,6 +669,10 @@ public class MergingDatagramSocket
} }
} }
// The receive thread is terminating, no reason to keep this
// container anymore.
close(true);
if (logger.isLoggable(Level.FINE)) if (logger.isLoggable(Level.FINE))
{ {
logger.fine("Finished: " + toString()); logger.fine("Finished: " + toString());
@ -860,6 +868,35 @@ public class MergingDatagramSocket
} }
} }
/**
* @return the underlying socket of this {@link SocketContainer}.
*/
private Object getSocket()
{
return datagramSocket != null ? datagramSocket : delegatingSocket;
}
/**
* Closes this {@link SocketContainer}, stopping it's reading thread,
* and, if necessary removing it from the merging socket.
* @param remove whether to remove this container from the merging
* socket.
*/
private void close(boolean remove)
{
if (closed && !remove)
{
return;
}
closed = true;
thread.interrupt();
if (remove)
{
MergingDatagramSocket.this.doRemove(getSocket());
}
}
/** /**
* Represents a {@link DatagramPacket} for the purposes of {@link * Represents a {@link DatagramPacket} for the purposes of {@link
* SocketContainer}. * SocketContainer}.