Skip to content

Commit

Permalink
respect timeout on websocket connect, issue #355
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdruppe committed Jan 6, 2023
1 parent ef44d53 commit 975e352
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion http2.d
Original file line number Diff line number Diff line change
Expand Up @@ -4469,11 +4469,52 @@ class WebSocket {
/// Group: foundational
void connect() {
this.isClient = true;

socket.blocking = false;

if(uri.unixSocketPath)
socket.connect(new UnixAddress(uri.unixSocketPath));
else
socket.connect(new InternetAddress(host, port)); // FIXME: ipv6 support...
// FIXME: websocket handshake could and really should be async too.


auto readSet = new SocketSet();
auto writeSet = new SocketSet();

readSet.reset();
writeSet.reset();

readSet.add(socket);
writeSet.add(socket);

auto selectGot = Socket.select(readSet, writeSet, null, config.timeoutFromInactivity);
if(selectGot == -1) {
// interrupted

throw new Exception("Websocket connection interrupted - retry might succeed");
} else if(selectGot == 0) {
// time out
socket.close();
throw new Exception("Websocket connection timed out");
} else {
if(writeSet.isSet(socket) || readSet.isSet(socket)) {
import core.stdc.stdint;
int32_t error;
int retopt = socket.getOption(SocketOptionLevel.SOCKET, SocketOption.ERROR, error);
if(retopt < 0 || error != 0) {
socket.close();
throw new Exception("Websocket connection failed - " ~ formatSocketError(error));
} else {
// FIXME: websocket handshake could and really should be async too.
socket.blocking = true; // just convenience
if(auto s = cast(SslClientSocket) socket) {
s.do_ssl_connect();
} else {
// we're ready
}
}
}
}

auto uri = this.uri.path.length ? this.uri.path : "/";
if(this.uri.query.length) {
Expand Down

0 comments on commit 975e352

Please sign in to comment.