Skip to content

Commit

Permalink
Added flag to increase time to wait for ws/wss handshake.
Browse files Browse the repository at this point in the history
For long latency clients then may not be able to deliver the websocket
handshake before we fall back to regular the RFB protocol.

Now it is possible to set expect_ws_handshake=1 which will increase
the timeout to be rfbMaxClientWait or maxClientWait if set.
  • Loading branch information
sgh committed Dec 16, 2020
1 parent 242fda8 commit f5ef3df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 11 additions & 7 deletions libvncserver/websockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ Connection: Upgrade\r\n\
Sec-WebSocket-Accept: %s\r\n\
\r\n"

#define WEBSOCKETS_CLIENT_CONNECT_WAIT_MS 100
#define WEBSOCKETS_CLIENT_SEND_WAIT_MS 100
#define WEBSOCKETS_CLIENT_CONNECT_WAIT_MS 500
#define WEBSOCKETS_MAX_HANDSHAKE_LEN 4096

#if defined(__linux__) && defined(NEED_TIMEVAL)
Expand Down Expand Up @@ -125,8 +124,14 @@ webSocketsCheck (rfbClientPtr cl)
char bbuf[4], *scheme;
int ret;

ret = rfbPeekExactTimeout(cl, bbuf, 4,
WEBSOCKETS_CLIENT_CONNECT_WAIT_MS);
/* If we expect the handshake - lets wait longer for it */
int timeout;
if (cl->screen->expect_ws_handshake)
timeout = cl->screen->maxClientWait ? cl->screen->maxClientWait : rfbMaxClientWait;
else
timeout = WEBSOCKETS_CLIENT_CONNECT_WAIT_MS;

ret = rfbPeekExactTimeout(cl, bbuf, 4, timeout);
if ((ret < 0) && (errno == ETIMEDOUT)) {
rfbLog("Normal socket connection\n");
return TRUE;
Expand All @@ -144,7 +149,7 @@ webSocketsCheck (rfbClientPtr cl)
rfbErr("webSocketsHandshake: rfbssl_init failed\n");
return FALSE;
}
ret = rfbPeekExactTimeout(cl, bbuf, 4, WEBSOCKETS_CLIENT_CONNECT_WAIT_MS);
ret = rfbPeekExactTimeout(cl, bbuf, 4, timeout);
scheme = "wss";
} else {
scheme = "ws";
Expand Down Expand Up @@ -189,8 +194,7 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
}

while (len < WEBSOCKETS_MAX_HANDSHAKE_LEN-1) {
if ((n = rfbReadExactTimeout(cl, buf+len, 1,
WEBSOCKETS_CLIENT_SEND_WAIT_MS)) <= 0) {
if ((n = rfbReadExact(cl, buf+len, 1)) <= 0) {
if ((n < 0) && (errno == ETIMEDOUT)) {
break;
}
Expand Down
3 changes: 3 additions & 0 deletions rfb/rfb.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ typedef struct _rfbScreenInfo
#elif defined(LIBVNCSERVER_HAVE_WIN32THREADS)
uintptr_t listener_thread;
#endif

char expect_ws_handshake; /**< Set to 1 to wait longer for client ws or wss handshake */

} rfbScreenInfo, *rfbScreenInfoPtr;


Expand Down

0 comments on commit f5ef3df

Please sign in to comment.