Skip to content
Merged
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 @@ -54,10 +54,9 @@ public class TlsHandshakeTimeoutServer implements Closeable {
private final boolean sendServerHello;

private volatile int port = -1;
private volatile boolean requestReceived = false;
private volatile ServerSocketChannel serverSocket;
private volatile SocketChannel socket;
private volatile Throwable throwable;
private Thread thread;

public TlsHandshakeTimeoutServer(final boolean sendServerHello) {
this.sendServerHello = sendServerHello;
Expand All @@ -67,22 +66,23 @@ public void start() throws IOException {
this.serverSocket = ServerSocketChannel.open();
this.serverSocket.bind(new InetSocketAddress("0.0.0.0", 0));
this.port = ((InetSocketAddress) this.serverSocket.getLocalAddress()).getPort();
new Thread(this::run).start();
this.thread = new Thread(this::run);
this.thread.start();
}

private void run() {
try {
socket = serverSocket.accept();
requestReceived = true;

if (sendServerHello) {
final SSLEngine sslEngine = initHandshake();

receiveClientHello(sslEngine);
sendServerHello(sslEngine);
}
} catch (final Throwable t) {
this.throwable = t;
} catch (final Throwable ignored) {
// Expected: the client will time out and disconnect, or
// close() will close the server socket to unblock accept()
}
}

Expand Down Expand Up @@ -155,10 +155,12 @@ public void close() {
} catch (final IOException ignore) {
}

if (throwable != null) {
throw new RuntimeException("Exception thrown while TlsHandshakeTimerOuter was running", throwable);
} else if (!requestReceived) {
throw new IllegalStateException("Never received a request");
if (thread != null) {
try {
thread.join(5000);
} catch (final InterruptedException ignore) {
Thread.currentThread().interrupt();
}
}
}
}
Loading