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

Optimize payload exhaustion

This commit is contained in:
Alex Hultman 2016-03-16 23:56:23 +01:00
parent 5960158958
commit c9bbe6c15f
3 changed files with 56 additions and 0 deletions

View file

@ -1455,3 +1455,53 @@ lws_remaining_packet_payload(struct lws *wsi)
{
return wsi->u.ws.rx_packet_length;
}
/* bypass slow per-byte parsing of payload data when in
* parser state LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED */
void
lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len)
{
int buffer_size, consumable, i;
unsigned char *buffer, *mask;
char *rx_ubuf;
unsigned char mask_idx;
if (wsi->lws_rx_parse_state == LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED) {
buffer_size = (wsi->protocol->rx_buffer_size ? wsi->protocol->rx_buffer_size
: LWS_MAX_SOCKET_IO_BUF);
consumable = buffer_size - wsi->u.ws.rx_ubuf_head;
/* do not consume more than what we should */
if (consumable > wsi->u.ws.rx_packet_length)
consumable = wsi->u.ws.rx_packet_length;
/* do not consume more than what is in the buffer */
if (consumable > *len)
consumable = *len;
/* we want to leave 1 byte for the parser to handle properly */
if (consumable > 1) {
consumable--;
rx_ubuf = wsi->u.ws.rx_ubuf + LWS_PRE + wsi->u.ws.rx_ubuf_head;
if (wsi->u.ws.all_zero_nonce)
memcpy(rx_ubuf, (*buf), consumable);
else {
buffer = *buf;
mask = wsi->u.ws.mask;
mask_idx = wsi->u.ws.mask_idx;
/* this is the main computation, can be further optimized */
for (i = 0; i < consumable; i++)
rx_ubuf[i] = buffer[i] ^ mask[(mask_idx++) & 3];
wsi->u.ws.mask_idx = mask_idx;
}
(*buf) += consumable;
wsi->u.ws.rx_ubuf_head += consumable;
wsi->u.ws.rx_packet_length -= consumable;
*len -= consumable;
}
}
}

View file

@ -1289,6 +1289,9 @@ lws_client_interpret_server_handshake(struct lws *wsi);
LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_rx_sm(struct lws *wsi, unsigned char c);
LWS_EXTERN void
lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
LWS_EXTERN int LWS_WARN_UNUSED_RESULT
lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);

View file

@ -1235,6 +1235,9 @@ lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
if (wsi->rxflow_buffer)
wsi->rxflow_pos++;
/* consume payload bytes efficiently */
lws_payload_until_length_exhausted(wsi, buf, &len);
/* process the byte */
m = lws_rx_sm(wsi, *(*buf)++);
if (m < 0)