From fa3e6c835710ee3afbba8cc6af06b48d9f682752 Mon Sep 17 00:00:00 2001 From: emcho Date: Tue, 4 Mar 2014 11:26:39 +0000 Subject: [PATCH] Adds support for socket timeouts. Contributed by Enrico Marocco --- .../socket/MultiplexingDatagramSocket.java | 63 ++++++++++++++++--- src/org/ice4j/socket/MultiplexingSocket.java | 61 +++++++++++++++--- 2 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/org/ice4j/socket/MultiplexingDatagramSocket.java b/src/org/ice4j/socket/MultiplexingDatagramSocket.java index 5c95f2b..9905764 100644 --- a/src/org/ice4j/socket/MultiplexingDatagramSocket.java +++ b/src/org/ice4j/socket/MultiplexingDatagramSocket.java @@ -87,6 +87,15 @@ public class MultiplexingDatagramSocket */ private final Object socketsSyncRoot = new Object(); + /** + * Buffer variable for storing the SO_TIMEOUT value set by the + * last setSoTimeout() call. Altough not strictly needed, + * getting the locally stored value as opposed to retrieving it + * from a parent getSoTimeout() call seems to + * significantly improve efficiency, at least on some platforms. + */ + private int soTimeout = 0; + /** * Initializes a new MultiplexingDatagramSocket instance which is * to enable DatagramPacket filtering and binds it to any available @@ -368,14 +377,16 @@ public class MultiplexingDatagramSocket * * @param p the DatagramPacket into which to place the incoming * data + * @throws SocketTimeoutException if setSoTimeout was + * previously called and the timeout has expired * @throws IOException if an I/O error occurs * @see DatagramSocket#receive(DatagramPacket) */ @Override public void receive(DatagramPacket p) - throws IOException + throws SocketTimeoutException, IOException { - receive(received, p); + receive(received, p, soTimeout); } /** @@ -390,15 +401,25 @@ public class MultiplexingDatagramSocket * from which the first is to be removed and returned if available * @param p the DatagramPacket into which to place the incoming * data + * @param timeout the maximum time in milliseconds to wait for a + * packet. A timeout of zero is interpreted as an infinite + * timeout + * @throws SocketTimeoutException if the timeout has expired * @throws IOException if an I/O error occurs */ - private void receive(List received, DatagramPacket p) - throws IOException + private void receive(List received, DatagramPacket p, + int timeout) + throws SocketTimeoutException, IOException { + long now, start = System.currentTimeMillis(); DatagramPacket r = null; do { + now = System.currentTimeMillis(); + if (timeout > 0 && now - start >= timeout) + throw new SocketTimeoutException("Socket timeout"); + synchronized (received) { if (!received.isEmpty()) @@ -434,7 +455,9 @@ public class MultiplexingDatagramSocket { try { - received.wait(1000); + received.wait((timeout > 0 ? + timeout - (now - start) : + 1000)); } catch (InterruptedException ie) { @@ -522,14 +545,14 @@ public class MultiplexingDatagramSocket * @param multiplexed the MultiplexedDatagramSocket which requests * the receipt of a DatagramPacket from the network * @param p the DatagramPacket to receive the data from the network + * @throws SocketTimeoutException if setSoTimeout was + * previously called and the timeout has expired * @throws IOException if an I/O error occurs */ - void receive( - MultiplexedDatagramSocket multiplexed, - DatagramPacket p) - throws IOException + void receive(MultiplexedDatagramSocket multiplexed, DatagramPacket p) + throws SocketTimeoutException, IOException { - receive(multiplexed.received, p); + receive(multiplexed.received, p, multiplexed.getSoTimeout()); } /** @@ -554,4 +577,24 @@ public class MultiplexingDatagramSocket } } } + + /** + * {@inheritDoc} + */ + @Override + public void setSoTimeout(int timeout) + throws SocketException + { + super.setSoTimeout(timeout); + soTimeout = timeout; + } + + /** + * {@inheritDoc} + */ + @Override + public int getSoTimeout() + { + return soTimeout; + } } diff --git a/src/org/ice4j/socket/MultiplexingSocket.java b/src/org/ice4j/socket/MultiplexingSocket.java index 08da536..5b04e48 100644 --- a/src/org/ice4j/socket/MultiplexingSocket.java +++ b/src/org/ice4j/socket/MultiplexingSocket.java @@ -83,6 +83,15 @@ public class MultiplexingSocket */ private final Object socketsSyncRoot = new Object(); + /** + * Buffer variable for storing the SO_TIMEOUT value set by the + * last setSoTimeout() call. Altough not strictly needed, + * getting the locally stored value as opposed to retrieving it + * from a parent getSoTimeout() call seems to + * significantly improve efficiency, at least on some platforms. + */ + private int soTimeout = 0; + /** * Initializes a new MultiplexingSocket instance. * @@ -256,7 +265,7 @@ public class MultiplexingSocket */ @Override public void receive(DatagramPacket p) - throws IOException + throws SocketTimeoutException, IOException { try { @@ -265,7 +274,7 @@ public class MultiplexingSocket catch(Exception e) { } - receive(received, p); + receive(received, p, soTimeout); } /** @@ -277,10 +286,8 @@ public class MultiplexingSocket * @param p the DatagramPacket to receive the data from the network * @throws IOException if an I/O error occurs */ - void receive( - MultiplexedSocket multiplexed, - DatagramPacket p) - throws IOException + void receive(MultiplexedSocket multiplexed, DatagramPacket p) + throws SocketTimeoutException, IOException { try { @@ -289,7 +296,7 @@ public class MultiplexingSocket catch(Exception e) { } - receive(multiplexed.received, p); + receive(multiplexed.received, p, multiplexed.getSoTimeout()); } /** @@ -304,15 +311,25 @@ public class MultiplexingSocket * from which the first is to be removed and returned if available * @param p the DatagramPacket into which to place the incoming * data + * @param timeout the maximum time in milliseconds to wait for a + * packet. A timeout of zero is interpreted as an infinite + * timeout + * @throws SocketTimeoutException if the timeout has expired * @throws IOException if an I/O error occurs */ - private void receive(List received, DatagramPacket p) - throws IOException + private void receive(List received, DatagramPacket p, + int timeout) + throws SocketTimeoutException, IOException { + long now, start = System.currentTimeMillis(); DatagramPacket r = null; do { + now = System.currentTimeMillis(); + if (timeout > 0 && now - start >= timeout) + throw new SocketTimeoutException("Socket timeout"); + boolean doReceive; synchronized (receiveSyncRoot) @@ -324,7 +341,10 @@ public class MultiplexingSocket doReceive = false; try { - receiveSyncRoot.wait(); + if (timeout > 0) + receiveSyncRoot.wait(timeout - (now - start)); + else + receiveSyncRoot.wait(); } catch (InterruptedException iex) { @@ -555,4 +575,25 @@ public class MultiplexingSocket { return super.getOutputStream(); } + + /** + * {@inheritDoc} + */ + @Override + public void setSoTimeout(int timeout) + throws SocketException + { + super.setSoTimeout(timeout); + soTimeout = timeout; + } + + /** + * {@inheritDoc} + */ + @Override + public int getSoTimeout() + { + return soTimeout; + } + }