Skip to content

Commit

Permalink
gh-47 fix connection never closed issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknow0 committed Jul 6, 2024
1 parent 6bc19a4 commit 19bbbee
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package unknow.server.nio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -33,7 +32,7 @@ public class NIOConnection {
private final Buffers pendingRead = new Buffers();

/** Stream of pending data */
private final InputStream in = new BuffersInputStream(pendingRead);
protected final BuffersInputStream in = new BuffersInputStream(pendingRead);

/** Output stream */
protected final Out out;
Expand Down Expand Up @@ -207,7 +206,7 @@ public Buffers pendingWrite() {
/**
* @return the current inputStream
*/
public final InputStream getIn() {
public final BuffersInputStream getIn() {
return in;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected void readFrom(ByteBuffer buf) throws InterruptedException, IOException
while (true) {
l = channel.read(rawIn);
if (l == -1) {
in.close();
return;
}
if (l == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ public boolean closed(long now, boolean stop) {
return true;
}

if (pendingWrite().isEmpty() && keepAliveIdle > 0) {
long e = now - keepAliveIdle;
if (lastRead() <= e && lastWrite() <= e) {
logger.info("keep alive idle reached {}", this);
if (pendingWrite().isEmpty()) {
if (in.isClosed())
return true;
if (keepAliveIdle > 0) {
long e = now - keepAliveIdle;
if (lastRead() <= e && lastWrite() <= e) {
logger.info("keep alive idle reached {}", this);
return true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ public boolean closed(long now, boolean stop) {
return true;
}

if (pendingWrite().isEmpty() && keepAliveIdle > 0) {
long e = now - keepAliveIdle;
if (lastRead() <= e && lastWrite() <= e) {
logger.info("keep alive idle reached {}", this);
if (pendingWrite().isEmpty()) {
if (in.isClosed())
return true;
if (keepAliveIdle > 0) {
long e = now - keepAliveIdle;
if (lastRead() <= e && lastWrite() <= e) {
logger.info("keep alive idle reached {}", this);
return true;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class BuffersInputStream extends InputStream {
private final Buffers buffers;
private final boolean wait;
private boolean close;

private long read;

Expand Down Expand Up @@ -46,8 +47,22 @@ public long readCount() {
return read;
}

@Override
public void close() throws IOException {
if (close)
return;
close = true;
try {
buffers.signal();
} catch (@SuppressWarnings("unused") InterruptedException e) {
Thread.currentThread().interrupt();
}
}

@Override
public int read() throws IOException {
if (close)
return -1;
try {
int b = buffers.read(wait);
if (b > 0) {
Expand All @@ -64,11 +79,15 @@ public int read() throws IOException {

@Override
public int read(byte[] b) throws IOException {
if (close)
return -1;
return read(b, 0, b.length);
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (close)
return -1;
try {
len = buffers.read(b, off, len, wait);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -124,4 +143,8 @@ private void ensureMark(int len) {
public int available() throws IOException {
return buffers.length();
}

public boolean isClosed() {
return close;
}
}

0 comments on commit 19bbbee

Please sign in to comment.