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.
@ -451,9 +457,14 @@ public class SinglePortUdpHarvester
*/ */
private void addBuffer(Buffer buf) private void addBuffer(Buffer buf)
{ {
// XXX Should we drop the first packet from the queue instead? synchronized (queue)
if (!queue.offer(buf)) {
logger.info("Dropping a packet because the queue is full."); // XXX Should we drop the first packet from the queue instead?
if (!queue.offer(buf))
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)
@ -520,13 +539,22 @@ public class SinglePortUdpHarvester
while (buf == null) while (buf == null)
{ {
try if (closed)
throw new SocketException("Socket closed");
synchronized (queue)
{ {
buf = queue.take(); if (queue.isEmpty())
} {
catch (InterruptedException ie) try
{ {
// XXX How should we handle this? queue.wait();
}
catch (InterruptedException ie)
{}
}
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()
{ {
candidates.remove(ufrag); synchronized (this)
synchronized (candidateSockets)
{ {
for (IceSocketWrapper s : candidateSockets.values()) if (freed)
{ return;
s.close(); freed = true;
}
candidateSockets.clear();
} }
StunStack stunStack = getStunStack(); candidates.remove(ufrag);
synchronized (sockets) synchronized (sockets)
{ {
StunStack stunStack = getStunStack();
for (Map.Entry<SocketAddress, DatagramSocket> e for (Map.Entry<SocketAddress, DatagramSocket> e
: sockets.entrySet()) : sockets.entrySet())
{ {
@ -662,21 +695,28 @@ public class SinglePortUdpHarvester
if (stunStack != null) if (stunStack != null)
{ {
TransportAddress localAddress TransportAddress localAddress
= new TransportAddress(socket.getLocalAddress(), = new TransportAddress(socket.getLocalAddress(),
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);
} }
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)