Skip to content

Commit 54f8cac

Browse files
committed
Native async
1 parent 1947599 commit 54f8cac

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

src/main/java/org/scalasbt/ipcsocket/JNIUnixDomainSocketLibraryProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ public int shutdown(int fd, int how) throws NativeErrorException {
4646

4747
public int available(int fd) throws NativeErrorException {
4848
int result = availableNative(fd);
49-
if (result < 0) {
50-
return returnOrThrow(pollReadNative(fd, 0), 0);
49+
if (result >= 0) {
50+
return result;
51+
} else if (pollRead(fd, 0)) {
52+
return 1;
5153
} else {
52-
return returnOrThrow(result, 0);
54+
return 0;
5355
}
5456
}
5557

src/main/java/org/scalasbt/ipcsocket/SocketChannels.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,8 @@ public static String readLine(SocketChannel channel, int readTimeoutMillis) thro
9999
numOfKeys = sel.select(readTimeoutMillis);
100100
}
101101
} else {
102-
if (readTimeoutMillis > 0) {
103-
if (ServerSocketChannels.isWin) {
104-
if (channel.supportedOptions().contains(SO_TIMEOUT)) {
105-
channel.setOption(SO_TIMEOUT, Integer.valueOf(readTimeoutMillis));
106-
}
107-
} else {
108-
throw new IOException("timeout requires JDK 17 or Windows");
109-
}
102+
if (channel.supportedOptions().contains(SO_TIMEOUT)) {
103+
channel.setOption(SO_TIMEOUT, Integer.valueOf(readTimeoutMillis));
110104
}
111105
}
112106
if (numOfKeys == 0) {
@@ -153,16 +147,13 @@ public static ByteBuffer readAll(SocketChannel channel, int readTimeoutMillis)
153147
}
154148
} else {
155149
if (readTimeoutMillis > 0) {
156-
// The following operation gets blocked on JDK 8
157-
// channel.register(sel, SelectionKey.OP_READ);
158-
// numOfKeys = sel.select(readTimeoutMillis);
159-
if (ServerSocketChannels.isWin) {
160-
if (channel.supportedOptions().contains(SO_TIMEOUT)) {
161-
channel.setOption(SO_TIMEOUT, Integer.valueOf(readTimeoutMillis));
162-
}
163-
} else {
164-
throw new IOException("timeout requires JDK 17 or Windows");
165-
}
150+
throw new IOException("timeout requires JDK 17 and non-Windows");
151+
}
152+
// The following operation gets blocked on JDK 8
153+
// channel.register(sel, SelectionKey.OP_READ);
154+
// numOfKeys = sel.select(readTimeoutMilis);
155+
if (channel.supportedOptions().contains(SO_TIMEOUT)) {
156+
channel.setOption(SO_TIMEOUT, Integer.valueOf(readTimeoutMillis));
166157
}
167158
}
168159
if (numOfKeys == 0) {

src/main/java/org/scalasbt/ipcsocket/UnixDomainSocket.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.ByteBuffer;
2525

2626
import java.net.Socket;
27+
import java.net.SocketTimeoutException;
2728

2829
import java.util.concurrent.atomic.AtomicInteger;
2930

@@ -137,7 +138,7 @@ public int available() throws IOException {
137138
public int read() throws IOException {
138139
byte[] buf = new byte[1];
139140
int result;
140-
if (doRead(buf, 0, 1) == 0) {
141+
if (doRead(buf, 0, 1, getSoTimeout()) == 0) {
141142
result = -1;
142143
} else {
143144
// Make sure to & with 0xFF to avoid sign extension
@@ -152,7 +153,7 @@ public int read(byte[] b, int off, int len) throws IOException {
152153
}
153154
int socketFd = fd.acquire();
154155
try {
155-
int result = doRead(b, off, len);
156+
int result = doRead(b, off, len, getSoTimeout());
156157
if (result == 0) {
157158
try {
158159
provider.close(socketFd);
@@ -168,12 +169,17 @@ public int read(byte[] b, int off, int len) throws IOException {
168169
}
169170
}
170171

171-
private int doRead(byte[] buf, int offset, int len) throws IOException {
172+
private int doRead(byte[] buf, int offset, int len, int timeoutMillis) throws IOException {
172173
try {
173174
int fdToRead = fd.acquire();
174175
if (fdToRead == -1) {
175176
return -1;
176177
}
178+
if (timeoutMillis > 0) {
179+
if (!provider.pollRead(fdToRead, timeoutMillis)) {
180+
throw new SocketTimeoutException("read timed out");
181+
}
182+
}
177183
return provider.read(fdToRead, buf, offset, len);
178184
} catch (NativeErrorException e) {
179185
throw new IOException(e);

src/test/java/org/scalasbt/ipcsocket/SocketChannelTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static boolean isJava17Plus() {
1414
return SocketChannels.isJava17Plus();
1515
}
1616

17-
/** Test the non-blocking echo server using JDK 17 Unix Domain Socket. */
17+
/** Test the non-blocking echo server. */
1818
@Test
1919
public void testNonBlockingEchoServer() throws IOException, InterruptedException {
2020
System.out.println(
@@ -28,13 +28,13 @@ public void testNonBlockingEchoServer() throws IOException, InterruptedException
2828
});
2929
}
3030

31-
/** Test the non-blocking echo server using JDK 17 Unix Domain Socket. */
31+
/** Test the non-blocking echo server. */
3232
@Test
3333
public void testTimeout() throws IOException, InterruptedException {
3434
System.out.println("SocketChannelTest#testTimeout(" + Boolean.toString(useJNI()) + ")");
3535
withSocket(
3636
sock -> {
37-
if (isJava17Plus() || ServerSocketChannels.isWin) {
37+
if (isJava17Plus() && !ServerSocketChannels.isWin) {
3838
String line = nonBlockingEchoServerTest(sock, 6000, 600);
3939
assertEquals("echo did not timeout", "<unavailable>", line);
4040
}

0 commit comments

Comments
 (0)