2013-01-16 12:21:29 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2015-12-17 17:03:59 +08:00
|
|
|
* Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
|
2013-01-16 12:21:29 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
|
|
|
static int
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_0405_frame_mask_generate(struct lws *wsi)
|
2013-01-16 12:21:29 +08:00
|
|
|
{
|
2015-12-28 14:24:49 +08:00
|
|
|
#if 0
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->u.ws.mask[0] = 0;
|
|
|
|
wsi->u.ws.mask[1] = 0;
|
|
|
|
wsi->u.ws.mask[2] = 0;
|
|
|
|
wsi->u.ws.mask[3] = 0;
|
2015-12-28 14:24:49 +08:00
|
|
|
#else
|
2016-01-26 20:56:56 +08:00
|
|
|
int n;
|
2013-01-16 12:21:29 +08:00
|
|
|
/* fetch the per-frame nonce */
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
n = lws_get_random(lws_get_context(wsi), wsi->u.ws.mask, 4);
|
2013-01-16 12:21:29 +08:00
|
|
|
if (n != 4) {
|
|
|
|
lwsl_parser("Unable to read from random device %s %d\n",
|
2015-12-17 17:03:59 +08:00
|
|
|
SYSTEM_RANDOM_FILEPATH, n);
|
2013-01-16 12:21:29 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2015-12-28 14:24:49 +08:00
|
|
|
#endif
|
2013-01-16 12:21:29 +08:00
|
|
|
/* start masking from first byte of masking key buffer */
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->u.ws.mask_idx = 0;
|
2013-01-16 12:21:29 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-16 14:35:27 +08:00
|
|
|
#ifdef _DEBUG
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2013-03-30 09:52:21 +08:00
|
|
|
LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
|
2013-01-16 12:21:29 +08:00
|
|
|
{
|
2013-01-16 14:35:27 +08:00
|
|
|
unsigned char *buf = (unsigned char *)vbuf;
|
2015-12-06 05:52:09 +08:00
|
|
|
unsigned int n, m, start;
|
2013-01-16 14:35:27 +08:00
|
|
|
char line[80];
|
|
|
|
char *p;
|
2013-01-16 12:21:29 +08:00
|
|
|
|
|
|
|
lwsl_parser("\n");
|
|
|
|
|
|
|
|
for (n = 0; n < len;) {
|
|
|
|
start = n;
|
2013-01-16 14:35:27 +08:00
|
|
|
p = line;
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2013-01-16 14:35:27 +08:00
|
|
|
p += sprintf(p, "%04X: ", start);
|
2013-01-16 12:21:29 +08:00
|
|
|
|
|
|
|
for (m = 0; m < 16 && n < len; m++)
|
2013-01-16 14:35:27 +08:00
|
|
|
p += sprintf(p, "%02X ", buf[n++]);
|
2013-01-16 12:21:29 +08:00
|
|
|
while (m++ < 16)
|
2013-01-16 14:35:27 +08:00
|
|
|
p += sprintf(p, " ");
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2013-01-16 14:35:27 +08:00
|
|
|
p += sprintf(p, " ");
|
2013-01-16 12:21:29 +08:00
|
|
|
|
|
|
|
for (m = 0; m < 16 && (start + m) < len; m++) {
|
2013-01-20 17:08:31 +08:00
|
|
|
if (buf[start + m] >= ' ' && buf[start + m] < 127)
|
2013-01-16 14:35:27 +08:00
|
|
|
*p++ = buf[start + m];
|
2013-01-16 12:21:29 +08:00
|
|
|
else
|
2013-01-16 14:35:27 +08:00
|
|
|
*p++ = '.';
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
while (m++ < 16)
|
2013-01-16 14:35:27 +08:00
|
|
|
*p++ = ' ';
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2013-01-16 14:35:27 +08:00
|
|
|
*p++ = '\n';
|
|
|
|
*p = '\0';
|
2013-01-20 17:08:31 +08:00
|
|
|
lwsl_debug("%s", line);
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
lwsl_debug("\n");
|
|
|
|
}
|
|
|
|
|
2013-01-16 14:35:27 +08:00
|
|
|
#endif
|
|
|
|
|
2013-02-23 10:50:10 +08:00
|
|
|
/*
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
* notice this returns number of bytes consumed, or -1
|
2013-02-23 10:50:10 +08:00
|
|
|
*/
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
|
2013-01-16 12:21:29 +08:00
|
|
|
{
|
2015-12-17 18:25:25 +08:00
|
|
|
struct lws_context *context = lws_get_context(wsi);
|
2013-12-10 21:15:00 +08:00
|
|
|
size_t real_len = len;
|
2015-12-06 05:52:09 +08:00
|
|
|
int n, m;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2014-04-01 14:20:44 +08:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2014-04-10 14:25:24 +08:00
|
|
|
/* just ignore sends after we cleared the truncation buffer */
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
|
|
|
|
!wsi->trunc_len)
|
2014-04-10 14:25:24 +08:00
|
|
|
return len;
|
2013-12-09 14:16:17 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->trunc_len && (buf < wsi->trunc_alloc ||
|
|
|
|
buf > (wsi->trunc_alloc + wsi->trunc_len +
|
|
|
|
wsi->trunc_offset))) {
|
2014-03-23 11:21:51 +08:00
|
|
|
lwsl_err("****** %x Sending new, pending truncated ...\n", wsi);
|
2013-12-09 14:16:17 +08:00
|
|
|
assert(0);
|
|
|
|
}
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_DO_SEND, &buf, len);
|
2014-04-02 19:45:42 +08:00
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
if (m) /* handled */ {
|
|
|
|
n = m;
|
|
|
|
goto handle_truncated_send;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
2015-11-02 20:34:12 +08:00
|
|
|
|
2015-11-14 13:48:58 +08:00
|
|
|
if (!lws_socket_is_valid(wsi->sock))
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
lwsl_warn("** error invalid sock but expected to send\n");
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2015-12-06 05:52:09 +08:00
|
|
|
/* nope, send it on the socket directly */
|
2013-01-29 12:37:35 +08:00
|
|
|
lws_latency_pre(context, wsi);
|
2014-04-06 06:26:35 +01:00
|
|
|
n = lws_ssl_capable_write(wsi, buf, len);
|
2015-12-17 17:03:59 +08:00
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n,
|
|
|
|
(unsigned int)n == len);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2014-04-06 06:26:35 +01:00
|
|
|
switch (n) {
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2014-10-16 08:23:46 +08:00
|
|
|
/* we're going to close, let close know sends aren't possible */
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
2014-04-06 06:26:35 +01:00
|
|
|
return -1;
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
2014-04-10 14:08:10 +08:00
|
|
|
/* nothing got sent, not fatal, retry the whole thing later */
|
2014-04-06 06:26:35 +01:00
|
|
|
n = 0;
|
2014-04-10 14:08:10 +08:00
|
|
|
break;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
|
|
|
handle_truncated_send:
|
|
|
|
/*
|
2014-04-10 14:08:10 +08:00
|
|
|
* we were already handling a truncated send?
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
*/
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->trunc_len) {
|
|
|
|
lwsl_info("%p partial adv %d (vs %d)\n", wsi, n, real_len);
|
|
|
|
wsi->trunc_offset += n;
|
|
|
|
wsi->trunc_len -= n;
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
if (!wsi->trunc_len) {
|
2013-12-09 14:16:17 +08:00
|
|
|
lwsl_info("***** %x partial send completed\n", wsi);
|
2014-03-23 11:41:15 +08:00
|
|
|
/* done with it, but don't free it */
|
2013-12-09 14:16:17 +08:00
|
|
|
n = real_len;
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
|
2014-04-10 14:25:24 +08:00
|
|
|
lwsl_info("***** %x signalling to close now\n", wsi);
|
|
|
|
return -1; /* retry closing now */
|
2014-04-10 17:06:59 +08:00
|
|
|
}
|
2014-04-10 11:23:18 +08:00
|
|
|
}
|
|
|
|
/* always callback on writeable */
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-11-02 13:10:33 +08:00
|
|
|
if ((unsigned int)n == real_len)
|
2014-04-10 14:08:10 +08:00
|
|
|
/* what we just sent went out cleanly */
|
|
|
|
return n;
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/*
|
|
|
|
* Newly truncated send. Buffer the remainder (it will get
|
|
|
|
* first priority next time the socket is writable)
|
|
|
|
*/
|
2015-12-17 17:03:59 +08:00
|
|
|
lwsl_info("%p new partial sent %d from %d total\n", wsi, n, real_len);
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/*
|
|
|
|
* - if we still have a suitable malloc lying around, use it
|
|
|
|
* - or, if too small, reallocate it
|
|
|
|
* - or, if no buffer, create it
|
|
|
|
*/
|
2015-12-17 17:03:59 +08:00
|
|
|
if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
|
|
|
|
lws_free(wsi->trunc_alloc);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
wsi->trunc_alloc_len = real_len - n;
|
|
|
|
wsi->trunc_alloc = lws_malloc(real_len - n);
|
|
|
|
if (!wsi->trunc_alloc) {
|
2014-04-10 14:08:10 +08:00
|
|
|
lwsl_err("truncated send: unable to malloc %d\n",
|
2015-12-06 05:52:09 +08:00
|
|
|
real_len - n);
|
2014-04-10 14:08:10 +08:00
|
|
|
return -1;
|
|
|
|
}
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
}
|
2015-12-17 17:03:59 +08:00
|
|
|
wsi->trunc_offset = 0;
|
|
|
|
wsi->trunc_len = real_len - n;
|
|
|
|
memcpy(wsi->trunc_alloc, buf + n, real_len - n);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
|
|
|
/* since something buffered, force it to get another chance to send */
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return real_len;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-04 08:43:54 +08:00
|
|
|
* lws_write() - Apply protocol then write data to client
|
2013-01-16 12:21:29 +08:00
|
|
|
* @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
|
2016-01-11 11:34:01 +08:00
|
|
|
* LWS_PRE bytes valid BEFORE the pointer.
|
2015-12-26 12:03:06 +08:00
|
|
|
* This is so the protocol header data can be added in-situ.
|
2013-01-16 12:21:29 +08:00
|
|
|
* @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
|
|
|
|
* data on a websockets connection. Remember to allow the extra
|
|
|
|
* bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
|
|
|
|
* are used.
|
|
|
|
*
|
|
|
|
* This function provides the way to issue data back to the client
|
|
|
|
* for both http and websocket protocols.
|
|
|
|
*
|
|
|
|
* In the case of sending using websocket protocol, be sure to allocate
|
|
|
|
* valid storage before and after buf as explained above. This scheme
|
|
|
|
* allows maximum efficiency of sending data and protocol in a single
|
|
|
|
* packet while not burdening the user code with any protocol knowledge.
|
2013-02-23 10:50:10 +08:00
|
|
|
*
|
|
|
|
* Return may be -1 for a fatal error needing connection close, or a
|
|
|
|
* positive number reflecting the amount of bytes actually sent. This
|
|
|
|
* can be less than the requested number of bytes due to OS memory
|
|
|
|
* pressure at any given time.
|
2013-01-16 12:21:29 +08:00
|
|
|
*/
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
|
|
|
|
enum lws_write_protocol wp)
|
2013-01-16 12:21:29 +08:00
|
|
|
{
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
2016-01-11 11:34:01 +08:00
|
|
|
int masked7 = (wsi->mode == LWSCM_WS_CLIENT);
|
2013-01-16 12:21:29 +08:00
|
|
|
unsigned char is_masked_bit = 0;
|
2015-12-06 05:52:09 +08:00
|
|
|
unsigned char *dropmask = NULL;
|
2013-01-16 12:21:29 +08:00
|
|
|
struct lws_tokens eff_buf;
|
2015-12-26 10:07:17 +08:00
|
|
|
int pre = 0, n;
|
2015-12-06 05:52:09 +08:00
|
|
|
size_t orig_len = len;
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
|
|
|
|
/* remove us from the list */
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws **w = &pt->tx_draining_ext_list;
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_debug("%s: TX EXT DRAINING: Remove from list\n", __func__);
|
|
|
|
wsi->u.ws.tx_draining_ext = 0;
|
|
|
|
/* remove us from context draining ext list */
|
|
|
|
while (*w) {
|
|
|
|
if (*w == wsi) {
|
|
|
|
*w = wsi->u.ws.tx_draining_ext_list;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
w = &((*w)->u.ws.tx_draining_ext_list);
|
|
|
|
}
|
|
|
|
wsi->u.ws.tx_draining_ext_list = NULL;
|
|
|
|
wp = (wsi->u.ws.tx_draining_stashed_wp & 0xc0) |
|
|
|
|
LWS_WRITE_CONTINUATION;
|
|
|
|
|
|
|
|
lwsl_ext("FORCED draining wp to 0x%02X\n", wp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wp == LWS_WRITE_HTTP ||
|
|
|
|
wp == LWS_WRITE_HTTP_FINAL ||
|
|
|
|
wp == LWS_WRITE_HTTP_HEADERS)
|
2013-01-16 12:21:29 +08:00
|
|
|
goto send_raw;
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
/* if not in a state to send stuff, then just send nothing */
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->state != LWSS_ESTABLISHED &&
|
2016-01-11 11:34:01 +08:00
|
|
|
((wsi->state != LWSS_RETURNED_CLOSE_ALREADY &&
|
|
|
|
wsi->state != LWSS_AWAITING_CLOSE_ACK) ||
|
|
|
|
wp != LWS_WRITE_CLOSE))
|
|
|
|
return 0;
|
2013-01-16 12:21:29 +08:00
|
|
|
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
/* if we are continuing a frame that already had its header done */
|
|
|
|
|
2015-12-28 14:24:49 +08:00
|
|
|
if (wsi->u.ws.inside_frame) {
|
|
|
|
lwsl_debug("INSIDE FRAME\n");
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
goto do_more_inside_frame;
|
2015-12-28 14:24:49 +08:00
|
|
|
}
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2014-08-18 22:21:51 +08:00
|
|
|
wsi->u.ws.clean_buffer = 1;
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* give a chance to the extensions to modify payload
|
2016-01-11 11:34:01 +08:00
|
|
|
* the extension may decide to produce unlimited payload erratically
|
|
|
|
* (eg, compression extension), so we require only that if he produces
|
|
|
|
* something, it will be a complete fragment of the length known at
|
|
|
|
* the time (just the fragment length known), and if he has
|
|
|
|
* more we will come back next time he is writeable and allow him to
|
|
|
|
* produce more fragments until he's drained.
|
|
|
|
*
|
|
|
|
* This allows what is sent each time it is writeable to be limited to
|
|
|
|
* a size that can be sent without partial sends or blocking, allows
|
|
|
|
* interleaving of control frames and other connection service.
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
*/
|
2013-01-16 12:21:29 +08:00
|
|
|
eff_buf.token = (char *)buf;
|
|
|
|
eff_buf.token_len = len;
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
switch ((int)wp) {
|
2013-01-17 14:46:43 +08:00
|
|
|
case LWS_WRITE_PING:
|
|
|
|
case LWS_WRITE_PONG:
|
|
|
|
case LWS_WRITE_CLOSE:
|
|
|
|
break;
|
|
|
|
default:
|
2016-01-11 11:34:01 +08:00
|
|
|
n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &eff_buf, wp);
|
|
|
|
if (n < 0)
|
2014-04-02 19:45:42 +08:00
|
|
|
return -1;
|
2016-01-11 11:34:01 +08:00
|
|
|
|
|
|
|
if (n && eff_buf.token_len) {
|
|
|
|
/* extension requires further draining */
|
|
|
|
wsi->u.ws.tx_draining_ext = 1;
|
2016-01-19 03:34:24 +08:00
|
|
|
wsi->u.ws.tx_draining_ext_list = pt->tx_draining_ext_list;
|
|
|
|
pt->tx_draining_ext_list = wsi;
|
2016-01-11 11:34:01 +08:00
|
|
|
/* we must come back to do more */
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
/*
|
|
|
|
* keep a copy of the write type for the overall
|
|
|
|
* action that has provoked generation of these
|
|
|
|
* fragments, so the last guy can use its FIN state.
|
|
|
|
*/
|
|
|
|
wsi->u.ws.tx_draining_stashed_wp = wp;
|
|
|
|
/* this is definitely not actually the last fragment
|
|
|
|
* because the extension asserted he has more coming
|
|
|
|
* So make sure this intermediate one doesn't go out
|
|
|
|
* with a FIN.
|
|
|
|
*/
|
|
|
|
wp |= LWS_WRITE_NO_FIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eff_buf.token_len && wsi->u.ws.stashed_write_pending) {
|
|
|
|
wsi->u.ws.stashed_write_pending = 0;
|
|
|
|
wp = (wp &0xc0) | (int)wsi->u.ws.stashed_write_type;
|
|
|
|
}
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
/*
|
|
|
|
* an extension did something we need to keep... for example, if
|
|
|
|
* compression extension, it has already updated its state according
|
|
|
|
* to this being issued
|
|
|
|
*/
|
2016-01-11 11:34:01 +08:00
|
|
|
if ((char *)buf != eff_buf.token) {
|
|
|
|
/*
|
|
|
|
* ext might eat it, but no have anything to issue yet
|
|
|
|
* in that case we have to follow his lead, but stash and
|
|
|
|
* replace the write type that was lost here the first time.
|
|
|
|
*/
|
|
|
|
if (len && !eff_buf.token_len) {
|
|
|
|
if (!wsi->u.ws.stashed_write_pending)
|
|
|
|
wsi->u.ws.stashed_write_type = (char)wp & 0x3f;
|
|
|
|
wsi->u.ws.stashed_write_pending = 1;
|
|
|
|
return len;
|
|
|
|
}
|
2014-03-23 11:21:51 +08:00
|
|
|
/*
|
|
|
|
* extension recreated it:
|
|
|
|
* need to buffer this if not all sent
|
|
|
|
*/
|
|
|
|
wsi->u.ws.clean_buffer = 0;
|
2016-01-11 11:34:01 +08:00
|
|
|
}
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2013-01-16 12:21:29 +08:00
|
|
|
buf = (unsigned char *)eff_buf.token;
|
|
|
|
len = eff_buf.token_len;
|
|
|
|
|
|
|
|
switch (wsi->ietf_spec_revision) {
|
|
|
|
case 13:
|
|
|
|
if (masked7) {
|
|
|
|
pre += 4;
|
|
|
|
dropmask = &buf[0 - pre];
|
|
|
|
is_masked_bit = 0x80;
|
|
|
|
}
|
2013-01-21 09:53:35 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
switch (wp & 0xf) {
|
2013-01-16 12:21:29 +08:00
|
|
|
case LWS_WRITE_TEXT:
|
2015-12-17 17:03:59 +08:00
|
|
|
n = LWSWSOPC_TEXT_FRAME;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
case LWS_WRITE_BINARY:
|
2015-12-17 17:03:59 +08:00
|
|
|
n = LWSWSOPC_BINARY_FRAME;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
case LWS_WRITE_CONTINUATION:
|
2015-12-17 17:03:59 +08:00
|
|
|
n = LWSWSOPC_CONTINUATION;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_WRITE_CLOSE:
|
2015-12-17 17:03:59 +08:00
|
|
|
n = LWSWSOPC_CLOSE;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
case LWS_WRITE_PING:
|
2015-12-17 17:03:59 +08:00
|
|
|
n = LWSWSOPC_PING;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
case LWS_WRITE_PONG:
|
2015-12-17 17:03:59 +08:00
|
|
|
n = LWSWSOPC_PONG;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
default:
|
2016-01-11 11:34:01 +08:00
|
|
|
lwsl_warn("lws_write: unknown write opc / wp\n");
|
2013-01-16 12:21:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (!(wp & LWS_WRITE_NO_FIN))
|
2013-01-16 12:21:29 +08:00
|
|
|
n |= 1 << 7;
|
|
|
|
|
|
|
|
if (len < 126) {
|
|
|
|
pre += 2;
|
|
|
|
buf[-pre] = n;
|
2015-12-06 08:00:03 +08:00
|
|
|
buf[-pre + 1] = (unsigned char)(len | is_masked_bit);
|
2013-01-16 12:21:29 +08:00
|
|
|
} else {
|
|
|
|
if (len < 65536) {
|
|
|
|
pre += 4;
|
|
|
|
buf[-pre] = n;
|
|
|
|
buf[-pre + 1] = 126 | is_masked_bit;
|
2015-12-06 08:00:03 +08:00
|
|
|
buf[-pre + 2] = (unsigned char)(len >> 8);
|
|
|
|
buf[-pre + 3] = (unsigned char)len;
|
2013-01-16 12:21:29 +08:00
|
|
|
} else {
|
|
|
|
pre += 10;
|
|
|
|
buf[-pre] = n;
|
|
|
|
buf[-pre + 1] = 127 | is_masked_bit;
|
|
|
|
#if defined __LP64__
|
|
|
|
buf[-pre + 2] = (len >> 56) & 0x7f;
|
|
|
|
buf[-pre + 3] = len >> 48;
|
|
|
|
buf[-pre + 4] = len >> 40;
|
|
|
|
buf[-pre + 5] = len >> 32;
|
|
|
|
#else
|
|
|
|
buf[-pre + 2] = 0;
|
|
|
|
buf[-pre + 3] = 0;
|
|
|
|
buf[-pre + 4] = 0;
|
|
|
|
buf[-pre + 5] = 0;
|
|
|
|
#endif
|
2015-12-06 08:00:03 +08:00
|
|
|
buf[-pre + 6] = (unsigned char)(len >> 24);
|
|
|
|
buf[-pre + 7] = (unsigned char)(len >> 16);
|
|
|
|
buf[-pre + 8] = (unsigned char)(len >> 8);
|
|
|
|
buf[-pre + 9] = (unsigned char)len;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
do_more_inside_frame:
|
|
|
|
|
2013-01-16 12:21:29 +08:00
|
|
|
/*
|
|
|
|
* Deal with masking if we are in client -> server direction and
|
2016-01-11 11:34:01 +08:00
|
|
|
* the wp demands it
|
2013-01-16 12:21:29 +08:00
|
|
|
*/
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (masked7) {
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
if (!wsi->u.ws.inside_frame)
|
2015-12-04 09:23:56 +08:00
|
|
|
if (lws_0405_frame_mask_generate(wsi)) {
|
2014-03-23 11:21:51 +08:00
|
|
|
lwsl_err("frame mask generation failed\n");
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-01-16 12:21:29 +08:00
|
|
|
|
|
|
|
/*
|
2013-01-21 09:53:35 +08:00
|
|
|
* in v7, just mask the payload
|
2013-01-16 12:21:29 +08:00
|
|
|
*/
|
2013-12-21 10:22:17 +08:00
|
|
|
if (dropmask) { /* never set if already inside frame */
|
|
|
|
for (n = 4; n < (int)len + 4; n++)
|
2016-01-11 11:34:01 +08:00
|
|
|
dropmask[n] = dropmask[n] ^ wsi->u.ws.mask[
|
|
|
|
(wsi->u.ws.mask_idx++) & 3];
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2013-01-21 09:53:35 +08:00
|
|
|
/* copy the frame nonce into place */
|
2016-01-11 11:34:01 +08:00
|
|
|
memcpy(dropmask, wsi->u.ws.mask, 4);
|
2013-12-21 10:22:17 +08:00
|
|
|
}
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
send_raw:
|
2016-01-11 11:34:01 +08:00
|
|
|
switch ((int)wp) {
|
2013-01-17 14:46:43 +08:00
|
|
|
case LWS_WRITE_CLOSE:
|
2015-12-26 10:07:17 +08:00
|
|
|
/* lwsl_hexdump(&buf[-pre], len); */
|
2013-01-17 14:46:43 +08:00
|
|
|
case LWS_WRITE_HTTP:
|
2014-10-18 12:23:05 +08:00
|
|
|
case LWS_WRITE_HTTP_FINAL:
|
2014-10-08 12:00:53 +08:00
|
|
|
case LWS_WRITE_HTTP_HEADERS:
|
2013-01-17 14:46:43 +08:00
|
|
|
case LWS_WRITE_PONG:
|
|
|
|
case LWS_WRITE_PING:
|
2014-10-08 12:15:15 +08:00
|
|
|
#ifdef LWS_USE_HTTP2
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->mode == LWSCM_HTTP2_SERVING) {
|
2014-10-12 08:38:16 +08:00
|
|
|
unsigned char flags = 0;
|
|
|
|
|
2014-10-08 12:00:53 +08:00
|
|
|
n = LWS_HTTP2_FRAME_TYPE_DATA;
|
2016-01-11 11:34:01 +08:00
|
|
|
if (wp == LWS_WRITE_HTTP_HEADERS) {
|
2014-10-08 12:00:53 +08:00
|
|
|
n = LWS_HTTP2_FRAME_TYPE_HEADERS;
|
2014-10-17 08:38:44 +08:00
|
|
|
flags = LWS_HTTP2_FLAG_END_HEADERS;
|
2014-10-19 07:36:20 +08:00
|
|
|
if (wsi->u.http2.send_END_STREAM)
|
|
|
|
flags |= LWS_HTTP2_FLAG_END_STREAM;
|
2014-10-12 08:38:16 +08:00
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if ((wp == LWS_WRITE_HTTP ||
|
|
|
|
wp == LWS_WRITE_HTTP_FINAL) &&
|
2015-12-26 10:07:17 +08:00
|
|
|
wsi->u.http.content_length) {
|
2014-10-18 12:23:05 +08:00
|
|
|
wsi->u.http.content_remain -= len;
|
2015-12-26 10:07:17 +08:00
|
|
|
lwsl_info("%s: content_remain = %lu\n", __func__,
|
|
|
|
wsi->u.http.content_remain);
|
2014-10-18 12:23:05 +08:00
|
|
|
if (!wsi->u.http.content_remain) {
|
|
|
|
lwsl_info("%s: selecting final write mode\n", __func__);
|
2016-01-11 11:34:01 +08:00
|
|
|
wp = LWS_WRITE_HTTP_FINAL;
|
2014-10-18 12:23:05 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (wp == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
|
2014-10-18 12:23:05 +08:00
|
|
|
lwsl_info("%s: setting END_STREAM\n", __func__);
|
|
|
|
flags |= LWS_HTTP2_FLAG_END_STREAM;
|
|
|
|
}
|
|
|
|
|
2015-12-26 10:07:17 +08:00
|
|
|
return lws_http2_frame_write(wsi, n, flags,
|
|
|
|
wsi->u.http2.my_stream_id, len, buf);
|
2014-10-08 12:00:53 +08:00
|
|
|
}
|
2014-10-08 12:15:15 +08:00
|
|
|
#endif
|
2015-12-26 10:07:17 +08:00
|
|
|
return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre);
|
2013-01-17 14:46:43 +08:00
|
|
|
default:
|
|
|
|
break;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* give any active extensions a chance to munge the buffer
|
|
|
|
* before send. We pass in a pointer to an lws_tokens struct
|
|
|
|
* prepared with the default buffer and content length that's in
|
|
|
|
* there. Rather than rewrite the default buffer, extensions
|
|
|
|
* that expect to grow the buffer can adapt .token to
|
|
|
|
* point to their own per-connection buffer in the extension
|
|
|
|
* user allocation. By default with no extensions or no
|
|
|
|
* extension callback handling, just the normal input buffer is
|
|
|
|
* used then so it is efficient.
|
|
|
|
*
|
|
|
|
* callback returns 1 in case it wants to spill more buffers
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
*
|
|
|
|
* This takes care of holding the buffer if send is incomplete, ie,
|
|
|
|
* if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
|
|
|
|
* the buffer). If wsi->u.ws.clean_buffer is 1, it will instead
|
|
|
|
* return to the user code how much OF THE USER BUFFER was consumed.
|
2013-01-16 12:21:29 +08:00
|
|
|
*/
|
|
|
|
|
2015-12-26 10:07:17 +08:00
|
|
|
n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre);
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->u.ws.inside_frame = 1;
|
2014-04-01 14:20:44 +08:00
|
|
|
if (n <= 0)
|
2013-02-23 10:50:10 +08:00
|
|
|
return n;
|
|
|
|
|
2015-12-26 10:07:17 +08:00
|
|
|
if (n == (int)len + pre) {
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
/* everything in the buffer was handled (or rebuffered...) */
|
|
|
|
wsi->u.ws.inside_frame = 0;
|
|
|
|
return orig_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* it is how many bytes of user buffer got sent... may be < orig_len
|
|
|
|
* in which case callback when writable has already been arranged
|
2015-12-04 08:43:54 +08:00
|
|
|
* and user code can call lws_write() again with the rest
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
* later.
|
|
|
|
*/
|
|
|
|
|
2015-12-26 10:07:17 +08:00
|
|
|
return n - pre;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
|
2013-01-22 07:20:08 +08:00
|
|
|
{
|
2015-12-16 18:19:08 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
unsigned long amount;
|
|
|
|
int n, m;
|
2013-01-22 07:20:08 +08:00
|
|
|
|
|
|
|
while (!lws_send_pipe_choked(wsi)) {
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->trunc_len) {
|
|
|
|
if (lws_issue_raw(wsi, wsi->trunc_alloc +
|
|
|
|
wsi->trunc_offset,
|
|
|
|
wsi->trunc_len) < 0) {
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
lwsl_info("%s: closing\n", __func__);
|
2014-04-10 14:25:24 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-12-09 14:16:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->u.http.filepos == wsi->u.http.filelen)
|
|
|
|
goto all_sent;
|
|
|
|
|
2015-12-11 13:12:58 +08:00
|
|
|
if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount,
|
2016-01-19 03:34:24 +08:00
|
|
|
pt->serv_buf,
|
|
|
|
LWS_MAX_SOCKET_IO_BUF) < 0)
|
2014-02-27 03:21:50 +01:00
|
|
|
return -1; /* caller will close */
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
|
|
|
|
n = (int)amount;
|
2014-02-27 03:21:50 +01:00
|
|
|
if (n) {
|
2015-12-06 05:52:09 +08:00
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
|
2016-02-15 20:36:02 +08:00
|
|
|
context->timeout_secs);
|
2014-10-18 12:23:05 +08:00
|
|
|
wsi->u.http.filepos += n;
|
2016-01-19 03:34:24 +08:00
|
|
|
m = lws_write(wsi, pt->serv_buf, n,
|
2015-12-06 05:52:09 +08:00
|
|
|
wsi->u.http.filepos == wsi->u.http.filelen ?
|
|
|
|
LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP);
|
2013-02-23 10:50:10 +08:00
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-04-02 14:25:10 +08:00
|
|
|
if (m != n)
|
2013-02-23 10:50:10 +08:00
|
|
|
/* adjust for what was not sent */
|
2015-12-17 17:03:59 +08:00
|
|
|
if (lws_plat_file_seek_cur(wsi, wsi->u.http.fd,
|
2015-12-10 12:50:10 +08:00
|
|
|
m - n) ==
|
|
|
|
(unsigned long)-1)
|
2014-11-30 13:00:47 +08:00
|
|
|
return -1;
|
2013-01-22 07:20:08 +08:00
|
|
|
}
|
2013-12-09 14:16:17 +08:00
|
|
|
all_sent:
|
2015-12-17 17:03:59 +08:00
|
|
|
if (!wsi->trunc_len && wsi->u.http.filepos == wsi->u.http.filelen) {
|
|
|
|
wsi->state = LWSS_HTTP;
|
2013-01-22 07:20:08 +08:00
|
|
|
|
2015-10-27 07:07:14 +08:00
|
|
|
/* we might be in keepalive, so close it off here */
|
2015-12-11 13:12:58 +08:00
|
|
|
lws_plat_file_close(wsi, wsi->u.http.fd);
|
2015-10-27 07:07:14 +08:00
|
|
|
wsi->u.http.fd = LWS_INVALID_FILE;
|
|
|
|
|
2013-01-22 07:20:08 +08:00
|
|
|
if (wsi->protocol->callback)
|
2013-06-29 10:24:16 +08:00
|
|
|
/* ignore callback returned value */
|
2016-01-20 16:56:06 +08:00
|
|
|
if (user_callback_handle_rxflow(
|
|
|
|
wsi->protocol->callback, wsi,
|
|
|
|
LWS_CALLBACK_HTTP_FILE_COMPLETION,
|
|
|
|
wsi->user_space, NULL, 0) < 0)
|
|
|
|
return -1;
|
2013-06-29 10:24:16 +08:00
|
|
|
return 1; /* >0 indicates completed */
|
2013-01-22 07:20:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 14:16:17 +08:00
|
|
|
lwsl_info("choked before able to send whole file (post)\n");
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2013-01-22 07:20:08 +08:00
|
|
|
|
2013-06-29 10:24:16 +08:00
|
|
|
return 0; /* indicates further processing must be done */
|
2013-01-22 07:20:08 +08:00
|
|
|
}
|
2014-04-06 06:26:35 +01:00
|
|
|
|
2015-11-08 12:10:26 +08:00
|
|
|
#if LWS_POSIX
|
2014-04-06 06:26:35 +01:00
|
|
|
LWS_VISIBLE int
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
|
2014-04-06 06:26:35 +01:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2015-10-23 08:10:55 +02:00
|
|
|
n = recv(wsi->sock, (char *)buf, len, 0);
|
2016-05-05 09:23:05 +08:00
|
|
|
if (n >= 0)
|
2014-04-10 14:08:10 +08:00
|
|
|
return n;
|
2015-11-08 12:10:26 +08:00
|
|
|
#if LWS_POSIX
|
2015-10-15 21:21:06 +08:00
|
|
|
if (LWS_ERRNO == LWS_EAGAIN ||
|
|
|
|
LWS_ERRNO == LWS_EWOULDBLOCK ||
|
|
|
|
LWS_ERRNO == LWS_EINTR)
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
2015-11-02 20:34:12 +08:00
|
|
|
#endif
|
2014-04-10 14:08:10 +08:00
|
|
|
lwsl_warn("error on reading from skt\n");
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2014-04-06 06:26:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
|
2014-04-06 06:26:35 +01:00
|
|
|
{
|
2015-11-08 12:10:26 +08:00
|
|
|
int n = 0;
|
2015-11-02 20:34:12 +08:00
|
|
|
|
|
|
|
#if LWS_POSIX
|
2015-10-23 08:10:55 +02:00
|
|
|
n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL);
|
2016-01-27 08:50:31 +08:00
|
|
|
// lwsl_info("%s: sent len %d result %d", __func__, len, n);
|
2014-04-10 14:08:10 +08:00
|
|
|
if (n >= 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (LWS_ERRNO == LWS_EAGAIN ||
|
|
|
|
LWS_ERRNO == LWS_EWOULDBLOCK ||
|
|
|
|
LWS_ERRNO == LWS_EINTR) {
|
|
|
|
if (LWS_ERRNO == LWS_EWOULDBLOCK)
|
|
|
|
lws_set_blocking_send(wsi);
|
2014-04-06 06:26:35 +01:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
2015-11-02 20:34:12 +08:00
|
|
|
#else
|
|
|
|
(void)n;
|
|
|
|
(void)wsi;
|
|
|
|
(void)buf;
|
|
|
|
(void)len;
|
|
|
|
// !!!
|
|
|
|
#endif
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2016-01-26 20:56:56 +08:00
|
|
|
lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n", len, wsi->sock, n, LWS_ERRNO);
|
2014-04-10 14:08:10 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2014-04-06 06:26:35 +01:00
|
|
|
}
|
2015-11-08 12:10:26 +08:00
|
|
|
#endif
|
2015-08-19 16:23:33 +02:00
|
|
|
LWS_VISIBLE int
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_ssl_pending_no_ssl(struct lws *wsi)
|
2015-08-19 16:23:33 +02:00
|
|
|
{
|
2015-11-02 13:10:33 +08:00
|
|
|
(void)wsi;
|
2015-08-19 16:23:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|