2 * Copyright (C) 2015-2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 #include <sys/types.h>
29 #include <sys/socket.h>
33 #include <json-c/json.h>
36 #include "afb-msg-json.h"
37 #include "afb-proto-ws.h"
43 /******** implementation of internal binder protocol per api **************/
46 This protocol is asymetric: there is a client and a server
48 The client can require the following actions:
54 The server must reply to the previous actions by
56 - answering success or failure of the call
58 - answering the required description
60 The server can also within the context of a call
62 - subscribe or unsubscribe an event
64 For the purpose of handling events the server can:
66 - create/destroy an event
68 - push or brodcast data as an event
71 /************** constants for protocol definition *************************/
73 #define CHAR_FOR_CALL 'C'
74 #define CHAR_FOR_REPLY 'Y'
75 #define CHAR_FOR_EVT_BROADCAST '*'
76 #define CHAR_FOR_EVT_ADD '+'
77 #define CHAR_FOR_EVT_DEL '-'
78 #define CHAR_FOR_EVT_PUSH '!'
79 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
80 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
81 #define CHAR_FOR_DESCRIBE 'D'
82 #define CHAR_FOR_DESCRIPTION 'd'
84 /******************* handling calls *****************************/
87 * structure for recording calls on client side
90 struct client_call *next; /* the next call */
91 struct afb_proto_ws *protows; /* the proto_ws */
93 uint32_t callid; /* the message identifier */
97 * structure for a ws request
99 struct afb_proto_ws_call {
100 struct client_call *next; /* the next call */
101 struct afb_proto_ws *protows; /* the client of the request */
102 uint32_t refcount; /* reference count */
103 uint32_t callid; /* the incoming request callid */
104 char *buffer; /* the incoming buffer */
108 * structure for recording describe requests
110 struct client_describe
112 struct client_describe *next;
113 struct afb_proto_ws *protows;
114 void (*callback)(void*, struct json_object*);
120 * structure for jobs of describing
122 struct afb_proto_ws_describe
124 struct afb_proto_ws *protows;
128 /******************* proto description for client or servers ******************/
132 /* count of references */
135 /* file descriptor */
138 /* resource control */
139 pthread_mutex_t mutex;
144 /* the client closure */
147 /* the client side interface */
148 const struct afb_proto_ws_client_itf *client_itf;
150 /* the server side interface */
151 const struct afb_proto_ws_server_itf *server_itf;
153 /* emitted calls (client side) */
154 struct client_call *calls;
156 /* pending description (client side) */
157 struct client_describe *describes;
159 /* on hangup callback */
160 void (*on_hangup)(void *closure);
163 /******************* streaming objects **********************************/
165 #define WRITEBUF_COUNT_MAX 32
168 struct iovec iovec[WRITEBUF_COUNT_MAX];
169 uint32_t uints[WRITEBUF_COUNT_MAX];
175 char *base, *head, *end;
180 struct afb_proto_ws *protows;
184 /******************* common useful tools **********************************/
187 * translate a pointer to some integer
188 * @param ptr the pointer to translate
191 static inline uint32_t ptr2id(void *ptr)
193 return (uint32_t)(((intptr_t)ptr) >> 6);
196 /******************* serialisation part **********************************/
198 static char *readbuf_get(struct readbuf *rb, uint32_t length)
200 char *before = rb->head;
201 char *after = before + length;
208 __attribute__((unused))
209 static int readbuf_char(struct readbuf *rb, char *value)
211 if (rb->head >= rb->end)
213 *value = *rb->head++;
217 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
219 char *after = rb->head + sizeof *value;
222 memcpy(value, rb->head, sizeof *value);
224 *value = le32toh(*value);
228 static int _readbuf_string_(struct readbuf *rb, const char **value, size_t *length, int nulok)
231 if (!readbuf_uint32(rb, &len))
242 *length = (size_t)(len - 1);
243 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
247 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
249 return _readbuf_string_(rb, value, length, 0);
252 static int readbuf_nullstring(struct readbuf *rb, const char **value, size_t *length)
254 return _readbuf_string_(rb, value, length, 1);
257 static int readbuf_object(struct readbuf *rb, struct json_object **object)
260 struct json_object *o;
261 int rc = readbuf_string(rb, &string, NULL);
263 o = json_tokener_parse(string);
264 if (o == NULL && strcmp(string, "null"))
265 o = json_object_new_string(string);
271 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
274 if (i == WRITEBUF_COUNT_MAX)
276 wb->iovec[i].iov_base = (void*)value;
277 wb->iovec[i].iov_len = length;
282 static int writebuf_char(struct writebuf *wb, char value)
285 if (i == WRITEBUF_COUNT_MAX)
287 *(char*)&wb->uints[i] = value;
288 wb->iovec[i].iov_base = &wb->uints[i];
289 wb->iovec[i].iov_len = 1;
294 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
297 if (i == WRITEBUF_COUNT_MAX)
299 wb->uints[i] = htole32(value);
300 wb->iovec[i].iov_base = &wb->uints[i];
301 wb->iovec[i].iov_len = sizeof wb->uints[i];
306 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
308 uint32_t len = (uint32_t)++length;
309 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
312 static int writebuf_string(struct writebuf *wb, const char *value)
314 return writebuf_string_length(wb, value, strlen(value));
317 static int writebuf_nullstring(struct writebuf *wb, const char *value)
319 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
322 static int writebuf_object(struct writebuf *wb, struct json_object *object)
324 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
325 return string != NULL && writebuf_string(wb, string);
328 /******************* ws request part for server *****************/
330 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
332 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
335 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
337 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
340 afb_proto_ws_unref(call->protows);
345 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
348 struct writebuf wb = { .count = 0 };
349 struct afb_proto_ws *protows = call->protows;
351 if (writebuf_char(&wb, CHAR_FOR_REPLY)
352 && writebuf_uint32(&wb, call->callid)
353 && writebuf_nullstring(&wb, error)
354 && writebuf_nullstring(&wb, info)
355 && writebuf_object(&wb, obj)) {
356 pthread_mutex_lock(&protows->mutex);
357 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
358 pthread_mutex_unlock(&protows->mutex);
368 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
371 struct writebuf wb = { .count = 0 };
372 struct afb_proto_ws *protows = call->protows;
374 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
375 && writebuf_uint32(&wb, call->callid)
376 && writebuf_uint32(&wb, (uint32_t)event_id)
377 && writebuf_string(&wb, event_name)) {
378 pthread_mutex_lock(&protows->mutex);
379 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
380 pthread_mutex_unlock(&protows->mutex);
390 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
393 struct writebuf wb = { .count = 0 };
394 struct afb_proto_ws *protows = call->protows;
396 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
397 && writebuf_uint32(&wb, call->callid)
398 && writebuf_uint32(&wb, (uint32_t)event_id)
399 && writebuf_string(&wb, event_name)) {
400 pthread_mutex_lock(&protows->mutex);
401 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
402 pthread_mutex_unlock(&protows->mutex);
412 /******************* client part **********************************/
414 /* search a memorized call */
415 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
417 struct client_call *call;
419 call = protows->calls;
420 while (call != NULL && call->callid != callid)
426 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
428 struct client_call *result;
430 pthread_mutex_lock(&protows->mutex);
431 result = client_call_search_locked(protows, callid);
432 pthread_mutex_unlock(&protows->mutex);
436 /* free and release the memorizing call */
437 static void client_call_destroy(struct client_call *call)
439 struct client_call **prv;
440 struct afb_proto_ws *protows = call->protows;
442 pthread_mutex_lock(&protows->mutex);
443 prv = &call->protows->calls;
444 while (*prv != NULL) {
451 pthread_mutex_unlock(&protows->mutex);
455 /* get event data from the message */
456 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
458 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
461 /* get event from the message */
462 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
466 /* get event data from the message */
467 if (!readbuf_uint32(rb, &callid)) {
472 *call = client_call_search_unlocked(protows, callid);
481 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
483 const char *event_name;
486 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
487 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
490 /* removes an event */
491 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
493 const char *event_name;
496 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
497 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
500 /* subscribes an event */
501 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
503 const char *event_name;
505 struct client_call *call;
507 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
508 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
511 /* unsubscribes an event */
512 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
514 const char *event_name;
516 struct client_call *call;
518 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
519 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
522 /* receives broadcasted events */
523 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
525 const char *event_name;
526 struct json_object *object;
528 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
529 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
533 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
535 const char *event_name;
537 struct json_object *object;
539 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
540 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
543 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
545 struct client_call *call;
546 struct json_object *object;
547 const char *error, *info;
549 if (!client_msg_call_get(protows, rb, &call))
552 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
553 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
555 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
557 client_call_destroy(call);
560 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
563 struct client_describe *desc, **prv;
564 struct json_object *object;
566 if (readbuf_uint32(rb, &descid)) {
567 pthread_mutex_lock(&protows->mutex);
568 prv = &protows->describes;
569 while ((desc = *prv) && desc->descid != descid)
572 pthread_mutex_unlock(&protows->mutex);
575 pthread_mutex_unlock(&protows->mutex);
576 if (!readbuf_object(rb, &object))
578 desc->callback(desc->closure, object);
584 /* callback when receiving binary data */
585 static void client_on_binary_job(int sig, void *closure)
587 struct binary *binary = closure;
590 switch (*binary->rb.head++) {
591 case CHAR_FOR_REPLY: /* reply */
592 client_on_reply(binary->protows, &binary->rb);
594 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
595 client_on_event_broadcast(binary->protows, &binary->rb);
597 case CHAR_FOR_EVT_ADD: /* creates the event */
598 client_on_event_create(binary->protows, &binary->rb);
600 case CHAR_FOR_EVT_DEL: /* removes the event */
601 client_on_event_remove(binary->protows, &binary->rb);
603 case CHAR_FOR_EVT_PUSH: /* pushs the event */
604 client_on_event_push(binary->protows, &binary->rb);
606 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
607 client_on_event_subscribe(binary->protows, &binary->rb);
609 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
610 client_on_event_unsubscribe(binary->protows, &binary->rb);
612 case CHAR_FOR_DESCRIPTION: /* description */
613 client_on_description(binary->protows, &binary->rb);
615 default: /* unexpected message */
616 /* TODO: close the connection */
620 free(binary->rb.base);
624 /* callback when receiving binary data */
625 static void client_on_binary(void *closure, char *data, size_t size)
628 struct binary *binary;
631 binary = malloc(sizeof *binary);
635 binary->protows = closure;
636 binary->rb.base = data;
637 binary->rb.head = data;
638 binary->rb.end = data + size;
639 rc = jobs_queue(NULL, 0, client_on_binary_job, binary);
648 int afb_proto_ws_client_call(
649 struct afb_proto_ws *protows,
651 struct json_object *args,
652 const char *sessionid,
654 const char *user_creds
658 struct client_call *call;
659 struct writebuf wb = { .count = 0 };
661 /* allocate call data */
662 call = malloc(sizeof *call);
667 call->request = request;
670 pthread_mutex_lock(&protows->mutex);
671 call->callid = ptr2id(call);
672 while(client_call_search_locked(protows, call->callid) != NULL)
674 call->protows = protows;
675 call->next = protows->calls;
676 protows->calls = call;
677 pthread_mutex_unlock(&protows->mutex);
679 /* creates the call message */
680 if (!writebuf_char(&wb, CHAR_FOR_CALL)
681 || !writebuf_uint32(&wb, call->callid)
682 || !writebuf_string(&wb, verb)
683 || !writebuf_string(&wb, sessionid)
684 || !writebuf_object(&wb, args)
685 || !writebuf_nullstring(&wb, user_creds)) {
691 pthread_mutex_lock(&protows->mutex);
692 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
693 pthread_mutex_unlock(&protows->mutex);
700 client_call_destroy(call);
705 /* get the description */
706 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
708 struct client_describe *desc, *d;
709 struct writebuf wb = { .count = 0 };
711 desc = malloc(sizeof *desc);
717 /* fill in stack the description of the task */
718 pthread_mutex_lock(&protows->mutex);
719 desc->descid = ptr2id(desc);
720 d = protows->describes;
722 if (d->descid != desc->descid)
726 d = protows->describes;
729 desc->callback = callback;
730 desc->closure = closure;
731 desc->protows = protows;
732 desc->next = protows->describes;
733 protows->describes = desc;
736 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
737 && writebuf_uint32(&wb, desc->descid)
738 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0) {
739 pthread_mutex_unlock(&protows->mutex);
743 d = protows->describes;
745 protows->describes = desc->next;
747 while(d && d->next != desc)
750 d->next = desc->next;
752 pthread_mutex_unlock(&protows->mutex);
755 /* TODO? callback(closure, NULL); */
759 /******************* client description part for server *****************************/
761 /* on call, propagate it to the ws service */
762 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
764 struct afb_proto_ws_call *call;
765 const char *uuid, *verb, *user_creds;
768 struct json_object *object;
770 afb_proto_ws_addref(protows);
772 /* reads the call message data */
773 if (!readbuf_uint32(rb, &callid)
774 || !readbuf_string(rb, &verb, &lenverb)
775 || !readbuf_string(rb, &uuid, NULL)
776 || !readbuf_object(rb, &object)
777 || !readbuf_nullstring(rb, &user_creds, NULL))
780 /* create the request */
781 call = malloc(sizeof *call);
785 call->protows = protows;
786 call->callid = callid;
788 call->buffer = rb->base;
789 rb->base = NULL; /* don't free the buffer */
791 protows->server_itf->on_call(protows->closure, call, verb, object, uuid, user_creds);
795 json_object_put(object);
798 afb_proto_ws_unref(protows);
801 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
804 struct writebuf wb = { .count = 0 };
806 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
807 && writebuf_uint32(&wb, descid)
808 && writebuf_object(&wb, descobj)) {
809 pthread_mutex_lock(&protows->mutex);
810 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
811 pthread_mutex_unlock(&protows->mutex);
818 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
820 int rc = server_send_description(describe->protows, describe->descid, description);
821 afb_proto_ws_unref(describe->protows);
826 /* on describe, propagate it to the ws service */
827 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
830 struct afb_proto_ws_describe *desc;
832 /* reads the descid */
833 if (readbuf_uint32(rb, &descid)) {
834 if (protows->server_itf->on_describe) {
835 /* create asynchronous job */
836 desc = malloc(sizeof *desc);
838 desc->descid = descid;
839 desc->protows = protows;
840 afb_proto_ws_addref(protows);
841 protows->server_itf->on_describe(protows->closure, desc);
845 server_send_description(protows, descid, NULL);
849 /* callback when receiving binary data */
850 static void server_on_binary_job(int sig, void *closure)
852 struct binary *binary = closure;
855 switch (*binary->rb.head++) {
857 server_on_call(binary->protows, &binary->rb);
859 case CHAR_FOR_DESCRIBE:
860 server_on_describe(binary->protows, &binary->rb);
862 default: /* unexpected message */
863 /* TODO: close the connection */
867 free(binary->rb.base);
871 static void server_on_binary(void *closure, char *data, size_t size)
874 struct binary *binary;
877 binary = malloc(sizeof *binary);
881 binary->protows = closure;
882 binary->rb.base = data;
883 binary->rb.head = data;
884 binary->rb.end = data + size;
885 rc = jobs_queue(NULL, 0, server_on_binary_job, binary);
894 /******************* server part: manage events **********************************/
896 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
898 struct writebuf wb = { .count = 0 };
901 if (writebuf_char(&wb, order)
902 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
903 && writebuf_string(&wb, event_name)
904 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))) {
905 pthread_mutex_lock(&protows->mutex);
906 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
907 pthread_mutex_unlock(&protows->mutex);
914 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
916 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
919 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
921 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
924 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
926 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
929 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
931 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
934 /*****************************************************/
936 /* callback when receiving a hangup */
937 static void on_hangup(void *closure)
939 struct afb_proto_ws *protows = closure;
940 struct client_describe *cd, *ncd;
942 ncd = protows->describes;
946 cd->callback(cd->closure, NULL);
951 fdev_unref(protows->fdev);
953 if (protows->on_hangup)
954 protows->on_hangup(protows->closure);
958 /*****************************************************/
960 static const struct afb_ws_itf proto_ws_client_ws_itf =
964 .on_binary = client_on_binary,
966 .on_hangup = on_hangup
969 static const struct afb_ws_itf server_ws_itf =
973 .on_binary = server_on_binary,
975 .on_hangup = on_hangup
978 /*****************************************************/
980 static struct afb_proto_ws *afb_proto_ws_create(struct fdev *fdev, const struct afb_proto_ws_server_itf *itfs, const struct afb_proto_ws_client_itf *itfc, void *closure, const struct afb_ws_itf *itf)
982 struct afb_proto_ws *protows;
984 protows = calloc(1, sizeof *protows);
988 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
989 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
990 protows->ws = afb_ws_create(fdev, itf, protows);
991 if (protows->ws != NULL) {
992 protows->fdev = fdev;
993 protows->refcount = 1;
994 protows->closure = closure;
995 protows->server_itf = itfs;
996 protows->client_itf = itfc;
997 pthread_mutex_init(&protows->mutex, NULL);
1005 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1007 return afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1010 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1012 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1015 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1017 if (!__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1018 afb_proto_ws_hangup(protows);
1019 afb_ws_destroy(protows->ws);
1020 pthread_mutex_destroy(&protows->mutex);
1025 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1027 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1030 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1032 return !!protows->client_itf;
1035 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1037 return !!protows->server_itf;
1040 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1042 afb_ws_hangup(protows->ws);
1045 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1047 protows->on_hangup = on_hangup;