Skip to content

Commit

Permalink
Works on Windows, but only with single pass of 8k
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Mar 10, 2024
1 parent 375a9c3 commit e8010f1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
7 changes: 3 additions & 4 deletions src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1662,12 +1662,11 @@ public long skip(long n) throws IOException {
}

private long implSkip(long nt) throws IOException {
var buf = ByteBuffer.allocateDirect(Math.toIntExact(nt));
readLock.lock();
try {
ensureOpenAndConnected();
boolean blocking = isBlocking();
int n = 0;
long n = 0;
try {
beginRead(blocking);

Expand All @@ -1680,11 +1679,11 @@ private long implSkip(long nt) throws IOException {
return IOStatus.EOF;

configureSocketNonBlockingIfVirtualThread();
n = IOUtil.read(fd, buf, -1, nd);
n = IOUtil.drainN(fdVal, nt);
if (blocking) {
while (IOStatus.okayToRetry(n) && isOpen()) {
park(Net.POLLIN);
n = IOUtil.read(fd, buf, -1, nd);
n = IOUtil.drainN(fdVal, nt);
}
}
} catch (ConnectionResetException e) {
Expand Down
56 changes: 38 additions & 18 deletions src/java.base/windows/native/libnio/ch/IOUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,29 +179,49 @@ Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
}

JNIEXPORT jlong JNICALL
Java_sun_nio_ch_IOUtil_drainN(JNIEnv *env, jclass cl, jint fd, jlong nt)
Java_sun_nio_ch_IOUtil_drainN(JNIEnv *env, jclass cl, jint fd, jlong len)
{
if (nt < 1)
if (len < 1)
return 0;

char buf[4096];
jlong readBytes = 0;
for (;;) {
const jlong remaining = nt - readBytes;
const int count = remaining < sizeof(buf) ? (int) remaining : sizeof(buf);
int n = recv((SOCKET) fd, buf, count, 0);
if (n == SOCKET_ERROR) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
JNU_ThrowIOExceptionWithLastError(env, "recv failed");
}
return readBytes;
/* set up */
int i = 0;
DWORD read = 0;
DWORD flags = 0;
WSABUF buf;

/* limit size */
if (len > 8192)
len = 8192;

/* destination buffer and size */
char cb[8192];
buf.buf = cb;
buf.len = (u_long) len;

/* read into the buffers */
i = WSARecv((SOCKET) fd, /* Socket */
&buf, /* pointers to the buffers */
(DWORD) 1, /* number of buffers to process */
&read, /* receives number of bytes read */
&flags, /* no flags */
0, /* no overlapped sockets */
0); /* no completion routine */

if (i == SOCKET_ERROR) {
int theErr = (jint) WSAGetLastError();
if (theErr == WSAEWOULDBLOCK) {
return IOS_UNAVAILABLE;
}
if (n <= 0)
return readBytes;
readBytes += n;
if (n < (int)sizeof(buf))
return readBytes;
if (theErr == WSAECONNRESET) {
JNU_ThrowByName(env, "sun/net/ConnectionResetException", "Connection reset");
} else {
JNU_ThrowIOExceptionWithLastError(env, "Read failed");
}
return IOS_THROWN;
}

return convertReturnVal(env, (jint) read, JNI_TRUE);
}

JNIEXPORT jint JNICALL
Expand Down

0 comments on commit e8010f1

Please sign in to comment.