afb-ws: optimize buffer management
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 8 Sep 2017 08:41:52 +0000 (10:41 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 8 Sep 2017 08:41:52 +0000 (10:41 +0200)
Change-Id: I6cdfc9ccb6253efe5e22844c6ea66dae2c6272a2
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-ws.c

index 6d48887..ff625fc 100644 (file)
@@ -99,11 +99,21 @@ 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);
        }
 }