Allows a listening endpoint to be shared among multiple protocols (e.g. ICE-TCP, HTTP).
This commit is contained in:
parent
c7b8fd2e2b
commit
446607e5d7
@ -32,6 +32,7 @@ import org.ice4j.socket.*;
|
|||||||
* to the appropriate <tt>Component</tt>.
|
* to the appropriate <tt>Component</tt>.
|
||||||
*
|
*
|
||||||
* @author Boris Grozev
|
* @author Boris Grozev
|
||||||
|
* @author Lyubomir Marinov
|
||||||
*/
|
*/
|
||||||
public class MultiplexingTcpHostHarvester
|
public class MultiplexingTcpHostHarvester
|
||||||
extends CandidateHarvester
|
extends CandidateHarvester
|
||||||
@ -49,11 +50,14 @@ public class MultiplexingTcpHostHarvester
|
|||||||
private static final int PURGE_INTERVAL = 20;
|
private static final int PURGE_INTERVAL = 20;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Channels which we have failed to read from after at least
|
* Closes a {@code Channel} and swallows any {@link IOException}.
|
||||||
* <tt>READ_TIMEOUT</tt> milliseconds will be considered failed and will
|
*
|
||||||
* be closed.
|
* @param channel the {@code Channel} to close
|
||||||
*/
|
*/
|
||||||
private static final int READ_TIMEOUT = 10000;
|
static void closeNoExceptions(Channel channel)
|
||||||
|
{
|
||||||
|
MuxServerSocketChannel.closeNoExceptions(channel);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of all addresses on the interfaces in <tt>interfaces</tt>
|
* Returns a list of all addresses on the interfaces in <tt>interfaces</tt>
|
||||||
@ -67,7 +71,6 @@ public class MultiplexingTcpHostHarvester
|
|||||||
int port,
|
int port,
|
||||||
List<NetworkInterface> interfaces)
|
List<NetworkInterface> interfaces)
|
||||||
throws IOException
|
throws IOException
|
||||||
|
|
||||||
{
|
{
|
||||||
List<TransportAddress> addresses = new LinkedList<TransportAddress>();
|
List<TransportAddress> addresses = new LinkedList<TransportAddress>();
|
||||||
|
|
||||||
@ -257,7 +260,6 @@ public class MultiplexingTcpHostHarvester
|
|||||||
boolean useIPv6 = !StackProperties.getBoolean(
|
boolean useIPv6 = !StackProperties.getBoolean(
|
||||||
StackProperties.DISABLE_IPv6,
|
StackProperties.DISABLE_IPv6,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
boolean useIPv6LinkLocal = !StackProperties.getBoolean(
|
boolean useIPv6LinkLocal = !StackProperties.getBoolean(
|
||||||
StackProperties.DISABLE_LINK_LOCAL_ADDRESSES,
|
StackProperties.DISABLE_LINK_LOCAL_ADDRESSES,
|
||||||
false);
|
false);
|
||||||
@ -267,6 +269,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
= StackProperties.getStringArray(StackProperties.ALLOWED_ADDRESSES,
|
= StackProperties.getStringArray(StackProperties.ALLOWED_ADDRESSES,
|
||||||
";");
|
";");
|
||||||
InetAddress[] allowedAddresses = null;
|
InetAddress[] allowedAddresses = null;
|
||||||
|
|
||||||
if (allowedAddressesStr != null)
|
if (allowedAddressesStr != null)
|
||||||
{
|
{
|
||||||
allowedAddresses = new InetAddress[allowedAddressesStr.length];
|
allowedAddresses = new InetAddress[allowedAddressesStr.length];
|
||||||
@ -282,6 +285,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
= StackProperties.getStringArray(StackProperties.BLOCKED_ADDRESSES,
|
= StackProperties.getStringArray(StackProperties.BLOCKED_ADDRESSES,
|
||||||
";");
|
";");
|
||||||
InetAddress[] blockedAddresses = null;
|
InetAddress[] blockedAddresses = null;
|
||||||
|
|
||||||
if (blockedAddressesStr != null)
|
if (blockedAddressesStr != null)
|
||||||
{
|
{
|
||||||
blockedAddresses = new InetAddress[blockedAddressesStr.length];
|
blockedAddresses = new InetAddress[blockedAddressesStr.length];
|
||||||
@ -317,6 +321,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
if (allowedAddresses != null)
|
if (allowedAddresses != null)
|
||||||
{
|
{
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
|
|
||||||
for (InetAddress allowedAddress : allowedAddresses)
|
for (InetAddress allowedAddress : allowedAddresses)
|
||||||
{
|
{
|
||||||
if (allowedAddress.equals(address))
|
if (allowedAddress.equals(address))
|
||||||
@ -325,7 +330,6 @@ public class MultiplexingTcpHostHarvester
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
logger.info("Not using " + address +" for TCP candidates, "
|
logger.info("Not using " + address +" for TCP candidates, "
|
||||||
@ -337,6 +341,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
if (blockedAddresses != null)
|
if (blockedAddresses != null)
|
||||||
{
|
{
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
|
|
||||||
for (InetAddress blockedAddress : blockedAddresses)
|
for (InetAddress blockedAddress : blockedAddresses)
|
||||||
{
|
{
|
||||||
if (blockedAddress.equals(address))
|
if (blockedAddress.equals(address))
|
||||||
@ -345,7 +350,6 @@ public class MultiplexingTcpHostHarvester
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found)
|
if (found)
|
||||||
{
|
{
|
||||||
logger.info("Not using " + address + " for TCP candidates, "
|
logger.info("Not using " + address + " for TCP candidates, "
|
||||||
@ -601,11 +605,25 @@ public class MultiplexingTcpHostHarvester
|
|||||||
{
|
{
|
||||||
for (TransportAddress transportAddress : localAddresses)
|
for (TransportAddress transportAddress : localAddresses)
|
||||||
{
|
{
|
||||||
ServerSocketChannel channel = ServerSocketChannel.open();
|
ServerSocketChannel channel
|
||||||
ServerSocket socket = channel.socket();
|
= MuxServerSocketChannel.openAndBind(
|
||||||
socket.bind(
|
/* properties */ null,
|
||||||
new InetSocketAddress(transportAddress.getAddress(),
|
new InetSocketAddress(
|
||||||
transportAddress.getPort()));
|
transportAddress.getAddress(),
|
||||||
|
transportAddress.getPort()),
|
||||||
|
/* backlog */ 0,
|
||||||
|
new DatagramPacketFilter()
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean accept(DatagramPacket p)
|
||||||
|
{
|
||||||
|
return isFirstDatagramPacket(p);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
serverSocketChannels.add(channel);
|
serverSocketChannels.add(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -616,6 +634,85 @@ public class MultiplexingTcpHostHarvester
|
|||||||
readThread.start();
|
readThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether a specific {@link DatagramPacket} is the first
|
||||||
|
* expected (i.e. supported) to be received from an accepted
|
||||||
|
* {@link SocketChannel} by this {@code MultiplexingTcpHostHarvester}. If
|
||||||
|
* {@link #ssltcp} signals that Google TURN SSLTCP is to be expected, then
|
||||||
|
* {@code p} must be
|
||||||
|
* {@link GoogleTurnSSLCandidateHarvester#SSL_CLIENT_HANDSHAKE}. Otherwise,
|
||||||
|
* it must be a STUN Binding request packetized for TCP.
|
||||||
|
*
|
||||||
|
* @param p the {@code DatagramPacket} to examine
|
||||||
|
* @return {@code true} if {@code p} looks like the first
|
||||||
|
* {@code DatagramPacket} expected to be received from an accepted
|
||||||
|
* {@code SocketChannel} by this {@code MultiplexingTcpHostHarvester};
|
||||||
|
* otherwise, {@code false}
|
||||||
|
*/
|
||||||
|
private boolean isFirstDatagramPacket(DatagramPacket p)
|
||||||
|
{
|
||||||
|
int len = p.getLength();
|
||||||
|
boolean b = false;
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
byte[] buf = p.getData();
|
||||||
|
int off = p.getOffset();
|
||||||
|
|
||||||
|
if (ssltcp)
|
||||||
|
{
|
||||||
|
// Google TURN SSLTCP
|
||||||
|
final byte[] googleTurnSslTcp
|
||||||
|
= GoogleTurnSSLCandidateHarvester.SSL_CLIENT_HANDSHAKE;
|
||||||
|
|
||||||
|
if (len >= googleTurnSslTcp.length)
|
||||||
|
{
|
||||||
|
b = true;
|
||||||
|
for (int i = 0, iEnd = googleTurnSslTcp.length, j = off;
|
||||||
|
i < iEnd;
|
||||||
|
i++, j++)
|
||||||
|
{
|
||||||
|
if (googleTurnSslTcp[i] != buf[j])
|
||||||
|
{
|
||||||
|
b = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 2 bytes uint16 length
|
||||||
|
// STUN Binding request:
|
||||||
|
// 2 bits 00
|
||||||
|
// 14 bits STUN Messsage Type
|
||||||
|
// 2 bytes Message Length
|
||||||
|
// 4 bytes Magic Cookie
|
||||||
|
|
||||||
|
// RFC 5389: For example, a Binding request has class=0b00
|
||||||
|
// (request) and method=0b000000000001 (Binding) and is encoded
|
||||||
|
// into the first 16 bits as 0x0001.
|
||||||
|
if (len >= 10 && buf[off + 2] == 0 && buf[off + 3] == 1)
|
||||||
|
{
|
||||||
|
final byte[] magicCookie = Message.MAGIC_COOKIE;
|
||||||
|
|
||||||
|
b = true;
|
||||||
|
for (int i = 0, iEnd = magicCookie.length, j = off + 6;
|
||||||
|
i < iEnd;
|
||||||
|
i++, j++)
|
||||||
|
{
|
||||||
|
if (magicCookie[i] != buf[j])
|
||||||
|
{
|
||||||
|
b = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes entries from {@link #components} for which the
|
* Removes entries from {@link #components} for which the
|
||||||
* <tt>WeakReference</tt> has been cleared.
|
* <tt>WeakReference</tt> has been cleared.
|
||||||
@ -663,7 +760,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
/**
|
/**
|
||||||
* Initializes a new <tt>AcceptThread</tt>.
|
* Initializes a new <tt>AcceptThread</tt>.
|
||||||
*/
|
*/
|
||||||
private AcceptThread()
|
public AcceptThread()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
setName("MultiplexingTcpHostHarvester AcceptThread");
|
setName("MultiplexingTcpHostHarvester AcceptThread");
|
||||||
@ -691,41 +788,31 @@ public class MultiplexingTcpHostHarvester
|
|||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
while (true)
|
do
|
||||||
{
|
{
|
||||||
int readyChannels;
|
|
||||||
if (close)
|
if (close)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Allow to go on, so we can quit if closed
|
|
||||||
readyChannels = selector.select(3000);
|
|
||||||
}
|
|
||||||
catch (IOException ioe)
|
|
||||||
{
|
|
||||||
logger.info("Failed to select an accept-ready socket: "
|
|
||||||
+ ioe);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (readyChannels > 0)
|
|
||||||
{
|
|
||||||
IOException exception = null;
|
IOException exception = null;
|
||||||
List<SocketChannel> channelsToAdd
|
List<SocketChannel> channelsToAdd
|
||||||
= new LinkedList<SocketChannel>();
|
= new LinkedList<SocketChannel>();
|
||||||
|
// Allow to go on, so we can quit if closed.
|
||||||
|
long selectTimeout = 3000;
|
||||||
|
|
||||||
for (SelectionKey key : selector.selectedKeys())
|
for (SelectionKey key : selector.keys())
|
||||||
|
{
|
||||||
|
if (key.isValid())
|
||||||
{
|
{
|
||||||
SocketChannel channel;
|
SocketChannel channel;
|
||||||
if (key.isAcceptable())
|
boolean acceptable = key.isAcceptable();
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
channel = ((ServerSocketChannel)
|
channel
|
||||||
key.channel()).accept();
|
= ((ServerSocketChannel) key.channel())
|
||||||
|
.accept();
|
||||||
}
|
}
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{
|
{
|
||||||
@ -733,11 +820,23 @@ public class MultiplexingTcpHostHarvester
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the accepted socket to newChannels, so
|
// Add the accepted channel to newChannels to allow the
|
||||||
// the 'read' thread can pick it up.
|
// 'read' thread to it up.
|
||||||
|
if (channel != null)
|
||||||
|
{
|
||||||
channelsToAdd.add(channel);
|
channelsToAdd.add(channel);
|
||||||
}
|
}
|
||||||
|
else if (acceptable)
|
||||||
|
{
|
||||||
|
// The SelectionKey reported the channel as
|
||||||
|
// acceptable but channel#accept() did not accept a
|
||||||
|
// non-null SocketChannel. Give the channel a little
|
||||||
|
// time to get its act together.
|
||||||
|
selectTimeout = 100;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We accepted from all serverSocketChannels.
|
||||||
selector.selectedKeys().clear();
|
selector.selectedKeys().clear();
|
||||||
|
|
||||||
if (!channelsToAdd.isEmpty())
|
if (!channelsToAdd.isEmpty())
|
||||||
@ -751,25 +850,29 @@ public class MultiplexingTcpHostHarvester
|
|||||||
|
|
||||||
if (exception != null)
|
if (exception != null)
|
||||||
{
|
{
|
||||||
logger.info("Failed to accept a socket, which"
|
logger.info(
|
||||||
+ "should have been ready to accept: "
|
"Failed to accept a socket, which should have been"
|
||||||
+ exception);
|
+ " ready to accept: " + exception);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} // while(true)
|
|
||||||
|
|
||||||
//now clean up and exit
|
|
||||||
for (ServerSocketChannel serverSocketChannel : serverSocketChannels)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
serverSocketChannel.close();
|
// Allow to go on, so we can quit if closed.
|
||||||
|
selector.select(selectTimeout);
|
||||||
}
|
}
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{
|
{
|
||||||
|
logger.info(
|
||||||
|
"Failed to select an accept-ready socket: " + ioe);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
while (true);
|
||||||
|
|
||||||
|
//now clean up and exit
|
||||||
|
for (ServerSocketChannel serverSocketChannel : serverSocketChannels)
|
||||||
|
closeNoExceptions(serverSocketChannel);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -780,6 +883,48 @@ public class MultiplexingTcpHostHarvester
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains a <tt>SocketChannel</tt> that <tt>ReadThread</tt> is reading
|
||||||
|
* from.
|
||||||
|
*/
|
||||||
|
private static class ChannelDesc
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The actual <tt>SocketChannel</tt>.
|
||||||
|
*/
|
||||||
|
public final SocketChannel channel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The time the channel was last found to be active.
|
||||||
|
*/
|
||||||
|
long lastActive = System.currentTimeMillis();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The buffer which stores the data so far read from the channel.
|
||||||
|
*/
|
||||||
|
ByteBuffer buffer = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether or not the initial "pseudo" SSL handshake has been read.
|
||||||
|
*/
|
||||||
|
boolean sslHandshakeRead = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The value of the RFC4571 "length" field read from the channel, or
|
||||||
|
* -1 if it hasn't been read (yet).
|
||||||
|
*/
|
||||||
|
int length = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new <tt>ChannelDesc</tt> with the given channel.
|
||||||
|
* @param channel the channel.
|
||||||
|
*/
|
||||||
|
public ChannelDesc(SocketChannel channel)
|
||||||
|
{
|
||||||
|
this.channel = channel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An <tt>IceSocketWrapper</tt> implementation which allows a
|
* An <tt>IceSocketWrapper</tt> implementation which allows a
|
||||||
* <tt>DatagramPacket</tt> to be pushed back and received on the first call
|
* <tt>DatagramPacket</tt> to be pushed back and received on the first call
|
||||||
@ -810,7 +955,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
* @param datagramPacket the <tt>DatagramPacket</tt> which will be used
|
* @param datagramPacket the <tt>DatagramPacket</tt> which will be used
|
||||||
* on the first call to {@link #receive(java.net.DatagramPacket)}
|
* on the first call to {@link #receive(java.net.DatagramPacket)}
|
||||||
*/
|
*/
|
||||||
private PushBackIceSocketWrapper(IceSocketWrapper wrappedWrapper,
|
public PushBackIceSocketWrapper(IceSocketWrapper wrappedWrapper,
|
||||||
DatagramPacket datagramPacket)
|
DatagramPacket datagramPacket)
|
||||||
{
|
{
|
||||||
this.wrapped = wrappedWrapper;
|
this.wrapped = wrappedWrapper;
|
||||||
@ -910,19 +1055,12 @@ public class MultiplexingTcpHostHarvester
|
|||||||
private class ReadThread
|
private class ReadThread
|
||||||
extends Thread
|
extends Thread
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Contains the <tt>SocketChanel</tt>s that we are currently reading
|
|
||||||
* from.
|
|
||||||
*/
|
|
||||||
private final List<ChannelDesc> channels
|
|
||||||
= new LinkedList<ChannelDesc>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>ReadThread</tt>.
|
* Initializes a new <tt>ReadThread</tt>.
|
||||||
*
|
*
|
||||||
* @throws IOException if the selector to be used fails to open.
|
* @throws IOException if the selector to be used fails to open.
|
||||||
*/
|
*/
|
||||||
private ReadThread()
|
public ReadThread()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
setName("MultiplexingTcpHostHarvester ReadThread");
|
setName("MultiplexingTcpHostHarvester ReadThread");
|
||||||
@ -942,20 +1080,16 @@ public class MultiplexingTcpHostHarvester
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
channel.configureBlocking(false);
|
channel.configureBlocking(false);
|
||||||
channel.register(readSelector, SelectionKey.OP_READ);
|
channel.register(
|
||||||
|
readSelector,
|
||||||
|
SelectionKey.OP_READ,
|
||||||
|
new ChannelDesc(channel));
|
||||||
}
|
}
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{
|
{
|
||||||
logger.info("Failed to register channel: " + ioe);
|
logger.info("Failed to register channel: " + ioe);
|
||||||
try
|
closeNoExceptions(channel);
|
||||||
{
|
|
||||||
channel.close();
|
|
||||||
}
|
}
|
||||||
catch (IOException ioe2)
|
|
||||||
{}
|
|
||||||
}
|
|
||||||
|
|
||||||
channels.add(new ChannelDesc(channel));
|
|
||||||
}
|
}
|
||||||
newChannels.clear();
|
newChannels.clear();
|
||||||
}
|
}
|
||||||
@ -969,35 +1103,35 @@ public class MultiplexingTcpHostHarvester
|
|||||||
{
|
{
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
|
|
||||||
for (Iterator<ChannelDesc> i = channels.iterator();
|
|
||||||
i.hasNext();)
|
|
||||||
{
|
|
||||||
ChannelDesc channelDesc = i.next();
|
|
||||||
|
|
||||||
if (channelDesc.lastActive != -1
|
|
||||||
&& now - channelDesc.lastActive > READ_TIMEOUT)
|
|
||||||
{
|
|
||||||
i.remove();
|
|
||||||
logger.info("Read timeout for socket: "
|
|
||||||
+ channelDesc.channel.socket());
|
|
||||||
|
|
||||||
// De-register from the selector
|
|
||||||
for (SelectionKey key : readSelector.keys())
|
for (SelectionKey key : readSelector.keys())
|
||||||
{
|
{
|
||||||
if (key.channel().equals(channelDesc.channel))
|
// An invalid key specifies that either the channel was closed
|
||||||
{
|
// (in which case we do not have to do anything else to it) or
|
||||||
key.cancel();
|
// that we no longer control the channel (i.e. we do not want to
|
||||||
}
|
// do anything else to it).
|
||||||
}
|
if (!key.isValid())
|
||||||
|
continue;
|
||||||
|
|
||||||
try
|
ChannelDesc channelDesc = (ChannelDesc) key.attachment();
|
||||||
|
|
||||||
|
if (channelDesc == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
long lastActive = channelDesc.lastActive;
|
||||||
|
|
||||||
|
if (lastActive != -1
|
||||||
|
&& now - lastActive
|
||||||
|
> MuxServerSocketChannel
|
||||||
|
.SOCKET_CHANNEL_READ_TIMEOUT)
|
||||||
{
|
{
|
||||||
channelDesc.channel.close();
|
// De-register from the Selector.
|
||||||
}
|
key.cancel();
|
||||||
catch (IOException ioe)
|
|
||||||
{
|
SocketChannel channel = channelDesc.channel;
|
||||||
logger.info("Failed to close channel: " + ioe);
|
|
||||||
}
|
logger.info("Read timeout for socket: " + channel.socket());
|
||||||
|
|
||||||
|
closeNoExceptions(channel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1029,6 +1163,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
{
|
{
|
||||||
TransportAddress transportAddress
|
TransportAddress transportAddress
|
||||||
= candidate.getTransportAddress();
|
= candidate.getTransportAddress();
|
||||||
|
|
||||||
if (candidate instanceof TcpHostCandidate
|
if (candidate instanceof TcpHostCandidate
|
||||||
&& Transport.TCP.equals(transportAddress.getTransport())
|
&& Transport.TCP.equals(transportAddress.getTransport())
|
||||||
&& localPort == transportAddress.getPort()
|
&& localPort == transportAddress.getPort()
|
||||||
@ -1040,25 +1175,6 @@ public class MultiplexingTcpHostHarvester
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds the <tt>ChannelDesc</tt> in {@link #channels} whose
|
|
||||||
* channel is <tt>channel</tt>.
|
|
||||||
*
|
|
||||||
* @param channel the channel to search for.
|
|
||||||
* @return the <tt>ChannelDesc</tt> in {@link #channels} whose
|
|
||||||
* channel is <tt>channel</tt>
|
|
||||||
*/
|
|
||||||
private ChannelDesc findChannelDesc(SelectableChannel channel)
|
|
||||||
{
|
|
||||||
for (ChannelDesc channelDesc : channels)
|
|
||||||
{
|
|
||||||
if (channelDesc.channel.equals(channel))
|
|
||||||
return channelDesc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes <tt>socket</tt> available to <tt>component</tt> and pushes back
|
* Makes <tt>socket</tt> available to <tt>component</tt> and pushes back
|
||||||
* <tt>datagramPacket</tt> into the STUN socket.
|
* <tt>datagramPacket</tt> into the STUN socket.
|
||||||
@ -1073,17 +1189,18 @@ public class MultiplexingTcpHostHarvester
|
|||||||
{
|
{
|
||||||
IceProcessingState state
|
IceProcessingState state
|
||||||
= component.getParentStream().getParentAgent().getState();
|
= component.getParentStream().getParentAgent().getState();
|
||||||
|
|
||||||
if (!IceProcessingState.WAITING.equals(state)
|
if (!IceProcessingState.WAITING.equals(state)
|
||||||
&& !IceProcessingState.RUNNING.equals(state))
|
&& !IceProcessingState.RUNNING.equals(state))
|
||||||
{
|
{
|
||||||
logger.info("Not adding a socket to an ICE agent with state "
|
logger.info(
|
||||||
|
"Not adding a socket to an ICE agent with state "
|
||||||
+ state);
|
+ state);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Socket to add to the candidate
|
// Socket to add to the candidate
|
||||||
IceSocketWrapper candidateSocket = null;
|
IceSocketWrapper candidateSocket = null;
|
||||||
|
|
||||||
// STUN-only filtered socket to add to the StunStack
|
// STUN-only filtered socket to add to the StunStack
|
||||||
IceSocketWrapper stunSocket = null;
|
IceSocketWrapper stunSocket = null;
|
||||||
|
|
||||||
@ -1124,7 +1241,6 @@ public class MultiplexingTcpHostHarvester
|
|||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{}
|
{}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1152,8 +1268,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
* <tt>channel</tt>, which is to be canceled in case no further
|
* <tt>channel</tt>, which is to be canceled in case no further
|
||||||
* reading is required from the channel.
|
* reading is required from the channel.
|
||||||
*/
|
*/
|
||||||
private void readFromChannel(ChannelDesc channel,
|
private void readFromChannel(ChannelDesc channel, SelectionKey key)
|
||||||
SelectionKey key)
|
|
||||||
{
|
{
|
||||||
if (channel.buffer == null)
|
if (channel.buffer == null)
|
||||||
{
|
{
|
||||||
@ -1178,10 +1293,12 @@ public class MultiplexingTcpHostHarvester
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (channel.channel.read(channel.buffer) == -1)
|
int read = channel.channel.read(channel.buffer);
|
||||||
{
|
|
||||||
throw new IOException("Socket closed.");
|
if (read == -1)
|
||||||
}
|
throw new IOException("End of stream!");
|
||||||
|
else if (read > 0)
|
||||||
|
channel.lastActive = System.currentTimeMillis();
|
||||||
|
|
||||||
if (!channel.buffer.hasRemaining())
|
if (!channel.buffer.hasRemaining())
|
||||||
{
|
{
|
||||||
@ -1214,11 +1331,11 @@ public class MultiplexingTcpHostHarvester
|
|||||||
throw new IOException("Expected a pseudo ssl"
|
throw new IOException("Expected a pseudo ssl"
|
||||||
+ " handshake, but received something else.");
|
+ " handshake, but received something else.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (channel.length == -1)
|
else if (channel.length == -1)
|
||||||
{
|
{
|
||||||
channel.buffer.flip();
|
channel.buffer.flip();
|
||||||
|
|
||||||
int fb = channel.buffer.get();
|
int fb = channel.buffer.get();
|
||||||
int sb = channel.buffer.get();
|
int sb = channel.buffer.get();
|
||||||
|
|
||||||
@ -1231,6 +1348,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
byte[] bytesRead = new byte[channel.length];
|
byte[] bytesRead = new byte[channel.length];
|
||||||
|
|
||||||
channel.buffer.flip();
|
channel.buffer.flip();
|
||||||
channel.buffer.get(bytesRead);
|
channel.buffer.get(bytesRead);
|
||||||
|
|
||||||
@ -1250,14 +1368,18 @@ public class MultiplexingTcpHostHarvester
|
|||||||
UsernameAttribute usernameAttribute
|
UsernameAttribute usernameAttribute
|
||||||
= (UsernameAttribute)
|
= (UsernameAttribute)
|
||||||
stunMessage.getAttribute(Attribute.USERNAME);
|
stunMessage.getAttribute(Attribute.USERNAME);
|
||||||
|
|
||||||
if (usernameAttribute == null)
|
if (usernameAttribute == null)
|
||||||
|
{
|
||||||
throw new IOException(
|
throw new IOException(
|
||||||
"No USERNAME attribute present.");
|
"No USERNAME attribute present.");
|
||||||
|
}
|
||||||
|
|
||||||
String usernameString
|
String usernameString
|
||||||
= new String(usernameAttribute.getUsername());
|
= new String(usernameAttribute.getUsername());
|
||||||
String localUfrag = usernameString.split(":")[0];
|
String localUfrag = usernameString.split(":")[0];
|
||||||
Component component = getComponent(localUfrag);
|
Component component = getComponent(localUfrag);
|
||||||
|
|
||||||
if (component == null)
|
if (component == null)
|
||||||
throw new IOException("No component found.");
|
throw new IOException("No component found.");
|
||||||
|
|
||||||
@ -1271,44 +1393,31 @@ public class MultiplexingTcpHostHarvester
|
|||||||
// Construct a DatagramPacket from the just-read packet
|
// Construct a DatagramPacket from the just-read packet
|
||||||
// which is to be pushed back
|
// which is to be pushed back
|
||||||
DatagramPacket p
|
DatagramPacket p
|
||||||
= new DatagramPacket(bytesRead,
|
= new DatagramPacket(bytesRead, bytesRead.length);
|
||||||
bytesRead.length);
|
|
||||||
Socket socket = channel.channel.socket();
|
Socket socket = channel.channel.socket();
|
||||||
|
|
||||||
p.setAddress(socket.getInetAddress());
|
p.setAddress(socket.getInetAddress());
|
||||||
p.setPort(socket.getPort());
|
p.setPort(socket.getPort());
|
||||||
|
|
||||||
handSocketToComponent(socket, component, p);
|
handSocketToComponent(socket, component, p);
|
||||||
|
|
||||||
// Successfully accepted, we don't need it anymore.
|
|
||||||
channels.remove(channel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{
|
{
|
||||||
logger.info("Failed to handle TCP socket "
|
logger.info(
|
||||||
|
"Failed to handle TCP socket "
|
||||||
+ channel.channel.socket() + ": " + ioe);
|
+ channel.channel.socket() + ": " + ioe);
|
||||||
channels.remove(channel);
|
|
||||||
key.cancel();
|
key.cancel();
|
||||||
try
|
closeNoExceptions(channel.channel);
|
||||||
{
|
|
||||||
channel.channel.close();
|
|
||||||
}
|
|
||||||
catch (IOException ioe2)
|
|
||||||
{}
|
|
||||||
}
|
}
|
||||||
catch (StunException se)
|
catch (StunException se)
|
||||||
{
|
{
|
||||||
logger.info("Failed to handle TCP socket "
|
logger.info(
|
||||||
|
"Failed to handle TCP socket "
|
||||||
+ channel.channel.socket() + ": " + se);
|
+ channel.channel.socket() + ": " + se);
|
||||||
channels.remove(channel);
|
|
||||||
key.cancel();
|
key.cancel();
|
||||||
try
|
closeNoExceptions(channel.channel);
|
||||||
{
|
|
||||||
channel.channel.close();
|
|
||||||
}
|
|
||||||
catch (IOException ioe)
|
|
||||||
{}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1318,11 +1427,7 @@ public class MultiplexingTcpHostHarvester
|
|||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Set<SelectionKey> selectedKeys;
|
do
|
||||||
int readyChannels = 0;
|
|
||||||
SelectableChannel selectedChannel;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
synchronized (MultiplexingTcpHostHarvester.this)
|
synchronized (MultiplexingTcpHostHarvester.this)
|
||||||
{
|
{
|
||||||
@ -1335,62 +1440,56 @@ public class MultiplexingTcpHostHarvester
|
|||||||
|
|
||||||
checkForNewChannels();
|
checkForNewChannels();
|
||||||
|
|
||||||
|
for (SelectionKey key : readSelector.keys())
|
||||||
|
{
|
||||||
|
if (key.isValid())
|
||||||
|
{
|
||||||
|
ChannelDesc channelDesc
|
||||||
|
= (ChannelDesc) key.attachment();
|
||||||
|
|
||||||
|
readFromChannel(channelDesc, key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We read from all SocketChannels.
|
||||||
|
readSelector.selectedKeys().clear();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
readyChannels = readSelector.select(READ_TIMEOUT / 2);
|
readSelector.select(
|
||||||
|
MuxServerSocketChannel.SOCKET_CHANNEL_READ_TIMEOUT
|
||||||
|
/ 2);
|
||||||
}
|
}
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{
|
{
|
||||||
logger.info("Failed to select a read-ready channel.");
|
logger.info("Failed to select a read-ready channel.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (readyChannels > 0)
|
|
||||||
{
|
|
||||||
selectedKeys = readSelector.selectedKeys();
|
|
||||||
for (SelectionKey key : selectedKeys)
|
|
||||||
{
|
|
||||||
if (key.isReadable())
|
|
||||||
{
|
|
||||||
selectedChannel = key.channel();
|
|
||||||
|
|
||||||
ChannelDesc channelDesc
|
|
||||||
= findChannelDesc(selectedChannel);
|
|
||||||
channelDesc.lastActive = System.currentTimeMillis();
|
|
||||||
|
|
||||||
readFromChannel(channelDesc, key);
|
|
||||||
}
|
}
|
||||||
}
|
while (true);
|
||||||
selectedKeys.clear();
|
|
||||||
}
|
|
||||||
} //while(true)
|
|
||||||
|
|
||||||
//we are all done, clean up.
|
//we are all done, clean up.
|
||||||
synchronized (newChannels)
|
synchronized (newChannels)
|
||||||
{
|
{
|
||||||
for (SocketChannel channel : newChannels)
|
for (SocketChannel channel : newChannels)
|
||||||
{
|
{
|
||||||
try
|
closeNoExceptions(channel);
|
||||||
{
|
|
||||||
channel.close();
|
|
||||||
}
|
|
||||||
catch (IOException ioe)
|
|
||||||
{}
|
|
||||||
}
|
}
|
||||||
newChannels.clear();
|
newChannels.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ChannelDesc channelDesc : channels)
|
for (SelectionKey key : readSelector.keys())
|
||||||
{
|
{
|
||||||
try
|
// An invalid key specifies that either the channel was closed
|
||||||
|
// (in which case we do not have to do anything else to it) or
|
||||||
|
// that we no longer control the channel (i.e. we do not want to
|
||||||
|
// do anything else to it).
|
||||||
|
if (key.isValid())
|
||||||
{
|
{
|
||||||
channelDesc.channel.close();
|
Channel channel = key.channel();
|
||||||
}
|
|
||||||
catch (IOException ioe)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
channels.clear();
|
if (channel.isOpen())
|
||||||
|
closeNoExceptions(channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -1399,47 +1498,5 @@ public class MultiplexingTcpHostHarvester
|
|||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{}
|
{}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Contains a <tt>SocketChannel</tt> that <tt>ReadThread</tt> is
|
|
||||||
* reading from.
|
|
||||||
*/
|
|
||||||
private class ChannelDesc
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The actual <tt>SocketChannel</tt>.
|
|
||||||
*/
|
|
||||||
private final SocketChannel channel;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The time the channel was last found to be active.
|
|
||||||
*/
|
|
||||||
private long lastActive = System.currentTimeMillis();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The buffer which stores the data so far read from the channel.
|
|
||||||
*/
|
|
||||||
ByteBuffer buffer = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether or not the initial "pseudo" SSL handshake has been read.
|
|
||||||
*/
|
|
||||||
boolean sslHandshakeRead = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The value of the RFC4571 "length" field read from the channel, or
|
|
||||||
* -1 if it hasn't (yet) been read.
|
|
||||||
*/
|
|
||||||
int length = -1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new <tt>ChannelDesc</tt> with the given channel.
|
|
||||||
* @param channel the channel.
|
|
||||||
*/
|
|
||||||
private ChannelDesc(SocketChannel channel)
|
|
||||||
{
|
|
||||||
this.channel = channel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
279
src/org/ice4j/socket/DelegatingServerSocket.java
Normal file
279
src/org/ice4j/socket/DelegatingServerSocket.java
Normal file
@ -0,0 +1,279 @@
|
|||||||
|
/*
|
||||||
|
* 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.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.channels.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements a {@code ServerSocket} which delegates (its method calls) to
|
||||||
|
* another {@code ServerSocket}. In other words, the former wraps the latter.
|
||||||
|
*
|
||||||
|
* @author Lyubomir Marinov
|
||||||
|
*/
|
||||||
|
public class DelegatingServerSocket
|
||||||
|
extends ServerSocket
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The {@link ServerSocketChannel} of this instance. If {@code null}, the
|
||||||
|
* one of the {@link #delegate} is used.
|
||||||
|
*/
|
||||||
|
protected final ServerSocketChannel channel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link ServerSocket} this instance delegates (its method calls) to.
|
||||||
|
*/
|
||||||
|
protected final ServerSocket delegate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new {@code DelegatingServerSocket} instance which is to
|
||||||
|
* delegate (its method calls) to a specific {@link ServerSocket}.
|
||||||
|
*
|
||||||
|
* @param delegate the {@code ServerSocket} the new instance is to delegate
|
||||||
|
* (it method calls) to
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public DelegatingServerSocket(ServerSocket delegate)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
this(delegate, delegate.getChannel());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new {@code DelegatingServerSocket} instance which is to
|
||||||
|
* delegate (its method calls) to a specific {@link ServerSocket} and is to
|
||||||
|
* report a specific {@link ServerSocketChannel}.
|
||||||
|
*
|
||||||
|
* @param delegate the {@code ServerSocket} the new instance is to delegate
|
||||||
|
* (it method calls) to
|
||||||
|
* @param channel the {@code ServerSocketChannel} to be reported by the new
|
||||||
|
* instance or {@code null} to report the one of {@code delegate}
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public DelegatingServerSocket(
|
||||||
|
ServerSocket delegate,
|
||||||
|
ServerSocketChannel channel)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
this.delegate = delegate;
|
||||||
|
this.channel = channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Socket accept()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.accept();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void bind(SocketAddress endpoint)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.bind(endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void bind(SocketAddress endpoint, int backlog)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.bind(endpoint, backlog);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* If {@link #channel} is not {@code null}, returns it. Otherwise, forwards
|
||||||
|
* to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ServerSocketChannel getChannel()
|
||||||
|
{
|
||||||
|
ServerSocketChannel channel = this.channel;
|
||||||
|
|
||||||
|
return (channel == null) ? delegate.getChannel() : channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public InetAddress getInetAddress()
|
||||||
|
{
|
||||||
|
return delegate.getInetAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getLocalPort()
|
||||||
|
{
|
||||||
|
return delegate.getLocalPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketAddress getLocalSocketAddress()
|
||||||
|
{
|
||||||
|
return delegate.getLocalSocketAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getReceiveBufferSize()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return delegate.getReceiveBufferSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean getReuseAddress()
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
return delegate.getReuseAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getSoTimeout()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.getSoTimeout();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isBound()
|
||||||
|
{
|
||||||
|
return delegate.isBound();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isClosed()
|
||||||
|
{
|
||||||
|
return delegate.isClosed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setPerformancePreferences(int connectionTime,
|
||||||
|
int latency,
|
||||||
|
int bandwidth)
|
||||||
|
{
|
||||||
|
delegate.setPerformancePreferences(connectionTime, latency, bandwidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setReceiveBufferSize(int size)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
delegate.setReceiveBufferSize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setReuseAddress(boolean on)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
delegate.setReuseAddress(on);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void setSoTimeout(int timeout)
|
||||||
|
throws SocketException
|
||||||
|
{
|
||||||
|
delegate.setSoTimeout(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return delegate.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
321
src/org/ice4j/socket/DelegatingServerSocketChannel.java
Normal file
321
src/org/ice4j/socket/DelegatingServerSocketChannel.java
Normal file
@ -0,0 +1,321 @@
|
|||||||
|
/*
|
||||||
|
* 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.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.channels.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import sun.nio.ch.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements a {@code ServerSocketChannel} which delegates (its method calls)
|
||||||
|
* to another {@code ServerSocketChannel}. In other words, the former wraps the
|
||||||
|
* latter.
|
||||||
|
*
|
||||||
|
* @author Lyubomir Marinov
|
||||||
|
*/
|
||||||
|
public class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
||||||
|
extends ServerSocketChannel
|
||||||
|
implements SelChImpl
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The {@link ServerSocketChannel} this instance delegates (its method
|
||||||
|
* calls) to.
|
||||||
|
*/
|
||||||
|
protected final T delegate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||||
|
* required by {@link Selector} and related functionality.
|
||||||
|
*/
|
||||||
|
protected final SelChImpl delegateAsSelChImpl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@code ServerSocket} to be reported by this instance.
|
||||||
|
*/
|
||||||
|
private ServerSocket socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Object</tt> which synchronizes the access to {@link #socket}.
|
||||||
|
*/
|
||||||
|
private final Object socketSyncRoot = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new {@code DelegatingServerSocketChannel} instance which is
|
||||||
|
* to delegate (its method calls) to a specific {@code ServerSocketChannel}.
|
||||||
|
*
|
||||||
|
* @param delegate the {@code ServerSocketChannel} the new instance is to
|
||||||
|
* delegate (its method calls) to
|
||||||
|
*/
|
||||||
|
public DelegatingServerSocketChannel(T delegate)
|
||||||
|
{
|
||||||
|
super(delegate.provider());
|
||||||
|
|
||||||
|
this.delegate = delegate;
|
||||||
|
|
||||||
|
delegateAsSelChImpl
|
||||||
|
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketChannel accept()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
SocketChannel channel = delegate.accept();
|
||||||
|
|
||||||
|
return (channel == null) ? null : implAccept(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate} and returns {@code this}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ServerSocketChannel bind(SocketAddress local, int backlog)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.bind(local, backlog);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public FileDescriptor getFD()
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.getFD();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getFDVal()
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.getFDVal();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketAddress getLocalAddress()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.getLocalAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <U> U getOption(SocketOption<U> name)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.getOption(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows extenders to optionally configure (e.g. wrap) a
|
||||||
|
* <tt>SocketChannel</tt> which has been accepted by {@link #delegate} and
|
||||||
|
* before it is returned by {@link #accept()}.
|
||||||
|
*
|
||||||
|
* @param accepted the <tt>SocketChannel</tt> accepted by <tt>delegate</tt>
|
||||||
|
* @return the <tt>SocketChannel</tt> to be returned by {@link #accept()}
|
||||||
|
* (in place of <tt>accepted</tt>)
|
||||||
|
* @throws IOException if an I/O error occurs
|
||||||
|
*/
|
||||||
|
protected SocketChannel implAccept(SocketChannel accepted)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return accepted;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void implCloseSelectableChannel()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void implConfigureBlocking(boolean block)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.configureBlocking(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows extenders to optionally configure (e.g. wrap) the
|
||||||
|
* {@code ServerSocket} of {@link #delegate} and before it is returned by
|
||||||
|
* {@link #socket()}.
|
||||||
|
*
|
||||||
|
* @param socket the {@code ServerSocket} of {@code delegate}
|
||||||
|
* @return the {@code ServerSocket} to be returned by {@link #socket()} (in
|
||||||
|
* place of {@code socket})
|
||||||
|
* @throws IOException if an I/O error occurs
|
||||||
|
*/
|
||||||
|
protected ServerSocket implSocket(ServerSocket socket)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return new DelegatingServerSocket(socket, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether this {@code DelegatingServerSocketChannel} is bound.
|
||||||
|
*
|
||||||
|
* @return {@code true} if this instancei bound; otherwise, {@code false}
|
||||||
|
*/
|
||||||
|
public boolean isBound()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return getLocalAddress() != null;
|
||||||
|
}
|
||||||
|
catch (IOException ioe)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void kill()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegateAsSelChImpl.kill();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate} and returns {@code this}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <U> ServerSocketChannel setOption(SocketOption<U> name, U value)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.setOption(name, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Allows wrapping the {@code socket} of {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ServerSocket socket()
|
||||||
|
{
|
||||||
|
ServerSocket socket = delegate.socket();
|
||||||
|
|
||||||
|
synchronized (socketSyncRoot)
|
||||||
|
{
|
||||||
|
if (this.socket == null)
|
||||||
|
{
|
||||||
|
if (socket != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.socket = implSocket(socket);
|
||||||
|
}
|
||||||
|
catch (IOException ioe)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(ioe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (socket == null)
|
||||||
|
{
|
||||||
|
this.socket = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// TODO For the sake of completeness, maybe check that the
|
||||||
|
// value of this.socket still delegates to the latest value of
|
||||||
|
// socket. However, the value of socket is very likely final so
|
||||||
|
// do not bother with it at the time of this writing.
|
||||||
|
}
|
||||||
|
return this.socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Set<SocketOption<?>> supportedOptions()
|
||||||
|
{
|
||||||
|
return delegate.supportedOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||||
|
{
|
||||||
|
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -208,10 +208,25 @@ public class DelegatingSocket
|
|||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
|
|
||||||
delegateAsDelegatingSocket
|
if (delegate instanceof DelegatingSocket)
|
||||||
= (delegate instanceof DelegatingSocket)
|
{
|
||||||
? (DelegatingSocket) delegate
|
// FIXME DelegatingSocketChannel was in need of a Socket which wraps
|
||||||
: null;
|
// another Socket and reports a specific SocketChannel. Since
|
||||||
|
// DelegatingSocket does that, DelegatingSocketChannel used it.
|
||||||
|
// Unfortunately, it turned out after much debugging that
|
||||||
|
// DelegatingSocket has the additional function of flagging where
|
||||||
|
// UDP packets sent over TCP are to be framed. As a temporary fix,
|
||||||
|
// do not treat the sockets of DelegatingSocketChannel as
|
||||||
|
// DelegatingSockets.
|
||||||
|
if (channel instanceof DelegatingSocketChannel)
|
||||||
|
delegateAsDelegatingSocket = null;
|
||||||
|
else
|
||||||
|
delegateAsDelegatingSocket = (DelegatingSocket) delegate;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
delegateAsDelegatingSocket = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -708,11 +723,11 @@ public class DelegatingSocket
|
|||||||
// Else, sends the packet to the final socket (outputStream).
|
// Else, sends the packet to the final socket (outputStream).
|
||||||
outputStream.write(p.getData(), p.getOffset(), p.getLength());
|
outputStream.write(p.getData(), p.getOffset(), p.getLength());
|
||||||
|
|
||||||
|
if (DelegatingDatagramSocket.logNonStun(++nbSentPackets))
|
||||||
|
{
|
||||||
InetSocketAddress localAddress
|
InetSocketAddress localAddress
|
||||||
= (InetSocketAddress) super.getLocalSocketAddress();
|
= (InetSocketAddress) super.getLocalSocketAddress();
|
||||||
|
|
||||||
if (DelegatingDatagramSocket.logNonStun(++nbSentPackets))
|
|
||||||
{
|
|
||||||
StunStack.logPacketToPcap(
|
StunStack.logPacketToPcap(
|
||||||
p,
|
p,
|
||||||
true,
|
true,
|
||||||
|
|||||||
404
src/org/ice4j/socket/DelegatingSocketChannel.java
Normal file
404
src/org/ice4j/socket/DelegatingSocketChannel.java
Normal file
@ -0,0 +1,404 @@
|
|||||||
|
/*
|
||||||
|
* 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.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.*;
|
||||||
|
import java.nio.channels.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import sun.nio.ch.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements a {@code SocketChannel} which delegates (its method calls) to
|
||||||
|
* another {@code SocketChannel}. In other words, the former wraps the latter.
|
||||||
|
*
|
||||||
|
* @author Lyubomir Marinov
|
||||||
|
*/
|
||||||
|
public class DelegatingSocketChannel<T extends SocketChannel>
|
||||||
|
extends SocketChannel
|
||||||
|
implements SelChImpl
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The {@link SocketChannel} this instance delegates (its method calls) to.
|
||||||
|
*/
|
||||||
|
protected final T delegate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
||||||
|
* required by {@link Selector} and related functionality.
|
||||||
|
*/
|
||||||
|
protected final SelChImpl delegateAsSelChImpl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@code Socket} to be reported by this instance.
|
||||||
|
*/
|
||||||
|
private Socket socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The <tt>Object</tt> which synchronizes the access to {@link #socket}.
|
||||||
|
*/
|
||||||
|
private final Object socketSyncRoot = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new {@code DelegatingSocketChannel} instance which is to
|
||||||
|
* delegate (its method calls) to a specific {@code SocketChannel}.
|
||||||
|
*
|
||||||
|
* @param delegate the {@code SocketChannel} the new instance is to delegate
|
||||||
|
* (its method calls) to
|
||||||
|
*/
|
||||||
|
public DelegatingSocketChannel(T delegate)
|
||||||
|
{
|
||||||
|
super(delegate.provider());
|
||||||
|
|
||||||
|
this.delegate = delegate;
|
||||||
|
|
||||||
|
delegateAsSelChImpl
|
||||||
|
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate} and returns {@code this}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketChannel bind(SocketAddress local)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.bind(local);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean connect(SocketAddress remote)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.connect(remote);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean finishConnect()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.finishConnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public FileDescriptor getFD()
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.getFD();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getFDVal()
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.getFDVal();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketAddress getLocalAddress()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.getLocalAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <U> U getOption(SocketOption<U> name)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.getOption(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketAddress getRemoteAddress()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.getRemoteAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void implCloseSelectableChannel()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void implConfigureBlocking(boolean block)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.configureBlocking(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows extenders to optionally configure (e.g. wrap) the {@code Socket}
|
||||||
|
* of {@link #delegate} and before it is returned by {@link #socket()}.
|
||||||
|
*
|
||||||
|
* @param socket the {@code Socket} of {@code delegate}
|
||||||
|
* @return the {@code Socket} to be returned by {@link #socket()} (in place
|
||||||
|
* of {@code socket})
|
||||||
|
* @throws IOException if an I/O error occurs
|
||||||
|
*/
|
||||||
|
protected Socket implSocket(Socket socket)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return new DelegatingSocket(socket, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isConnected()
|
||||||
|
{
|
||||||
|
return delegate.isConnected();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isConnectionPending()
|
||||||
|
{
|
||||||
|
return delegate.isConnectionPending();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void kill()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegateAsSelChImpl.kill();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int read(ByteBuffer dst)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.read(dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public long read(ByteBuffer[] dsts, int offset, int length)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.read(dsts, offset, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate} and returns {@code this}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <U> SocketChannel setOption(SocketOption<U> name, U value)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.setOption(name, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate} and returns {@code this}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketChannel shutdownInput()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.shutdownInput();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate} and returns {@code this}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SocketChannel shutdownOutput()
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
delegate.shutdownOutput();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Allows wrapping the {@code socket} of {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Socket socket()
|
||||||
|
{
|
||||||
|
Socket socket = delegate.socket();
|
||||||
|
|
||||||
|
synchronized (socketSyncRoot)
|
||||||
|
{
|
||||||
|
if (this.socket == null)
|
||||||
|
{
|
||||||
|
if (socket != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.socket = implSocket(socket);
|
||||||
|
}
|
||||||
|
catch (IOException ioe)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(ioe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (socket == null)
|
||||||
|
{
|
||||||
|
this.socket = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// TODO For the sake of completeness, maybe check that the
|
||||||
|
// value of this.socket still delegates to the latest value of
|
||||||
|
// socket. However, the value of socket is very likely final so
|
||||||
|
// do not bother with it at the time of this writing.
|
||||||
|
}
|
||||||
|
return this.socket;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Set<SocketOption<?>> supportedOptions()
|
||||||
|
{
|
||||||
|
return delegate.supportedOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
||||||
|
{
|
||||||
|
delegateAsSelChImpl.translateAndSetInterestOps(ops, sk);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk)
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.translateAndSetReadyOps(ops, sk);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk)
|
||||||
|
{
|
||||||
|
return delegateAsSelChImpl.translateAndUpdateReadyOps(ops, sk);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int write(ByteBuffer src)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.write(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Forwards to {@link #delegate}.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public long write(ByteBuffer[] srcs, int offset, int length)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
return delegate.write(srcs, offset, length);
|
||||||
|
}
|
||||||
|
}
|
||||||
267
src/org/ice4j/socket/HttpDemuxFilter.java
Normal file
267
src/org/ice4j/socket/HttpDemuxFilter.java
Normal file
@ -0,0 +1,267 @@
|
|||||||
|
/*
|
||||||
|
* Jitsi Videobridge, OpenSource video conferencing.
|
||||||
|
*
|
||||||
|
* Distributable under LGPL license.
|
||||||
|
* See terms of license at gnu.org.
|
||||||
|
*/
|
||||||
|
package org.ice4j.socket;
|
||||||
|
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.charset.*;
|
||||||
|
|
||||||
|
import org.ice4j.ice.harvest.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements a {@link DatagramPacketFilter} which allows demultiplexing HTTP(S)
|
||||||
|
* out of {@link MuxServerSocketChannel}. Accepts HTTP, SSL v2, and TLS. Rejects
|
||||||
|
* Google TURN SSLTCP.
|
||||||
|
*
|
||||||
|
* @author Lyubomir Marinov
|
||||||
|
*/
|
||||||
|
public class HttpDemuxFilter
|
||||||
|
implements DatagramPacketFilter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The US-ASCII {@code byte}s of {@link #REQUEST_METHOD_STRINGS}. Explicitly
|
||||||
|
* defined for the purposes of performance.
|
||||||
|
*/
|
||||||
|
private static byte[][] REQUEST_METHOD_BYTES;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum US-ASCII character with which an element of
|
||||||
|
* {@link #REQUEST_METHOD_STRINGS} starts.
|
||||||
|
*/
|
||||||
|
private static final char REQUEST_METHOD_MAX_CHAR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum length in {@code byte}s of a US-ASCII representation of an
|
||||||
|
* HTTP request method supported by the class {@code HttpDemuxFilter}.
|
||||||
|
*/
|
||||||
|
public static final int REQUEST_METHOD_MAX_LENGTH;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum US-ASCII character with which an element of
|
||||||
|
* {@link #REQUEST_METHOD_STRINGS} starts.
|
||||||
|
*/
|
||||||
|
private static final char REQUEST_METHOD_MIN_CHAR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The HTTP request methods recognized by the class {@code HttpDemuxFilter}.
|
||||||
|
*/
|
||||||
|
private static String[] REQUEST_METHOD_STRINGS
|
||||||
|
= {
|
||||||
|
"CONNECT",
|
||||||
|
"DELETE",
|
||||||
|
"GET",
|
||||||
|
"HEAD",
|
||||||
|
"MOVE",
|
||||||
|
"OPTIONS",
|
||||||
|
"POST",
|
||||||
|
"PRI",
|
||||||
|
"PROXY",
|
||||||
|
"PUT",
|
||||||
|
"TRACE"
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum number of bytes required by the class {@code HttpDemuxFilter}
|
||||||
|
* in order to declare that (the data of) a specific {@link DatagramPacket}
|
||||||
|
* represents TLS.
|
||||||
|
*/
|
||||||
|
public static final int TLS_MIN_LENGTH = 11;
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
// Gather statistics about the supported HTTP request methods in order
|
||||||
|
// to speed up the analysis of DatagramPackets later on.
|
||||||
|
char maxChar = 'A';
|
||||||
|
int maxLength = Integer.MAX_VALUE;
|
||||||
|
char minChar = 'Z';
|
||||||
|
Charset ascii = Charset.forName("US-ASCII");
|
||||||
|
|
||||||
|
REQUEST_METHOD_BYTES = new byte[REQUEST_METHOD_STRINGS.length][];
|
||||||
|
for (int i = 0; i < REQUEST_METHOD_STRINGS.length; i++)
|
||||||
|
{
|
||||||
|
String s = REQUEST_METHOD_STRINGS[i];
|
||||||
|
|
||||||
|
if (s != null && s.length() != 0)
|
||||||
|
{
|
||||||
|
char ch = s.charAt(0);
|
||||||
|
byte[] bytes = s.getBytes(ascii);
|
||||||
|
int length = bytes.length;
|
||||||
|
|
||||||
|
if (maxChar < ch)
|
||||||
|
maxChar = ch;
|
||||||
|
if (maxLength < length)
|
||||||
|
maxLength = length;
|
||||||
|
if (minChar > ch)
|
||||||
|
minChar = ch;
|
||||||
|
|
||||||
|
REQUEST_METHOD_BYTES[i] = bytes;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
REQUEST_METHOD_BYTES[i] = new byte[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
REQUEST_METHOD_MAX_CHAR = maxChar;
|
||||||
|
REQUEST_METHOD_MAX_LENGTH = maxLength;
|
||||||
|
REQUEST_METHOD_MIN_CHAR = minChar;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether a specific {@link DatagramPacket} looks like the
|
||||||
|
* beginning of HTTP(S) client communication. Accepts HTTP, SSL v2, and
|
||||||
|
* TLS. Rejects Google TURN SSLTCP.
|
||||||
|
*
|
||||||
|
* @param p the {@code DatagramPacket} to analyze
|
||||||
|
* @return {@code true} if {@code p} looks like the beginning of HTTP(S)
|
||||||
|
* client communication; otherwise, {@code false}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean accept(DatagramPacket p)
|
||||||
|
{
|
||||||
|
int len = p.getLength();
|
||||||
|
boolean accept = false;
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
byte[] buf = p.getData();
|
||||||
|
int off = p.getOffset();
|
||||||
|
// The first bytes of HTTP, SSL v2, and TCP are different so quickly
|
||||||
|
// determine which one of the three is possible and, respectively,
|
||||||
|
// which two of the three are impossible.
|
||||||
|
int b0 = 0xFF & buf[off];
|
||||||
|
boolean http, sslv2, tls;
|
||||||
|
|
||||||
|
if (b0 == 22 /* TLS handshake */)
|
||||||
|
{
|
||||||
|
http = false;
|
||||||
|
sslv2 = false;
|
||||||
|
tls = true;
|
||||||
|
}
|
||||||
|
else if (b0 > 0x80 /* SSL v2 client hello */)
|
||||||
|
{
|
||||||
|
http = false;
|
||||||
|
sslv2 = true;
|
||||||
|
tls = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
http = true;
|
||||||
|
sslv2 = false;
|
||||||
|
tls = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTP
|
||||||
|
if (http)
|
||||||
|
{
|
||||||
|
// Request-Line = Method SP Request-URI SP HTTP-Version CRLF
|
||||||
|
// HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
|
||||||
|
if (b0 >= REQUEST_METHOD_MIN_CHAR
|
||||||
|
&& b0 <= REQUEST_METHOD_MAX_CHAR
|
||||||
|
&& len >= REQUEST_METHOD_MAX_LENGTH + 1 /* SP */)
|
||||||
|
{
|
||||||
|
// Match a supported HTTP request method.
|
||||||
|
for (byte[] bytes : REQUEST_METHOD_BYTES)
|
||||||
|
{
|
||||||
|
int length = bytes.length;
|
||||||
|
|
||||||
|
if (buf[off + length] == ' ' /* SP */)
|
||||||
|
{
|
||||||
|
boolean equals = true;
|
||||||
|
|
||||||
|
for (int i = 1, j = off + 1; i < length; i++, j++)
|
||||||
|
{
|
||||||
|
if (bytes[i] != buf[j])
|
||||||
|
{
|
||||||
|
equals = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (equals)
|
||||||
|
{
|
||||||
|
accept = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Only one of HTTP, SSL v2, and TCP was deemed possible by
|
||||||
|
// looking at the first byte. If p didn't look like HTTP here,
|
||||||
|
// then it will not look like SSL v2 or TLS.
|
||||||
|
return accept;
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPS
|
||||||
|
if (tls)
|
||||||
|
{
|
||||||
|
// 1 byte ContentType type = handshake(22)
|
||||||
|
// 2 bytes ProtocolVersion version = { major(3) , minor<1..3> }
|
||||||
|
// 2 bytes uint16 length
|
||||||
|
// 1 byte HandshakeType msg_type = client_hello(1)
|
||||||
|
// 3 bytes uint24 length
|
||||||
|
// 2 bytes ProtocolVersion client_version
|
||||||
|
if (len >= TLS_MIN_LENGTH
|
||||||
|
&& /* major */ (0xFF & buf[off + 1]) == 3)
|
||||||
|
{
|
||||||
|
int minor = 0xFF & buf[off + 2];
|
||||||
|
|
||||||
|
if (1 <= minor
|
||||||
|
&& minor <= 3
|
||||||
|
&& /* msg_type */ (0xFF & buf[off + 5])
|
||||||
|
== /* client_hello */ 1
|
||||||
|
&& /* major */ (0xFF & buf[off + 9]) == 3)
|
||||||
|
{
|
||||||
|
minor = 0xFF & buf[off + 10];
|
||||||
|
if (1 <= minor && minor <= 3)
|
||||||
|
accept = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Only one of HTTP, SSL v2, and TCP was deemed possible by
|
||||||
|
// looking at the first byte. If p didn't look like TLS here,
|
||||||
|
// then it will not look like HTTP or SSL v2.
|
||||||
|
return accept;
|
||||||
|
}
|
||||||
|
if (sslv2)
|
||||||
|
{
|
||||||
|
final byte[] googleTurnSslTcp
|
||||||
|
= GoogleTurnSSLCandidateHarvester.SSL_CLIENT_HANDSHAKE;
|
||||||
|
|
||||||
|
// 2 bytes uint15 length
|
||||||
|
// 1 byte uint8 msg_type = 1
|
||||||
|
// 2 bytes Version version
|
||||||
|
if (len > 5
|
||||||
|
&& len >= googleTurnSslTcp.length
|
||||||
|
&& /* msg_type */ (0xFF & buf[off + 2]) == 1
|
||||||
|
&& /* major */ (0xFF & buf[off + 3]) == 3)
|
||||||
|
{
|
||||||
|
int minor = 0xFF & buf[off + 4];
|
||||||
|
|
||||||
|
if (1 <= minor && minor <= 3)
|
||||||
|
{
|
||||||
|
// Reject Google TURN SSLTCP.
|
||||||
|
boolean equals = true;
|
||||||
|
|
||||||
|
for (int i = 0, iEnd = googleTurnSslTcp.length, j = off;
|
||||||
|
i < iEnd;
|
||||||
|
i++, j++)
|
||||||
|
{
|
||||||
|
if (googleTurnSslTcp[i] != buf[j])
|
||||||
|
{
|
||||||
|
equals = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
accept = !equals;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Only one of HTTP, SSL v2, and TCP was deemed possible by
|
||||||
|
// looking at the first byte. If p didn't look like SSL v2 here,
|
||||||
|
// then it will not look like HTTP or TLS.
|
||||||
|
return accept;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return accept;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -50,7 +50,7 @@ public class IceTcpSocketWrapper
|
|||||||
{
|
{
|
||||||
socket = delegate;
|
socket = delegate;
|
||||||
|
|
||||||
if(delegate instanceof DelegatingSocket)
|
if (delegate instanceof DelegatingSocket)
|
||||||
{
|
{
|
||||||
inputStream = null;
|
inputStream = null;
|
||||||
outputStream = null;
|
outputStream = null;
|
||||||
|
|||||||
1323
src/org/ice4j/socket/MuxServerSocketChannel.java
Normal file
1323
src/org/ice4j/socket/MuxServerSocketChannel.java
Normal file
File diff suppressed because it is too large
Load Diff
97
src/org/ice4j/socket/PreReadSocketChannel.java
Normal file
97
src/org/ice4j/socket/PreReadSocketChannel.java
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* 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.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.*;
|
||||||
|
import java.nio.channels.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepends a {@link DatagramPacket} to a {@link SocketChannel}. In other words,
|
||||||
|
* enables looking ahead at the {@code byte}s of a {@code SocketChannel}.
|
||||||
|
*
|
||||||
|
* @author Lyubomir Marinov
|
||||||
|
*/
|
||||||
|
public class PreReadSocketChannel
|
||||||
|
extends DelegatingSocketChannel<SocketChannel>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The {@link DatagramPacket} which is to be prepended to the
|
||||||
|
* {@link SocketChannel} which is the {@link #delegate} of this
|
||||||
|
* {@link DelegatingSocketChannel}.
|
||||||
|
*/
|
||||||
|
private final DatagramPacket preRead;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new {@code PreReadSocketChannel} instance which is to
|
||||||
|
* prepend a specific {@link DatagrapPacket} to a specific
|
||||||
|
* {@link SocketChannel}.
|
||||||
|
*
|
||||||
|
* @param preRead the {@code DatagramPacket} to prepend to {@code delegate}
|
||||||
|
* @param delegate the {@code SocketChannel} to prepend {@code preRead} to
|
||||||
|
*/
|
||||||
|
public PreReadSocketChannel(DatagramPacket preRead, SocketChannel delegate)
|
||||||
|
{
|
||||||
|
super(delegate);
|
||||||
|
|
||||||
|
this.preRead = preRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* Reads from {@link #preRead} first (until it is fully read) and then
|
||||||
|
* continues with the {@code super} implementation.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int read(ByteBuffer dst)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
synchronized (preRead)
|
||||||
|
{
|
||||||
|
int len = preRead.getLength();
|
||||||
|
int read;
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
int toRead = Math.min(len, dst.remaining());
|
||||||
|
|
||||||
|
if (toRead > 0)
|
||||||
|
{
|
||||||
|
byte[] buf = preRead.getData();
|
||||||
|
int off = preRead.getOffset();
|
||||||
|
|
||||||
|
dst.put(buf, off, toRead);
|
||||||
|
read = toRead;
|
||||||
|
preRead.setData(buf, off + read, len - read);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
read = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
read = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (read == 0)
|
||||||
|
{
|
||||||
|
read = super.read(dst);
|
||||||
|
}
|
||||||
|
else if (dst.hasRemaining())
|
||||||
|
{
|
||||||
|
int superRead = super.read(dst);
|
||||||
|
|
||||||
|
if (superRead > 0)
|
||||||
|
read += superRead;
|
||||||
|
}
|
||||||
|
return read;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user