Skip to content

Commit

Permalink
Fixed test failures on JDK 17. Updated pom.xml.
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Feb 16, 2022
1 parent 2026971 commit b4b166e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@

<dependencies>
<dependency>
<groupId>asm</groupId>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>3.3</version>
<version>9.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.3</version>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<version>1.2.17</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
Expand Down Expand Up @@ -93,7 +93,7 @@
<target name="compile-native">
<property name="build.dir" value="target"/>
<property name="src.dir" value="src"/>
<property name="gcc.extra.args" value=""/>
<property name="gcc.args" value="-O3 -fno-omit-frame-pointer -momit-leaf-frame-pointer"/>

<echo message="Compiling native library..."/>

Expand All @@ -104,8 +104,9 @@
</pathconvert>

<exec os="Linux" executable="gcc">
<arg line="-g -O3 -fPIC -D_GNU_SOURCE ${gcc.extra.args}
-o ${build.dir}/native/libonenio.so -shared -Wl,-soname,libonenio.so
<arg line="-D_GNU_SOURCE -fPIC -shared -Wl,-soname,libonenio.so ${gcc.args}
-o ${build.dir}/native/libonenio.so
-I ${java.home}/include -I ${java.home}/include/linux
-I ${java.home}/../include -I ${java.home}/../include/linux
${src.native} -ldl -lrt"/>
</exec>
Expand Down
42 changes: 29 additions & 13 deletions src/one/nio/net/SelectableJavaSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,63 @@
package one.nio.net;

import one.nio.util.JavaInternals;
import sun.nio.ch.Net;
import sun.nio.ch.SelChImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.SocketTimeoutException;
import java.nio.channels.SelectableChannel;

import static one.nio.util.JavaInternals.unsafe;

/**
* @author ivan.grigoryev
*/
public abstract class SelectableJavaSocket extends Socket {
private static final MethodHandle poll = getPollMethodHandle();
private static final Log log = LogFactory.getLog(SelectableJavaSocket.class);

private static final MethodHandle poll = getMethodHandle("sun.nio.ch.Net", "poll", FileDescriptor.class, int.class, long.class);
private static final MethodHandle getFD = getMethodHandle("sun.nio.ch.SelChImpl", "getFD");

private static MethodHandle getPollMethodHandle() {
static final int POLL_READ = getFieldValue("sun.nio.ch.Net", "POLLIN");
static final int POLL_WRITE = getFieldValue("sun.nio.ch.Net", "POLLOUT");

private static MethodHandle getMethodHandle(String cls, String name, Class<?>... params) {
try {
Method m = JavaInternals.getMethod(Net.class, "poll", FileDescriptor.class, int.class, long.class);
if (m != null) {
return MethodHandles.publicLookup().unreflect(m);
}
Method m = Class.forName(cls).getDeclaredMethod(name, params);
JavaInternals.setAccessible(m);
return MethodHandles.publicLookup().unreflect(m);
} catch (Throwable e) {
// ignore
log.debug("Failed to access sun.nio.ch API", e);
}
return null;
}

static final int POLL_READ = Net.POLLIN;
static final int POLL_WRITE = Net.POLLOUT;
private static int getFieldValue(String cls, String name) {
try {
Field f = Class.forName(cls).getDeclaredField(name);
return unsafe.getShort(unsafe.staticFieldBase(f), unsafe.staticFieldOffset(f));
} catch (Throwable e) {
log.debug("Failed to access sun.nio.ch API", e);
return 0;
}
}

void checkTimeout(int events, long timeout) throws IOException {
if (timeout <= 0 || poll == null) {
if (timeout <= 0 || poll == null || getFD == null) {
return;
}

try {
long endTime = System.currentTimeMillis() + timeout;
do {
int result = (int) poll.invokeExact(((SelChImpl) getSelectableChannel()).getFD(), events, timeout);
FileDescriptor fd = (FileDescriptor) getFD.invoke(getSelectableChannel());
int result = (int) poll.invokeExact(fd, events, timeout);
if (result > 0) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions test/one/nio/net/SocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static void testIPv4() throws IOException {
Socket s = Socket.create();
s.setTimeout(3000);

s.connect("www.ru", 80);
s.connect("google.com", 80);
System.out.println("connected from " + s.getLocalAddress() + " to " + s.getRemoteAddress());
s.close();
}
Expand All @@ -38,7 +38,7 @@ private static void testIPv6() throws IOException {
Socket s = Socket.create();
s.setTimeout(3000);

s.connect("::1", 22);
s.connect("2a00:1450:4010:c07::71", 80);
System.out.println("connected from " + s.getLocalAddress() + " to " + s.getRemoteAddress());

byte[] b = new byte[1000];
Expand Down

0 comments on commit b4b166e

Please sign in to comment.