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();
if ((address == null) || (port < 0))
{
return new DatagramPacket(data, p.getOffset(), p.getLength());
}
else
{
return return
new DatagramPacket( new DatagramPacket(
cData, p.getOffset(), p.getLength(), data, p.getOffset(), p.getLength(),
p.getAddress(), p.getPort()); 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)
@ -220,6 +242,7 @@ public class MultiplexingDatagramSocket
} }
} }
} }
}
/** /**
* Copies the properties of a specific <tt>DatagramPacket</tt> to another * Copies the properties of a specific <tt>DatagramPacket</tt> to another
@ -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())) if (filter.equals(socket.getFilter()))
return socket; return socket;
}
// Create a new socket for the specified filter. // Create a new socket for the specified filter.
MultiplexedDatagramSocket socket MultiplexedDatagramSocket socket
@ -374,52 +399,88 @@ 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)
{ {
doReceive = false; wait = true;
try
{
receiveSyncRoot.wait();
}
catch (InterruptedException iex)
{
continue;
}
} }
else else
{ {
doReceive = true; wait = false;
inReceive = true; inReceive = true;
} }
} }
else
try
{ {
doReceive = false; if (wait)
r = received.remove(0); {
} synchronized (received)
} {
if (doReceive) if (received.isEmpty())
{ {
try try
{ {
super.receive(p); received.wait(1000);
}
catch (InterruptedException ie)
{
}
}
else
{
received.notifyAll();
}
}
continue;
}
DatagramPacket c = clone(p);
synchronized (receiveSyncRoot) synchronized (receiveSyncRoot)
{ {
if (setReceiveBufferSize)
{
setReceiveBufferSize = false;
try
{
super.setReceiveBufferSize(receiveBufferSize);
}
catch (Throwable t)
{
if (t instanceof ThreadDeath)
throw (ThreadDeath) t;
}
}
}
super.receive(c);
synchronized (socketsSyncRoot) synchronized (socketsSyncRoot)
{ {
boolean accepted = false; boolean accepted = false;
for (MultiplexedDatagramSocket socket : sockets) for (MultiplexedDatagramSocket socket : sockets)
if (socket.getFilter().accept(p))
{ {
socket.received.add(clone(p)); if (socket.getFilter().accept(c))
{
synchronized (socket.received)
{
socket.received.add(accepted ? clone(c) : c);
socket.received.notifyAll();
}
accepted = true; accepted = true;
/* /*
@ -428,9 +489,15 @@ public class MultiplexingDatagramSocket
*/ */
//break; //break;
} }
}
if (!accepted) if (!accepted)
this.received.add(clone(p)); {
synchronized (this.received)
{
this.received.add(c);
this.received.notifyAll();
}
} }
} }
} }
@ -438,13 +505,12 @@ public class MultiplexingDatagramSocket
{ {
synchronized (receiveSyncRoot) synchronized (receiveSyncRoot)
{ {
if (!wait)
inReceive = false; inReceive = false;
receiveSyncRoot.notify();
} }
} }
} }
} while (true);
while (r == null);
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;
}
}
}
} }