Fixes an issue which could cause CPU starvation while receiving from MultiplexingDatagramSocket.

This commit is contained in:
lubomir.marinov 2013-11-20 08:55:51 +00:00
parent 140de712e7
commit 137de02c4f

View File

@ -57,11 +57,24 @@ public class MultiplexingDatagramSocket
private final List<DatagramPacket> received private final List<DatagramPacket> received
= new LinkedList<DatagramPacket>(); = new LinkedList<DatagramPacket>();
/**
* The value with which {@link DatagramSocket#setReceiveBufferSize(int)} is
* to be invoked if {@link #setReceiveBufferSize} is <tt>true</tt>.
*/
private int receiveBufferSize;
/** /**
* The <tt>Object</tt> which synchronizes the access to {@link #inReceive}. * The <tt>Object</tt> which synchronizes the access to {@link #inReceive}.
*/ */
private final Object receiveSyncRoot = new Object(); private final Object receiveSyncRoot = new Object();
/**
* The indicator which determines whether
* {@link DatagramSocket#setReceiveBufferSize(int)} is to be invoked with
* the value of {@link #receiveBufferSize}.
*/
private boolean setReceiveBufferSize = false;
/** /**
* The <tt>MultiplexedDatagramSocket</tt>s filtering * The <tt>MultiplexedDatagramSocket</tt>s filtering
* <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>. * <tt>DatagramPacket</tt>s away from this <tt>DatagramSocket</tt>.
@ -177,13 +190,21 @@ public class MultiplexingDatagramSocket
{ {
synchronized (p) synchronized (p)
{ {
byte[] pData = p.getData(); byte[] data = p.getData().clone();
byte[] cData = (pData == null) ? null : pData.clone(); InetAddress address = p.getAddress();
int port = p.getPort();
return if ((address == null) || (port < 0))
new DatagramPacket( {
cData, p.getOffset(), p.getLength(), return new DatagramPacket(data, p.getOffset(), p.getLength());
p.getAddress(), p.getPort()); }
else
{
return
new DatagramPacket(
data, p.getOffset(), p.getLength(),
address, port);
}
} }
} }
@ -200,6 +221,7 @@ public class MultiplexingDatagramSocket
int socketCount = sockets.length; int socketCount = sockets.length;
for (int i = 0; i < socketCount; i++) for (int i = 0; i < socketCount; i++)
{
if (sockets[i].equals(multiplexed)) if (sockets[i].equals(multiplexed))
{ {
if (socketCount == 1) if (socketCount == 1)
@ -218,6 +240,7 @@ public class MultiplexingDatagramSocket
} }
break; break;
} }
}
} }
} }
@ -300,8 +323,10 @@ public class MultiplexingDatagramSocket
* create a new one and return the existing. * create a new one and return the existing.
*/ */
for (MultiplexedDatagramSocket socket : sockets) for (MultiplexedDatagramSocket socket : sockets)
if (filter.equals(socket.getFilter())) {
return socket; if (filter.equals(socket.getFilter()))
return socket;
}
// Create a new socket for the specified filter. // Create a new socket for the specified filter.
MultiplexedDatagramSocket socket MultiplexedDatagramSocket socket
@ -374,77 +399,118 @@ public class MultiplexingDatagramSocket
do do
{ {
boolean doReceive; synchronized (received)
{
if (!received.isEmpty())
{
r = received.remove(0);
if (r != null)
break;
}
}
boolean wait;
synchronized (receiveSyncRoot) synchronized (receiveSyncRoot)
{ {
if (received.isEmpty()) if (inReceive)
{ {
if (inReceive) wait = true;
{
doReceive = false;
try
{
receiveSyncRoot.wait();
}
catch (InterruptedException iex)
{
continue;
}
}
else
{
doReceive = true;
inReceive = true;
}
} }
else else
{ {
doReceive = false; wait = false;
r = received.remove(0); inReceive = true;
} }
} }
if (doReceive)
try
{ {
try if (wait)
{ {
super.receive(p); synchronized (received)
synchronized (receiveSyncRoot)
{ {
synchronized (socketsSyncRoot) if (received.isEmpty())
{ {
boolean accepted = false; try
{
received.wait(1000);
}
catch (InterruptedException ie)
{
}
}
else
{
received.notifyAll();
}
}
continue;
}
for (MultiplexedDatagramSocket socket : sockets) DatagramPacket c = clone(p);
if (socket.getFilter().accept(p))
{
socket.received.add(clone(p));
accepted = true;
/* synchronized (receiveSyncRoot)
* Emil: Don't break because we want all {
* filtering sockets to get this. if (setReceiveBufferSize)
*/ {
//break; setReceiveBufferSize = false;
} try
{
if (!accepted) super.setReceiveBufferSize(receiveBufferSize);
this.received.add(clone(p)); }
catch (Throwable t)
{
if (t instanceof ThreadDeath)
throw (ThreadDeath) t;
} }
} }
} }
finally super.receive(c);
synchronized (socketsSyncRoot)
{ {
synchronized (receiveSyncRoot) boolean accepted = false;
for (MultiplexedDatagramSocket socket : sockets)
{ {
inReceive = false; if (socket.getFilter().accept(c))
receiveSyncRoot.notify(); {
synchronized (socket.received)
{
socket.received.add(accepted ? clone(c) : c);
socket.received.notifyAll();
}
accepted = true;
/*
* Emil: Don't break because we want all
* filtering sockets to get this.
*/
//break;
}
}
if (!accepted)
{
synchronized (this.received)
{
this.received.add(c);
this.received.notifyAll();
}
} }
} }
} }
finally
{
synchronized (receiveSyncRoot)
{
if (!wait)
inReceive = false;
}
}
} }
while (r == null); while (true);
copy(r, p); copy(r, p);
} }
@ -465,4 +531,27 @@ public class MultiplexingDatagramSocket
{ {
receive(multiplexed.received, p); receive(multiplexed.received, p);
} }
/**
* {@inheritDoc}
*/
@Override
public void setReceiveBufferSize(int receiveBufferSize)
throws SocketException
{
synchronized (receiveSyncRoot)
{
this.receiveBufferSize = receiveBufferSize;
if (inReceive)
{
setReceiveBufferSize = true;
}
else
{
super.setReceiveBufferSize(receiveBufferSize);
setReceiveBufferSize = false;
}
}
}
} }