From 01b206e804d466254b04c67a8325e6546040cb6d Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sat, 23 Mar 2013 09:53:17 +0800 Subject: [PATCH] SOL_SNDBUF set to protocol rx size This tells the OS to reserve a TX buffer at least the size of the biggest RX frame expected, for both server and client connections. In Linux, the OS reserves 2 x the requested amount. This is aimed at reducing the partial large atomic frame send problem to the point it's only coming at large atomic frames the OS balks at reserving the size for. If you have a lot of data to send, it is better to split it into multiple writes, and use the FIN / CONTINUATION websocket stuff to manage it. See the "fraggle" test app for example code of how to do that. Signed-off-by: Andy Green --- lib/client.c | 14 +++++++++++--- lib/handshake.c | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/client.c b/lib/client.c index 963c659ef..836203ca9 100644 --- a/lib/client.c +++ b/lib/client.c @@ -379,6 +379,7 @@ lws_client_interpret_server_handshake(struct libwebsocket_context *context, const char *c; #endif int n; + int close_reason = LWS_CLOSE_STATUS_PROTOCOL_ERR; /* * well, what the server sent looked reasonable for syntax. @@ -641,10 +642,15 @@ check_accept: wsi->u.ws.rx_user_buffer = malloc(n); if (!wsi->u.ws.rx_user_buffer) { lwsl_err("Out of Mem allocating rx buffer %d\n", n); - goto bail3; + goto bail2; } lwsl_info("Allocating client RX buffer %d\n", n); + if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n)) { + lwsl_warn("Failed to set SNDBUF to %d", n); + goto bail3; + } + lwsl_debug("handshake OK for protocol %s\n", wsi->protocol->name); /* call him back to inform him he is up */ @@ -675,6 +681,9 @@ check_accept: return 0; bail3: + free(wsi->u.ws.rx_user_buffer); + wsi->u.ws.rx_user_buffer = NULL; + close_reason = LWS_CLOSE_STATUS_NOSTATUS; bail2: if (wsi->protocol) @@ -689,8 +698,7 @@ bail2: if (wsi->u.hdr.ah) free(wsi->u.hdr.ah); - libwebsocket_close_and_free_session(context, wsi, - LWS_CLOSE_STATUS_PROTOCOL_ERR); + libwebsocket_close_and_free_session(context, wsi, close_reason); return 1; } diff --git a/lib/handshake.c b/lib/handshake.c index 4e9bd7091..3fa30f12a 100644 --- a/lib/handshake.c +++ b/lib/handshake.c @@ -258,6 +258,11 @@ libwebsocket_read(struct libwebsocket_context *context, } lwsl_info("Allocating RX buffer %d\n", n); + if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n)) { + lwsl_warn("Failed to set SNDBUF to %d", n); + goto bail; + } + lwsl_parser("accepted v%02d connection\n", wsi->ietf_spec_revision); #endif