diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-09-08 10:41:52 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-09-08 10:41:52 +0200 |
commit | ba58e7ffa5c36300d09165253c58f25add228273 (patch) | |
tree | 8423b155ca383f784fc17253bc2c396cf9beec4e | |
parent | 95fbde72511e2c14d35906d63b9afba2006f9632 (diff) |
afb-ws: optimize buffer management
Change-Id: I6cdfc9ccb6253efe5e22844c6ea66dae2c6272a2
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | src/afb-ws.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/afb-ws.c b/src/afb-ws.c index 6d488878..ff625fca 100644 --- a/src/afb-ws.c +++ b/src/afb-ws.c @@ -99,12 +99,22 @@ struct afb_ws static inline struct buf aws_pick_buffer(struct afb_ws *ws) { struct buf result = ws->buffer; + if (result.buffer) + result.buffer[result.size] = 0; ws->buffer.buffer = NULL; ws->buffer.size = 0; return result; } /* + * Clear the current buffer + */ +static inline void aws_clear_buffer(struct afb_ws *ws) +{ + ws->buffer.size = 0; +} + +/* * Disconnect the websocket 'ws' and calls on_hangup if * 'call_on_hangup' is not null. */ @@ -116,7 +126,7 @@ static void aws_disconnect(struct afb_ws *ws, int call_on_hangup) sd_event_source_unref(ws->evsrc); ws->evsrc = NULL; websock_destroy(wsi); - free(aws_pick_buffer(ws).buffer); + free(ws->buffer.buffer); ws->state = waiting; if (call_on_hangup && ws->itf->on_hangup) ws->itf->on_hangup(ws->closure); @@ -425,7 +435,7 @@ static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size) struct buf b; ws->state = waiting; - free(aws_pick_buffer(ws).buffer); + aws_clear_buffer(ws); if (ws->itf->on_close == NULL) { websock_drop(ws->ws); afb_ws_hangup(ws); @@ -443,7 +453,7 @@ static void aws_on_close(struct afb_ws *ws, uint16_t code, size_t size) static void aws_drop_error(struct afb_ws *ws, uint16_t code) { ws->state = waiting; - free(aws_pick_buffer(ws).buffer); + aws_clear_buffer(ws); websock_drop(ws->ws); websock_error(ws->ws, code, NULL, 0); } @@ -462,7 +472,6 @@ static void aws_continue(struct afb_ws *ws, int last, size_t size) istxt = ws->state == reading_text; ws->state = waiting; b = aws_pick_buffer(ws); - b.buffer[b.size] = 0; (istxt ? ws->itf->on_text : ws->itf->on_binary)(ws->closure, b.buffer, b.size); } } |