Skip to content

Commit f5ef3df

Browse files
committed
Added flag to increase time to wait for ws/wss handshake.
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.
1 parent 242fda8 commit f5ef3df

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

libvncserver/websockets.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ Connection: Upgrade\r\n\
8080
Sec-WebSocket-Accept: %s\r\n\
8181
\r\n"
8282

83-
#define WEBSOCKETS_CLIENT_CONNECT_WAIT_MS 100
84-
#define WEBSOCKETS_CLIENT_SEND_WAIT_MS 100
83+
#define WEBSOCKETS_CLIENT_CONNECT_WAIT_MS 500
8584
#define WEBSOCKETS_MAX_HANDSHAKE_LEN 4096
8685

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

128-
ret = rfbPeekExactTimeout(cl, bbuf, 4,
129-
WEBSOCKETS_CLIENT_CONNECT_WAIT_MS);
127+
/* If we expect the handshake - lets wait longer for it */
128+
int timeout;
129+
if (cl->screen->expect_ws_handshake)
130+
timeout = cl->screen->maxClientWait ? cl->screen->maxClientWait : rfbMaxClientWait;
131+
else
132+
timeout = WEBSOCKETS_CLIENT_CONNECT_WAIT_MS;
133+
134+
ret = rfbPeekExactTimeout(cl, bbuf, 4, timeout);
130135
if ((ret < 0) && (errno == ETIMEDOUT)) {
131136
rfbLog("Normal socket connection\n");
132137
return TRUE;
@@ -144,7 +149,7 @@ webSocketsCheck (rfbClientPtr cl)
144149
rfbErr("webSocketsHandshake: rfbssl_init failed\n");
145150
return FALSE;
146151
}
147-
ret = rfbPeekExactTimeout(cl, bbuf, 4, WEBSOCKETS_CLIENT_CONNECT_WAIT_MS);
152+
ret = rfbPeekExactTimeout(cl, bbuf, 4, timeout);
148153
scheme = "wss";
149154
} else {
150155
scheme = "ws";
@@ -189,8 +194,7 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
189194
}
190195

191196
while (len < WEBSOCKETS_MAX_HANDSHAKE_LEN-1) {
192-
if ((n = rfbReadExactTimeout(cl, buf+len, 1,
193-
WEBSOCKETS_CLIENT_SEND_WAIT_MS)) <= 0) {
197+
if ((n = rfbReadExact(cl, buf+len, 1)) <= 0) {
194198
if ((n < 0) && (errno == ETIMEDOUT)) {
195199
break;
196200
}

rfb/rfb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ typedef struct _rfbScreenInfo
366366
#elif defined(LIBVNCSERVER_HAVE_WIN32THREADS)
367367
uintptr_t listener_thread;
368368
#endif
369+
370+
char expect_ws_handshake; /**< Set to 1 to wait longer for client ws or wss handshake */
371+
369372
} rfbScreenInfo, *rfbScreenInfoPtr;
370373

371374

0 commit comments

Comments
 (0)