FS-6476 regression where sock would sometimes drop while reading logical frames

This commit is contained in:
Anthony Minessale 2014-04-29 18:24:59 -04:00
parent af32ca00e3
commit 2cdae46b19
3 changed files with 15 additions and 18 deletions

View File

@ -1 +1 @@
Thu Apr 17 04:32:56 CDT 2014 Tue Apr 29 18:23:46 EDT 2014

View File

@ -11,6 +11,9 @@
#define ms_sleep(x) Sleep( x ); #define ms_sleep(x) Sleep( x );
#endif #endif
#define WS_BLOCK 1
#define WS_NOBLOCK 0
#define SHA1_HASH_SIZE 20 #define SHA1_HASH_SIZE 20
struct ws_globals_s ws_globals; struct ws_globals_s ws_globals;
@ -242,7 +245,7 @@ int ws_handshake(wsh_t *wsh)
return -3; return -3;
} }
while((bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, wsh->buflen - wsh->datalen)) > 0) { while((bytes = ws_raw_read(wsh, wsh->buffer + wsh->datalen, wsh->buflen - wsh->datalen, WS_BLOCK)) > 0) {
wsh->datalen += bytes; wsh->datalen += bytes;
if (strstr(wsh->buffer, "\r\n\r\n") || strstr(wsh->buffer, "\n\n")) { if (strstr(wsh->buffer, "\r\n\r\n") || strstr(wsh->buffer, "\n\n")) {
break; break;
@ -315,7 +318,7 @@ int ws_handshake(wsh_t *wsh)
} }
ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes) ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block)
{ {
ssize_t r; ssize_t r;
int err = 0; int err = 0;
@ -323,15 +326,13 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes)
if (wsh->ssl) { if (wsh->ssl) {
do { do {
r = SSL_read(wsh->ssl, data, bytes); r = SSL_read(wsh->ssl, data, bytes);
#ifndef _MSC_VER
if (wsh->x++) usleep(10000); ms_sleep(10);
#else
if (wsh->x++) Sleep(10);
#endif
if (r == -1) { if (r == -1) {
err = SSL_get_error(wsh->ssl, r); err = SSL_get_error(wsh->ssl, r);
if (wsh->handshake && err == SSL_ERROR_WANT_READ) { if (!block && err == SSL_ERROR_WANT_READ) {
r = -2; r = -2;
goto end; goto end;
} }
@ -344,11 +345,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes)
do { do {
r = recv(wsh->sock, data, bytes, 0); r = recv(wsh->sock, data, bytes, 0);
#ifndef _MSC_VER ms_sleep(10);
if (wsh->x++) usleep(10000);
#else
if (wsh->x++) Sleep(10);
#endif
} while (r == -1 && xp_is_blocking(xp_errno()) && wsh->x < 100); } while (r == -1 && xp_is_blocking(xp_errno()) && wsh->x < 100);
if (wsh->x >= 100) { if (wsh->x >= 100) {
@ -633,7 +630,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
return ws_close(wsh, WS_PROTO_ERR); return ws_close(wsh, WS_PROTO_ERR);
} }
if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9)) < 0) { if ((wsh->datalen = ws_raw_read(wsh, wsh->buffer, 9, WS_NOBLOCK)) < 0) {
if (wsh->datalen == -2) { if (wsh->datalen == -2) {
return -2; return -2;
} }
@ -641,7 +638,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
} }
if (wsh->datalen < need) { if (wsh->datalen < need) {
if ((wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen)) < need) { if ((wsh->datalen += ws_raw_read(wsh, wsh->buffer + wsh->datalen, 9 - wsh->datalen, WS_BLOCK)) < need) {
/* too small - protocol err */ /* too small - protocol err */
return ws_close(wsh, WS_PROTO_ERR); return ws_close(wsh, WS_PROTO_ERR);
} }
@ -733,7 +730,7 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
wsh->rplen = wsh->plen - need; wsh->rplen = wsh->plen - need;
while(need) { while(need) {
ssize_t r = ws_raw_read(wsh, wsh->payload + wsh->rplen, need); ssize_t r = ws_raw_read(wsh, wsh->payload + wsh->rplen, need, WS_BLOCK);
if (r < 1) { if (r < 1) {
/* invalid read - protocol err .. */ /* invalid read - protocol err .. */

View File

@ -95,7 +95,7 @@ ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes); ssize_t ws_feed_buf(wsh_t *wsh, void *data, size_t bytes);
ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes); ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block);
ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes); ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data); ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes); ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);