2013-01-16 12:21:29 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2017-09-23 12:55:21 +08:00
|
|
|
* Copyright (C) 2010-2017 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
|
|
|
|
*/
|
|
|
|
|
2018-05-03 10:49:36 +08:00
|
|
|
#include "core/private.h"
|
2013-01-16 12:21:29 +08:00
|
|
|
|
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);
|
2017-05-07 10:02:03 +08:00
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
2013-12-10 21:15:00 +08:00
|
|
|
size_t real_len = len;
|
2016-03-18 23:55:59 +08:00
|
|
|
unsigned int n;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
// lwsl_hexdump_err(buf, len);
|
2018-03-27 09:17:19 +08:00
|
|
|
|
2017-12-07 07:20:47 +08:00
|
|
|
/*
|
|
|
|
* Detect if we got called twice without going through the
|
|
|
|
* event loop to handle pending. This would be caused by either
|
|
|
|
* back-to-back writes in one WRITABLE (illegal) or calling lws_write()
|
|
|
|
* from outside the WRITABLE callback (illegal).
|
|
|
|
*/
|
|
|
|
if (wsi->could_have_pending) {
|
|
|
|
lwsl_hexdump_level(LLL_ERR, buf, len);
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_err("** %p: vh: %s, prot: %s, role %s: "
|
2017-12-07 07:20:47 +08:00
|
|
|
"Illegal back-to-back write of %lu detected...\n",
|
|
|
|
wsi, wsi->vhost->name, wsi->protocol->name,
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->role_ops->name,
|
2017-12-07 07:20:47 +08:00
|
|
|
(unsigned long)len);
|
2018-04-13 06:43:11 +08:00
|
|
|
// assert(0);
|
2017-12-07 07:20:47 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_WRITE, 1);
|
|
|
|
|
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 */
|
2018-04-02 11:55:17 +08:00
|
|
|
if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE && !wsi->trunc_len)
|
2017-10-25 08:00:23 +08:00
|
|
|
return (int)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 ||
|
2016-07-23 14:18:25 +08:00
|
|
|
buf > (wsi->trunc_alloc + wsi->trunc_len + wsi->trunc_offset))) {
|
2017-12-07 07:20:47 +08:00
|
|
|
lwsl_hexdump_level(LLL_ERR, buf, len);
|
|
|
|
lwsl_err("** %p: vh: %s, prot: %s, Sending new %lu, pending truncated ...\n"
|
2017-10-28 07:42:44 +08:00
|
|
|
" It's illegal to do an lws_write outside of\n"
|
|
|
|
" the writable callback: fix your code\n",
|
2017-12-07 07:20:47 +08:00
|
|
|
wsi, wsi->vhost->name, wsi->protocol->name,
|
|
|
|
(unsigned long)len);
|
2013-12-09 14:16:17 +08:00
|
|
|
assert(0);
|
2016-04-14 15:07:44 +08:00
|
|
|
|
|
|
|
return -1;
|
2013-12-09 14:16:17 +08:00
|
|
|
}
|
2018-04-20 10:33:23 +08:00
|
|
|
|
2017-10-13 10:33:02 +08:00
|
|
|
if (!wsi->http2_substream && !lws_socket_is_valid(wsi->desc.sockfd))
|
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
|
|
|
|
2016-03-18 23:55:59 +08:00
|
|
|
/* limit sending */
|
2017-03-16 10:46:31 +08:00
|
|
|
if (wsi->protocol->tx_packet_size)
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)wsi->protocol->tx_packet_size;
|
2017-03-16 10:46:31 +08:00
|
|
|
else {
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)wsi->protocol->rx_buffer_size;
|
2017-03-16 10:46:31 +08:00
|
|
|
if (!n)
|
|
|
|
n = context->pt_serv_buf_size;
|
|
|
|
}
|
2016-05-15 08:59:48 +08:00
|
|
|
n += LWS_PRE + 4;
|
2016-03-18 23:55:59 +08:00
|
|
|
if (n > len)
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)len;
|
2016-03-18 15:02:27 +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);
|
2016-03-18 23:55:59 +08:00
|
|
|
n = lws_ssl_capable_write(wsi, buf, n);
|
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n, n == len);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2017-12-07 07:20:47 +08:00
|
|
|
/* something got written, it can have been truncated now */
|
|
|
|
wsi->could_have_pending = 1;
|
|
|
|
|
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:
|
2017-12-07 07:20:47 +08:00
|
|
|
/*
|
|
|
|
* nothing got sent, not fatal. Retry the whole thing later,
|
|
|
|
* ie, implying treat it was a truncated send so it gets
|
|
|
|
* retried
|
|
|
|
*/
|
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
|
|
|
}
|
2018-04-20 10:33:23 +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-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) {
|
2017-02-05 22:07:34 +08:00
|
|
|
lwsl_info("%p partial adv %d (vs %ld)\n", wsi, n, (long)real_len);
|
2015-12-17 17:03:59 +08:00
|
|
|
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) {
|
2017-10-28 07:42:44 +08:00
|
|
|
lwsl_info("** %p partial send completed\n", wsi);
|
2014-03-23 11:41:15 +08:00
|
|
|
/* done with it, but don't free it */
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)real_len;
|
2018-04-02 11:55:17 +08:00
|
|
|
if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE) {
|
2017-10-28 07:42:44 +08:00
|
|
|
lwsl_info("** %p signalling to close now\n", wsi);
|
2014-04-10 14:25:24 +08:00
|
|
|
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
|
2017-12-07 07:20:47 +08:00
|
|
|
* first priority next time the socket is writable).
|
2014-04-10 14:08:10 +08:00
|
|
|
*/
|
2017-05-07 08:19:55 +08:00
|
|
|
lwsl_debug("%p new partial sent %d from %lu total\n", wsi, n,
|
2017-02-04 13:09:00 +01:00
|
|
|
(unsigned long)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
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITE_PARTIALS, 1);
|
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, n);
|
|
|
|
|
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
|
|
|
|
2017-10-25 08:00:23 +08:00
|
|
|
wsi->trunc_alloc_len = (unsigned int)(real_len - n);
|
2017-10-28 07:42:44 +08:00
|
|
|
wsi->trunc_alloc = lws_malloc(real_len - n,
|
|
|
|
"truncated send alloc");
|
2015-12-17 17:03:59 +08:00
|
|
|
if (!wsi->trunc_alloc) {
|
2017-02-04 13:09:00 +01:00
|
|
|
lwsl_err("truncated send: unable to malloc %lu\n",
|
|
|
|
(unsigned long)(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;
|
2017-10-25 08:00:23 +08:00
|
|
|
wsi->trunc_len = (unsigned int)(real_len - n);
|
2015-12-17 17:03:59 +08:00
|
|
|
memcpy(wsi->trunc_alloc, buf + n, real_len - n);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2018-04-12 15:56:38 +08:00
|
|
|
#if !defined(LWS_WITH_ESP32)
|
2018-03-24 08:07:00 +08:00
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
|
|
|
/* stash original destination for fulfilling UDP partials */
|
|
|
|
wsi->udp->sa_pending = wsi->udp->sa;
|
|
|
|
wsi->udp->salen_pending = wsi->udp->salen;
|
|
|
|
}
|
2018-04-12 15:56:38 +08:00
|
|
|
#endif
|
2018-03-24 08:07:00 +08:00
|
|
|
|
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
|
|
|
|
2017-10-25 08:00:23 +08:00
|
|
|
return (int)real_len;
|
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];
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2017-07-19 04:39:14 +08:00
|
|
|
if (wsi->parent_carries_io) {
|
|
|
|
struct lws_write_passthru pas;
|
|
|
|
|
|
|
|
pas.buf = buf;
|
|
|
|
pas.len = len;
|
|
|
|
pas.wp = wp;
|
|
|
|
pas.wsi = wsi;
|
|
|
|
|
|
|
|
if (wsi->parent->protocol->callback(wsi->parent,
|
|
|
|
LWS_CALLBACK_CHILD_WRITE_VIA_PARENT,
|
|
|
|
wsi->parent->user_space,
|
|
|
|
(void *)&pas, 0))
|
|
|
|
return 1;
|
|
|
|
|
2017-10-25 08:00:23 +08:00
|
|
|
return (int)len;
|
2017-07-19 04:39:14 +08:00
|
|
|
}
|
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_LWS_WRITE, 1);
|
|
|
|
|
2017-05-08 10:49:10 +08:00
|
|
|
if ((int)len < 0) {
|
|
|
|
lwsl_err("%s: suspicious len int %d, ulong %lu\n", __func__,
|
|
|
|
(int)len, (unsigned long)len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_WRITE, len);
|
|
|
|
|
2016-04-15 12:00:23 +08:00
|
|
|
#ifdef LWS_WITH_ACCESS_LOG
|
2018-04-27 19:16:50 +08:00
|
|
|
wsi->http.access_log.sent += len;
|
2016-04-15 12:00:23 +08:00
|
|
|
#endif
|
2016-04-15 14:01:29 +08:00
|
|
|
if (wsi->vhost)
|
2016-12-12 13:36:25 +08:00
|
|
|
wsi->vhost->conn_stats.tx += len;
|
2016-04-15 12:00:23 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
assert(wsi->role_ops);
|
|
|
|
if (!wsi->role_ops->write_role_protocol)
|
|
|
|
return lws_issue_raw(wsi, buf, len);
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return wsi->role_ops->write_role_protocol(wsi, buf, len, &wp);
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-05-07 10:02:03 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2018-04-12 15:56:38 +08:00
|
|
|
int n = 0;
|
2014-04-06 06:26:35 +01:00
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(context, pt, LWSSTATS_C_API_READ, 1);
|
|
|
|
|
2018-03-24 08:07:00 +08:00
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
2018-04-12 15:56:38 +08:00
|
|
|
#if !defined(LWS_WITH_ESP32)
|
2018-03-24 08:07:00 +08:00
|
|
|
wsi->udp->salen = sizeof(wsi->udp->sa);
|
|
|
|
n = recvfrom(wsi->desc.sockfd, (char *)buf, len, 0,
|
|
|
|
&wsi->udp->sa, &wsi->udp->salen);
|
2018-04-12 15:56:38 +08:00
|
|
|
#endif
|
2018-03-24 08:07:00 +08:00
|
|
|
} else
|
|
|
|
n = recv(wsi->desc.sockfd, (char *)buf, len, 0);
|
|
|
|
|
2016-05-05 09:06:09 +08:00
|
|
|
if (n >= 0) {
|
2016-04-15 14:01:29 +08:00
|
|
|
if (wsi->vhost)
|
2016-12-12 13:36:25 +08:00
|
|
|
wsi->vhost->conn_stats.rx += n;
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(context, pt, LWSSTATS_B_READ, n);
|
2018-03-24 08:07:00 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return n;
|
2016-04-15 14:01:29 +08:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
|
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;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2016-05-05 09:06:09 +08:00
|
|
|
lwsl_notice("error on reading from skt : %d\n", LWS_ERRNO);
|
2014-04-10 14:08:10 +08:00
|
|
|
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
|
|
|
|
2018-03-24 08:07:00 +08:00
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
2018-04-12 15:56:38 +08:00
|
|
|
#if !defined(LWS_WITH_ESP32)
|
2018-03-24 08:07:00 +08:00
|
|
|
if (wsi->trunc_len)
|
|
|
|
n = sendto(wsi->desc.sockfd, buf, len, 0, &wsi->udp->sa_pending, wsi->udp->salen_pending);
|
|
|
|
else
|
|
|
|
n = sendto(wsi->desc.sockfd, buf, len, 0, &wsi->udp->sa, wsi->udp->salen);
|
2018-04-12 15:56:38 +08:00
|
|
|
#endif
|
2018-03-24 08:07:00 +08:00
|
|
|
} else
|
|
|
|
n = send(wsi->desc.sockfd, (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) {
|
2017-02-18 17:26:40 +08:00
|
|
|
if (LWS_ERRNO == LWS_EWOULDBLOCK) {
|
2014-04-10 14:08:10 +08:00
|
|
|
lws_set_blocking_send(wsi);
|
2017-02-18 17:26:40 +08:00
|
|
|
}
|
2014-04-06 06:26:35 +01:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2017-09-23 12:55:21 +08:00
|
|
|
lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n",
|
|
|
|
len, wsi->desc.sockfd, n, LWS_ERRNO);
|
2018-03-11 11:26:06 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2014-04-06 06:26:35 +01:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
|
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;
|
2017-05-07 08:19:55 +08:00
|
|
|
#if defined(LWS_WITH_ESP32)
|
|
|
|
return 100;
|
|
|
|
#else
|
2017-05-03 21:28:26 +08:00
|
|
|
return 0;
|
2017-05-07 08:19:55 +08:00
|
|
|
#endif
|
2015-08-19 16:23:33 +02:00
|
|
|
}
|