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
|
* The list of <tt>DatagramPacket</tt>s to be received through this
|
||||||
* <tt>DatagramSocket</tt> i.e. accepted by {@link #filter}.
|
* <tt>DatagramSocket</tt> i.e. accepted by {@link #filter}.
|
||||||
*/
|
*/
|
||||||
final List<DatagramPacket> received
|
final SocketReceiveBuffer received
|
||||||
= new SocketReceiveBuffer()
|
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||||
{
|
|
||||||
private static final long serialVersionUID
|
|
||||||
= -5763976093759762087L;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getReceiveBufferSize()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return MultiplexedDatagramSocket.this.getReceiveBufferSize();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>MultiplexedDatagramSocket</tt> which is unbound and
|
* 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
|
* The list of <tt>DatagramPacket</tt>s to be received through this
|
||||||
* <tt>Socket</tt> i.e. accepted by {@link #filter}.
|
* <tt>Socket</tt> i.e. accepted by {@link #filter}.
|
||||||
*/
|
*/
|
||||||
final List<DatagramPacket> received
|
final SocketReceiveBuffer received
|
||||||
= new SocketReceiveBuffer()
|
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||||
{
|
|
||||||
private static final long serialVersionUID
|
|
||||||
= 678744096057601141L;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getReceiveBufferSize()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return MultiplexedSocket.this.getReceiveBufferSize();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>MultiplexedSocket</tt> which is unbound and filters
|
* Initializes a new <tt>MultiplexedSocket</tt> which is unbound and filters
|
||||||
|
|||||||
@ -81,7 +81,7 @@ public class MultiplexingDatagramSocket
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected List<DatagramPacket> getReceived()
|
protected SocketReceiveBuffer getReceived()
|
||||||
{
|
{
|
||||||
return received;
|
return received;
|
||||||
}
|
}
|
||||||
@ -90,7 +90,7 @@ public class MultiplexingDatagramSocket
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected List<DatagramPacket> getReceived(
|
protected SocketReceiveBuffer getReceived(
|
||||||
MultiplexedDatagramSocket socket)
|
MultiplexedDatagramSocket socket)
|
||||||
{
|
{
|
||||||
return socket.received;
|
return socket.received;
|
||||||
@ -103,19 +103,8 @@ public class MultiplexingDatagramSocket
|
|||||||
* {@link MultiplexedDatagramSocket} of this instance at the time of the
|
* {@link MultiplexedDatagramSocket} of this instance at the time of the
|
||||||
* reading from the network.
|
* reading from the network.
|
||||||
*/
|
*/
|
||||||
private final List<DatagramPacket> received
|
private final SocketReceiveBuffer received
|
||||||
= new SocketReceiveBuffer()
|
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||||
{
|
|
||||||
private static final long serialVersionUID
|
|
||||||
= 3125772367019091216L;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getReceiveBufferSize()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return MultiplexingDatagramSocket.this.getReceiveBufferSize();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Buffer variable for storing the SO_TIMEOUT value set by the
|
* Buffer variable for storing the SO_TIMEOUT value set by the
|
||||||
|
|||||||
@ -92,7 +92,7 @@ public class MultiplexingSocket
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected List<DatagramPacket> getReceived()
|
protected SocketReceiveBuffer getReceived()
|
||||||
{
|
{
|
||||||
return received;
|
return received;
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ public class MultiplexingSocket
|
|||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected List<DatagramPacket> getReceived(
|
protected SocketReceiveBuffer getReceived(
|
||||||
MultiplexedSocket socket)
|
MultiplexedSocket socket)
|
||||||
{
|
{
|
||||||
return socket.received;
|
return socket.received;
|
||||||
@ -118,19 +118,8 @@ public class MultiplexingSocket
|
|||||||
* <tt>Socket</tt> i.e. not accepted by the <tt>DatagramFilter</tt>s of
|
* <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.
|
* {@link #sockets} at the time of the reading from the network.
|
||||||
*/
|
*/
|
||||||
private final List<DatagramPacket> received
|
private final SocketReceiveBuffer received
|
||||||
= new SocketReceiveBuffer()
|
= new SocketReceiveBuffer(this::getReceiveBufferSize);
|
||||||
{
|
|
||||||
private static final long serialVersionUID
|
|
||||||
= 4097024214973676873L;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getReceiveBufferSize()
|
|
||||||
throws SocketException
|
|
||||||
{
|
|
||||||
return MultiplexingSocket.this.getReceiveBufferSize();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Buffer variable for storing the SO_TIMEOUT value set by the last
|
* Buffer variable for storing the SO_TIMEOUT value set by the last
|
||||||
|
|||||||
@ -265,7 +265,7 @@ abstract class MultiplexingXXXSocketSupport
|
|||||||
{
|
{
|
||||||
if (getFilter(socket).accept(p))
|
if (getFilter(socket).accept(p))
|
||||||
{
|
{
|
||||||
List<DatagramPacket> socketReceived = getReceived(socket);
|
SocketReceiveBuffer socketReceived = getReceived(socket);
|
||||||
|
|
||||||
synchronized (socketReceived)
|
synchronized (socketReceived)
|
||||||
{
|
{
|
||||||
@ -281,7 +281,7 @@ abstract class MultiplexingXXXSocketSupport
|
|||||||
}
|
}
|
||||||
if (!accepted)
|
if (!accepted)
|
||||||
{
|
{
|
||||||
List<DatagramPacket> thisReceived = getReceived();
|
SocketReceiveBuffer thisReceived = getReceived();
|
||||||
|
|
||||||
synchronized (thisReceived)
|
synchronized (thisReceived)
|
||||||
{
|
{
|
||||||
@ -370,7 +370,7 @@ abstract class MultiplexingXXXSocketSupport
|
|||||||
* not accepted by any (existing) {@code DatagramPacketFilter} at the time of
|
* not accepted by any (existing) {@code DatagramPacketFilter} at the time of
|
||||||
* receipt
|
* receipt
|
||||||
*/
|
*/
|
||||||
protected abstract List<DatagramPacket> getReceived();
|
protected abstract SocketReceiveBuffer getReceived();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the list of {@code DatagramPacket}s received by this multiplexing
|
* 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
|
* socket and accepted by the {@code DatagramPacketFilter} of the
|
||||||
* multiplexed {@code socket} at the time of receipt
|
* multiplexed {@code socket} at the time of receipt
|
||||||
*/
|
*/
|
||||||
protected abstract List<DatagramPacket> getReceived(
|
protected abstract SocketReceiveBuffer getReceived(
|
||||||
MultiplexedXXXSocketT socket);
|
MultiplexedXXXSocketT socket);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -488,47 +488,27 @@ abstract class MultiplexingXXXSocketSupport
|
|||||||
{
|
{
|
||||||
// Pull the packets which have been received already and are accepted by
|
// Pull the packets which have been received already and are accepted by
|
||||||
// the specified multiplexed socket out of the multiplexing socket.
|
// the specified multiplexed socket out of the multiplexing socket.
|
||||||
List<DatagramPacket> thisReceived = getReceived();
|
final SocketReceiveBuffer thisReceived = getReceived();
|
||||||
DatagramPacketFilter socketFilter = getFilter(socket);
|
final DatagramPacketFilter socketFilter = getFilter(socket);
|
||||||
List<DatagramPacket> toMove = null;
|
final List<DatagramPacket> toMove;
|
||||||
|
|
||||||
synchronized (thisReceived)
|
synchronized (thisReceived)
|
||||||
{
|
{
|
||||||
if (thisReceived.isEmpty())
|
toMove = thisReceived.scan(socketFilter);
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push the packets which have been accepted already and are accepted by
|
// Push the packets which have been accepted already and are accepted by
|
||||||
// the specified multiplexed socket into the multiplexed socket in
|
// the specified multiplexed socket into the multiplexed socket in
|
||||||
// question.
|
// question.
|
||||||
if (toMove != null)
|
if (!toMove.isEmpty())
|
||||||
{
|
{
|
||||||
List<DatagramPacket> socketReceived = getReceived(socket);
|
final SocketReceiveBuffer socketReceived = getReceived(socket);
|
||||||
|
|
||||||
synchronized (socketReceived)
|
synchronized (socketReceived)
|
||||||
{
|
{
|
||||||
socketReceived.addAll(toMove);
|
for (DatagramPacket datagramPacket : toMove) {
|
||||||
|
socketReceived.add(datagramPacket);
|
||||||
|
}
|
||||||
// The notifyAll will practically likely be unnecessary because
|
// The notifyAll will practically likely be unnecessary because
|
||||||
// the specified socket will likely be a newly-created one to
|
// the specified socket will likely be a newly-created one to
|
||||||
// which noone else has a reference. Anyway, dp the invocation
|
// 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
|
* @throws SocketTimeoutException if <tt>timeout</tt> is positive and has
|
||||||
* expired
|
* expired
|
||||||
*/
|
*/
|
||||||
void receive(List<DatagramPacket> received, DatagramPacket p, int timeout)
|
void receive(SocketReceiveBuffer received, DatagramPacket p, int timeout)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
@ -573,12 +553,9 @@ abstract class MultiplexingXXXSocketSupport
|
|||||||
// SocketTimeoutException.
|
// SocketTimeoutException.
|
||||||
synchronized (received)
|
synchronized (received)
|
||||||
{
|
{
|
||||||
if (!received.isEmpty())
|
receivedPacket = received.poll();
|
||||||
{
|
if (receivedPacket != null)
|
||||||
receivedPacket = received.remove(0);
|
break;
|
||||||
if (receivedPacket != null)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Throw a SocketTimeoutException if the timeout is over/up.
|
// Throw a SocketTimeoutException if the timeout is over/up.
|
||||||
|
|||||||
@ -19,132 +19,269 @@ package org.ice4j.socket;
|
|||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.*;
|
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>DatagramSocket</tt> or a <tt>Socket</tt>. The list enforces the
|
||||||
* <tt>SO_RCVBUF</tt> option for the associated <tt>DatagramSocket</tt> or
|
* <tt>SO_RCVBUF</tt> option for the associated <tt>DatagramSocket</tt> or
|
||||||
* <tt>Socket</tt>.
|
* <tt>Socket</tt>.
|
||||||
*
|
*
|
||||||
* @author Lyubomir Marinov
|
* @author Lyubomir Marinov
|
||||||
|
* @author Yura Yaroshevich
|
||||||
*/
|
*/
|
||||||
abstract class SocketReceiveBuffer
|
class SocketReceiveBuffer
|
||||||
extends LinkedList<DatagramPacket>
|
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Default size in bytes of socket receive buffer.
|
||||||
|
*/
|
||||||
private static final int DEFAULT_RECEIVE_BUFFER_SIZE = 1024 * 1024;
|
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
|
* Queue to store received datagrams.
|
||||||
* <tt>DatagramSocket</tt> or <tt>Socket</tt>. Cached for the sake of
|
|
||||||
* performance.
|
|
||||||
*/
|
*/
|
||||||
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 SocketReceiveBuffer(Callable<Integer> receiveBufferSizeSupplier)
|
||||||
public boolean add(DatagramPacket p)
|
|
||||||
{
|
{
|
||||||
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
|
// Keep track of the (total) size in bytes of this receive buffer in
|
||||||
// order to be able to enforce SO_RCVBUF.
|
// order to be able to enforce SO_RCVBUF restriction.
|
||||||
if (added && (p != null))
|
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 (matchedDatagrams == null)
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
{
|
||||||
// For the sake of performance, do not invoke the method
|
matchedDatagrams = new ArrayList<>();
|
||||||
// getReceiveBufferSize() of DatagramSocket or Socket on
|
}
|
||||||
// every packet added to this buffer.
|
matchedDatagrams.add(p);
|
||||||
int receiveBufferSize = this.receiveBufferSize;
|
|
||||||
|
|
||||||
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
|
||||||
{
|
= this.receiveBufferSizeSupplier.call();
|
||||||
receiveBufferSize = getReceiveBufferSize();
|
}
|
||||||
}
|
catch (Exception e)
|
||||||
catch (SocketException sex)
|
{
|
||||||
{
|
// 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)
|
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)
|
this.cachedReceiveBufferSize = receiveBufferSize;
|
||||||
remove(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return added;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the value of the <tt>SO_RCVBUF</tt> option for the associated
|
* Updates computed value of total datagrams size in bytes
|
||||||
* <tt>DatagramSocket</tt> or <tt>Socket</tt> which is the buffer size used
|
* stored in {@link #buffer} with datagram just removed from buffer.
|
||||||
* by the platform for input on the <tt>DatagramSocket</tt> or
|
* @param p datagram packed removed from {@link #buffer}
|
||||||
* <tt>Socket</tt>.
|
*/
|
||||||
*
|
void trackDatagramRemoved(DatagramPacket p)
|
||||||
* @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)
|
|
||||||
{
|
{
|
||||||
int pSize = p.getLength();
|
final int pSize = p.getLength();
|
||||||
|
if (pSize <= 0)
|
||||||
if (pSize > 0)
|
|
||||||
{
|
{
|
||||||
size -= pSize;
|
return;
|
||||||
if (size < 0)
|
}
|
||||||
size = 0;
|
|
||||||
|
totalBuffersByteSize -= pSize;
|
||||||
|
if (totalBuffersByteSize < 0)
|
||||||
|
{
|
||||||
|
totalBuffersByteSize = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user