From e273e24ee67dd7534239417adffc72d338b4951a Mon Sep 17 00:00:00 2001 From: "lubomir.marinov" Date: Tue, 14 Oct 2014 12:16:39 +0000 Subject: [PATCH] Attempts to respect DatagramSocket and Socket SO_RCVBUF and, thus, limits the possible retained memory. --- .../socket/MultiplexedDatagramSocket.java | 14 +- src/org/ice4j/socket/MultiplexedSocket.java | 14 +- .../socket/MultiplexingDatagramSocket.java | 13 +- src/org/ice4j/socket/MultiplexingSocket.java | 13 +- src/org/ice4j/socket/SocketReceiveBuffer.java | 140 ++++++++++++++++++ 5 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 src/org/ice4j/socket/SocketReceiveBuffer.java diff --git a/src/org/ice4j/socket/MultiplexedDatagramSocket.java b/src/org/ice4j/socket/MultiplexedDatagramSocket.java index d54ddb8..d199c98 100644 --- a/src/org/ice4j/socket/MultiplexedDatagramSocket.java +++ b/src/org/ice4j/socket/MultiplexedDatagramSocket.java @@ -44,7 +44,19 @@ public class MultiplexedDatagramSocket * The list of DatagramPackets to be received through this * DatagramSocket i.e. accepted by {@link #filter}. */ - final List received = new LinkedList(); + final List received + = new SocketReceiveBuffer() + { + private static final long serialVersionUID + = -5763976093759762087L; + + @Override + public int getReceiveBufferSize() + throws SocketException + { + return MultiplexedDatagramSocket.this.getReceiveBufferSize(); + } + }; /** * Initializes a new MultiplexedDatagramSocket which is unbound and diff --git a/src/org/ice4j/socket/MultiplexedSocket.java b/src/org/ice4j/socket/MultiplexedSocket.java index dda3577..bab0a9d 100644 --- a/src/org/ice4j/socket/MultiplexedSocket.java +++ b/src/org/ice4j/socket/MultiplexedSocket.java @@ -56,7 +56,19 @@ public class MultiplexedSocket * The list of DatagramPackets to be received through this * Socket i.e. accepted by {@link #filter}. */ - final List received = new LinkedList(); + final List received + = new SocketReceiveBuffer() + { + private static final long serialVersionUID + = 678744096057601141L; + + @Override + public int getReceiveBufferSize() + throws SocketException + { + return MultiplexedSocket.this.getReceiveBufferSize(); + } + }; /** * Initializes a new MultiplexedSocket which is unbound and filters diff --git a/src/org/ice4j/socket/MultiplexingDatagramSocket.java b/src/org/ice4j/socket/MultiplexingDatagramSocket.java index dce053a..34e3e7c 100644 --- a/src/org/ice4j/socket/MultiplexingDatagramSocket.java +++ b/src/org/ice4j/socket/MultiplexingDatagramSocket.java @@ -226,7 +226,18 @@ public class MultiplexingDatagramSocket * of {@link #sockets} at the time of the reading from the network. */ private final List received - = new LinkedList(); + = new SocketReceiveBuffer() + { + private static final long serialVersionUID + = 3125772367019091216L; + + @Override + public int getReceiveBufferSize() + throws SocketException + { + return MultiplexingDatagramSocket.this.getReceiveBufferSize(); + } + }; /** * The Object which synchronizes the access to {@link #inReceive}. diff --git a/src/org/ice4j/socket/MultiplexingSocket.java b/src/org/ice4j/socket/MultiplexingSocket.java index 93121cf..bd399e0 100644 --- a/src/org/ice4j/socket/MultiplexingSocket.java +++ b/src/org/ice4j/socket/MultiplexingSocket.java @@ -63,7 +63,18 @@ public class MultiplexingSocket * {@link #sockets} at the time of the reading from the network. */ private final List received - = new LinkedList(); + = new SocketReceiveBuffer() + { + private static final long serialVersionUID + = 4097024214973676873L; + + @Override + public int getReceiveBufferSize() + throws SocketException + { + return MultiplexingSocket.this.getReceiveBufferSize(); + } + }; /** * The Object which synchronizes the access to {@link #inReceive}. diff --git a/src/org/ice4j/socket/SocketReceiveBuffer.java b/src/org/ice4j/socket/SocketReceiveBuffer.java new file mode 100644 index 0000000..33c112c --- /dev/null +++ b/src/org/ice4j/socket/SocketReceiveBuffer.java @@ -0,0 +1,140 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the SIP Communicator community (http://sip-communicator.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.socket; + +import java.net.*; +import java.util.*; + +/** + * Implements a list 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 + */ +abstract class SocketReceiveBuffer + extends LinkedList +{ + private static final int DEFAULT_RECEIVE_BUFFER_SIZE = 1024 * 1024; + + private static final long serialVersionUID = 2804762379509257652L; + + /** + * The value of the SO_RCVBUF option for the associated + * DatagramSocket or Socket. Cached for the sake of + * performance. + */ + private int receiveBufferSize; + + /** + * The (total) size in bytes of this receive buffer. + */ + private int size; + + /** + * {@inheritDoc} + */ + @Override + public boolean add(DatagramPacket p) + { + boolean added = super.add(p); + + // 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)) + { + int pSize = p.getLength(); + + if (pSize > 0) + { + 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) + { + // 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; + + if ((receiveBufferSize <= 0) || (modCount % 1000 == 0)) + { + try + { + receiveBufferSize = getReceiveBufferSize(); + } + catch (SocketException sex) + { + } + 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; + } + } + this.receiveBufferSize = receiveBufferSize; + } + if (size > receiveBufferSize) + remove(0); + } + } + } + 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) + { + int pSize = p.getLength(); + + if (pSize > 0) + { + size -= pSize; + if (size < 0) + size = 0; + } + } + return p; + } +}