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

lws_write_ws_flags helper

This commit is contained in:
Andy Green 2018-03-25 07:03:10 +08:00
parent 94f3981bef
commit bd6fc106d9
6 changed files with 44 additions and 46 deletions

View file

@ -82,7 +82,10 @@ lws_client_socket_service(struct lws_context *context, struct lws *wsi,
struct lws_pollfd *pollfd)
{
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
char *p = (char *)&pt->serv_buf[0], ebuf[128];
char *p = (char *)&pt->serv_buf[0];
#if defined(LWS_OPENSSL_SUPPORT)
char ebuf[128];
#endif
const char *cce = NULL;
unsigned char c;
char *sb = p;

View file

@ -4827,6 +4827,23 @@ lws_write(struct lws *wsi, unsigned char *buf, size_t len,
/* helper for case where buffer may be const */
#define lws_write_http(wsi, buf, len) \
lws_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP)
/* helper for multi-frame ws message flags */
static inline int
lws_write_ws_flags(int initial, int is_start, int is_end)
{
int r;
if (is_start)
r = initial;
else
r = LWS_WRITE_CONTINUATION;
if (!is_end)
r |= LWS_WRITE_NO_FIN;
return r;
}
///@}
/** \defgroup callback-when-writeable Callback when writeable

View file

@ -66,8 +66,8 @@ callback_minimal_pmd_bulk(struct lws *wsi, enum lws_callback_reasons reason,
{
struct per_session_data__minimal_pmd_bulk *pss =
(struct per_session_data__minimal_pmd_bulk *)user;
uint8_t buf[LWS_PRE + MESSAGE_CHUNK_SIZE], *p;
int n, m, msg_flag;
uint8_t buf[LWS_PRE + MESSAGE_CHUNK_SIZE], *start = &buf[LWS_PRE], *p;
int n, m, flags;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
@ -80,17 +80,16 @@ callback_minimal_pmd_bulk(struct lws *wsi, enum lws_callback_reasons reason,
if (pss->position == MESSAGE_SIZE)
break;
if (!pss->position)
msg_flag = LWS_WRITE_TEXT;
else
msg_flag = LWS_WRITE_CONTINUATION;
/* fill up one chunk's worth of message content */
p = &buf[LWS_PRE];
p = start;
n = MESSAGE_CHUNK_SIZE;
if (n > MESSAGE_SIZE - pss->position)
n = MESSAGE_SIZE - pss->position;
flags = lws_write_ws_flags(LWS_WRITE_TEXT, !pss->position,
pss->position + n == MESSAGE_SIZE);
/*
* select between producing compressible repeated text,
* or uncompressible PRNG output
@ -115,11 +114,9 @@ callback_minimal_pmd_bulk(struct lws *wsi, enum lws_callback_reasons reason,
*p++ = 0x40 + ((pss->rng >> (n & 15)) & 0x3f);
}
#endif
if (pss->position != MESSAGE_SIZE) /* if not the end, no FIN */
msg_flag |= LWS_WRITE_NO_FIN;
n = lws_ptr_diff(p, &buf[LWS_PRE]);
m = lws_write(wsi, &buf[LWS_PRE], n, msg_flag);
n = lws_ptr_diff(p, start);
m = lws_write(wsi, start, n, flags);
if (m < n) {
lwsl_err("ERROR %d writing ws\n", n);
return -1;

View file

@ -129,7 +129,7 @@ callback_lws_status(struct lws *wsi, enum lws_callback_reasons reason,
case LWS_CALLBACK_SERVER_WRITEABLE:
switch (pss->walk) {
case WALK_INITIAL:
n = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;;
n = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;
p += lws_snprintf(p, end - p,
"{ \"version\":\"%s\","
" \"wss_over_h2\":\"%d\","

View file

@ -60,7 +60,7 @@ callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user,
{
struct per_session_data__echo *pss =
(struct per_session_data__echo *)user;
int n;
int n, flags;
switch (reason) {
@ -75,29 +75,19 @@ callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user,
do_tx:
if ((int)pss->len == -1)
break;
n = LWS_WRITE_CONTINUATION;
if (!pss->continuation) {
if (pss->binary)
n = LWS_WRITE_BINARY;
else
n = LWS_WRITE_TEXT;
pss->continuation = 1;
}
if (!pss->final)
n |= LWS_WRITE_NO_FIN;
flags = lws_write_ws_flags(pss->binary ? LWS_WRITE_BINARY :
LWS_WRITE_TEXT, pss->continuation, pss->final);
lwsl_info("+++ test-echo: writing %d, with final %d\n",
pss->len, pss->final);
pss->tx += pss->len;
n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, n);
n = lws_write(wsi, &pss->buf[LWS_PRE], pss->len, flags);
if (n < 0) {
lwsl_err("ERROR %d writing to socket, hanging up\n", n);
return 1;
}
if (n < (int)pss->len) {
lwsl_err("Partial write\n");
return -1;
}
pss->len = -1;
if (pss->final)
pss->continuation = 0;

View file

@ -55,15 +55,11 @@ static int
callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
int n;
unsigned char buf[LWS_PRE + 8000];
unsigned char buf[LWS_PRE + 8000], *bp = &buf[LWS_PRE];
struct per_session_data__fraggle *psf = user;
int chunk;
int write_mode = LWS_WRITE_CONTINUATION;
unsigned long sum;
unsigned char *p = (unsigned char *)in;
unsigned char *bp = &buf[LWS_PRE];
int ran;
int n, chunk, flags, ran;
unsigned long sum;
switch (reason) {
@ -138,7 +134,6 @@ callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason,
psf->packets_left);
psf->sum = 0;
psf->total_message = 0;
write_mode = LWS_WRITE_BINARY;
psf->state = FRAGSTATE_RANDOM_PAYLOAD;
/* fallthru */
@ -160,18 +155,14 @@ callback_fraggle(struct lws *wsi, enum lws_callback_reasons reason,
psf->sum += bp[n];
psf->packets_left--;
if (psf->packets_left)
write_mode |= LWS_WRITE_NO_FIN;
else
flags = lws_write_ws_flags(LWS_WRITE_BINARY, !psf->sum,
!psf->packets_left);
if (!psf->packets_left)
psf->state = FRAGSTATE_POST_PAYLOAD_SUM;
n = lws_write(wsi, bp, chunk, write_mode);
n = lws_write(wsi, bp, chunk, flags);
if (n < 0)
return -1;
if (n < chunk) {
lwsl_err("Partial write\n");
return -1;
}
lws_callback_on_writable(wsi);
break;