Fixed socket receive buffer interface misuse (#167)
* Use type with tiny interface (offer/poll/scan/isEmpty) for socket receive buffer. * Fixed computation of total bytes stored in buffer due to misuse of iterator and addAll methods of list. * Rename offer back to add and do not return status. * Fixed formatting. * Declare variables as final. * Check if `toMove` is empty, because it can not be null with new SocketReceiveBuffer. * Fix after rebase.
This commit is contained in:
parent
ce42c77b82
commit
e3e78dcda3
@ -55,19 +55,8 @@ public class MultiplexedDatagramSocket
|
||||
* The list of <tt>DatagramPacket</tt>s to be received through this
|
||||
* <tt>DatagramSocket</tt> i.e. accepted by {@link #filter}.
|
||||
*/
|
||||
final List<DatagramPacket> received
|
||||
= new SocketReceiveBuffer()
|
||||
{
|
||||
private static final long serialVersionUID
|
||||
= -5763976093759762087L;
|
||||
|
||||
@Override
|
||||
public int getReceiveBufferSize()
|
||||
throws SocketException
|
||||
{
|
||||
return MultiplexedDatagramSocket.this.getReceiveBufferSize();
|
||||
}
|
||||
};
|
||||
final SocketReceiveBuffer received
|
||||
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>MultiplexedDatagramSocket</tt> which is unbound and
|
||||
|
||||
@ -67,19 +67,8 @@ public class MultiplexedSocket
|
||||
* The list of <tt>DatagramPacket</tt>s to be received through this
|
||||
* <tt>Socket</tt> i.e. accepted by {@link #filter}.
|
||||
*/
|
||||
final List<DatagramPacket> received
|
||||
= new SocketReceiveBuffer()
|
||||
{
|
||||
private static final long serialVersionUID
|
||||
= 678744096057601141L;
|
||||
|
||||
@Override
|
||||
public int getReceiveBufferSize()
|
||||
throws SocketException
|
||||
{
|
||||
return MultiplexedSocket.this.getReceiveBufferSize();
|
||||
}
|
||||
};
|
||||
final SocketReceiveBuffer received
|
||||
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||
|
||||
/**
|
||||
* Initializes a new <tt>MultiplexedSocket</tt> which is unbound and filters
|
||||
|
||||
@ -81,7 +81,7 @@ public class MultiplexingDatagramSocket
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected List<DatagramPacket> getReceived()
|
||||
protected SocketReceiveBuffer getReceived()
|
||||
{
|
||||
return received;
|
||||
}
|
||||
@ -90,7 +90,7 @@ public class MultiplexingDatagramSocket
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected List<DatagramPacket> getReceived(
|
||||
protected SocketReceiveBuffer getReceived(
|
||||
MultiplexedDatagramSocket socket)
|
||||
{
|
||||
return socket.received;
|
||||
@ -103,19 +103,8 @@ public class MultiplexingDatagramSocket
|
||||
* {@link MultiplexedDatagramSocket} of this instance at the time of the
|
||||
* reading from the network.
|
||||
*/
|
||||
private final List<DatagramPacket> received
|
||||
= new SocketReceiveBuffer()
|
||||
{
|
||||
private static final long serialVersionUID
|
||||
= 3125772367019091216L;
|
||||
|
||||
@Override
|
||||
public int getReceiveBufferSize()
|
||||
throws SocketException
|
||||
{
|
||||
return MultiplexingDatagramSocket.this.getReceiveBufferSize();
|
||||
}
|
||||
};
|
||||
private final SocketReceiveBuffer received
|
||||
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||
|
||||
/**
|
||||
* Buffer variable for storing the SO_TIMEOUT value set by the
|
||||
|
||||
@ -92,7 +92,7 @@ public class MultiplexingSocket
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected List<DatagramPacket> getReceived()
|
||||
protected SocketReceiveBuffer getReceived()
|
||||
{
|
||||
return received;
|
||||
}
|
||||
@ -101,7 +101,7 @@ public class MultiplexingSocket
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected List<DatagramPacket> getReceived(
|
||||
protected SocketReceiveBuffer getReceived(
|
||||
MultiplexedSocket socket)
|
||||
{
|
||||
return socket.received;
|
||||
@ -118,19 +118,8 @@ public class MultiplexingSocket
|
||||
* <tt>Socket</tt> i.e. not accepted by the <tt>DatagramFilter</tt>s of
|
||||
* {@link #sockets} at the time of the reading from the network.
|
||||
*/
|
||||
private final List<DatagramPacket> received
|
||||
= new SocketReceiveBuffer()
|
||||
{
|
||||
private static final long serialVersionUID
|
||||
= 4097024214973676873L;
|
||||
|
||||
@Override
|
||||
public int getReceiveBufferSize()
|
||||
throws SocketException
|
||||
{
|
||||
return MultiplexingSocket.this.getReceiveBufferSize();
|
||||
}
|
||||
};
|
||||
private final SocketReceiveBuffer received
|
||||
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||
|
||||
/**
|
||||
* Buffer variable for storing the SO_TIMEOUT value set by the last
|
||||
|
||||
@ -265,7 +265,7 @@ abstract class MultiplexingXXXSocketSupport
|
||||
{
|
||||
if (getFilter(socket).accept(p))
|
||||
{
|
||||
List<DatagramPacket> socketReceived = getReceived(socket);
|
||||
SocketReceiveBuffer socketReceived = getReceived(socket);
|
||||
|
||||
synchronized (socketReceived)
|
||||
{
|
||||
@ -281,7 +281,7 @@ abstract class MultiplexingXXXSocketSupport
|
||||
}
|
||||
if (!accepted)
|
||||
{
|
||||
List<DatagramPacket> thisReceived = getReceived();
|
||||
SocketReceiveBuffer thisReceived = getReceived();
|
||||
|
||||
synchronized (thisReceived)
|
||||
{
|
||||
@ -370,7 +370,7 @@ abstract class MultiplexingXXXSocketSupport
|
||||
* not accepted by any (existing) {@code DatagramPacketFilter} at the time of
|
||||
* receipt
|
||||
*/
|
||||
protected abstract List<DatagramPacket> getReceived();
|
||||
protected abstract SocketReceiveBuffer getReceived();
|
||||
|
||||
/**
|
||||
* Gets the list of {@code DatagramPacket}s received by this multiplexing
|
||||
@ -383,7 +383,7 @@ abstract class MultiplexingXXXSocketSupport
|
||||
* socket and accepted by the {@code DatagramPacketFilter} of the
|
||||
* multiplexed {@code socket} at the time of receipt
|
||||
*/
|
||||
protected abstract List<DatagramPacket> getReceived(
|
||||
protected abstract SocketReceiveBuffer getReceived(
|
||||
MultiplexedXXXSocketT socket);
|
||||
|
||||
/**
|
||||
@ -488,47 +488,27 @@ abstract class MultiplexingXXXSocketSupport
|
||||
{
|
||||
// Pull the packets which have been received already and are accepted by
|
||||
// the specified multiplexed socket out of the multiplexing socket.
|
||||
List<DatagramPacket> thisReceived = getReceived();
|
||||
DatagramPacketFilter socketFilter = getFilter(socket);
|
||||
List<DatagramPacket> toMove = null;
|
||||
final SocketReceiveBuffer thisReceived = getReceived();
|
||||
final DatagramPacketFilter socketFilter = getFilter(socket);
|
||||
final List<DatagramPacket> toMove;
|
||||
|
||||
synchronized (thisReceived)
|
||||
{
|
||||
if (thisReceived.isEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Iterator<DatagramPacket> i = thisReceived.iterator();
|
||||
i.hasNext();)
|
||||
{
|
||||
DatagramPacket p = i.next();
|
||||
|
||||
if (socketFilter.accept(p))
|
||||
{
|
||||
if (toMove == null)
|
||||
toMove = new LinkedList<>();
|
||||
toMove.add(p);
|
||||
|
||||
// XXX In the method receive, we allow multiple filters
|
||||
// to accept one and the same packet.
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
toMove = thisReceived.scan(socketFilter);
|
||||
}
|
||||
|
||||
// Push the packets which have been accepted already and are accepted by
|
||||
// the specified multiplexed socket into the multiplexed socket in
|
||||
// question.
|
||||
if (toMove != null)
|
||||
if (!toMove.isEmpty())
|
||||
{
|
||||
List<DatagramPacket> socketReceived = getReceived(socket);
|
||||
final SocketReceiveBuffer socketReceived = getReceived(socket);
|
||||
|
||||
synchronized (socketReceived)
|
||||
{
|
||||
socketReceived.addAll(toMove);
|
||||
for (DatagramPacket datagramPacket : toMove) {
|
||||
socketReceived.add(datagramPacket);
|
||||
}
|
||||
// The notifyAll will practically likely be unnecessary because
|
||||
// the specified socket will likely be a newly-created one to
|
||||
// which noone else has a reference. Anyway, dp the invocation
|
||||
@ -557,7 +537,7 @@ abstract class MultiplexingXXXSocketSupport
|
||||
* @throws SocketTimeoutException if <tt>timeout</tt> is positive and has
|
||||
* expired
|
||||
*/
|
||||
void receive(List<DatagramPacket> received, DatagramPacket p, int timeout)
|
||||
void receive(SocketReceiveBuffer received, DatagramPacket p, int timeout)
|
||||
throws IOException
|
||||
{
|
||||
long startTime = System.currentTimeMillis();
|
||||
@ -573,12 +553,9 @@ abstract class MultiplexingXXXSocketSupport
|
||||
// SocketTimeoutException.
|
||||
synchronized (received)
|
||||
{
|
||||
if (!received.isEmpty())
|
||||
{
|
||||
receivedPacket = received.remove(0);
|
||||
if (receivedPacket != null)
|
||||
break;
|
||||
}
|
||||
receivedPacket = received.poll();
|
||||
if (receivedPacket != null)
|
||||
break;
|
||||
}
|
||||
|
||||
// Throw a SocketTimeoutException if the timeout is over/up.
|
||||
|
||||
@ -19,132 +19,269 @@ package org.ice4j.socket;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Implements a list of <tt>DatagramPacket</tt>s received by a
|
||||
* Implements a buffer of <tt>DatagramPacket</tt>s received by a
|
||||
* <tt>DatagramSocket</tt> or a <tt>Socket</tt>. The list enforces the
|
||||
* <tt>SO_RCVBUF</tt> option for the associated <tt>DatagramSocket</tt> or
|
||||
* <tt>Socket</tt>.
|
||||
*
|
||||
* @author Lyubomir Marinov
|
||||
* @author Yura Yaroshevich
|
||||
*/
|
||||
abstract class SocketReceiveBuffer
|
||||
extends LinkedList<DatagramPacket>
|
||||
class SocketReceiveBuffer
|
||||
{
|
||||
/**
|
||||
* Default size in bytes of socket receive buffer.
|
||||
*/
|
||||
private static final int DEFAULT_RECEIVE_BUFFER_SIZE = 1024 * 1024;
|
||||
|
||||
private static final long serialVersionUID = 2804762379509257652L;
|
||||
/**
|
||||
* Maxumum number of datagrams buffer is capable to store regardless
|
||||
* of total datagram size in bytes.
|
||||
*/
|
||||
private static final int DATAGRAMS_BUFFER_CAPACITY = 10000;
|
||||
|
||||
/**
|
||||
* The value of the <tt>SO_RCVBUF</tt> option for the associated
|
||||
* <tt>DatagramSocket</tt> or <tt>Socket</tt>. Cached for the sake of
|
||||
* performance.
|
||||
* Queue to store received datagrams.
|
||||
*/
|
||||
private int receiveBufferSize;
|
||||
private final BlockingQueue<DatagramPacket> buffer
|
||||
= new ArrayBlockingQueue<>(DATAGRAMS_BUFFER_CAPACITY);
|
||||
|
||||
/**
|
||||
* The (total) size in bytes of this receive buffer.
|
||||
* An instance of datagram size tracker to compute total number of bytes
|
||||
* stored in datagrams witin {@link #buffer}.
|
||||
*/
|
||||
private int size;
|
||||
private final DatagramSizeTracker tracker;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* Constructs {@link SocketReceiveBuffer} with user-provided
|
||||
* @param receiveBufferSizeSupplier a function to obtain receive buffer
|
||||
* size from associated socket.
|
||||
*/
|
||||
@Override
|
||||
public boolean add(DatagramPacket p)
|
||||
public SocketReceiveBuffer(Callable<Integer> receiveBufferSizeSupplier)
|
||||
{
|
||||
boolean added = super.add(p);
|
||||
this.tracker = new DatagramSizeTracker(receiveBufferSizeSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if receive buffer is empty
|
||||
* @return true if buffer is empty, false - otherwise.
|
||||
*/
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return buffer.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds {@link DatagramPacket} at the end of the socket receive buffer.
|
||||
* @param p datagram to add into receive buffer
|
||||
*/
|
||||
public void add(DatagramPacket p)
|
||||
{
|
||||
while (!buffer.offer(p))
|
||||
{
|
||||
// ensure buffer capacity restriction enforced
|
||||
poll();
|
||||
}
|
||||
|
||||
tracker.trackDatagramAdded(p);
|
||||
|
||||
while (tracker.isExceedReceiveBufferSize() && buffer.size() > 1)
|
||||
{
|
||||
// enforce SO_RCVBUF restriction
|
||||
poll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls socket receive buffer for already stored {@link DatagramPacket}
|
||||
* @return the first datagram in the buffer, or {@code null} if buffer
|
||||
* is empty.
|
||||
*/
|
||||
public DatagramPacket poll()
|
||||
{
|
||||
DatagramPacket p = buffer.poll();
|
||||
|
||||
// Keep track of the (total) size in bytes of this receive buffer in
|
||||
// order to be able to enforce SO_RCVBUF.
|
||||
if (added && (p != null))
|
||||
// order to be able to enforce SO_RCVBUF restriction.
|
||||
if (p != null)
|
||||
{
|
||||
int pSize = p.getLength();
|
||||
tracker.trackDatagramRemoved(p);
|
||||
}
|
||||
|
||||
if (pSize > 0)
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scans buffer of received {@link DatagramPacket}s and move
|
||||
* datagrams which matches the {@code filter} into returned list.
|
||||
* @param filter a predicate to filter {@link DatagramPacket} stored
|
||||
* in receive buffer.
|
||||
* @return list of datagrams matched to {@code filter}.
|
||||
*/
|
||||
public List<DatagramPacket> scan(DatagramPacketFilter filter)
|
||||
{
|
||||
List<DatagramPacket> matchedDatagrams = null;
|
||||
final Iterator<DatagramPacket> it = buffer.iterator();
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
final DatagramPacket p = it.next();
|
||||
|
||||
if (filter.accept(p))
|
||||
{
|
||||
size += pSize;
|
||||
|
||||
// If the added packet is the only element of this list, do not
|
||||
// drop it because of the enforcement of SO_RCVBUF.
|
||||
if (size() > 1)
|
||||
if (matchedDatagrams == null)
|
||||
{
|
||||
// For the sake of performance, do not invoke the method
|
||||
// getReceiveBufferSize() of DatagramSocket or Socket on
|
||||
// every packet added to this buffer.
|
||||
int receiveBufferSize = this.receiveBufferSize;
|
||||
matchedDatagrams = new ArrayList<>();
|
||||
}
|
||||
matchedDatagrams.add(p);
|
||||
|
||||
if ((receiveBufferSize <= 0) || (modCount % 1000 == 0))
|
||||
it.remove();
|
||||
|
||||
tracker.trackDatagramRemoved(p);
|
||||
}
|
||||
}
|
||||
|
||||
if (matchedDatagrams != null)
|
||||
{
|
||||
return matchedDatagrams;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper class to keep track of total size in bytes of all
|
||||
* {@link DatagramPacket} instances stored in {@link #buffer} to
|
||||
* be able to enforce SO_RCVBUF.
|
||||
*/
|
||||
private final class DatagramSizeTracker
|
||||
{
|
||||
/**
|
||||
* The value of the <tt>SO_RCVBUF</tt> option for the associated
|
||||
* <tt>DatagramSocket</tt> or <tt>Socket</tt>. Cached for the sake of
|
||||
* performance.
|
||||
*/
|
||||
private int cachedReceiveBufferSize;
|
||||
|
||||
/**
|
||||
* The (total) size in bytes of this receive buffer.
|
||||
*/
|
||||
private int totalBuffersByteSize;
|
||||
|
||||
/**
|
||||
* Counts total number of datagrams added to buffer.
|
||||
*/
|
||||
private int totalDatagramsAdded;
|
||||
|
||||
/**
|
||||
* A user provided getter of receive buffer size, might
|
||||
* fail with {@link Exception} when called.
|
||||
*/
|
||||
private final Callable<Integer> receiveBufferSizeSupplier;
|
||||
|
||||
/**
|
||||
* Create a tracker of datagram packet size stored in buffer.
|
||||
* @param receiveBufferSizeSupplier a function to obtain receive buffer
|
||||
* size from associated socket.
|
||||
*/
|
||||
public DatagramSizeTracker(
|
||||
Callable<Integer> receiveBufferSizeSupplier)
|
||||
{
|
||||
this.receiveBufferSizeSupplier = receiveBufferSizeSupplier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if total bytes stored in {@link #buffer} exceeds socket's
|
||||
* receive buffer size.
|
||||
* @return true if size exceeded, false - otherwise
|
||||
*/
|
||||
boolean isExceedReceiveBufferSize()
|
||||
{
|
||||
return totalBuffersByteSize > cachedReceiveBufferSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates computed value of total datagrams size in bytes
|
||||
* stored in {@link #buffer} with datagram just added to buffer.
|
||||
* @param p datagram packed added to {@link #buffer}
|
||||
*/
|
||||
void trackDatagramAdded(DatagramPacket p)
|
||||
{
|
||||
++totalDatagramsAdded;
|
||||
|
||||
final int pSize = p.getLength();
|
||||
if (pSize <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
totalBuffersByteSize += pSize;
|
||||
|
||||
// If the added packet is the only element of this list, do not
|
||||
// drop it because of the enforcement of SO_RCVBUF.
|
||||
if (buffer.size() > 1)
|
||||
{
|
||||
// For the sake of performance, do not invoke the method
|
||||
// getReceiveBufferSize() of DatagramSocket or Socket on
|
||||
// every packet added to this buffer.
|
||||
int receiveBufferSize = this.cachedReceiveBufferSize;
|
||||
|
||||
if ((receiveBufferSize <= 0)
|
||||
|| (totalDatagramsAdded % 1000 == 0))
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
receiveBufferSize = getReceiveBufferSize();
|
||||
}
|
||||
catch (SocketException sex)
|
||||
{
|
||||
}
|
||||
receiveBufferSize
|
||||
= this.receiveBufferSizeSupplier.call();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
if (receiveBufferSize <= 0)
|
||||
{
|
||||
receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
|
||||
}
|
||||
else if (receiveBufferSize
|
||||
< DEFAULT_RECEIVE_BUFFER_SIZE)
|
||||
{
|
||||
// Well, a manual page on SO_RCVBUF talks about
|
||||
// doubling. In order to stay on the safe side and
|
||||
// given that there was no limit on the size of the
|
||||
// buffer before, double the receive buffer size.
|
||||
receiveBufferSize *= 2;
|
||||
if (receiveBufferSize <= 0)
|
||||
{
|
||||
receiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
|
||||
receiveBufferSize
|
||||
= DEFAULT_RECEIVE_BUFFER_SIZE;
|
||||
}
|
||||
else if (receiveBufferSize
|
||||
< DEFAULT_RECEIVE_BUFFER_SIZE)
|
||||
{
|
||||
// Well, a manual page on SO_RCVBUF talks about
|
||||
// doubling. In order to stay on the safe side and
|
||||
// given that there was no limit on the size of the
|
||||
// buffer before, double the receive buffer size.
|
||||
receiveBufferSize *= 2;
|
||||
if (receiveBufferSize <= 0)
|
||||
{
|
||||
receiveBufferSize
|
||||
= DEFAULT_RECEIVE_BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
this.receiveBufferSize = receiveBufferSize;
|
||||
}
|
||||
if (size > receiveBufferSize)
|
||||
remove(0);
|
||||
this.cachedReceiveBufferSize = receiveBufferSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the <tt>SO_RCVBUF</tt> option for the associated
|
||||
* <tt>DatagramSocket</tt> or <tt>Socket</tt> which is the buffer size used
|
||||
* by the platform for input on the <tt>DatagramSocket</tt> or
|
||||
* <tt>Socket</tt>.
|
||||
*
|
||||
* @return the value of the <tt>SO_RCVBUF</tt> option for the associated
|
||||
* <tt>DatagramSocket</tt> or <tt>Socket</tt>
|
||||
* @throws SocketException if there is an error in the underlying protocol
|
||||
*/
|
||||
public abstract int getReceiveBufferSize()
|
||||
throws SocketException;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public DatagramPacket remove(int index)
|
||||
{
|
||||
DatagramPacket p = super.remove(index);
|
||||
|
||||
// Keep track of the (total) size in bytes of this receive buffer in
|
||||
// order to be able to enforce SO_RCVBUF.
|
||||
if (p != null)
|
||||
/**
|
||||
* Updates computed value of total datagrams size in bytes
|
||||
* stored in {@link #buffer} with datagram just removed from buffer.
|
||||
* @param p datagram packed removed from {@link #buffer}
|
||||
*/
|
||||
void trackDatagramRemoved(DatagramPacket p)
|
||||
{
|
||||
int pSize = p.getLength();
|
||||
|
||||
if (pSize > 0)
|
||||
final int pSize = p.getLength();
|
||||
if (pSize <= 0)
|
||||
{
|
||||
size -= pSize;
|
||||
if (size < 0)
|
||||
size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
totalBuffersByteSize -= pSize;
|
||||
if (totalBuffersByteSize < 0)
|
||||
{
|
||||
totalBuffersByteSize = 0;
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user