2013-01-16 12:21:29 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2014-03-23 11:21:51 +08:00
|
|
|
* Copyright (C) 2010-2014 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
|
|
|
|
libwebsocket_0405_frame_mask_generate(struct libwebsocket *wsi)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* fetch the per-frame nonce */
|
|
|
|
|
|
|
|
n = libwebsockets_get_random(wsi->protocol->owning_server,
|
2013-02-11 17:13:32 +08:00
|
|
|
wsi->u.ws.frame_masking_nonce_04, 4);
|
2013-01-16 12:21:29 +08:00
|
|
|
if (n != 4) {
|
|
|
|
lwsl_parser("Unable to read from random device %s %d\n",
|
|
|
|
SYSTEM_RANDOM_FILEPATH, n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start masking from first byte of masking key buffer */
|
2013-01-21 11:04:23 +08:00
|
|
|
wsi->u.ws.frame_mask_index = 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
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int m;
|
|
|
|
int start;
|
2013-01-16 14:35:27 +08:00
|
|
|
unsigned char *buf = (unsigned char *)vbuf;
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2013-01-16 12:21:29 +08:00
|
|
|
int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len)
|
|
|
|
{
|
2013-01-29 12:37:35 +08:00
|
|
|
struct libwebsocket_context *context = wsi->protocol->owning_server;
|
2013-01-16 12:21:29 +08:00
|
|
|
int n;
|
2013-12-10 21:15:00 +08:00
|
|
|
size_t real_len = len;
|
2013-01-16 12:21:29 +08:00
|
|
|
int m;
|
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 */
|
|
|
|
if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
|
|
|
|
!wsi->truncated_send_len)
|
|
|
|
return len;
|
2013-12-09 14:16:17 +08:00
|
|
|
|
2014-03-23 11:41:15 +08:00
|
|
|
if (wsi->truncated_send_len && (buf < wsi->truncated_send_malloc ||
|
2014-03-23 11:21:51 +08:00
|
|
|
buf > (wsi->truncated_send_malloc +
|
|
|
|
wsi->truncated_send_len +
|
|
|
|
wsi->truncated_send_offset))) {
|
|
|
|
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
|
|
|
|
2014-04-02 19:45:42 +08:00
|
|
|
m = lws_ext_callback_for_each_active(wsi,
|
|
|
|
LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, &buf, len);
|
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
if (m) /* handled */ {
|
|
|
|
n = m;
|
|
|
|
goto handle_truncated_send;
|
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 (wsi->sock < 0)
|
|
|
|
lwsl_warn("** error invalid sock but expected to send\n");
|
2013-01-16 12:21:29 +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);
|
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n, 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
|
|
|
*/
|
2014-03-23 11:41:15 +08:00
|
|
|
if (wsi->truncated_send_len) {
|
2014-03-23 11:21:51 +08:00
|
|
|
lwsl_info("***** %x partial send moved on by %d (vs %d)\n",
|
|
|
|
wsi, n, real_len);
|
2013-12-09 14:16:17 +08:00
|
|
|
wsi->truncated_send_offset += n;
|
|
|
|
wsi->truncated_send_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
|
|
|
|
2013-12-09 14:16:17 +08:00
|
|
|
if (!wsi->truncated_send_len) {
|
|
|
|
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;
|
2014-04-10 17:06:59 +08:00
|
|
|
if (wsi->state == WSI_STATE_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 */
|
|
|
|
libwebsocket_callback_on_writable(
|
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
|
|
|
wsi->protocol->owning_server, wsi);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
if (n == real_len)
|
|
|
|
/* 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
|
|
|
if (n && wsi->u.ws.clean_buffer)
|
2014-03-23 11:41:15 +08:00
|
|
|
/*
|
2014-04-10 14:08:10 +08:00
|
|
|
* This buffer unaffected by extension rewriting.
|
|
|
|
* It means the user code is expected to deal with
|
|
|
|
* partial sends. (lws knows the header was already
|
|
|
|
* sent, so on next send will just resume sending
|
|
|
|
* payload)
|
2014-03-23 11:41:15 +08:00
|
|
|
*/
|
2014-04-10 14:08:10 +08:00
|
|
|
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)
|
|
|
|
*/
|
|
|
|
lwsl_info("***** %x 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
|
|
|
|
*/
|
|
|
|
if (!wsi->truncated_send_malloc ||
|
|
|
|
real_len - n > wsi->truncated_send_allocation) {
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_free(wsi->truncated_send_malloc);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
|
|
|
wsi->truncated_send_allocation = real_len - n;
|
2014-12-04 23:59:35 +01:00
|
|
|
wsi->truncated_send_malloc = lws_malloc(real_len - n);
|
2014-04-10 14:08:10 +08:00
|
|
|
if (!wsi->truncated_send_malloc) {
|
|
|
|
lwsl_err("truncated send: unable to malloc %d\n",
|
|
|
|
real_len - n);
|
|
|
|
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
|
|
|
}
|
2014-04-10 14:08:10 +08:00
|
|
|
wsi->truncated_send_offset = 0;
|
|
|
|
wsi->truncated_send_len = real_len - n;
|
|
|
|
memcpy(wsi->truncated_send_malloc, buf + n, real_len - n);
|
|
|
|
|
|
|
|
/* since something buffered, force it to get another chance to send */
|
|
|
|
libwebsocket_callback_on_writable(wsi->protocol->owning_server, 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* libwebsocket_write() - Apply protocol then write data to client
|
|
|
|
* @wsi: Websocket instance (available from user callback)
|
|
|
|
* @buf: The data to send. For data being sent on a websocket
|
|
|
|
* connection (ie, not default http), this buffer MUST have
|
|
|
|
* LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
|
|
|
|
* and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
|
|
|
|
* in the buffer after (buf + len). This is so the protocol
|
|
|
|
* header and trailer data can be added in-situ.
|
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
2013-03-30 09:52:21 +08:00
|
|
|
LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
|
2013-01-16 12:21:29 +08:00
|
|
|
size_t len, enum libwebsocket_write_protocol protocol)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int pre = 0;
|
|
|
|
int post = 0;
|
2013-01-21 09:53:35 +08:00
|
|
|
int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT;
|
2013-01-16 12:21:29 +08:00
|
|
|
unsigned char *dropmask = NULL;
|
|
|
|
unsigned char is_masked_bit = 0;
|
2013-02-23 10:50:10 +08:00
|
|
|
size_t orig_len = len;
|
2013-01-16 12:21:29 +08:00
|
|
|
struct lws_tokens eff_buf;
|
|
|
|
|
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 (len == 0 && protocol != LWS_WRITE_CLOSE &&
|
|
|
|
protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) {
|
2013-01-16 12:21:29 +08:00
|
|
|
lwsl_warn("zero length libwebsocket_write attempt\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-18 12:23:05 +08:00
|
|
|
if (protocol == LWS_WRITE_HTTP ||
|
|
|
|
protocol == LWS_WRITE_HTTP_FINAL ||
|
|
|
|
protocol == LWS_WRITE_HTTP_HEADERS)
|
2013-01-16 12:21:29 +08:00
|
|
|
goto send_raw;
|
|
|
|
|
|
|
|
/* websocket protocol, either binary or text */
|
|
|
|
|
|
|
|
if (wsi->state != WSI_STATE_ESTABLISHED)
|
|
|
|
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
|
|
|
/* if we are continuing a frame that already had its header done */
|
|
|
|
|
|
|
|
if (wsi->u.ws.inside_frame)
|
|
|
|
goto do_more_inside_frame;
|
|
|
|
|
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
|
|
|
|
* pre-TX mangling is not allowed to truncate
|
|
|
|
*/
|
2013-01-16 12:21:29 +08:00
|
|
|
eff_buf.token = (char *)buf;
|
|
|
|
eff_buf.token_len = len;
|
|
|
|
|
2013-01-17 14:46:43 +08:00
|
|
|
switch (protocol) {
|
|
|
|
case LWS_WRITE_PING:
|
|
|
|
case LWS_WRITE_PONG:
|
|
|
|
case LWS_WRITE_CLOSE:
|
|
|
|
break;
|
|
|
|
default:
|
2014-04-02 19:45:42 +08:00
|
|
|
if (lws_ext_callback_for_each_active(wsi,
|
|
|
|
LWS_EXT_CALLBACK_PAYLOAD_TX, &eff_buf, 0) < 0)
|
|
|
|
return -1;
|
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
|
|
|
|
*/
|
|
|
|
if ((char *)buf != eff_buf.token)
|
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;
|
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:
|
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
|
|
|
if (masked7) {
|
|
|
|
pre += 4;
|
|
|
|
dropmask = &buf[0 - pre];
|
|
|
|
is_masked_bit = 0x80;
|
|
|
|
}
|
2013-01-21 09:53:35 +08:00
|
|
|
|
2013-01-16 12:21:29 +08:00
|
|
|
switch (protocol & 0xf) {
|
|
|
|
case LWS_WRITE_TEXT:
|
2013-01-21 09:53:35 +08:00
|
|
|
n = LWS_WS_OPCODE_07__TEXT_FRAME;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
case LWS_WRITE_BINARY:
|
2013-01-21 09:53:35 +08:00
|
|
|
n = LWS_WS_OPCODE_07__BINARY_FRAME;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
case LWS_WRITE_CONTINUATION:
|
2013-01-21 09:53:35 +08:00
|
|
|
n = LWS_WS_OPCODE_07__CONTINUATION;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_WRITE_CLOSE:
|
2013-01-21 09:53:35 +08:00
|
|
|
n = LWS_WS_OPCODE_07__CLOSE;
|
2013-01-16 12:21:29 +08:00
|
|
|
|
|
|
|
/*
|
2013-01-21 09:53:35 +08:00
|
|
|
* 06+ has a 2-byte status code in network order
|
|
|
|
* we can do this because we demand post-buf
|
2013-01-16 12:21:29 +08:00
|
|
|
*/
|
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
if (wsi->u.ws.close_reason) {
|
2013-01-21 09:53:35 +08:00
|
|
|
/* reason codes count as data bytes */
|
|
|
|
buf -= 2;
|
2013-01-21 11:04:23 +08:00
|
|
|
buf[0] = wsi->u.ws.close_reason >> 8;
|
|
|
|
buf[1] = wsi->u.ws.close_reason;
|
2013-01-21 09:53:35 +08:00
|
|
|
len += 2;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LWS_WRITE_PING:
|
2013-01-21 09:53:35 +08:00
|
|
|
n = LWS_WS_OPCODE_07__PING;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
case LWS_WRITE_PONG:
|
2013-01-21 09:53:35 +08:00
|
|
|
n = LWS_WS_OPCODE_07__PONG;
|
2013-01-16 12:21:29 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_warn("lws_write: unknown write opc / protocol\n");
|
2013-01-16 12:21:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(protocol & LWS_WRITE_NO_FIN))
|
|
|
|
n |= 1 << 7;
|
|
|
|
|
|
|
|
if (len < 126) {
|
|
|
|
pre += 2;
|
|
|
|
buf[-pre] = n;
|
|
|
|
buf[-pre + 1] = len | is_masked_bit;
|
|
|
|
} else {
|
|
|
|
if (len < 65536) {
|
|
|
|
pre += 4;
|
|
|
|
buf[-pre] = n;
|
|
|
|
buf[-pre + 1] = 126 | is_masked_bit;
|
|
|
|
buf[-pre + 2] = len >> 8;
|
|
|
|
buf[-pre + 3] = len;
|
|
|
|
} 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
|
|
|
|
buf[-pre + 6] = len >> 24;
|
|
|
|
buf[-pre + 7] = len >> 16;
|
|
|
|
buf[-pre + 8] = len >> 8;
|
|
|
|
buf[-pre + 9] = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
* the protocol demands it
|
|
|
|
*/
|
|
|
|
|
2013-01-21 09:53:35 +08:00
|
|
|
if (wsi->mode == LWS_CONNMODE_WS_CLIENT) {
|
|
|
|
|
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)
|
|
|
|
if (libwebsocket_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++)
|
|
|
|
dropmask[n] = dropmask[n] ^
|
2013-02-11 17:13:32 +08:00
|
|
|
wsi->u.ws.frame_masking_nonce_04[
|
|
|
|
(wsi->u.ws.frame_mask_index++) & 3];
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2013-01-21 09:53:35 +08:00
|
|
|
/* copy the frame nonce into place */
|
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
|
|
|
memcpy(dropmask, wsi->u.ws.frame_masking_nonce_04, 4);
|
2013-12-21 10:22:17 +08:00
|
|
|
}
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
send_raw:
|
2013-01-17 14:46:43 +08:00
|
|
|
switch (protocol) {
|
|
|
|
case LWS_WRITE_CLOSE:
|
2013-02-11 17:13:32 +08:00
|
|
|
/* lwsl_hexdump(&buf[-pre], len + post); */
|
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
|
2014-10-08 12:00:53 +08:00
|
|
|
if (wsi->mode == LWS_CONNMODE_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;
|
2014-10-12 08:38:16 +08:00
|
|
|
if (protocol == 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
|
|
|
}
|
2014-10-18 12:23:05 +08:00
|
|
|
|
|
|
|
if ((protocol == LWS_WRITE_HTTP || protocol == LWS_WRITE_HTTP_FINAL) && wsi->u.http.content_length) {
|
|
|
|
wsi->u.http.content_remain -= len;
|
|
|
|
lwsl_info("%s: content_remain = %lu\n", __func__, wsi->u.http.content_remain);
|
|
|
|
if (!wsi->u.http.content_remain) {
|
|
|
|
lwsl_info("%s: selecting final write mode\n", __func__);
|
|
|
|
protocol = LWS_WRITE_HTTP_FINAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-19 07:36:20 +08:00
|
|
|
if (protocol == 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;
|
|
|
|
}
|
|
|
|
|
2014-10-12 08:38:16 +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
|
2013-02-23 10:50:10 +08:00
|
|
|
return lws_issue_raw(wsi, (unsigned char *)buf - pre,
|
|
|
|
len + pre + post);
|
2013-01-17 14:46:43 +08:00
|
|
|
default:
|
|
|
|
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
|
|
|
wsi->u.ws.inside_frame = 1;
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2013-02-23 10:50:10 +08:00
|
|
|
n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre + post);
|
2014-04-01 14:20:44 +08:00
|
|
|
if (n <= 0)
|
2013-02-23 10:50:10 +08:00
|
|
|
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
|
|
|
if (n == len + pre + post) {
|
|
|
|
/* 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
|
|
|
|
* and user code can call libwebsocket_write() again with the rest
|
|
|
|
* later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return n - (pre + post);
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
2013-03-30 09:52:21 +08:00
|
|
|
LWS_VISIBLE int libwebsockets_serve_http_file_fragment(
|
2013-02-11 17:13:32 +08:00
|
|
|
struct libwebsocket_context *context, struct libwebsocket *wsi)
|
2013-01-22 07:20:08 +08:00
|
|
|
{
|
2014-02-27 03:21:50 +01:00
|
|
|
int n;
|
|
|
|
int m;
|
2013-01-22 07:20:08 +08:00
|
|
|
|
|
|
|
while (!lws_send_pipe_choked(wsi)) {
|
2013-12-09 14:16:17 +08:00
|
|
|
|
2014-03-23 11:41:15 +08:00
|
|
|
if (wsi->truncated_send_len) {
|
2014-04-10 14:25:24 +08:00
|
|
|
if (lws_issue_raw(wsi, wsi->truncated_send_malloc +
|
2013-12-09 14:16:17 +08:00
|
|
|
wsi->truncated_send_offset,
|
2014-04-10 14:25:24 +08:00
|
|
|
wsi->truncated_send_len) < 0) {
|
|
|
|
lwsl_info("closing from libwebsockets_serve_http_file_fragment\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-09 14:16:17 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->u.http.filepos == wsi->u.http.filelen)
|
|
|
|
goto all_sent;
|
|
|
|
|
2014-04-02 14:25:10 +08:00
|
|
|
compatible_file_read(n, wsi->u.http.fd, context->service_buffer,
|
2013-02-11 17:13:32 +08:00
|
|
|
sizeof(context->service_buffer));
|
2014-02-27 03:21:50 +01:00
|
|
|
if (n < 0)
|
|
|
|
return -1; /* caller will close */
|
|
|
|
if (n) {
|
2014-10-18 12:23:05 +08:00
|
|
|
wsi->u.http.filepos += n;
|
2013-02-23 10:50:10 +08:00
|
|
|
m = libwebsocket_write(wsi, context->service_buffer, n,
|
2014-10-18 12:23:05 +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 */
|
2014-11-30 13:00:47 +08:00
|
|
|
if (compatible_file_seek_cur(wsi->u.http.fd, m - n) < 0)
|
|
|
|
return -1;
|
2013-01-22 07:20:08 +08:00
|
|
|
}
|
2013-12-09 14:16:17 +08:00
|
|
|
all_sent:
|
2014-03-23 11:41:15 +08:00
|
|
|
if (!wsi->truncated_send_len &&
|
2013-12-09 14:16:17 +08:00
|
|
|
wsi->u.http.filepos == wsi->u.http.filelen) {
|
2013-01-22 07:20:08 +08:00
|
|
|
wsi->state = WSI_STATE_HTTP;
|
|
|
|
|
|
|
|
if (wsi->protocol->callback)
|
2013-06-29 10:24:16 +08:00
|
|
|
/* ignore callback returned value */
|
|
|
|
user_callback_handle_rxflow(
|
2013-02-11 17:13:32 +08:00
|
|
|
wsi->protocol->callback, context, wsi,
|
|
|
|
LWS_CALLBACK_HTTP_FILE_COMPLETION,
|
|
|
|
wsi->user_space, NULL, 0);
|
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");
|
2013-01-22 07:20:08 +08:00
|
|
|
libwebsocket_callback_on_writable(context, wsi);
|
|
|
|
|
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
|
|
|
|
|
|
|
LWS_VISIBLE int
|
2014-10-09 08:14:30 +08:00
|
|
|
lws_ssl_capable_read_no_ssl(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, unsigned char *buf, int len)
|
2014-04-06 06:26:35 +01:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = recv(wsi->sock, buf, len, 0);
|
2014-04-10 14:08:10 +08:00
|
|
|
if (n >= 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
lwsl_warn("error on reading from skt\n");
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2014-04-06 06:26:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_capable_write_no_ssl(struct libwebsocket *wsi, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2014-04-06 12:41:20 +01:00
|
|
|
n = send(wsi->sock, buf, len, 0);
|
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;
|
|
|
|
}
|
|
|
|
lwsl_debug("ERROR writing len %d to skt %d\n", len, n);
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2014-04-06 06:26:35 +01:00
|
|
|
}
|