Adds support for socket timeouts. Contributed by Enrico Marocco
This commit is contained in:
parent
945dcae9c0
commit
fa3e6c8357
@ -87,6 +87,15 @@ public class MultiplexingDatagramSocket
|
|||||||
*/
|
*/
|
||||||
private final Object socketsSyncRoot = new Object();
|
private final Object socketsSyncRoot = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffer variable for storing the SO_TIMEOUT value set by the
|
||||||
|
* last <tt>setSoTimeout()</tt> call. Altough not strictly needed,
|
||||||
|
* getting the locally stored value as opposed to retrieving it
|
||||||
|
* from a parent <tt>getSoTimeout()</tt> call seems to
|
||||||
|
* significantly improve efficiency, at least on some platforms.
|
||||||
|
*/
|
||||||
|
private int soTimeout = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
|
* Initializes a new <tt>MultiplexingDatagramSocket</tt> instance which is
|
||||||
* to enable <tt>DatagramPacket</tt> filtering and binds it to any available
|
* to enable <tt>DatagramPacket</tt> filtering and binds it to any available
|
||||||
@ -368,14 +377,16 @@ public class MultiplexingDatagramSocket
|
|||||||
*
|
*
|
||||||
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
||||||
* data
|
* data
|
||||||
|
* @throws SocketTimeoutException if <tt>setSoTimeout</tt> was
|
||||||
|
* previously called and the timeout has expired
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
* @see DatagramSocket#receive(DatagramPacket)
|
* @see DatagramSocket#receive(DatagramPacket)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void receive(DatagramPacket p)
|
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
|
* from which the first is to be removed and returned if available
|
||||||
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
||||||
* data
|
* 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
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
private void receive(List<DatagramPacket> received, DatagramPacket p)
|
private void receive(List<DatagramPacket> received, DatagramPacket p,
|
||||||
throws IOException
|
int timeout)
|
||||||
|
throws SocketTimeoutException, IOException
|
||||||
{
|
{
|
||||||
|
long now, start = System.currentTimeMillis();
|
||||||
DatagramPacket r = null;
|
DatagramPacket r = null;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
now = System.currentTimeMillis();
|
||||||
|
if (timeout > 0 && now - start >= timeout)
|
||||||
|
throw new SocketTimeoutException("Socket timeout");
|
||||||
|
|
||||||
synchronized (received)
|
synchronized (received)
|
||||||
{
|
{
|
||||||
if (!received.isEmpty())
|
if (!received.isEmpty())
|
||||||
@ -434,7 +455,9 @@ public class MultiplexingDatagramSocket
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
received.wait(1000);
|
received.wait((timeout > 0 ?
|
||||||
|
timeout - (now - start) :
|
||||||
|
1000));
|
||||||
}
|
}
|
||||||
catch (InterruptedException ie)
|
catch (InterruptedException ie)
|
||||||
{
|
{
|
||||||
@ -522,14 +545,14 @@ public class MultiplexingDatagramSocket
|
|||||||
* @param multiplexed the <tt>MultiplexedDatagramSocket</tt> which requests
|
* @param multiplexed the <tt>MultiplexedDatagramSocket</tt> which requests
|
||||||
* the receipt of a <tt>DatagramPacket</tt> from the network
|
* the receipt of a <tt>DatagramPacket</tt> from the network
|
||||||
* @param p the <tt>DatagramPacket</tt> to receive the data from the network
|
* @param p the <tt>DatagramPacket</tt> to receive the data from the network
|
||||||
|
* @throws SocketTimeoutException if <tt>setSoTimeout</tt> was
|
||||||
|
* previously called and the timeout has expired
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
void receive(
|
void receive(MultiplexedDatagramSocket multiplexed, DatagramPacket p)
|
||||||
MultiplexedDatagramSocket multiplexed,
|
throws SocketTimeoutException, IOException
|
||||||
DatagramPacket p)
|
|
||||||
throws 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -83,6 +83,15 @@ public class MultiplexingSocket
|
|||||||
*/
|
*/
|
||||||
private final Object socketsSyncRoot = new Object();
|
private final Object socketsSyncRoot = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffer variable for storing the SO_TIMEOUT value set by the
|
||||||
|
* last <tt>setSoTimeout()</tt> call. Altough not strictly needed,
|
||||||
|
* getting the locally stored value as opposed to retrieving it
|
||||||
|
* from a parent <tt>getSoTimeout()</tt> call seems to
|
||||||
|
* significantly improve efficiency, at least on some platforms.
|
||||||
|
*/
|
||||||
|
private int soTimeout = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>MultiplexingSocket</tt> instance.
|
* Initializes a new <tt>MultiplexingSocket</tt> instance.
|
||||||
*
|
*
|
||||||
@ -256,7 +265,7 @@ public class MultiplexingSocket
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void receive(DatagramPacket p)
|
public void receive(DatagramPacket p)
|
||||||
throws IOException
|
throws SocketTimeoutException, IOException
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -265,7 +274,7 @@ public class MultiplexingSocket
|
|||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
receive(received, p);
|
receive(received, p, soTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -277,10 +286,8 @@ public class MultiplexingSocket
|
|||||||
* @param p the <tt>DatagramPacket</tt> to receive the data from the network
|
* @param p the <tt>DatagramPacket</tt> to receive the data from the network
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
void receive(
|
void receive(MultiplexedSocket multiplexed, DatagramPacket p)
|
||||||
MultiplexedSocket multiplexed,
|
throws SocketTimeoutException, IOException
|
||||||
DatagramPacket p)
|
|
||||||
throws IOException
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -289,7 +296,7 @@ public class MultiplexingSocket
|
|||||||
catch(Exception e)
|
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
|
* from which the first is to be removed and returned if available
|
||||||
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
|
||||||
* data
|
* 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
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
private void receive(List<DatagramPacket> received, DatagramPacket p)
|
private void receive(List<DatagramPacket> received, DatagramPacket p,
|
||||||
throws IOException
|
int timeout)
|
||||||
|
throws SocketTimeoutException, IOException
|
||||||
{
|
{
|
||||||
|
long now, start = System.currentTimeMillis();
|
||||||
DatagramPacket r = null;
|
DatagramPacket r = null;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
now = System.currentTimeMillis();
|
||||||
|
if (timeout > 0 && now - start >= timeout)
|
||||||
|
throw new SocketTimeoutException("Socket timeout");
|
||||||
|
|
||||||
boolean doReceive;
|
boolean doReceive;
|
||||||
|
|
||||||
synchronized (receiveSyncRoot)
|
synchronized (receiveSyncRoot)
|
||||||
@ -324,6 +341,9 @@ public class MultiplexingSocket
|
|||||||
doReceive = false;
|
doReceive = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (timeout > 0)
|
||||||
|
receiveSyncRoot.wait(timeout - (now - start));
|
||||||
|
else
|
||||||
receiveSyncRoot.wait();
|
receiveSyncRoot.wait();
|
||||||
}
|
}
|
||||||
catch (InterruptedException iex)
|
catch (InterruptedException iex)
|
||||||
@ -555,4 +575,25 @@ public class MultiplexingSocket
|
|||||||
{
|
{
|
||||||
return super.getOutputStream();
|
return super.getOutputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setSoTimeout(int timeout)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
super.setSoTimeout(timeout);
|
||||||
|
soTimeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getSoTimeout()
|
||||||
|
{
|
||||||
|
return soTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user