mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-16 00:00:07 +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:
parent
3d995cf7c5
commit
2cc0a7f6f6
5 changed files with 50 additions and 15 deletions
|
@ -13,6 +13,17 @@ creation, but able to be updated from a remote copy.
|
|||
|
||||

|
||||
|
||||
## 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
|
||||
|
|
|
@ -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) {
|
||||
|
@ -410,7 +410,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 */
|
||||
|
||||
|
@ -467,7 +468,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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ secstream_raw(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;
|
||||
int f = 0, txr;
|
||||
size_t buflen;
|
||||
|
||||
switch (reason) {
|
||||
|
@ -78,7 +78,8 @@ secstream_raw(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|||
if (!h)
|
||||
return 0;
|
||||
|
||||
h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, 0);
|
||||
if (h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, 0) < 0)
|
||||
return -1;
|
||||
|
||||
return 0; /* don't passthru */
|
||||
|
||||
|
@ -88,7 +89,12 @@ secstream_raw(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|||
return 0;
|
||||
|
||||
buflen = lws_ptr_diff(end, p);
|
||||
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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue