diff --git a/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java b/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java index 7ad8992..20e7743 100644 --- a/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/MultiplexedDatagramSocket.java @@ -55,19 +55,8 @@ public class MultiplexedDatagramSocket * The list of DatagramPackets to be received through this * DatagramSocket i.e. accepted by {@link #filter}. */ - final List 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 MultiplexedDatagramSocket which is unbound and diff --git a/src/main/java/org/ice4j/socket/MultiplexedSocket.java b/src/main/java/org/ice4j/socket/MultiplexedSocket.java index 21c27d6..4871bdd 100644 --- a/src/main/java/org/ice4j/socket/MultiplexedSocket.java +++ b/src/main/java/org/ice4j/socket/MultiplexedSocket.java @@ -67,19 +67,8 @@ public class MultiplexedSocket * The list of DatagramPackets to be received through this * Socket i.e. accepted by {@link #filter}. */ - final List 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 MultiplexedSocket which is unbound and filters diff --git a/src/main/java/org/ice4j/socket/MultiplexingDatagramSocket.java b/src/main/java/org/ice4j/socket/MultiplexingDatagramSocket.java index 7e48826..43be893 100644 --- a/src/main/java/org/ice4j/socket/MultiplexingDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/MultiplexingDatagramSocket.java @@ -81,7 +81,7 @@ public class MultiplexingDatagramSocket * {@inheritDoc} */ @Override - protected List getReceived() + protected SocketReceiveBuffer getReceived() { return received; } @@ -90,7 +90,7 @@ public class MultiplexingDatagramSocket * {@inheritDoc} */ @Override - protected List 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 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 diff --git a/src/main/java/org/ice4j/socket/MultiplexingSocket.java b/src/main/java/org/ice4j/socket/MultiplexingSocket.java index 5607f4c..f74ee2e 100644 --- a/src/main/java/org/ice4j/socket/MultiplexingSocket.java +++ b/src/main/java/org/ice4j/socket/MultiplexingSocket.java @@ -92,7 +92,7 @@ public class MultiplexingSocket * {@inheritDoc} */ @Override - protected List getReceived() + protected SocketReceiveBuffer getReceived() { return received; } @@ -101,7 +101,7 @@ public class MultiplexingSocket * {@inheritDoc} */ @Override - protected List getReceived( + protected SocketReceiveBuffer getReceived( MultiplexedSocket socket) { return socket.received; @@ -118,19 +118,8 @@ public class MultiplexingSocket * Socket i.e. not accepted by the DatagramFilters of * {@link #sockets} at the time of the reading from the network. */ - private final List 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 diff --git a/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java b/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java index e9bed09..984369a 100644 --- a/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java +++ b/src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java @@ -265,7 +265,7 @@ abstract class MultiplexingXXXSocketSupport { if (getFilter(socket).accept(p)) { - List socketReceived = getReceived(socket); + SocketReceiveBuffer socketReceived = getReceived(socket); synchronized (socketReceived) { @@ -281,7 +281,7 @@ abstract class MultiplexingXXXSocketSupport } if (!accepted) { - List 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 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 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 thisReceived = getReceived(); - DatagramPacketFilter socketFilter = getFilter(socket); - List toMove = null; + final SocketReceiveBuffer thisReceived = getReceived(); + final DatagramPacketFilter socketFilter = getFilter(socket); + final List toMove; synchronized (thisReceived) { - if (thisReceived.isEmpty()) - { - return; - } - else - { - for (Iterator 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 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 timeout is positive and has * expired */ - void receive(List 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. diff --git a/src/main/java/org/ice4j/socket/SocketReceiveBuffer.java b/src/main/java/org/ice4j/socket/SocketReceiveBuffer.java index c4cde08..812869b 100644 --- a/src/main/java/org/ice4j/socket/SocketReceiveBuffer.java +++ b/src/main/java/org/ice4j/socket/SocketReceiveBuffer.java @@ -19,132 +19,269 @@ package org.ice4j.socket; import java.net.*; import java.util.*; +import java.util.concurrent.*; /** - * Implements a list of DatagramPackets received by a + * Implements a buffer of DatagramPackets received by a * DatagramSocket or a Socket. The list enforces the * SO_RCVBUF option for the associated DatagramSocket or * Socket. * * @author Lyubomir Marinov + * @author Yura Yaroshevich */ -abstract class SocketReceiveBuffer - extends LinkedList +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 SO_RCVBUF option for the associated - * DatagramSocket or Socket. Cached for the sake of - * performance. + * Queue to store received datagrams. */ - private int receiveBufferSize; + private final BlockingQueue 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 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 scan(DatagramPacketFilter filter) + { + List matchedDatagrams = null; + final Iterator 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 SO_RCVBUF option for the associated + * DatagramSocket or Socket. 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 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 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 SO_RCVBUF option for the associated - * DatagramSocket or Socket which is the buffer size used - * by the platform for input on the DatagramSocket or - * Socket. - * - * @return the value of the SO_RCVBUF option for the associated - * DatagramSocket or Socket - * @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; } }