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

ss: handle rx and tx return values properly

You can disconnect the stream by returning -1 from tx().  You can
give up your chance to send anything by returning 1 from tx().
Returning 0 sends `*len` amount of the provided buffer.

Returning <0 from rx() also disconnects the stream.
This commit is contained in:
Andy Green 2020-05-04 14:34:45 +01:00
parent a59aaf6af9
commit 85dc0883a2
4 changed files with 41 additions and 12 deletions

View file

@ -13,6 +13,17 @@ creation, but able to be updated from a remote copy.
![overview](../doc-assets/ss-explain.png)
## Convention for rx and tx callback return
Function|Return|Meaning
---|---|---
tx|0|Send the amount of `buf` stored in `*len`
tx|>0|Do not send anything
tx|<0|Finished with stream
rx|>=0|accepted
rx|<0|Finished with stream
# JSON Policy Database
Example JSON policy... formatting is shown for clarity but whitespace can be

View file

@ -162,7 +162,7 @@ secstream_h1(struct lws *wsi, enum lws_callback_reasons reason, void *user,
lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
uint8_t buf[LWS_PRE + 1520], *p = &buf[LWS_PRE],
*end = &buf[sizeof(buf) - 1];
int f = 0, m, status;
int f = 0, m, status, txr;
size_t buflen;
switch (reason) {
@ -398,7 +398,8 @@ malformed:
// lwsl_notice("%s: HTTP_READ: client side sent len %d fl 0x%x\n",
// __func__, (int)len, (int)f);
h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, f);
if (h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, f) < 0)
return -1;
return 0; /* don't passthru */
@ -455,7 +456,12 @@ malformed:
#endif
if (h->info.tx(ss_to_userobj(h), h->txord++, p, &buflen, &f)) {
txr = h->info.tx(ss_to_userobj(h), h->txord++, p, &buflen, &f);
if (txr < 0) {
lwsl_debug("%s: tx handler asked to close\n", __func__);
return -1;
}
if (txr > 0) {
/* don't want to send anything */
lwsl_debug("%s: dont want to write\n", __func__);
return 0;

View file

@ -31,8 +31,8 @@ secstream_mqtt(struct lws *wsi, enum lws_callback_reasons reason, void *user,
lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
lws_mqtt_publish_param_t mqpp, *pmqpp;
uint8_t buf[LWS_PRE + 1400];
int f = 0, txr;
size_t buflen;
int f = 0;
switch (reason) {
@ -93,8 +93,9 @@ secstream_mqtt(struct lws *wsi, enum lws_callback_reasons reason, void *user,
h->subseq = 1;
h->info.rx(ss_to_userobj(h), (const uint8_t *)pmqpp->payload,
len, f);
if (h->info.rx(ss_to_userobj(h), (const uint8_t *)pmqpp->payload,
len, f) < 0)
return -1;
return 0; /* don't passthru */
@ -143,8 +144,13 @@ secstream_mqtt(struct lws *wsi, enum lws_callback_reasons reason, void *user,
buflen = sizeof(buf) - LWS_PRE;
if (h->info.tx(ss_to_userobj(h), h->txord++, buf + LWS_PRE,
&buflen, &f))
txr = h->info.tx(ss_to_userobj(h), h->txord++, buf + LWS_PRE,
&buflen, &f);
if (txr < 0) {
lwsl_debug("%s: tx handler asked to close\n", __func__);
return -1;
}
if (txr > 0)
/* don't want to send anything */
return 0;

View file

@ -30,8 +30,8 @@ secstream_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user,
{
lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
uint8_t buf[LWS_PRE + 1400];
int f = 0, f1, txr;
size_t buflen;
int f = 0, f1;
switch (reason) {
@ -83,7 +83,8 @@ secstream_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user,
h->subseq = 1;
h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, f);
if (h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, f) < 0)
return -1;
return 0; /* don't passthru */
@ -98,8 +99,13 @@ secstream_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user,
}
buflen = sizeof(buf) - LWS_PRE;
if (h->info.tx(ss_to_userobj(h), h->txord++, buf + LWS_PRE,
&buflen, &f))
txr = h->info.tx(ss_to_userobj(h), h->txord++, buf + LWS_PRE,
&buflen, &f);
if (txr < 0) {
lwsl_debug("%s: tx handler asked to close\n", __func__);
return -1;
}
if (txr > 0)
/* don't want to send anything */
return 0;