Merge pull request #240 from jitsi/remove-private-jdk-api
Remove private jdk api
This commit is contained in:
commit
15981cf5f3
26
.github/workflows/maven.yml
vendored
26
.github/workflows/maven.yml
vendored
@ -19,7 +19,7 @@ jobs:
|
|||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
java: [ 8, 11 ]
|
java: [ 8, 11, 17 ]
|
||||||
|
|
||||||
name: Java ${{ matrix.java }}
|
name: Java ${{ matrix.java }}
|
||||||
|
|
||||||
@ -28,17 +28,11 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Set up JDK ${{ matrix.java }}
|
- name: Set up JDK ${{ matrix.java }}
|
||||||
uses: actions/setup-java@v1
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
|
distribution: temurin
|
||||||
java-version: ${{ matrix.java }}
|
java-version: ${{ matrix.java }}
|
||||||
|
cache: maven
|
||||||
- name: Cache local Maven repository
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ~/.m2/repository
|
|
||||||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-maven-
|
|
||||||
|
|
||||||
- name: Build and test with Maven
|
- name: Build and test with Maven
|
||||||
run: mvn -B -Dgpg.skip -Drelease=true -Pcoverage verify
|
run: mvn -B -Dgpg.skip -Drelease=true -Pcoverage verify
|
||||||
@ -58,21 +52,15 @@ jobs:
|
|||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Set up JDK
|
- name: Set up JDK
|
||||||
uses: actions/setup-java@v1
|
uses: actions/setup-java@v2
|
||||||
with:
|
with:
|
||||||
|
distribution: temurin
|
||||||
java-version: ${{ env.RELEASE_JAVA_VERSION }}
|
java-version: ${{ env.RELEASE_JAVA_VERSION }}
|
||||||
|
cache: maven
|
||||||
server-id: ossrh
|
server-id: ossrh
|
||||||
server-username: SONATYPE_USER
|
server-username: SONATYPE_USER
|
||||||
server-password: SONATYPE_PW
|
server-password: SONATYPE_PW
|
||||||
|
|
||||||
- name: Cache local Maven repository
|
|
||||||
uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: ~/.m2/repository
|
|
||||||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-maven-
|
|
||||||
|
|
||||||
- name: Set version
|
- name: Set version
|
||||||
run: |
|
run: |
|
||||||
VERSION=`git describe --match "v[0-9\.]*" --long --dirty --always`
|
VERSION=`git describe --match "v[0-9\.]*" --long --dirty --always`
|
||||||
|
|||||||
22
pom.xml
22
pom.xml
@ -38,11 +38,10 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<jicoco.version>1.1-90-gfa0e11e</jicoco.version>
|
<jicoco.version>1.1-95-gd8fd866</jicoco.version>
|
||||||
<kotlin.version>1.5.20</kotlin.version>
|
<kotlin.version>1.5.31</kotlin.version>
|
||||||
<kotest.version>4.6.1</kotest.version>
|
<kotest.version>4.6.3</kotest.version>
|
||||||
<junit.version>5.7.0</junit.version>
|
<junit.version>5.8.1</junit.version>
|
||||||
<dokka.version>1.4.32</dokka.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -59,7 +58,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.groupId}</groupId>
|
<groupId>${project.groupId}</groupId>
|
||||||
<artifactId>jitsi-utils</artifactId>
|
<artifactId>jitsi-utils</artifactId>
|
||||||
<version>1.0-94-ge7c7a43</version>
|
<version>1.0-107-g566f8f8</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.groupId}</groupId>
|
<groupId>${project.groupId}</groupId>
|
||||||
@ -71,7 +70,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.platform</groupId>
|
<groupId>org.junit.platform</groupId>
|
||||||
<artifactId>junit-platform-launcher</artifactId>
|
<artifactId>junit-platform-launcher</artifactId>
|
||||||
<version>1.7.0</version>
|
<version>1.8.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -107,7 +106,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
<version>3.6.28</version>
|
<version>4.0.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@ -156,7 +155,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.0</version>
|
<version>3.8.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<!-- Replacing default-compile as it is treated specially by maven -->
|
<!-- Replacing default-compile as it is treated specially by maven -->
|
||||||
<execution>
|
<execution>
|
||||||
@ -255,6 +254,9 @@
|
|||||||
<version>2.22.2</version>
|
<version>2.22.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<includes>**/*</includes>
|
<includes>**/*</includes>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<java.util.logging.config.file>${project.build.testSourceDirectory}/../resources/logging.properties</java.util.logging.config.file>
|
||||||
|
</systemPropertyVariables>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -478,7 +480,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.jetbrains.dokka</groupId>
|
<groupId>org.jetbrains.dokka</groupId>
|
||||||
<artifactId>dokka-maven-plugin</artifactId>
|
<artifactId>dokka-maven-plugin</artifactId>
|
||||||
<version>${dokka.version}</version>
|
<version>${kotlin.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
|
|||||||
@ -19,7 +19,6 @@ package org.ice4j.ice.harvest;
|
|||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
import org.ice4j.ice.*;
|
import org.ice4j.ice.*;
|
||||||
import org.ice4j.message.*;
|
|
||||||
import org.ice4j.socket.*;
|
import org.ice4j.socket.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -61,7 +60,15 @@ public abstract class AbstractTcpListener
|
|||||||
*/
|
*/
|
||||||
static void closeNoExceptions(Channel channel)
|
static void closeNoExceptions(Channel channel)
|
||||||
{
|
{
|
||||||
MuxServerSocketChannelFactory.closeNoExceptions(channel);
|
try
|
||||||
|
{
|
||||||
|
channel.close();
|
||||||
|
}
|
||||||
|
catch (IOException ioe)
|
||||||
|
{
|
||||||
|
// The whole idea of the method is to close a specific Channel
|
||||||
|
// without caring about any possible IOException.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,7 +82,6 @@ public abstract class AbstractTcpListener
|
|||||||
private static List<TransportAddress> getLocalAddresses(
|
private static List<TransportAddress> getLocalAddresses(
|
||||||
int port,
|
int port,
|
||||||
List<NetworkInterface> interfaces)
|
List<NetworkInterface> interfaces)
|
||||||
throws IOException
|
|
||||||
{
|
{
|
||||||
List<TransportAddress> addresses = new LinkedList<>();
|
List<TransportAddress> addresses = new LinkedList<>();
|
||||||
|
|
||||||
@ -101,83 +107,6 @@ public abstract class AbstractTcpListener
|
|||||||
return addresses;
|
return addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines whether a specific {@link DatagramPacket} is the first
|
|
||||||
* expected (i.e. supported) to be received from an accepted
|
|
||||||
* {@link SocketChannel} by this {@link AbstractTcpListener}. This is true
|
|
||||||
* if it is contains the hard-coded SSL client handshake (
|
|
||||||
* {@link GoogleTurnSSLCandidateHarvester#SSL_CLIENT_HANDSHAKE}), or
|
|
||||||
* a STUN Binding Request.
|
|
||||||
*
|
|
||||||
* @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 TcpHarvester};
|
|
||||||
* otherwise, {@code false}
|
|
||||||
*/
|
|
||||||
private static boolean isFirstDatagramPacket(DatagramPacket p)
|
|
||||||
{
|
|
||||||
int len = p.getLength();
|
|
||||||
boolean b = false;
|
|
||||||
|
|
||||||
if (len > 0)
|
|
||||||
{
|
|
||||||
byte[] buf = p.getData();
|
|
||||||
int off = p.getOffset();
|
|
||||||
|
|
||||||
// Check for 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// nothing found, lets check for stun binding requests
|
|
||||||
if (!b)
|
|
||||||
{
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The thread which <tt>accept</tt>s TCP connections from the sockets in
|
* The thread which <tt>accept</tt>s TCP connections from the sockets in
|
||||||
* {@link #serverSocketChannels}.
|
* {@link #serverSocketChannels}.
|
||||||
@ -218,12 +147,6 @@ public abstract class AbstractTcpListener
|
|||||||
private final List<ServerSocketChannel> serverSocketChannels
|
private final List<ServerSocketChannel> serverSocketChannels
|
||||||
= new LinkedList<>();
|
= new LinkedList<>();
|
||||||
|
|
||||||
/**
|
|
||||||
* The object used to synchronize access to the collection of sessions that
|
|
||||||
* the implementation of this class uses.
|
|
||||||
*/
|
|
||||||
protected final Object sessionsSyncRoot = new Object();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>TcpHarvester</tt>, which is to
|
* Initializes a new <tt>TcpHarvester</tt>, which is to
|
||||||
* listen on port number <tt>port</tt> on all IP addresses on all available
|
* listen on port number <tt>port</tt> on all IP addresses on all available
|
||||||
@ -432,21 +355,7 @@ public abstract class AbstractTcpListener
|
|||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
ServerSocketChannel channel = MuxServerSocketChannelFactory
|
ServerSocketChannel channel = MuxServerSocketChannelFactory
|
||||||
.openAndBindMuxServerSocketChannel(
|
.openAndBindServerSocketChannel(null, address, 0);
|
||||||
/* properties */ null,
|
|
||||||
address,
|
|
||||||
/* backlog */ 0,
|
|
||||||
new DatagramPacketFilter()
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean accept(DatagramPacket p)
|
|
||||||
{
|
|
||||||
return isFirstDatagramPacket(p);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
serverSocketChannels.add(channel);
|
serverSocketChannels.add(channel);
|
||||||
}
|
}
|
||||||
@ -457,8 +366,6 @@ public abstract class AbstractTcpListener
|
|||||||
* @param ufrag the local username fragment for the session.
|
* @param ufrag the local username fragment for the session.
|
||||||
* @param pushback the first "datagram" (RFC4571-framed), already read from
|
* @param pushback the first "datagram" (RFC4571-framed), already read from
|
||||||
* the socket's stream.
|
* the socket's stream.
|
||||||
* @throws IllegalStateException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
protected abstract void acceptSession(
|
protected abstract void acceptSession(
|
||||||
Socket socket, String ufrag, DatagramPacket pushback)
|
Socket socket, String ufrag, DatagramPacket pushback)
|
||||||
@ -598,7 +505,9 @@ public abstract class AbstractTcpListener
|
|||||||
selector.close();
|
selector.close();
|
||||||
}
|
}
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{}
|
{
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -613,11 +522,6 @@ public abstract class AbstractTcpListener
|
|||||||
*/
|
*/
|
||||||
public final SocketChannel channel;
|
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.
|
* The buffer which stores the data so far read from the channel.
|
||||||
*/
|
*/
|
||||||
@ -783,11 +687,8 @@ public abstract class AbstractTcpListener
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>ReadThread</tt>.
|
* Initializes a new <tt>ReadThread</tt>.
|
||||||
*
|
|
||||||
* @throws IOException if the selector to be used fails to open.
|
|
||||||
*/
|
*/
|
||||||
public ReadThread()
|
public ReadThread()
|
||||||
throws IOException
|
|
||||||
{
|
{
|
||||||
setName("TcpHarvester ReadThread");
|
setName("TcpHarvester ReadThread");
|
||||||
setDaemon(true);
|
setDaemon(true);
|
||||||
@ -821,50 +722,6 @@ public abstract class AbstractTcpListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes any inactive channels registered with {@link #readSelector}.
|
|
||||||
* A channel is considered inactive if it hasn't been available for
|
|
||||||
* reading for
|
|
||||||
* {@link MuxServerSocketChannelFactory#SOCKET_CHANNEL_READ_TIMEOUT}
|
|
||||||
* milliseconds.
|
|
||||||
*/
|
|
||||||
private void cleanup()
|
|
||||||
{
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
|
|
||||||
for (SelectionKey key : readSelector.keys())
|
|
||||||
{
|
|
||||||
// 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())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
ChannelDesc channelDesc = (ChannelDesc) key.attachment();
|
|
||||||
|
|
||||||
if (channelDesc == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
long lastActive = channelDesc.lastActive;
|
|
||||||
|
|
||||||
if (lastActive != -1
|
|
||||||
&& now - lastActive
|
|
||||||
> MuxServerSocketChannelFactory
|
|
||||||
.SOCKET_CHANNEL_READ_TIMEOUT)
|
|
||||||
{
|
|
||||||
// De-register from the Selector.
|
|
||||||
key.cancel();
|
|
||||||
|
|
||||||
SocketChannel channel = channelDesc.channel;
|
|
||||||
|
|
||||||
logger.info("Read timeout for socket: " + channel.socket());
|
|
||||||
|
|
||||||
closeNoExceptions(channel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to read, without blocking, from <tt>channel</tt> to its
|
* Tries to read, without blocking, from <tt>channel</tt> to its
|
||||||
* buffer. If after reading the buffer is filled, handles the data in
|
* buffer. If after reading the buffer is filled, handles the data in
|
||||||
@ -915,8 +772,6 @@ public abstract class AbstractTcpListener
|
|||||||
|
|
||||||
if (read == -1)
|
if (read == -1)
|
||||||
throw new IOException("End of stream!");
|
throw new IOException("End of stream!");
|
||||||
else if (read > 0)
|
|
||||||
channel.lastActive = System.currentTimeMillis();
|
|
||||||
|
|
||||||
if (!channel.buffer.hasRemaining())
|
if (!channel.buffer.hasRemaining())
|
||||||
{
|
{
|
||||||
@ -1102,9 +957,6 @@ public abstract class AbstractTcpListener
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean up stale channels
|
|
||||||
cleanup();
|
|
||||||
|
|
||||||
checkForNewChannels();
|
checkForNewChannels();
|
||||||
|
|
||||||
for (SelectionKey key : readSelector.keys())
|
for (SelectionKey key : readSelector.keys())
|
||||||
@ -1164,7 +1016,9 @@ public abstract class AbstractTcpListener
|
|||||||
readSelector.close();
|
readSelector.close();
|
||||||
}
|
}
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{}
|
{
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -387,12 +387,11 @@ public class PseudoTCPBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return current timestamp limited to 32 bits
|
* @return current timestamp limited to 32 bits
|
||||||
*/
|
*/
|
||||||
public static long now()
|
public static long now()
|
||||||
{
|
{
|
||||||
return System.currentTimeMillis() & 0xFFFFFFFFL;
|
return (System.nanoTime() / 1000000) & 0xFFFFFFFFL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1960,7 +1959,7 @@ public class PseudoTCPBase
|
|||||||
options_specified.add(kind);
|
options_specified.add(kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options_specified.indexOf(TCP_OPT_WND_SCALE) == -1)//options_specified.size() - 1)
|
if (!options_specified.contains(TCP_OPT_WND_SCALE))
|
||||||
{
|
{
|
||||||
logger.log(Level.WARNING, "Peer doesn't support window scaling");
|
logger.log(Level.WARNING, "Peer doesn't support window scaling");
|
||||||
if (getM_rwnd_scale() > 0)
|
if (getM_rwnd_scale() > 0)
|
||||||
|
|||||||
@ -245,6 +245,7 @@ public class PseudoTcpSocket extends Socket
|
|||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public synchronized void close()
|
public synchronized void close()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -256,6 +257,12 @@ public class PseudoTcpSocket extends Socket
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OutputStream getOutputStream() throws IOException
|
||||||
|
{
|
||||||
|
return socketImpl.getOutputStream();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows to set up the remote address directly.
|
* Allows to set up the remote address directly.
|
||||||
* Otherwise, when using the other <tt>accept</tt> methods,
|
* Otherwise, when using the other <tt>accept</tt> methods,
|
||||||
|
|||||||
@ -78,17 +78,10 @@ class PseudoTcpSocketImpl
|
|||||||
*/
|
*/
|
||||||
private long readTimeout;
|
private long readTimeout;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private PseudoTcpInputStream inputStream;
|
private PseudoTcpInputStream inputStream;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
private PseudoTcpOutputStream outputstream;
|
private PseudoTcpOutputStream outputstream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param conv_id conversation id, must be the same on both sides
|
* @param conv_id conversation id, must be the same on both sides
|
||||||
@ -196,6 +189,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param stream if true, create a stream socket; otherwise, create a datagram socket.
|
* @param stream if true, create a stream socket; otherwise, create a datagram socket.
|
||||||
* @throws IOException if an I/O error occurs while creating the socket.
|
* @throws IOException if an I/O error occurs while creating the socket.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void create(boolean stream)
|
protected void create(boolean stream)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -208,6 +202,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param port the port number.
|
* @param port the port number.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void connect(String host, int port)
|
protected void connect(String host, int port)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -220,6 +215,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param port the port number.
|
* @param port the port number.
|
||||||
* @throws IOException if an I/O error occurs when attempting a connection.
|
* @throws IOException if an I/O error occurs when attempting a connection.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void connect(InetAddress address, int port)
|
protected void connect(InetAddress address, int port)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -234,6 +230,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param timeout the timeout value, in milliseconds, or zero for no timeout.
|
* @param timeout the timeout value, in milliseconds, or zero for no timeout.
|
||||||
* @throws IOException if an I/O error occurs when attempting a connection.
|
* @throws IOException if an I/O error occurs when attempting a connection.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void connect(SocketAddress address, int timeout)
|
protected void connect(SocketAddress address, int timeout)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -247,6 +244,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param port the port number.
|
* @param port the port number.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void bind(InetAddress host, int port)
|
public void bind(InetAddress host, int port)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -264,6 +262,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param backlog the maximum length of the queue.
|
* @param backlog the maximum length of the queue.
|
||||||
* @throws IOException if an I/O error occurs when creating the queue.
|
* @throws IOException if an I/O error occurs when creating the queue.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void listen(int backlog)
|
protected void listen(int backlog)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -271,6 +270,7 @@ class PseudoTcpSocketImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Map<Integer, Object> options = new HashMap<>();
|
private Map<Integer, Object> options = new HashMap<>();
|
||||||
|
@Override
|
||||||
public void setOption(int optID, Object value)
|
public void setOption(int optID, Object value)
|
||||||
throws SocketException
|
throws SocketException
|
||||||
{
|
{
|
||||||
@ -278,6 +278,7 @@ class PseudoTcpSocketImpl
|
|||||||
options.put(optID, value);
|
options.put(optID, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public Object getOption(int optID)
|
public Object getOption(int optID)
|
||||||
throws SocketException
|
throws SocketException
|
||||||
{
|
{
|
||||||
@ -334,7 +335,6 @@ class PseudoTcpSocketImpl
|
|||||||
*
|
*
|
||||||
* @param remoteAddress to which this socket connects to
|
* @param remoteAddress to which this socket connects to
|
||||||
* @param timeout for this operation in ms
|
* @param timeout for this operation in ms
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
void doConnect(InetSocketAddress remoteAddress, long timeout)
|
void doConnect(InetSocketAddress remoteAddress, long timeout)
|
||||||
throws IOException
|
throws IOException
|
||||||
@ -355,10 +355,9 @@ class PseudoTcpSocketImpl
|
|||||||
while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED
|
while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED
|
||||||
&& (noTimeout || (elapsed < timeout)) )
|
&& (noTimeout || (elapsed < timeout)) )
|
||||||
{
|
{
|
||||||
long start = System.currentTimeMillis();
|
long start = System.nanoTime();
|
||||||
state_notify.wait(timeout);
|
state_notify.wait(timeout);
|
||||||
long end = System.currentTimeMillis();
|
elapsed += TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
|
||||||
elapsed += end - start;
|
|
||||||
}
|
}
|
||||||
if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED)
|
if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED)
|
||||||
{
|
{
|
||||||
@ -431,6 +430,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param s the accepted connection.
|
* @param s the accepted connection.
|
||||||
* @throws IOException if an I/O error occurs when accepting the connection.
|
* @throws IOException if an I/O error occurs when accepting the connection.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void accept(SocketImpl s)
|
protected void accept(SocketImpl s)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -464,6 +464,7 @@ class PseudoTcpSocketImpl
|
|||||||
pseudoTcp.notifyClock(PseudoTCPBase.now());
|
pseudoTcp.notifyClock(PseudoTCPBase.now());
|
||||||
receiveThread = new Thread(new Runnable()
|
receiveThread = new Thread(new Runnable()
|
||||||
{
|
{
|
||||||
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
receivePackets();
|
receivePackets();
|
||||||
@ -483,6 +484,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param tcp the {@link PseudoTCPBase} that caused an event
|
* @param tcp the {@link PseudoTCPBase} that caused an event
|
||||||
* @see PseudoTcpNotify#onTcpOpen(PseudoTCPBase)
|
* @see PseudoTcpNotify#onTcpOpen(PseudoTCPBase)
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void onTcpOpen(PseudoTCPBase tcp)
|
public void onTcpOpen(PseudoTCPBase tcp)
|
||||||
{
|
{
|
||||||
logger.log(Level.FINE, "tcp opened");
|
logger.log(Level.FINE, "tcp opened");
|
||||||
@ -501,6 +503,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param tcp the {@link PseudoTCPBase} that caused an event
|
* @param tcp the {@link PseudoTCPBase} that caused an event
|
||||||
* @see PseudoTcpNotify#onTcpReadable(PseudoTCPBase)
|
* @see PseudoTcpNotify#onTcpReadable(PseudoTCPBase)
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void onTcpReadable(PseudoTCPBase tcp)
|
public void onTcpReadable(PseudoTCPBase tcp)
|
||||||
{
|
{
|
||||||
if (logger.isLoggable(Level.FINER))
|
if (logger.isLoggable(Level.FINER))
|
||||||
@ -522,6 +525,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param tcp the {@link PseudoTCPBase} that caused an event
|
* @param tcp the {@link PseudoTCPBase} that caused an event
|
||||||
* @see PseudoTcpNotify#onTcpWriteable(PseudoTCPBase)
|
* @see PseudoTcpNotify#onTcpWriteable(PseudoTCPBase)
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void onTcpWriteable(PseudoTCPBase tcp)
|
public void onTcpWriteable(PseudoTCPBase tcp)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -545,6 +549,7 @@ class PseudoTcpSocketImpl
|
|||||||
*
|
*
|
||||||
* @see PseudoTcpNotify#onTcpClosed(PseudoTCPBase, IOException)
|
* @see PseudoTcpNotify#onTcpClosed(PseudoTCPBase, IOException)
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void onTcpClosed(PseudoTCPBase tcp, IOException e)
|
public void onTcpClosed(PseudoTCPBase tcp, IOException e)
|
||||||
{
|
{
|
||||||
if (e != null)
|
if (e != null)
|
||||||
@ -606,6 +611,7 @@ class PseudoTcpSocketImpl
|
|||||||
*
|
*
|
||||||
* @see PseudoTcpNotify#tcpWritePacket(PseudoTCPBase, byte[], int)
|
* @see PseudoTcpNotify#tcpWritePacket(PseudoTCPBase, byte[], int)
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public WriteResult tcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len)
|
public WriteResult tcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len)
|
||||||
{
|
{
|
||||||
if (logger.isLoggable(Level.FINEST))
|
if (logger.isLoggable(Level.FINEST))
|
||||||
@ -739,13 +745,7 @@ class PseudoTcpSocketImpl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Runnable clockTaskRunner = new Runnable()
|
private final Runnable clockTaskRunner = this::runClock;
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
runClock();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private void scheduleClockTask(long sleep)
|
private void scheduleClockTask(long sleep)
|
||||||
{
|
{
|
||||||
@ -777,6 +777,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @return an output stream for writing to this socket.
|
* @return an output stream for writing to this socket.
|
||||||
* @throws IOException if an I/O error occurs when creating the output stream.
|
* @throws IOException if an I/O error occurs when creating the output stream.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public OutputStream getOutputStream()
|
public OutputStream getOutputStream()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -793,6 +794,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @return a stream for reading from this socket.
|
* @return a stream for reading from this socket.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public InputStream getInputStream()
|
public InputStream getInputStream()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -808,6 +810,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @return the number of bytes that can be read from this socket without blocking.
|
* @return the number of bytes that can be read from this socket without blocking.
|
||||||
* @throws IOException if an I/O error occurs when determining the number of bytes available.
|
* @throws IOException if an I/O error occurs when determining the number of bytes available.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected int available()
|
protected int available()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -816,16 +819,16 @@ class PseudoTcpSocketImpl
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes this socket.
|
* Closes this socket.
|
||||||
* @throws IOException
|
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void close()
|
public void close()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
pseudoTcp.close(true);
|
pseudoTcp.close(true);
|
||||||
//System.out.println("ON CLOSE: in flight "+pseudoTcp.GetBytesInFlight());
|
//System.out.println("ON CLOSE: in flight "+pseudoTcp.getBytesInFlight());
|
||||||
//System.out.println("ON CLOSE: buff not sent "+pseudoTcp.GetBytesBufferedNotSent());
|
//System.out.println("ON CLOSE: buff not sent "+pseudoTcp.getBytesBufferedNotSent());
|
||||||
onTcpClosed(pseudoTcp, null);
|
onTcpClosed(pseudoTcp, null);
|
||||||
socket.close();
|
socket.close();
|
||||||
joinAllThreads();
|
joinAllThreads();
|
||||||
@ -851,6 +854,7 @@ class PseudoTcpSocketImpl
|
|||||||
* @param data The byte of data to send
|
* @param data The byte of data to send
|
||||||
* @throws IOException if there is an error sending the data.
|
* @throws IOException if there is an error sending the data.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void sendUrgentData(int data)
|
protected void sendUrgentData(int data)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -908,7 +912,7 @@ class PseudoTcpSocketImpl
|
|||||||
public int read(byte[] buffer, int offset, int length)
|
public int read(byte[] buffer, int offset, int length)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
long start = System.currentTimeMillis();
|
long start = System.nanoTime();
|
||||||
int read;
|
int read;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -929,7 +933,7 @@ class PseudoTcpSocketImpl
|
|||||||
if (readTimeout > 0)
|
if (readTimeout > 0)
|
||||||
{
|
{
|
||||||
//Check for timeout
|
//Check for timeout
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
|
||||||
long left = readTimeout - elapsed;
|
long left = readTimeout - elapsed;
|
||||||
if (left <= 0)
|
if (left <= 0)
|
||||||
{
|
{
|
||||||
@ -1033,12 +1037,6 @@ class PseudoTcpSocketImpl
|
|||||||
write(bytes);
|
write(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(byte[] bytes) throws IOException
|
|
||||||
{
|
|
||||||
write(bytes, 0, bytes.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method blocks until all data has been written.
|
* This method blocks until all data has been written.
|
||||||
*
|
*
|
||||||
@ -1053,7 +1051,7 @@ class PseudoTcpSocketImpl
|
|||||||
{
|
{
|
||||||
int toSend = length;
|
int toSend = length;
|
||||||
int sent;
|
int sent;
|
||||||
long start = System.currentTimeMillis();
|
long start = System.nanoTime();
|
||||||
while (toSend > 0)
|
while (toSend > 0)
|
||||||
{
|
{
|
||||||
synchronized (pseudoTcp)
|
synchronized (pseudoTcp)
|
||||||
@ -1073,7 +1071,7 @@ class PseudoTcpSocketImpl
|
|||||||
{
|
{
|
||||||
if (writeTimeout > 0)
|
if (writeTimeout > 0)
|
||||||
{
|
{
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
|
||||||
long left = writeTimeout - elapsed;
|
long left = writeTimeout - elapsed;
|
||||||
if (left <= 0)
|
if (left <= 0)
|
||||||
{
|
{
|
||||||
@ -1121,7 +1119,7 @@ class PseudoTcpSocketImpl
|
|||||||
public synchronized void flush() throws IOException
|
public synchronized void flush() throws IOException
|
||||||
{
|
{
|
||||||
logger.log(Level.FINE, "Flushing...");
|
logger.log(Level.FINE, "Flushing...");
|
||||||
long start = System.currentTimeMillis();
|
long start = System.nanoTime();
|
||||||
final Object ackNotify = pseudoTcp.getAckNotify();
|
final Object ackNotify = pseudoTcp.getAckNotify();
|
||||||
synchronized (ackNotify)
|
synchronized (ackNotify)
|
||||||
{
|
{
|
||||||
@ -1132,8 +1130,8 @@ class PseudoTcpSocketImpl
|
|||||||
if (writeTimeout > 0)
|
if (writeTimeout > 0)
|
||||||
{
|
{
|
||||||
//Check write timeout
|
//Check write timeout
|
||||||
long elapsed = System.currentTimeMillis() - start;
|
long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
|
||||||
long left = writeTimeout - elapsed;
|
long left = writeTimeout - elapsed;
|
||||||
if (left <= 0)
|
if (left <= 0)
|
||||||
{
|
{
|
||||||
IOException e =
|
IOException e =
|
||||||
@ -1151,7 +1149,7 @@ class PseudoTcpSocketImpl
|
|||||||
catch (InterruptedException ex)
|
catch (InterruptedException ex)
|
||||||
{
|
{
|
||||||
throw new IOException("Flush stream interrupted", ex);
|
throw new IOException("Flush stream interrupted", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.log(Level.FINE, "Flushing completed");
|
logger.log(Level.FINE, "Flushing completed");
|
||||||
@ -1160,6 +1158,7 @@ class PseudoTcpSocketImpl
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException
|
public void close() throws IOException
|
||||||
{
|
{
|
||||||
|
PseudoTcpSocketImpl.this.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,11 +18,9 @@
|
|||||||
package org.ice4j.socket;
|
package org.ice4j.socket;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.*;
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.channels.*;
|
import java.nio.channels.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If supported by the runtime, initializes {@link ServerSocketChannel}s which
|
* If supported by the runtime, initializes {@link ServerSocketChannel}s which
|
||||||
@ -33,19 +31,6 @@ import java.util.logging.*;
|
|||||||
*/
|
*/
|
||||||
public class MuxServerSocketChannelFactory
|
public class MuxServerSocketChannelFactory
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* The {@code Logger} used by the {@code MuxServerSocketChannelFactory}
|
|
||||||
* class (and its instances) to print debug information.
|
|
||||||
*/
|
|
||||||
private static final Logger logger
|
|
||||||
= Logger.getLogger(MuxServerSocketChannelFactory.class.getName());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The reflection of the {@code openAndBind} method of the
|
|
||||||
* {@code MuxServerSocketChannel} class.
|
|
||||||
*/
|
|
||||||
private static final Method OPEN_AND_BIND_METHOD;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum number of milliseconds to wait for an accepted
|
* The maximum number of milliseconds to wait for an accepted
|
||||||
* {@code SocketChannel} to provide incoming/readable data before it is
|
* {@code SocketChannel} to provide incoming/readable data before it is
|
||||||
@ -56,158 +41,12 @@ public class MuxServerSocketChannelFactory
|
|||||||
/**
|
/**
|
||||||
* The name of the {@code boolean} property of the {@code socket} property
|
* The name of the {@code boolean} property of the {@code socket} property
|
||||||
* of the {@code ServerSocketChannel} returned by
|
* of the {@code ServerSocketChannel} returned by
|
||||||
* {@link #openAndBindMuxServerSocketChannel(Map, SocketAddress, int,
|
* {@link #openAndBindServerSocketChannel(Map, SocketAddress, int)} which
|
||||||
* DatagramPacketFilter)} which specifies the value of the
|
* specifies the value of the {@code SO_REUSEADDR} socket option.
|
||||||
* {@code SO_REUSEADDR} socket option.
|
|
||||||
*/
|
*/
|
||||||
public static final String SOCKET_REUSE_ADDRESS_PROPERTY_NAME
|
public static final String SOCKET_REUSE_ADDRESS_PROPERTY_NAME
|
||||||
= "socket.reuseAddress";
|
= "socket.reuseAddress";
|
||||||
|
|
||||||
static
|
|
||||||
{
|
|
||||||
Class<?> clazz;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
clazz
|
|
||||||
= Class.forName("org.ice4j.socket.jdk8.MuxServerSocketChannel");
|
|
||||||
}
|
|
||||||
catch (ClassNotFoundException cnfex)
|
|
||||||
{
|
|
||||||
// The class cannot be located probably because ICE4J was not built
|
|
||||||
// on JDK 8.
|
|
||||||
clazz = null;
|
|
||||||
logger.warning(
|
|
||||||
"ICE4J does not support sharing of listening endpoints"
|
|
||||||
+ " (probably because it was not built on JDK 8).");
|
|
||||||
}
|
|
||||||
catch (LinkageError lerr)
|
|
||||||
{
|
|
||||||
// The linkage fails probably because ICE4J was built on JDK 8 but
|
|
||||||
// is not run on JDK 8.
|
|
||||||
clazz = null;
|
|
||||||
logger.warning(
|
|
||||||
"ICE4J does not support sharing of listening endpoints"
|
|
||||||
+ " (probably because it is not running on JDK 8).");
|
|
||||||
}
|
|
||||||
|
|
||||||
Method method;
|
|
||||||
|
|
||||||
if (clazz == null)
|
|
||||||
{
|
|
||||||
method = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
method
|
|
||||||
= clazz.getDeclaredMethod(
|
|
||||||
"openAndBind",
|
|
||||||
Map.class,
|
|
||||||
SocketAddress.class,
|
|
||||||
int.class,
|
|
||||||
DatagramPacketFilter.class);
|
|
||||||
}
|
|
||||||
catch (NoSuchMethodException nsmex)
|
|
||||||
{
|
|
||||||
throw new RuntimeException(nsmex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
OPEN_AND_BIND_METHOD = method;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes a {@code Channel} and swallows any {@link IOException}.
|
|
||||||
*
|
|
||||||
* @param channel the {@code Channel} to close
|
|
||||||
*/
|
|
||||||
public static void closeNoExceptions(Channel channel)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
channel.close();
|
|
||||||
}
|
|
||||||
catch (IOException ioe)
|
|
||||||
{
|
|
||||||
// The whole idea of the method is to close a specific Channel
|
|
||||||
// without caring about any possible IOException.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens and binds a new {@code MuxServerSocketChannel} instance if
|
|
||||||
* supported by the runtime. If there are other (existing)
|
|
||||||
* {@code MuxServerSocketChannel} open and bound on the specified listening
|
|
||||||
* {@code endpoint}, the new instance will share it with them. If the
|
|
||||||
* sharing of listening endpoints is not supported by the runtime, falls
|
|
||||||
* back to {@link #openAndBindServerSocketChannel(Map, SocketAddress, int)}.
|
|
||||||
*
|
|
||||||
* @param properties a {@code Map} of the values to be assigned to
|
|
||||||
* properties of the underlying {@link ServerSocketChannel} which is to
|
|
||||||
* actually listen on the specified {@code endpoint}. If the new instance is
|
|
||||||
* not the first to open and bind the specified {@code endpoint}, the
|
|
||||||
* {@code properties} and their respective values may not be used.
|
|
||||||
* @param endpoint the IP and port the new instance is to bind to
|
|
||||||
* @param backlog the requested maximum number of pending incoming
|
|
||||||
* connections to be queued. If the new instance is not the first to open
|
|
||||||
* and bind the specified {@code endpoint}, the value may not be used.
|
|
||||||
* @param filter the {@code DatagramPacketFilter} to demultiplex (i.e.
|
|
||||||
* recognize) the content meant for the new instance
|
|
||||||
* @return a new {@code MuxServerSocketChannel} instance open and bound on
|
|
||||||
* the specified listening {@code endpoint}
|
|
||||||
* @throws IOException if an I/O error occurs
|
|
||||||
*/
|
|
||||||
public static ServerSocketChannel openAndBindMuxServerSocketChannel(
|
|
||||||
Map<String, Object> properties,
|
|
||||||
SocketAddress endpoint,
|
|
||||||
int backlog,
|
|
||||||
DatagramPacketFilter filter)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
Method method = OPEN_AND_BIND_METHOD;
|
|
||||||
|
|
||||||
if (method == null)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
openAndBindServerSocketChannel(properties, endpoint, backlog);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return
|
|
||||||
(ServerSocketChannel)
|
|
||||||
method.invoke(
|
|
||||||
null,
|
|
||||||
properties, endpoint, backlog, filter);
|
|
||||||
}
|
|
||||||
catch (IllegalAccessException iaex)
|
|
||||||
{
|
|
||||||
IllegalAccessError iaerr
|
|
||||||
= new IllegalAccessError(iaex.getMessage());
|
|
||||||
|
|
||||||
iaerr.initCause(iaex);
|
|
||||||
throw iaerr;
|
|
||||||
}
|
|
||||||
catch (InvocationTargetException itex)
|
|
||||||
{
|
|
||||||
Throwable cause = itex.getCause();
|
|
||||||
|
|
||||||
if (cause == null)
|
|
||||||
throw new RuntimeException(itex);
|
|
||||||
else if (cause instanceof Error)
|
|
||||||
throw (Error) cause;
|
|
||||||
else if (cause instanceof IOException)
|
|
||||||
throw (IOException) cause;
|
|
||||||
else if (cause instanceof RuntimeException)
|
|
||||||
throw (RuntimeException) cause;
|
|
||||||
else
|
|
||||||
throw new RuntimeException(cause);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens and binds a new {@code ServerSocketChannel} instance.
|
* Opens and binds a new {@code ServerSocketChannel} instance.
|
||||||
*
|
*
|
||||||
@ -251,7 +90,7 @@ public class MuxServerSocketChannelFactory
|
|||||||
else if (value instanceof Boolean)
|
else if (value instanceof Boolean)
|
||||||
on = (Boolean) value;
|
on = (Boolean) value;
|
||||||
else
|
else
|
||||||
on = Boolean.valueOf(value.toString());
|
on = Boolean.parseBoolean(value.toString());
|
||||||
|
|
||||||
socket.setReuseAddress(on);
|
socket.setReuseAddress(on);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,79 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2015 Atlassian Pty Ltd
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
import java.net.*;
|
|
||||||
import java.nio.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Associates a {@link ByteBuffer} with a {@link DatagramPacket} so that the
|
|
||||||
* {@code ByteBuffer} may be used for writing into a {@code byte} array and the
|
|
||||||
* {@code DatagramPacket} may be used for reading from the same {@code byte}
|
|
||||||
* array.
|
|
||||||
*
|
|
||||||
* @author Lyubomir Marinov
|
|
||||||
*/
|
|
||||||
class DatagramBuffer
|
|
||||||
extends Timestamped<ByteBuffer>
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The {@code DatagramPacket} which is associated with the
|
|
||||||
* {@link ByteBuffer} {@link #o} and shares its {@code data} with.
|
|
||||||
*/
|
|
||||||
private final DatagramPacket datagramPacket;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new {@code DatagramBuffer} instance with a specific
|
|
||||||
* capacity of {@code byte}s shared between a {@code ByteBuffer} and a
|
|
||||||
* {@code DatagramPacket}.
|
|
||||||
*
|
|
||||||
* @param capacity the maximum number of {@code byte}s to be written into
|
|
||||||
* and read from the new instance
|
|
||||||
*/
|
|
||||||
public DatagramBuffer(int capacity)
|
|
||||||
{
|
|
||||||
super(ByteBuffer.allocate(capacity));
|
|
||||||
|
|
||||||
datagramPacket
|
|
||||||
= new DatagramPacket(o.array(), /* offset */ 0, /* length */ 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the {@code ByteBuffer} (view) of this instance.
|
|
||||||
*
|
|
||||||
* @return the {@code ByteBuffer} (view) of this instance
|
|
||||||
*/
|
|
||||||
public ByteBuffer getByteBuffer()
|
|
||||||
{
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the {@code DatagramPacket} (view) of this instance. The
|
|
||||||
* {@code length} of the {@code DatagramPacket} equals the {@code position}
|
|
||||||
* of the {@code ByteBuffer} so that the {@code byte}s written into the
|
|
||||||
* {@code ByteBuffer} may be read from the {@code DatagramPacket}.
|
|
||||||
*
|
|
||||||
* @return the {@code DatagramPacket} (view) of this instance
|
|
||||||
*/
|
|
||||||
public DatagramPacket getDatagramPacket()
|
|
||||||
{
|
|
||||||
datagramPacket.setLength(getByteBuffer().position());
|
|
||||||
return datagramPacket;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,217 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2018 - present 8x8, Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.lang.reflect.*;
|
|
||||||
import java.nio.channels.*;
|
|
||||||
import java.util.logging.*;
|
|
||||||
|
|
||||||
import org.ice4j.socket.*;
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
class DelegatingServerSocketChannel<T extends ServerSocketChannel>
|
|
||||||
extends BaseDelegatingServerSocketChannel<T>
|
|
||||||
implements SelChImpl
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The {@link Logger} used by the {@link DelegatingServerSocketChannel}
|
|
||||||
* class and its instances for logging output.
|
|
||||||
*/
|
|
||||||
private static final java.util.logging.Logger classLogger
|
|
||||||
= java.util.logging.Logger.getLogger(
|
|
||||||
DelegatingServerSocketChannel.class.getName());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
|
||||||
* required by {@link Selector} and related functionality.
|
|
||||||
*/
|
|
||||||
protected final SelChImpl delegateAsSelChImpl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The translateAndSetInterestOps method available in java 8.
|
|
||||||
*/
|
|
||||||
private final Method translateAndSetInterestOpsMethod;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The translateInterestOps method available in java 11.
|
|
||||||
*/
|
|
||||||
private final Method translateInterestOpsMethod;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
delegateAsSelChImpl
|
|
||||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
|
||||||
|
|
||||||
Method method;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
method = SelChImpl.class.getMethod(
|
|
||||||
"translateAndSetInterestOps",
|
|
||||||
Integer.TYPE, SelectionKeyImpl.class);
|
|
||||||
}
|
|
||||||
catch(NoSuchMethodException e)
|
|
||||||
{
|
|
||||||
method = null;
|
|
||||||
}
|
|
||||||
translateAndSetInterestOpsMethod = method;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
method = SelChImpl.class.getMethod(
|
|
||||||
"translateInterestOps", Integer.TYPE);
|
|
||||||
}
|
|
||||||
catch(NoSuchMethodException e)
|
|
||||||
{
|
|
||||||
method = null;
|
|
||||||
|
|
||||||
}
|
|
||||||
translateInterestOpsMethod = method;
|
|
||||||
|
|
||||||
if (translateInterestOpsMethod == null
|
|
||||||
&& translateAndSetInterestOpsMethod == null)
|
|
||||||
{
|
|
||||||
classLogger.log(
|
|
||||||
Level.SEVERE, "Cannot find translateInterestOps " +
|
|
||||||
"or translateAndSetInterestOps!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@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 void kill()
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
delegateAsSelChImpl.kill();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* Forwards to {@link #delegate}.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (translateAndSetInterestOpsMethod != null)
|
|
||||||
{
|
|
||||||
translateAndSetInterestOpsMethod.invoke(
|
|
||||||
delegateAsSelChImpl, ops, sk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IllegalAccessException | InvocationTargetException e)
|
|
||||||
{
|
|
||||||
classLogger.log(
|
|
||||||
Level.SEVERE,
|
|
||||||
"Cannot execute method translateAndSetInterestOpsMethod", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* Forwards to {@link #delegate}.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public int translateInterestOps(int ops)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (translateInterestOpsMethod != null)
|
|
||||||
{
|
|
||||||
return (Integer) translateInterestOpsMethod.invoke(
|
|
||||||
delegateAsSelChImpl, ops);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IllegalAccessException | InvocationTargetException e)
|
|
||||||
{
|
|
||||||
classLogger.log(
|
|
||||||
Level.SEVERE,
|
|
||||||
"Cannot execute method translateInterestOpsMethod", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,216 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2018 - present 8x8, Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.lang.reflect.*;
|
|
||||||
import java.nio.channels.*;
|
|
||||||
import java.util.logging.*;
|
|
||||||
|
|
||||||
import org.ice4j.socket.*;
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
class DelegatingSocketChannel<T extends SocketChannel>
|
|
||||||
extends BaseDelegatingSocketChannel<T>
|
|
||||||
implements SelChImpl
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The {@link Logger} used by the {@link DelegatingSocketChannel}
|
|
||||||
* class and its instances for logging output.
|
|
||||||
*/
|
|
||||||
private static final java.util.logging.Logger classLogger
|
|
||||||
= java.util.logging.Logger.getLogger(
|
|
||||||
DelegatingSocketChannel.class.getName());
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The view of {@link #delegate} as a <tt>SelChImpl</tt> interface instance
|
|
||||||
* required by {@link Selector} and related functionality.
|
|
||||||
*/
|
|
||||||
protected final SelChImpl delegateAsSelChImpl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The translateAndSetInterestOps method available in java 8.
|
|
||||||
*/
|
|
||||||
private final Method translateAndSetInterestOpsMethod;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The translateInterestOps method available in java 11.
|
|
||||||
*/
|
|
||||||
private final Method translateInterestOpsMethod;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
delegateAsSelChImpl
|
|
||||||
= (delegate instanceof SelChImpl) ? (SelChImpl) delegate : null;
|
|
||||||
|
|
||||||
Method method;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
method = SelChImpl.class.getMethod(
|
|
||||||
"translateAndSetInterestOps",
|
|
||||||
Integer.TYPE, SelectionKeyImpl.class);
|
|
||||||
}
|
|
||||||
catch(NoSuchMethodException e)
|
|
||||||
{
|
|
||||||
method = null;
|
|
||||||
|
|
||||||
}
|
|
||||||
translateAndSetInterestOpsMethod = method;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
method = SelChImpl.class.getMethod(
|
|
||||||
"translateInterestOps", Integer.TYPE);
|
|
||||||
}
|
|
||||||
catch(NoSuchMethodException e)
|
|
||||||
{
|
|
||||||
method = null;
|
|
||||||
}
|
|
||||||
translateInterestOpsMethod = method;
|
|
||||||
|
|
||||||
if (translateInterestOpsMethod == null
|
|
||||||
&& translateAndSetInterestOpsMethod == null)
|
|
||||||
{
|
|
||||||
classLogger.log(
|
|
||||||
Level.SEVERE, "Cannot find translateInterestOps " +
|
|
||||||
"or translateAndSetInterestOps!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@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 void kill()
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
delegateAsSelChImpl.kill();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* Forwards to {@link #delegate}.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (translateAndSetInterestOpsMethod != null)
|
|
||||||
{
|
|
||||||
translateAndSetInterestOpsMethod.invoke(
|
|
||||||
delegateAsSelChImpl, ops, sk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IllegalAccessException | InvocationTargetException e)
|
|
||||||
{
|
|
||||||
classLogger.log(
|
|
||||||
Level.SEVERE,
|
|
||||||
"Cannot execute method translateAndSetInterestOpsMethod", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* Forwards to {@link #delegate}.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public int translateInterestOps(int ops)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (translateInterestOpsMethod != null)
|
|
||||||
{
|
|
||||||
return (Integer) translateInterestOpsMethod.invoke(
|
|
||||||
delegateAsSelChImpl, ops);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IllegalAccessException | InvocationTargetException e)
|
|
||||||
{
|
|
||||||
classLogger.log(
|
|
||||||
Level.SEVERE,
|
|
||||||
"Cannot execute method translateInterestOpsMethod", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2015 Atlassian Pty Ltd
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.ice4j.socket.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a {@link ServerSocket} associated with a
|
|
||||||
* {@code MuxServerSocketChannel}.
|
|
||||||
*
|
|
||||||
* @author Lyubomir Marinov
|
|
||||||
*/
|
|
||||||
class MuxServerSocket
|
|
||||||
extends DelegatingServerSocket
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Initializes a new {@code MuxServerSocket} instance which delegates (its
|
|
||||||
* method calls) to a specific {@code MuxingServerSocket} and is associated
|
|
||||||
* with a specific {@code MuxServerSocketChannel}.
|
|
||||||
*
|
|
||||||
* @param delegate the {@code MuxingServerSocket} the new instance is to
|
|
||||||
* delegate (its method calls) to. Technically, it is the {@code socket} of
|
|
||||||
* the {@code delegate} of {@code channel}.
|
|
||||||
* @param channel the {@code MuxServerSocketChannel} associated with the new
|
|
||||||
* instance
|
|
||||||
* @throws IOException if an I/O error occurs
|
|
||||||
*/
|
|
||||||
public MuxServerSocket(
|
|
||||||
MuxingServerSocket delegate,
|
|
||||||
MuxServerSocketChannel channel)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
super(
|
|
||||||
Objects.requireNonNull(delegate, "delegate"),
|
|
||||||
Objects.requireNonNull(channel, "channel"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,298 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2015 Atlassian Pty Ltd
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.*;
|
|
||||||
import java.nio.channels.*;
|
|
||||||
import java.util.*;
|
|
||||||
import org.ice4j.socket.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements a {@link ServerSocketChannel} which is capable of sharing its
|
|
||||||
* listening endpoint with multiple others like it.
|
|
||||||
*
|
|
||||||
* @author Lyubomir Marinov
|
|
||||||
*/
|
|
||||||
public class MuxServerSocketChannel
|
|
||||||
extends DelegatingServerSocketChannel<MuxingServerSocketChannel>
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Opens and binds a new {@code MuxServerSocketChannel} instance. If there
|
|
||||||
* are other (existing) {@code MuxServerSocketChannel} open and bound on the
|
|
||||||
* specified listening {@code endpoint}, the new instance will share it with
|
|
||||||
* them.
|
|
||||||
*
|
|
||||||
* @param properties a {@code Map} of the values to be assigned to
|
|
||||||
* properties of the underlying {@link ServerSocketChannel} which is to
|
|
||||||
* actually listen on the specified {@code endpoint}. If the new instance is
|
|
||||||
* not the first to open and bind the specified {@code endpoint}, the
|
|
||||||
* {@code properties} and their respective values may not be used.
|
|
||||||
* @param endpoint the IP and port the new instance is to bind to
|
|
||||||
* @param backlog the requested maximum number of pending incoming
|
|
||||||
* connections to be queued. If the new instance is not the first to open
|
|
||||||
* and bind the specified {@code endpoint}, the value may not be used.
|
|
||||||
* @param filter the {@code DatagramPacketFilter} to demultiplex (i.e.
|
|
||||||
* recognize) the content meant for the new instance
|
|
||||||
* @return a new {@code MuxServerSocketChannel} instance open and bound on
|
|
||||||
* the specified listening {@code endpoint}
|
|
||||||
* @throws IOException if an I/O error occurs
|
|
||||||
*/
|
|
||||||
public static MuxServerSocketChannel openAndBind(
|
|
||||||
Map<String, Object> properties,
|
|
||||||
SocketAddress endpoint,
|
|
||||||
int backlog,
|
|
||||||
DatagramPacketFilter filter)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
return
|
|
||||||
MuxingServerSocketChannel.openAndBind(
|
|
||||||
properties,
|
|
||||||
endpoint,
|
|
||||||
backlog,
|
|
||||||
filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The (ordered) list (i.e. queue) of <tt>SocketChannel</tt>s to be returned
|
|
||||||
* by {@link #accept()}.
|
|
||||||
*/
|
|
||||||
private final Queue<Timestamped<SocketChannel>> acceptQ
|
|
||||||
= new LinkedList<>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The {@code DatagramPacketFilter} which demultiplexes
|
|
||||||
* {@code SocketChannel}s accepted by the associated
|
|
||||||
* {@code MuxingServerSocketChannel}.
|
|
||||||
*/
|
|
||||||
protected final DatagramPacketFilter filter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <tt>Object</tt> which synchronizes the access to the state of this
|
|
||||||
* <tt>MuxServerSocketChannel</tt> such as {@link #acceptQ}.
|
|
||||||
*/
|
|
||||||
private final Object syncRoot = new Object();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new {@code MuxServerSocketChannel} instance which is to
|
|
||||||
* demultiplex {@link SocketChannel}s accepted by a specific
|
|
||||||
* {@link MuxingServerSocketChannel} using a specific
|
|
||||||
* {@link DatagramPacketFilter}. The new instance shares the listening
|
|
||||||
* endpoint of {@code delegate} with any other associated
|
|
||||||
* {@code MuxServerSocketChannel}s.
|
|
||||||
*
|
|
||||||
* @param delegate the {@code MuxingServerSocketChannel} which is actually
|
|
||||||
* open and bound to a listening endpoint and accepts {@code SocketChannel}s
|
|
||||||
* to be filtered by associated
|
|
||||||
* {@code MuxServerSocketChannel}s
|
|
||||||
* @param filter the {@code DatagramPacketFilter} which is to demultiplex
|
|
||||||
* {@code SocketChannel}s accepted by {@code delegate}
|
|
||||||
*/
|
|
||||||
protected MuxServerSocketChannel(
|
|
||||||
MuxingServerSocketChannel delegate,
|
|
||||||
DatagramPacketFilter filter)
|
|
||||||
{
|
|
||||||
super(Objects.requireNonNull(delegate, "delegate"));
|
|
||||||
|
|
||||||
this.filter = Objects.requireNonNull(filter, "filter");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public SocketChannel accept()
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
SocketChannel accepted = null;
|
|
||||||
boolean interrupted = false;
|
|
||||||
|
|
||||||
// Pop a SocketChannel from acceptQ.
|
|
||||||
try
|
|
||||||
{
|
|
||||||
do
|
|
||||||
{
|
|
||||||
if (!isOpen())
|
|
||||||
{
|
|
||||||
throw new ClosedChannelException();
|
|
||||||
}
|
|
||||||
else if (!isBound())
|
|
||||||
{
|
|
||||||
throw new NotYetBoundException();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
synchronized (syncRoot)
|
|
||||||
{
|
|
||||||
Timestamped<SocketChannel> timestamped = acceptQ.poll();
|
|
||||||
|
|
||||||
if (timestamped == null)
|
|
||||||
{
|
|
||||||
if (isBlocking())
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
syncRoot.wait();
|
|
||||||
}
|
|
||||||
catch (InterruptedException ie)
|
|
||||||
{
|
|
||||||
interrupted = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if ((accepted = timestamped.o).isOpen())
|
|
||||||
{
|
|
||||||
// Allow the MuxServerSocketChannel class and/or its
|
|
||||||
// super(s) to have a final say on the accepted
|
|
||||||
// SocketChannel such as wrapping it into a suitable
|
|
||||||
// specialization of DelegatingSocketChannel.
|
|
||||||
accepted = implAccept(accepted);
|
|
||||||
if (accepted != null)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (true);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
// Restore the interrupted state of the current thread if we've
|
|
||||||
// cleared it in the loop above.
|
|
||||||
if (interrupted)
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
}
|
|
||||||
return accepted;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Weeds out abandoned {@code SocketChannels} from {@link #acceptQ} i.e.
|
|
||||||
* which were classified/filtered into this {@code MuxServerSocketChannel}
|
|
||||||
* but were not accepted (out of it) for a long time.
|
|
||||||
*
|
|
||||||
* @param now the (system) time in milliseconds at which the method is
|
|
||||||
* invoked
|
|
||||||
*/
|
|
||||||
void closeAbandonedSocketChannels(long now)
|
|
||||||
{
|
|
||||||
synchronized (syncRoot)
|
|
||||||
{
|
|
||||||
Collection<Timestamped<SocketChannel>> chs = acceptQ;
|
|
||||||
|
|
||||||
if (!chs.isEmpty())
|
|
||||||
{
|
|
||||||
for (Iterator<Timestamped<SocketChannel>> i = chs.iterator();
|
|
||||||
i.hasNext();)
|
|
||||||
{
|
|
||||||
Timestamped<SocketChannel> ch = i.next();
|
|
||||||
|
|
||||||
if (now - ch.timestamp
|
|
||||||
>= MuxingServerSocketChannel
|
|
||||||
.SOCKET_CHANNEL_READ_TIMEOUT)
|
|
||||||
{
|
|
||||||
i.remove();
|
|
||||||
MuxingServerSocketChannel.closeNoExceptions(ch.o);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines whether the {@link #filter} of this instance accepts a
|
|
||||||
* specific {@code SocketChannel} from which a specific
|
|
||||||
* {@code DatagramPacket} has been read and, if it does, queues the
|
|
||||||
* {@code channel} to be accepted through this instance.
|
|
||||||
*
|
|
||||||
* @param p the {@code DatagramPacket} which has been read from
|
|
||||||
* {@code channel} and which is to be analyzed by the {@code filter} of this
|
|
||||||
* instance
|
|
||||||
* @param channel the {@code SocketChannel} from which {@code p} has been
|
|
||||||
* read and which is to be queued for acceptance through this instance if
|
|
||||||
* the {@code filter} accepts {@code p}
|
|
||||||
* @return {@code true} if the {@code filter} of this instance accepts
|
|
||||||
* {@code p} and {@code channel} was queued for acceptance through this
|
|
||||||
* instance; otherwise, {@code false}
|
|
||||||
*/
|
|
||||||
protected boolean filterAccept(DatagramPacket p, SocketChannel channel)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
filter.accept(p) && qAccept(new PreReadSocketChannel(p, channel));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* Adjusts the blocking mode of {@link #delegate}.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void implConfigureBlocking(boolean block)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
// If at least one MuxServerSocketChannel is configured as non-blocking,
|
|
||||||
// then MuxingServerSocketChannel has to be configured as non-blocking
|
|
||||||
// as well.
|
|
||||||
if (!block)
|
|
||||||
delegate.configureBlocking(block);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*
|
|
||||||
* Associates a {@link MuxServerSocket} with this
|
|
||||||
* {@code MuxServerSocketChannel}.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected MuxServerSocket implSocket(ServerSocket socket)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
return new MuxServerSocket((MuxingServerSocket) socket, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queues a {@link SocketChannel} for acceptance through this instance.
|
|
||||||
*
|
|
||||||
* @param channel the {@code SocketChannel} to queue for acceptance through
|
|
||||||
* this instance
|
|
||||||
* @return {@code true} if {@code channel} was queued for acceptance through
|
|
||||||
* this instance; otherwise, {@code false}
|
|
||||||
*/
|
|
||||||
boolean qAccept(SocketChannel channel)
|
|
||||||
{
|
|
||||||
boolean b;
|
|
||||||
|
|
||||||
synchronized (syncRoot)
|
|
||||||
{
|
|
||||||
if (acceptQ.offer(
|
|
||||||
new Timestamped<>(channel, System.currentTimeMillis())))
|
|
||||||
{
|
|
||||||
syncRoot.notifyAll();
|
|
||||||
b = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
b = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2015 Atlassian Pty Ltd
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.*;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import org.ice4j.socket.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a {@link ServerSocket} associated with a
|
|
||||||
* {@code MuxingServerSocketChannel}.
|
|
||||||
*
|
|
||||||
* @author Lyubomir Marinov
|
|
||||||
*/
|
|
||||||
class MuxingServerSocket
|
|
||||||
extends DelegatingServerSocket
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Initializes a new {@code MuxingServerSocket} instance which delegates
|
|
||||||
* (its method calls) to a specific {@code ServerSocket} and is associated
|
|
||||||
* with a specific {@code MuxingServerSocketChannel}.
|
|
||||||
*
|
|
||||||
* @param delegate the {@code ServerSocket} the new instance is to delegate
|
|
||||||
* (its method calls) to. Technically, it is the {@code socket} of the
|
|
||||||
* {@code delegate} of {@code channel}.
|
|
||||||
* @param channel the {@code MuxingServerSocketChannel} associated with the
|
|
||||||
* new instance
|
|
||||||
* @throws IOException if an I/O error occurs
|
|
||||||
*/
|
|
||||||
public MuxingServerSocket(
|
|
||||||
ServerSocket delegate,
|
|
||||||
MuxingServerSocketChannel channel)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
super(
|
|
||||||
Objects.requireNonNull(delegate, "delegate"),
|
|
||||||
Objects.requireNonNull(channel, "channel"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@ -1,108 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2015 Atlassian Pty Ltd
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
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 DatagramPacket} 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,66 +0,0 @@
|
|||||||
/*
|
|
||||||
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
|
||||||
*
|
|
||||||
* Copyright @ 2015 Atlassian Pty Ltd
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an {@code Object} with a timestamp.
|
|
||||||
*
|
|
||||||
* @param <T> the type of the {@code Object} which has a timestamp (associated)
|
|
||||||
* @author Lyubomir Marinov
|
|
||||||
*/
|
|
||||||
class Timestamped<T>
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The {@code Object} associated with {@link #timestamp}.
|
|
||||||
*/
|
|
||||||
final T o;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The timestamp (in milliseconds) associated with {@link #o}.
|
|
||||||
*/
|
|
||||||
long timestamp;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new {@code Timestamped} instance with a specific
|
|
||||||
* {@code Object} and a default timestamp (value).
|
|
||||||
*
|
|
||||||
* @param o the {@code Object} to associate with a timestamp
|
|
||||||
*/
|
|
||||||
public Timestamped(T o)
|
|
||||||
{
|
|
||||||
this(o, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new {@code Timestamped} instance with a specific
|
|
||||||
* {@code Object} and a specific timestamp (value).
|
|
||||||
*
|
|
||||||
* @param o the {@code Object} to associate with {@code timestamp}
|
|
||||||
* @param timestamp the timestamp (in milliseconds) to associate with
|
|
||||||
* {@code o}
|
|
||||||
*/
|
|
||||||
public Timestamped(T o, long timestamp)
|
|
||||||
{
|
|
||||||
// It makes no sense to associate a timestamp with a null.
|
|
||||||
if (o == null)
|
|
||||||
throw new NullPointerException("o");
|
|
||||||
|
|
||||||
this.o = o;
|
|
||||||
this.timestamp = timestamp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -485,8 +485,11 @@ public class TransactionSupportTests
|
|||||||
clientAddress,
|
clientAddress,
|
||||||
responseCollector);
|
responseCollector);
|
||||||
|
|
||||||
//wait a while
|
//wait until a response arrives
|
||||||
Thread.sleep(1200);
|
synchronized (responseCollector)
|
||||||
|
{
|
||||||
|
responseCollector.wait(5000);
|
||||||
|
}
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
Vector<StunMessageEvent> reqs
|
Vector<StunMessageEvent> reqs
|
||||||
@ -496,7 +499,7 @@ public class TransactionSupportTests
|
|||||||
"Not all retransmissions were made for the expected period of time");
|
"Not all retransmissions were made for the expected period of time");
|
||||||
|
|
||||||
//wait for a send
|
//wait for a send
|
||||||
Thread.sleep(1800);
|
requestCollector.waitForRequest(1800);
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
reqs = requestCollector.getRequestsForTransaction(
|
reqs = requestCollector.getRequestsForTransaction(
|
||||||
@ -559,15 +562,25 @@ public class TransactionSupportTests
|
|||||||
* Blocks until a request arrives or 50 ms pass.
|
* Blocks until a request arrives or 50 ms pass.
|
||||||
*/
|
*/
|
||||||
public void waitForRequest()
|
public void waitForRequest()
|
||||||
|
{
|
||||||
|
waitForRequest(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blocks until a request arrives or the timeout passes.
|
||||||
|
*/
|
||||||
|
public void waitForRequest(long timeoutMillis)
|
||||||
{
|
{
|
||||||
synchronized(this)
|
synchronized(this)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
wait(50);
|
wait(timeoutMillis);
|
||||||
}
|
}
|
||||||
catch (InterruptedException e)
|
catch (InterruptedException e)
|
||||||
{}
|
{
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -592,7 +605,7 @@ public class TransactionSupportTests
|
|||||||
* transaction and the runtime type of which specifies the failure reason
|
* transaction and the runtime type of which specifies the failure reason
|
||||||
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
|
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
|
||||||
*/
|
*/
|
||||||
protected void processFailure(BaseStunMessageEvent event)
|
protected synchronized void processFailure(BaseStunMessageEvent event)
|
||||||
{
|
{
|
||||||
String receivedResponse;
|
String receivedResponse;
|
||||||
|
|
||||||
@ -603,6 +616,7 @@ public class TransactionSupportTests
|
|||||||
else
|
else
|
||||||
receivedResponse = "failure";
|
receivedResponse = "failure";
|
||||||
receivedResponses.add(receivedResponse);
|
receivedResponses.add(receivedResponse);
|
||||||
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -610,9 +624,10 @@ public class TransactionSupportTests
|
|||||||
*
|
*
|
||||||
* @param response the event to log.
|
* @param response the event to log.
|
||||||
*/
|
*/
|
||||||
public void processResponse(StunResponseEvent response)
|
public synchronized void processResponse(StunResponseEvent response)
|
||||||
{
|
{
|
||||||
receivedResponses.add(response);
|
receivedResponses.add(response);
|
||||||
|
notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.util.concurrent.atomic.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
|
|
||||||
@ -37,6 +38,7 @@ public class PseudoTcpStreamTest
|
|||||||
* Test one-way transfer with @link(PseudoTcpStream)
|
* Test one-way transfer with @link(PseudoTcpStream)
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
|
@Timeout(10)
|
||||||
public void testConnectTransferClose()
|
public void testConnectTransferClose()
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
@ -57,6 +59,8 @@ public class PseudoTcpStreamTest
|
|||||||
server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
|
server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
|
||||||
final InetSocketAddress serverAddress =
|
final InetSocketAddress serverAddress =
|
||||||
new InetSocketAddress(InetAddress.getLoopbackAddress(), server.getLocalPort());
|
new InetSocketAddress(InetAddress.getLoopbackAddress(), server.getLocalPort());
|
||||||
|
AtomicBoolean clientThreadEnded = new AtomicBoolean();
|
||||||
|
AtomicBoolean serverThreadEnded = new AtomicBoolean();
|
||||||
Thread serverThread = new Thread(() ->
|
Thread serverThread = new Thread(() ->
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -76,6 +80,7 @@ public class PseudoTcpStreamTest
|
|||||||
receiveBuffer(server.getInputStream(), sizeB);
|
receiveBuffer(server.getInputStream(), sizeB);
|
||||||
assertArrayEquals(bufferB, recvdBufferB);
|
assertArrayEquals(bufferB, recvdBufferB);
|
||||||
// server.close();
|
// server.close();
|
||||||
|
serverThreadEnded.set(true);
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
@ -91,11 +96,15 @@ public class PseudoTcpStreamTest
|
|||||||
{
|
{
|
||||||
client.setDebugName("R");
|
client.setDebugName("R");
|
||||||
client.connect(serverAddress, 5000);
|
client.connect(serverAddress, 5000);
|
||||||
|
OutputStream os = client.getOutputStream();
|
||||||
|
|
||||||
// write single array
|
// write single array
|
||||||
for (int i = 0; i < singleStepCount; i++)
|
for (int i = 0; i < singleStepCount; i++)
|
||||||
client.getOutputStream().write(bufferSingle[i]);
|
os.write(bufferSingle[i]);
|
||||||
|
|
||||||
// write whole array
|
// write whole array
|
||||||
client.getOutputStream().write(bufferA);
|
os.write(bufferA);
|
||||||
|
|
||||||
// write by parts
|
// write by parts
|
||||||
int partCount = 7;
|
int partCount = 7;
|
||||||
boolean notExact = sizeB % partCount != 0;
|
boolean notExact = sizeB % partCount != 0;
|
||||||
@ -111,12 +120,13 @@ public class PseudoTcpStreamTest
|
|||||||
int written = 0;
|
int written = 0;
|
||||||
for (int j : partsSize)
|
for (int j : partsSize)
|
||||||
{
|
{
|
||||||
client.getOutputStream().write(bufferB, written, j);
|
os.write(bufferB, written, j);
|
||||||
written += j;
|
written += j;
|
||||||
}
|
}
|
||||||
assertEquals(sizeB, written);
|
assertEquals(sizeB, written);
|
||||||
client.getOutputStream().flush();
|
os.flush();
|
||||||
client.close();
|
client.close();
|
||||||
|
clientThreadEnded.set(true);
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
@ -133,8 +143,16 @@ public class PseudoTcpStreamTest
|
|||||||
transferTimeout);
|
transferTimeout);
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
clientThread.join();
|
clientThread.join(10_000);
|
||||||
serverThread.join();
|
if (!clientThreadEnded.get())
|
||||||
|
{
|
||||||
|
fail("client thread did not end");
|
||||||
|
}
|
||||||
|
serverThread.join(10_000);
|
||||||
|
if (!serverThreadEnded.get())
|
||||||
|
{
|
||||||
|
fail("server thread did not end");
|
||||||
|
}
|
||||||
server.close();
|
server.close();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@ -608,9 +608,9 @@ public abstract class PseudoTcpTestBase
|
|||||||
runClocks = false;
|
runClocks = false;
|
||||||
localClockThread.interrupt();
|
localClockThread.interrupt();
|
||||||
remoteClockThread.interrupt();
|
remoteClockThread.interrupt();
|
||||||
localClockThread.join();
|
localClockThread.join(5000);
|
||||||
localClockThread = null;
|
localClockThread = null;
|
||||||
remoteClockThread.join();
|
remoteClockThread.join(5000);
|
||||||
remoteClockThread = null;
|
remoteClockThread = null;
|
||||||
}
|
}
|
||||||
catch (InterruptedException ex)
|
catch (InterruptedException ex)
|
||||||
|
|||||||
@ -1,32 +0,0 @@
|
|||||||
package org.ice4j.socket.jdk8;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.SocketAddress;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.*;
|
|
||||||
|
|
||||||
public class MuxServerSocketChannelTest
|
|
||||||
{
|
|
||||||
@Disabled
|
|
||||||
@Test
|
|
||||||
void testOpenAndBindReuseWhenBindingToAllInterfacesFirst()
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
SocketAddress allInterfaces = new InetSocketAddress("::", 4443);
|
|
||||||
SocketAddress oneInterface = new InetSocketAddress("localhost", 4443);
|
|
||||||
|
|
||||||
MuxServerSocketChannel channelForAllInterfaces =
|
|
||||||
MuxServerSocketChannel
|
|
||||||
.openAndBind(null, allInterfaces, 0, p -> false);
|
|
||||||
|
|
||||||
MuxServerSocketChannel channelForOneInterface =
|
|
||||||
MuxServerSocketChannel
|
|
||||||
.openAndBind(null, oneInterface, 0, p -> false);
|
|
||||||
|
|
||||||
assertEquals(channelForAllInterfaces.getFDVal(),
|
|
||||||
channelForOneInterface.getFDVal());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -63,6 +63,11 @@ public class DatagramCollector
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void waitForPacket()
|
public void waitForPacket()
|
||||||
|
{
|
||||||
|
waitForPacket(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void waitForPacket(long timeoutMillis)
|
||||||
{
|
{
|
||||||
synchronized(this)
|
synchronized(this)
|
||||||
{
|
{
|
||||||
@ -71,12 +76,12 @@ public class DatagramCollector
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
wait(50);
|
wait(timeoutMillis);
|
||||||
}
|
}
|
||||||
catch (InterruptedException e)
|
catch (InterruptedException e)
|
||||||
{
|
{
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@
|
|||||||
package org.ice4j.stack;
|
package org.ice4j.stack;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
@ -50,7 +51,7 @@ public class ShallowStackTest
|
|||||||
private TransportAddress dummyServerAddress = null;
|
private TransportAddress dummyServerAddress = null;
|
||||||
private TransportAddress localAddress = null;
|
private TransportAddress localAddress = null;
|
||||||
|
|
||||||
private DatagramCollector dgramCollector = new DatagramCollector();
|
private final DatagramCollector dgramCollector = new DatagramCollector();
|
||||||
|
|
||||||
private IceSocketWrapper localSock = null;
|
private IceSocketWrapper localSock = null;
|
||||||
|
|
||||||
@ -127,7 +128,7 @@ public class ShallowStackTest
|
|||||||
new SimpleResponseCollector());
|
new SimpleResponseCollector());
|
||||||
|
|
||||||
//wait for its arrival
|
//wait for its arrival
|
||||||
dgramCollector.waitForPacket();
|
dgramCollector.waitForPacket(1000);
|
||||||
|
|
||||||
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
||||||
|
|
||||||
@ -145,7 +146,7 @@ public class ShallowStackTest
|
|||||||
|
|
||||||
dgramCollector.startListening(dummyServerSocket);
|
dgramCollector.startListening(dummyServerSocket);
|
||||||
|
|
||||||
dgramCollector.waitForPacket();
|
dgramCollector.waitForPacket(1000);
|
||||||
|
|
||||||
receivedPacket = dgramCollector.collectPacket();
|
receivedPacket = dgramCollector.collectPacket();
|
||||||
|
|
||||||
@ -318,7 +319,7 @@ public class ShallowStackTest
|
|||||||
|
|
||||||
dgramCollector.startListening(dummyServerSocket);
|
dgramCollector.startListening(dummyServerSocket);
|
||||||
|
|
||||||
long firstTime = System.currentTimeMillis();
|
long firstTime = System.nanoTime();
|
||||||
|
|
||||||
stunStack.sendRequest(bindingRequest,
|
stunStack.sendRequest(bindingRequest,
|
||||||
dummyServerAddress,
|
dummyServerAddress,
|
||||||
@ -326,7 +327,7 @@ public class ShallowStackTest
|
|||||||
new SimpleResponseCollector());
|
new SimpleResponseCollector());
|
||||||
|
|
||||||
//wait for its arrival
|
//wait for its arrival
|
||||||
dgramCollector.waitForPacket();
|
dgramCollector.waitForPacket(1000);
|
||||||
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
DatagramPacket receivedPacket = dgramCollector.collectPacket();
|
||||||
|
|
||||||
assertTrue((receivedPacket.getLength() > 0),
|
assertTrue((receivedPacket.getLength() > 0),
|
||||||
@ -341,7 +342,7 @@ public class ShallowStackTest
|
|||||||
|
|
||||||
// wait for the 1st retransmission with originalWait
|
// wait for the 1st retransmission with originalWait
|
||||||
dgramCollector.startListening(dummyServerSocket);
|
dgramCollector.startListening(dummyServerSocket);
|
||||||
dgramCollector.waitForPacket();
|
dgramCollector.waitForPacket(1000);
|
||||||
receivedPacket = dgramCollector.collectPacket();
|
receivedPacket = dgramCollector.collectPacket();
|
||||||
|
|
||||||
assertTrue((receivedPacket.getLength() > 0),
|
assertTrue((receivedPacket.getLength() > 0),
|
||||||
@ -355,8 +356,7 @@ public class ShallowStackTest
|
|||||||
"The retransmitted request did not match the original.");
|
"The retransmitted request did not match the original.");
|
||||||
|
|
||||||
// verify the retransmission is longer than the originalWait
|
// verify the retransmission is longer than the originalWait
|
||||||
long secondTime = System.currentTimeMillis();
|
assertTrue(System.nanoTime() - firstTime >= TimeUnit.MILLISECONDS.toNanos(originalWait));
|
||||||
assertTrue((secondTime - firstTime) >= originalWait);
|
|
||||||
|
|
||||||
System.clearProperty(StackProperties.FIRST_CTRAN_RETRANS_AFTER);
|
System.clearProperty(StackProperties.FIRST_CTRAN_RETRANS_AFTER);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,6 +51,7 @@ java.util.logging.ConsoleHandler.formatter = org.ice4j.util.Ice4jLogFormatter
|
|||||||
|
|
||||||
# Things coming from ice4j
|
# Things coming from ice4j
|
||||||
org.ice4j.level = INFO
|
org.ice4j.level = INFO
|
||||||
|
org.ice4j.pseudotcp.PseudoTCPBase.level = SEVERE
|
||||||
test.level = INFO
|
test.level = INFO
|
||||||
#org.ice4j.ice.checks.levell = FINEST
|
#org.ice4j.ice.checks.levell = FINEST
|
||||||
#org.ice4j.ice.ConnectivityCheckClient.level = FINER
|
#org.ice4j.ice.ConnectivityCheckClient.level = FINER
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user