1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

deprecate LWS_SEND_BUFFER_POST_PADDING

The only guy who cared about this for a long while
(since I eliminated the pre-standard protocol variants)
was sending a close frame.

 - Set it to 0 so old code remains happy.  It only affects
user code buffer commit, if there's overcommit no harm
done so no effect directly on user ABI.

 - Remove all uses inside the library.  The sample apps
don't have it any more and that's the recommendation now.

Signed-off-by: Andy Green <andy.green@linaro.org>
This commit is contained in:
Andy Green 2015-12-26 12:03:06 +08:00
parent de1a6a539f
commit 3246ebb3f5
13 changed files with 71 additions and 48 deletions

View file

@ -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
=======================

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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,

View file

@ -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

View file

@ -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);

View file

@ -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;

View file

@ -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;
};

View file

@ -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;

View file

@ -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;

View file

@ -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];

View file

@ -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;
/*