diff --git a/changelog b/changelog index a7eac226..2ab975bc 100644 --- a/changelog +++ b/changelog @@ -4,7 +4,7 @@ Changelog User api additions ------------------ -The info struct gained two new members +1) The info struct gained two new members - max_http_header_data: 0 for default (1024) or set the maximum amount of known http header payload that lws can deal with. Payload in unknown http @@ -25,7 +25,7 @@ callback after the HTTP handshake... for ws connections that is ESTABLISHED and for HTTP connections the HTTP callback. So these settings are not related to the maximum number of simultaneous -connections but the number of HTTP handshakes that may be expected or ongoing, +connections, but the number of HTTP handshakes that may be expected or ongoing, or have just completed, at one time. The reason it's useful is it changes the memory allocation for header processing to be one-time at context creation instead of every time there is a new connection, and gives you control over @@ -35,6 +35,24 @@ Setting max_http_header_pool to 1 is fine it will just queue incoming connections before the accept as necessary, you can still have as many simultaneous post-header connections as you like. +User api changes +---------------- + +1) LWS_SEND_BUFFER_POST_PADDING is now 0 and deprecated. You can remove it; if +you still use it, obviously it does nothing. Old binary code with nonzero +LWS_SEND_BUFFER_POST_PADDING is perfectly compatible, the old code just +allocated a buffer bigger than needed. + +The example apps no longer user it. + +The only path who made use of it was sending with LWS_WRITE_CLOSE. + +If you use LWS_WRITE_CLOSE by hand in your user code, you need to allow an +extra 2 bytes space at the end of your buffer. This ONLY applies to +LWS_WRITE_CLOSE, which you normally don't send directly, but cause by returning +nonzero from a callback letting the library actually send it. + + v1.6.0-chrome48-firefox42 ======================= diff --git a/lib/client.c b/lib/client.c index 86ce7223..d712e023 100644 --- a/lib/client.c +++ b/lib/client.c @@ -743,7 +743,7 @@ check_accept: n = wsi->protocol->rx_buffer_size; if (!n) n = LWS_MAX_SOCKET_IO_BUF; - n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; + n += LWS_SEND_BUFFER_PRE_PADDING; wsi->u.ws.rx_user_buffer = lws_malloc(n); if (!wsi->u.ws.rx_user_buffer) { lwsl_err("Out of Mem allocating rx buffer %d\n", n); diff --git a/lib/extension-deflate-frame.c b/lib/extension-deflate-frame.c index 77cb6602..eebc053b 100644 --- a/lib/extension-deflate-frame.c +++ b/lib/extension-deflate-frame.c @@ -55,13 +55,11 @@ int lws_extension_callback_deflate_frame( conn->compressed_out = 0; conn->buf_pre = NULL; conn->buf_in = lws_malloc(LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_in_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_in_length); if (!conn->buf_in) goto bail; conn->buf_out = lws_malloc(LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_out_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_out_length); if (!conn->buf_out) goto bail; lwsl_ext("zlibs constructed\n"); @@ -177,8 +175,7 @@ bail: } conn->buf_in = lws_realloc(conn->buf_in, LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_in_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_in_length); if (!conn->buf_in) { lwsl_err("Out of memory\n"); return -1; @@ -240,8 +237,7 @@ bail: } conn->buf_out = lws_realloc(conn->buf_out, LWS_SEND_BUFFER_PRE_PADDING + - conn->buf_out_length + - LWS_SEND_BUFFER_POST_PADDING); + conn->buf_out_length); if (!conn->buf_out) { lwsl_err("Out of memory\n"); return -1; diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 58a5e204..6b04d810 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -74,8 +74,7 @@ lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason) struct lws_context *context = wsi->context; int n, m, ret, old_state; struct lws_tokens eff_buf; - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4]; if (!wsi) return; diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h index 1c6a3320..f4761e96 100644 --- a/lib/libwebsockets.h +++ b/lib/libwebsockets.h @@ -1396,9 +1396,17 @@ lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs); /* * IMPORTANT NOTICE! * - * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY) + * When sending with websocket protocol + * + * LWS_WRITE_TEXT, + * LWS_WRITE_BINARY, + * LWS_WRITE_CONTINUATION, + * LWS_WRITE_CLOSE, + * LWS_WRITE_PING, + * LWS_WRITE_PONG + * * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE - * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len). + * the buffer pointer you pass to lws_write(). * * This allows us to add protocol info before and after the data, and send as * one packet on the network without payload copying, for maximum efficiency. @@ -1406,25 +1414,33 @@ lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs); * So for example you need this kind of code to use lws_write with a * 128-byte payload * - * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING]; + * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128]; * * // fill your part of the buffer... for example here it's all zeros * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128); * * lws_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128, LWS_WRITE_TEXT); * - * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just - * use the whole buffer without taking care of the above. - */ - -/* - * this is the frame nonce plus two header plus 8 length - * there's an additional two for mux extension per mux nesting level - * 2 byte prepend on close will already fit because control frames cannot use - * the big length style - */ - -/* + * When sending + * + * LWS_WRITE_CLOSE + * + * only, you must allow your buffer to be 2 bytes longer than otherwise + * needed. + * + * When sending HTTP, with + * + * LWS_WRITE_HTTP, + * LWS_WRITE_HTTP_HEADERS + * LWS_WRITE_HTTP_FINAL + * + * there is no protocol data prepended, and don't need to take care about the + * LWS_SEND_BUFFER_PRE_PADDING bytes valid before the buffer pointer. + * + * LWS_SEND_BUFFER_PRE_PADDING is at least the frame nonce + 2 header + 8 length + * LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off. + * The example apps no longer use it. + * * Pad LWS_SEND_BUFFER_PRE_PADDING to the CPU word size, so that word references * to the address immediately after the padding won't cause an unaligned access * error. Sometimes for performance reasons the recommended padding is even @@ -1446,7 +1462,7 @@ lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs); #define _LWS_PAD(n) (((n) % _LWS_PAD_SIZE) ? \ ((n) + (_LWS_PAD_SIZE - ((n) % _LWS_PAD_SIZE))) : (n)) #define LWS_SEND_BUFFER_PRE_PADDING _LWS_PAD(4 + 10) -#define LWS_SEND_BUFFER_POST_PADDING 4 +#define LWS_SEND_BUFFER_POST_PADDING 0 LWS_VISIBLE LWS_EXTERN int lws_write(struct lws *wsi, unsigned char *buf, size_t len, diff --git a/lib/output.c b/lib/output.c index eb164c94..b25487ce 100644 --- a/lib/output.c +++ b/lib/output.c @@ -213,10 +213,8 @@ handle_truncated_send: * @wsi: Websocket instance (available from user callback) * @buf: The data to send. For data being sent on a websocket * connection (ie, not default http), this buffer MUST have - * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer - * and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid - * in the buffer after (buf + len). This is so the protocol - * header and trailer data can be added in-situ. + * LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer. + * This is so the protocol header data can be added in-situ. * @len: Count of the data bytes in the payload starting from buf * @protocol: Use LWS_WRITE_HTTP to reply to an http connection, and one * of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate diff --git a/lib/server.c b/lib/server.c index aacdeb96..fa99f132 100644 --- a/lib/server.c +++ b/lib/server.c @@ -579,7 +579,7 @@ upgrade_ws: n = wsi->protocol->rx_buffer_size; if (!n) n = LWS_MAX_SOCKET_IO_BUF; - n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; + n += LWS_SEND_BUFFER_PRE_PADDING; wsi->u.ws.rx_user_buffer = lws_malloc(n); if (!wsi->u.ws.rx_user_buffer) { lwsl_err("Out of Mem allocating rx buffer %d\n", n); diff --git a/test-server/test-client.c b/test-server/test-client.c index 7ef91e71..726615a5 100644 --- a/test-server/test-client.c +++ b/test-server/test-client.c @@ -134,8 +134,7 @@ static int callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096]; unsigned int rands[4]; int l = 0; int n; diff --git a/test-server/test-echo.c b/test-server/test-echo.c index a13f3e10..47984ac8 100644 --- a/test-server/test-echo.c +++ b/test-server/test-echo.c @@ -47,7 +47,7 @@ static int versa, state; #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server" struct per_session_data__echo { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_ECHO_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_ECHO_PAYLOAD]; unsigned int len; unsigned int index; }; diff --git a/test-server/test-fraggle.c b/test-server/test-fraggle.c index 6dd35903..d2ab0278 100644 --- a/test-server/test-fraggle.c +++ b/test-server/test-fraggle.c @@ -57,8 +57,7 @@ callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { int n; - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 8000 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 8000]; struct per_session_data__fraggle *psf = user; int chunk; int write_mode = LWS_WRITE_CONTINUATION; diff --git a/test-server/test-ping.c b/test-server/test-ping.c index 2a685a18..16065f14 100644 --- a/test-server/test-ping.c +++ b/test-server/test-ping.c @@ -57,8 +57,7 @@ static unsigned int interval_us = 1000000; static unsigned int size = 64; static int flood; static const char *address; -static unsigned char pingbuf[LWS_SEND_BUFFER_PRE_PADDING + MAX_MIRROR_PAYLOAD + - LWS_SEND_BUFFER_POST_PADDING]; +static unsigned char pingbuf[LWS_SEND_BUFFER_PRE_PADDING + MAX_MIRROR_PAYLOAD]; static char peer_name[128]; static unsigned long started; static int screen_width = 80; diff --git a/test-server/test-server-dumb-increment.c b/test-server/test-server-dumb-increment.c index d06d3231..e83a12be 100644 --- a/test-server/test-server-dumb-increment.c +++ b/test-server/test-server-dumb-increment.c @@ -26,8 +26,7 @@ int callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { - unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + - LWS_SEND_BUFFER_POST_PADDING]; + unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512]; struct per_session_data__dumb_increment *pss = (struct per_session_data__dumb_increment *)user; unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; diff --git a/test-server/test-server-mirror.c b/test-server/test-server-mirror.c index cb7bcc58..a92922fa 100644 --- a/test-server/test-server-mirror.c +++ b/test-server/test-server-mirror.c @@ -79,11 +79,12 @@ callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason, pss->ringbuffer_tail++; if (((ringbuffer_head - pss->ringbuffer_tail) & - (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) + (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15)) lws_rx_flow_allow_all_protocol(lws_get_context(wsi), lws_get_protocol(wsi)); - if (lws_partial_buffered(wsi) || lws_send_pipe_choked(wsi)) { + if (lws_partial_buffered(wsi) || + lws_send_pipe_choked(wsi)) { lws_callback_on_writable(wsi); break; } @@ -101,8 +102,7 @@ callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason, free(ringbuffer[ringbuffer_head].payload); ringbuffer[ringbuffer_head].payload = - malloc(LWS_SEND_BUFFER_PRE_PADDING + len + - LWS_SEND_BUFFER_POST_PADDING); + malloc(LWS_SEND_BUFFER_PRE_PADDING + len); ringbuffer[ringbuffer_head].len = len; memcpy((char *)ringbuffer[ringbuffer_head].payload + LWS_SEND_BUFFER_PRE_PADDING, in, len); @@ -121,7 +121,7 @@ choke: done: lws_callback_on_writable_all_protocol(lws_get_context(wsi), - lws_get_protocol(wsi)); + lws_get_protocol(wsi)); break; /*