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();
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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
|
||||
* data
|
||||
* @throws SocketTimeoutException if <tt>setSoTimeout</tt> 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 <tt>DatagramPacket</tt> 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<DatagramPacket> received, DatagramPacket p)
|
||||
throws IOException
|
||||
private void receive(List<DatagramPacket> 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 <tt>MultiplexedDatagramSocket</tt> which requests
|
||||
* the receipt of a <tt>DatagramPacket</tt> 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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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 <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.
|
||||
*
|
||||
@ -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 <tt>DatagramPacket</tt> 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 <tt>DatagramPacket</tt> 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<DatagramPacket> received, DatagramPacket p)
|
||||
throws IOException
|
||||
private void receive(List<DatagramPacket> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user