+/*
+ * Returns the current buffer of 'ws' that is reset.
+ */
+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.
+ */
+static void aws_disconnect(struct afb_ws *ws, int call_on_hangup)
+{
+ struct websock *wsi = ws->ws;
+ if (wsi != NULL) {
+ ws->ws = NULL;
+ fdev_unref(ws->fdev);
+ websock_destroy(wsi);
+ free(ws->buffer.buffer);
+ ws->state = waiting;
+ if (call_on_hangup && ws->itf->on_hangup)
+ ws->itf->on_hangup(ws->closure);
+ }
+}
+
+static void fdevcb(void *ws, uint32_t revents, struct fdev *fdev)
+{
+ if ((revents & EPOLLIN) != 0)
+ aws_on_readable(ws);
+ if ((revents & EPOLLHUP) != 0)
+ afb_ws_hangup(ws);
+}
+
+/*
+ * Creates the afb_ws structure for the file descritor
+ * 'fd' and the callbacks described by the interface 'itf'
+ * and its 'closure'.
+ * When the creation is a success, the systemd event loop 'eloop' is
+ * used for handling event for 'fd'.
+ *
+ * Returns the handle for the afb_ws created or NULL on error.
+ */
+struct afb_ws *afb_ws_create(struct fdev *fdev, const struct afb_ws_itf *itf, void *closure)