Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ public void start() throws IOException {
while (running.get()) {
try {
Socket clientSocket = serverSocket.accept();
clientSocket.setTcpNoDelay(true);
ClientHandler clientHandler = new ClientHandler(clientSocket);
clients.add(clientHandler);
clientHandler.start();
Expand Down Expand Up @@ -445,12 +446,22 @@ public class ClientHandler implements Closeable {
@Override
public void close() {
running.set(false);
try {
// FIN after the frames already written, never an RST: closing with unread input
// resets the connection and purges anything still queued for the peer.
socket.shutdownOutput();
if (readThread == Thread.currentThread()) {
drainInput();
}
} catch (IOException e) {
// ignore
}
try {
socket.close();
} catch (IOException e) {
// ignore
}
if (readThread != null) {
if (readThread != null && readThread != Thread.currentThread()) {
try {
readThread.join(5000);
} catch (InterruptedException e) {
Expand All @@ -459,6 +470,21 @@ public void close() {
}
}

private void drainInput() throws IOException {
socket.setSoTimeout(20);
byte[] scratch = new byte[8192];
long deadlineNanos = System.nanoTime() + 200_000_000L;
while (System.nanoTime() < deadlineNanos) {
try {
if (in.read(scratch) < 0) {
return;
}
} catch (SocketTimeoutException e) {
return;
}
}
}

public synchronized void sendBinary(byte[] data) throws IOException {
writeFrame(WebSocketOpcode.BINARY, data, data.length);
}
Expand Down
Loading