From b26fa6e17ce40f5ad982b41142389bca0d420427 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Sun, 4 Dec 2016 12:49:46 +0800 Subject: [PATCH] FS-9810 fix ws write fail on slow network --- libs/libks/src/kws.c | 33 +++++++++++++++++---- libs/sofia-sip/.update | 2 +- libs/sofia-sip/libsofia-sip-ua/tport/ws.c | 35 ++++++++++++++++++----- src/mod/endpoints/mod_verto/ws.c | 35 ++++++++++++++++++----- 4 files changed, 84 insertions(+), 21 deletions(-) diff --git a/libs/libks/src/kws.c b/libs/libks/src/kws.c index 6127d274ef..d364b50f0e 100644 --- a/libs/libks/src/kws.c +++ b/libs/libks/src/kws.c @@ -42,6 +42,9 @@ #define WS_BLOCK 1 #define WS_NOBLOCK 0 +#define WS_INIT_SANITY 5000 +#define WS_WRITE_SANITY 2000 + #define SHA1_HASH_SIZE 20 static const char c64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -412,7 +415,7 @@ KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes) { ks_ssize_t r; - int sanity = 2000; + int sanity = WS_WRITE_SANITY; int ssl_err = 0; ks_size_t wrote = 0; @@ -424,8 +427,17 @@ KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes) wrote += r; } - if (sanity < 2000) { - ks_sleep_ms(1); + if (sanity < WS_WRITE_SANITY) { + int ms = 1; + + if (kws->block) { + if (sanity < WS_WRITE_SANITY * 3 / 4) { + ms = 60; + } else if (sanity < WS_WRITE_SANITY / 2) { + ms = 10; + } + } + ks_sleep_ms(ms); } if (r == -1) { @@ -448,8 +460,17 @@ KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes) wrote += r; } - if (sanity < 2000) { - ks_sleep_ms(1); + if (sanity < WS_WRITE_SANITY) { + int ms = 1; + + if (kws->block) { + if (sanity < WS_WRITE_SANITY * 3 / 4) { + ms = 60; + } else if (sanity < WS_WRITE_SANITY / 2) { + ms = 10; + } + } + ks_sleep_ms(ms); } } while (--sanity > 0 && ((r == -1 && ks_errno_is_blocking(ks_errno())) || (kws->block && wrote < bytes))); @@ -674,7 +695,7 @@ KS_DECLARE(ks_status_t) kws_init(kws_t **kwsP, ks_socket_t sock, SSL_CTX *ssl_ct } kws->sock = sock; - kws->sanity = 5000; + kws->sanity = WS_INIT_SANITY; kws->ssl_ctx = ssl_ctx; kws->buflen = 1024 * 64; diff --git a/libs/sofia-sip/.update b/libs/sofia-sip/.update index b3d2ea9e75..db518f18ec 100644 --- a/libs/sofia-sip/.update +++ b/libs/sofia-sip/.update @@ -1 +1 @@ -Mon Oct 10 15:54:37 CDT 2016 +Sun Dec 4 12:46:08 CST 2016 diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/ws.c b/libs/sofia-sip/libsofia-sip-ua/tport/ws.c index 087374cc9f..4040c0f6ab 100644 --- a/libs/sofia-sip/libsofia-sip-ua/tport/ws.c +++ b/libs/sofia-sip/libsofia-sip-ua/tport/ws.c @@ -20,6 +20,9 @@ #define WS_BLOCK 1 #define WS_NOBLOCK 0 +#define WS_INIT_SANITY 5000 +#define WS_WRITE_SANITY 2000 + #define SHA1_HASH_SIZE 20 struct ws_globals_s ws_globals; @@ -407,7 +410,7 @@ 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 r; - int sanity = 2000; + int sanity = WS_WRITE_SANITY; int ssl_err = 0; size_t wrote = 0; @@ -419,8 +422,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) wrote += r; } - if (sanity < 2000) { - ms_sleep(1); + if (sanity < WS_WRITE_SANITY) { + int ms = 1; + + if (wsh->block) { + if (sanity < WS_WRITE_SANITY * 3 / 4) { + ms = 60; + } else if (sanity < WS_WRITE_SANITY / 2) { + ms = 10; + } + } + ms_sleep(ms); } if (r == -1) { @@ -443,8 +455,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) wrote += r; } - if (sanity < 2000) { - ms_sleep(1); + if (sanity < WS_WRITE_SANITY) { + int ms = 1; + + if (wsh->block) { + if (sanity < WS_WRITE_SANITY * 3 / 4) { + ms = 60; + } else if (sanity < WS_WRITE_SANITY / 2) { + ms = 10; + } + } + ms_sleep(ms); } } while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes))); @@ -453,7 +474,7 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) //printf("wRITE FAIL: %s\n", strerror(errno)); //} - return r; + return r < 0 ? r : wrote; } #ifdef _MSC_VER @@ -587,7 +608,7 @@ int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int wsh->sock = sock; wsh->block = block; - wsh->sanity = 5000; + wsh->sanity = WS_INIT_SANITY; wsh->ssl_ctx = ssl_ctx; wsh->stay_open = stay_open; diff --git a/src/mod/endpoints/mod_verto/ws.c b/src/mod/endpoints/mod_verto/ws.c index 087374cc9f..4040c0f6ab 100644 --- a/src/mod/endpoints/mod_verto/ws.c +++ b/src/mod/endpoints/mod_verto/ws.c @@ -20,6 +20,9 @@ #define WS_BLOCK 1 #define WS_NOBLOCK 0 +#define WS_INIT_SANITY 5000 +#define WS_WRITE_SANITY 2000 + #define SHA1_HASH_SIZE 20 struct ws_globals_s ws_globals; @@ -407,7 +410,7 @@ 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 r; - int sanity = 2000; + int sanity = WS_WRITE_SANITY; int ssl_err = 0; size_t wrote = 0; @@ -419,8 +422,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) wrote += r; } - if (sanity < 2000) { - ms_sleep(1); + if (sanity < WS_WRITE_SANITY) { + int ms = 1; + + if (wsh->block) { + if (sanity < WS_WRITE_SANITY * 3 / 4) { + ms = 60; + } else if (sanity < WS_WRITE_SANITY / 2) { + ms = 10; + } + } + ms_sleep(ms); } if (r == -1) { @@ -443,8 +455,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) wrote += r; } - if (sanity < 2000) { - ms_sleep(1); + if (sanity < WS_WRITE_SANITY) { + int ms = 1; + + if (wsh->block) { + if (sanity < WS_WRITE_SANITY * 3 / 4) { + ms = 60; + } else if (sanity < WS_WRITE_SANITY / 2) { + ms = 10; + } + } + ms_sleep(ms); } } while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes))); @@ -453,7 +474,7 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) //printf("wRITE FAIL: %s\n", strerror(errno)); //} - return r; + return r < 0 ? r : wrote; } #ifdef _MSC_VER @@ -587,7 +608,7 @@ int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int wsh->sock = sock; wsh->block = block; - wsh->sanity = 5000; + wsh->sanity = WS_INIT_SANITY; wsh->ssl_ctx = ssl_ctx; wsh->stay_open = stay_open;