Makes sure that threads in receive() get a SocketException when the

socket closes.
This commit is contained in:
boris 2015-03-13 13:39:01 +00:00
parent cd826a1233
commit 23d3b809cb

View File

@ -429,6 +429,12 @@ public class SinglePortUdpHarvester
*/ */
private SocketAddress remoteAddress; private SocketAddress remoteAddress;
/**
* The flag which indicates that this <tt>DatagramSocket</tt> has been
* closed.
*/
private boolean closed = false;
/** /**
* Initializes a new <tt>MySocket</tt> instance with the given * Initializes a new <tt>MySocket</tt> instance with the given
* remote address. * remote address.
@ -450,10 +456,15 @@ public class SinglePortUdpHarvester
* not block. * not block.
*/ */
private void addBuffer(Buffer buf) private void addBuffer(Buffer buf)
{
synchronized (queue)
{ {
// XXX Should we drop the first packet from the queue instead? // XXX Should we drop the first packet from the queue instead?
if (!queue.offer(buf)) if (!queue.offer(buf))
logger.info("Dropping a packet because the queue is full."); logger.info("Dropping a packet because the queue is full.");
queue.notifyAll();
}
} }
/** /**
@ -498,6 +509,14 @@ public class SinglePortUdpHarvester
@Override @Override
public void close() public void close()
{ {
closed = true;
synchronized (queue)
{
// Wake up any threads still in receive()
queue.notifyAll();
}
// We could be called by the super-class constructor, in which // We could be called by the super-class constructor, in which
// case this.removeAddress is not initialized yet. // case this.removeAddress is not initialized yet.
if (remoteAddress != null) if (remoteAddress != null)
@ -519,14 +538,23 @@ public class SinglePortUdpHarvester
Buffer buf = null; Buffer buf = null;
while (buf == null) while (buf == null)
{
if (closed)
throw new SocketException("Socket closed");
synchronized (queue)
{
if (queue.isEmpty())
{ {
try try
{ {
buf = queue.take(); queue.wait();
} }
catch (InterruptedException ie) catch (InterruptedException ie)
{ {}
// XXX How should we handle this? }
buf = queue.poll();
} }
} }
@ -597,6 +625,12 @@ public class SinglePortUdpHarvester
*/ */
private final String ufrag; private final String ufrag;
/**
* The flag which indicates that this <tt>MyCandidate</tt> has been
* freed.
*/
private boolean freed = false;
/** /**
* The collection of <tt>IceSocketWrapper</tt>s that can potentially * The collection of <tt>IceSocketWrapper</tt>s that can potentially
* be used by the ice4j user to read/write from/to this candidate. * be used by the ice4j user to read/write from/to this candidate.
@ -640,20 +674,19 @@ public class SinglePortUdpHarvester
@Override @Override
public void free() public void free()
{ {
synchronized (this)
{
if (freed)
return;
freed = true;
}
candidates.remove(ufrag); candidates.remove(ufrag);
synchronized (candidateSockets)
{
for (IceSocketWrapper s : candidateSockets.values())
{
s.close();
}
candidateSockets.clear();
}
StunStack stunStack = getStunStack();
synchronized (sockets) synchronized (sockets)
{ {
StunStack stunStack = getStunStack();
for (Map.Entry<SocketAddress, DatagramSocket> e for (Map.Entry<SocketAddress, DatagramSocket> e
: sockets.entrySet()) : sockets.entrySet())
{ {
@ -666,7 +699,8 @@ public class SinglePortUdpHarvester
socket.getLocalPort(), socket.getLocalPort(),
Transport.UDP); Transport.UDP);
TransportAddress remoteAddress TransportAddress remoteAddress
= new TransportAddress((InetSocketAddress)e.getKey(), = new TransportAddress((InetSocketAddress) e.getKey(),
Transport.UDP); Transport.UDP);
stunStack.removeSocket(localAddress, remoteAddress); stunStack.removeSocket(localAddress, remoteAddress);
@ -674,9 +708,15 @@ public class SinglePortUdpHarvester
socket.close(); socket.close();
} }
sockets.clear(); sockets.clear();
} }
synchronized (candidateSockets)
{
candidateSockets.clear();
}
super.free(); super.free();
} }
@ -686,13 +726,21 @@ public class SinglePortUdpHarvester
* @param socket the socket to add. * @param socket the socket to add.
* @param remoteAddress the remote address for the socket. * @param remoteAddress the remote address for the socket.
*/ */
private void addSocket(DatagramSocket socket, private synchronized void addSocket(DatagramSocket socket,
InetSocketAddress remoteAddress) InetSocketAddress remoteAddress)
throws IOException throws IOException
{ {
if (freed)
{
throw new IOException("Candidate freed");
}
Component component = getParentComponent(); Component component = getParentComponent();
if (component == null) if (component == null)
return; {
throw new IOException("No parent component");
}
IceProcessingState state IceProcessingState state
= component.getParentStream().getParentAgent().getState(); = component.getParentStream().getParentAgent().getState();
if (!IceProcessingState.WAITING.equals(state) if (!IceProcessingState.WAITING.equals(state)