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.
20 #define NO_PLUGIN_VERBOSE_MACRO
30 #include <sys/types.h>
31 #include <sys/socket.h>
35 #include <json-c/json.h>
38 #include "afb-msg-json.h"
39 #include "afb-proto-ws.h"
45 /******** implementation of internal binder protocol per api **************/
48 This protocol is asymetric: there is a client and a server
50 The client can require the following actions:
56 The server must reply to the previous actions by
58 - answering success or failure of the call
60 - answering the required description
62 The server can also within the context of a call
64 - subscribe or unsubscribe an event
66 For the purpose of handling events the server can:
68 - create/destroy an event
70 - push or brodcast data as an event
73 /************** constants for protocol definition *************************/
75 #define CHAR_FOR_CALL 'C'
76 #define CHAR_FOR_REPLY 'Y'
77 #define CHAR_FOR_EVT_BROADCAST '*'
78 #define CHAR_FOR_EVT_ADD '+'
79 #define CHAR_FOR_EVT_DEL '-'
80 #define CHAR_FOR_EVT_PUSH '!'
81 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
82 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
83 #define CHAR_FOR_DESCRIBE 'D'
84 #define CHAR_FOR_DESCRIPTION 'd'
86 /******************* handling calls *****************************/
89 * structure for recording calls on client side
92 struct client_call *next; /* the next call */
93 struct afb_proto_ws *protows; /* the proto_ws */
95 uint32_t callid; /* the message identifier */
99 * structure for a ws request
101 struct afb_proto_ws_call {
102 struct client_call *next; /* the next call */
103 struct afb_proto_ws *protows; /* the client of the request */
104 uint32_t refcount; /* reference count */
105 uint32_t callid; /* the incoming request callid */
106 char *buffer; /* the incoming buffer */
110 * structure for recording describe requests
112 struct client_describe
114 struct client_describe *next;
115 struct afb_proto_ws *protows;
116 void (*callback)(void*, struct json_object*);
122 * structure for jobs of describing
124 struct afb_proto_ws_describe
126 struct afb_proto_ws *protows;
130 /******************* proto description for client or servers ******************/
134 /* count of references */
137 /* file descriptor */
140 /* resource control */
141 pthread_mutex_t mutex;
146 /* the client closure */
149 /* the client side interface */
150 const struct afb_proto_ws_client_itf *client_itf;
152 /* the server side interface */
153 const struct afb_proto_ws_server_itf *server_itf;
155 /* emitted calls (client side) */
156 struct client_call *calls;
158 /* pending description (client side) */
159 struct client_describe *describes;
161 /* on hangup callback */
162 void (*on_hangup)(void *closure);
165 /******************* streaming objects **********************************/
167 #define WRITEBUF_COUNT_MAX 32
170 struct iovec iovec[WRITEBUF_COUNT_MAX];
171 uint32_t uints[WRITEBUF_COUNT_MAX];
177 char *base, *head, *end;
182 struct afb_proto_ws *protows;
186 /******************* common useful tools **********************************/
189 * translate a pointer to some integer
190 * @param ptr the pointer to translate
193 static inline uint32_t ptr2id(void *ptr)
195 return (uint32_t)(((intptr_t)ptr) >> 6);
198 /******************* serialisation part **********************************/
200 static char *readbuf_get(struct readbuf *rb, uint32_t length)
202 char *before = rb->head;
203 char *after = before + length;
210 __attribute__((unused))
211 static int readbuf_char(struct readbuf *rb, char *value)
213 if (rb->head >= rb->end)
215 *value = *rb->head++;
219 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
221 char *after = rb->head + sizeof *value;
224 memcpy(value, rb->head, sizeof *value);
226 *value = le32toh(*value);
230 static int _readbuf_string_(struct readbuf *rb, const char **value, size_t *length, int nulok)
233 if (!readbuf_uint32(rb, &len))
244 *length = (size_t)(len - 1);
245 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
249 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
251 return _readbuf_string_(rb, value, length, 0);
254 static int readbuf_nullstring(struct readbuf *rb, const char **value, size_t *length)
256 return _readbuf_string_(rb, value, length, 1);
259 static int readbuf_object(struct readbuf *rb, struct json_object **object)
262 struct json_object *o;
263 int rc = readbuf_string(rb, &string, NULL);
265 o = json_tokener_parse(string);
266 if (o == NULL && strcmp(string, "null"))
267 o = json_object_new_string(string);
273 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
276 if (i == WRITEBUF_COUNT_MAX)
278 wb->iovec[i].iov_base = (void*)value;
279 wb->iovec[i].iov_len = length;
284 static int writebuf_char(struct writebuf *wb, char value)
287 if (i == WRITEBUF_COUNT_MAX)
289 *(char*)&wb->uints[i] = value;
290 wb->iovec[i].iov_base = &wb->uints[i];
291 wb->iovec[i].iov_len = 1;
296 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
299 if (i == WRITEBUF_COUNT_MAX)
301 wb->uints[i] = htole32(value);
302 wb->iovec[i].iov_base = &wb->uints[i];
303 wb->iovec[i].iov_len = sizeof wb->uints[i];
308 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
310 uint32_t len = (uint32_t)++length;
311 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
314 static int writebuf_string(struct writebuf *wb, const char *value)
316 return writebuf_string_length(wb, value, strlen(value));
319 static int writebuf_nullstring(struct writebuf *wb, const char *value)
321 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
324 static int writebuf_object(struct writebuf *wb, struct json_object *object)
326 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
327 return string != NULL && writebuf_string(wb, string);
330 /******************* ws request part for server *****************/
332 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
334 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
337 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
339 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
342 afb_proto_ws_unref(call->protows);
347 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
350 struct writebuf wb = { .count = 0 };
351 struct afb_proto_ws *protows = call->protows;
353 if (writebuf_char(&wb, CHAR_FOR_REPLY)
354 && writebuf_uint32(&wb, call->callid)
355 && writebuf_nullstring(&wb, error)
356 && writebuf_nullstring(&wb, info)
357 && writebuf_object(&wb, obj)) {
358 pthread_mutex_lock(&protows->mutex);
359 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
360 pthread_mutex_unlock(&protows->mutex);
370 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
373 struct writebuf wb = { .count = 0 };
374 struct afb_proto_ws *protows = call->protows;
376 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
377 && writebuf_uint32(&wb, call->callid)
378 && writebuf_uint32(&wb, (uint32_t)event_id)
379 && writebuf_string(&wb, event_name)) {
380 pthread_mutex_lock(&protows->mutex);
381 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
382 pthread_mutex_unlock(&protows->mutex);
392 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
395 struct writebuf wb = { .count = 0 };
396 struct afb_proto_ws *protows = call->protows;
398 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
399 && writebuf_uint32(&wb, call->callid)
400 && writebuf_uint32(&wb, (uint32_t)event_id)
401 && writebuf_string(&wb, event_name)) {
402 pthread_mutex_lock(&protows->mutex);
403 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
404 pthread_mutex_unlock(&protows->mutex);
414 /******************* client part **********************************/
416 /* search a memorized call */
417 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
419 struct client_call *call;
421 call = protows->calls;
422 while (call != NULL && call->callid != callid)
428 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
430 struct client_call *result;
432 pthread_mutex_lock(&protows->mutex);
433 result = client_call_search_locked(protows, callid);
434 pthread_mutex_unlock(&protows->mutex);
438 /* free and release the memorizing call */
439 static void client_call_destroy(struct client_call *call)
441 struct client_call **prv;
442 struct afb_proto_ws *protows = call->protows;
444 pthread_mutex_lock(&protows->mutex);
445 prv = &call->protows->calls;
446 while (*prv != NULL) {
453 pthread_mutex_unlock(&protows->mutex);
457 /* get event data from the message */
458 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
460 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
463 /* get event from the message */
464 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
468 /* get event data from the message */
469 if (!readbuf_uint32(rb, &callid)) {
474 *call = client_call_search_unlocked(protows, callid);
483 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
485 const char *event_name;
488 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
489 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
492 /* removes an event */
493 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
495 const char *event_name;
498 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
499 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
502 /* subscribes an event */
503 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
505 const char *event_name;
507 struct client_call *call;
509 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
510 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
513 /* unsubscribes an event */
514 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
516 const char *event_name;
518 struct client_call *call;
520 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
521 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
524 /* receives broadcasted events */
525 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
527 const char *event_name;
528 struct json_object *object;
530 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
531 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
535 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
537 const char *event_name;
539 struct json_object *object;
541 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
542 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
545 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
547 struct client_call *call;
548 struct json_object *object;
549 const char *error, *info;
551 if (!client_msg_call_get(protows, rb, &call))
554 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
555 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
557 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
559 client_call_destroy(call);
562 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
565 struct client_describe *desc, **prv;
566 struct json_object *object;
568 if (readbuf_uint32(rb, &descid)) {
569 pthread_mutex_lock(&protows->mutex);
570 prv = &protows->describes;
571 while ((desc = *prv) && desc->descid != descid)
574 pthread_mutex_unlock(&protows->mutex);
577 pthread_mutex_unlock(&protows->mutex);
578 if (!readbuf_object(rb, &object))
580 desc->callback(desc->closure, object);
586 /* callback when receiving binary data */
587 static void client_on_binary_job(int sig, void *closure)
589 struct binary *binary = closure;
592 switch (*binary->rb.head++) {
593 case CHAR_FOR_REPLY: /* reply */
594 client_on_reply(binary->protows, &binary->rb);
596 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
597 client_on_event_broadcast(binary->protows, &binary->rb);
599 case CHAR_FOR_EVT_ADD: /* creates the event */
600 client_on_event_create(binary->protows, &binary->rb);
602 case CHAR_FOR_EVT_DEL: /* removes the event */
603 client_on_event_remove(binary->protows, &binary->rb);
605 case CHAR_FOR_EVT_PUSH: /* pushs the event */
606 client_on_event_push(binary->protows, &binary->rb);
608 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
609 client_on_event_subscribe(binary->protows, &binary->rb);
611 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
612 client_on_event_unsubscribe(binary->protows, &binary->rb);
614 case CHAR_FOR_DESCRIPTION: /* description */
615 client_on_description(binary->protows, &binary->rb);
617 default: /* unexpected message */
618 /* TODO: close the connection */
622 free(binary->rb.base);
626 /* callback when receiving binary data */
627 static void client_on_binary(void *closure, char *data, size_t size)
630 struct binary *binary;
633 binary = malloc(sizeof *binary);
637 binary->protows = closure;
638 binary->rb.base = data;
639 binary->rb.head = data;
640 binary->rb.end = data + size;
641 rc = jobs_queue(NULL, 0, client_on_binary_job, binary);
650 int afb_proto_ws_client_call(
651 struct afb_proto_ws *protows,
653 struct json_object *args,
654 const char *sessionid,
656 const char *user_creds
660 struct client_call *call;
661 struct writebuf wb = { .count = 0 };
663 /* allocate call data */
664 call = malloc(sizeof *call);
669 call->request = request;
672 pthread_mutex_lock(&protows->mutex);
673 call->callid = ptr2id(call);
674 while(client_call_search_locked(protows, call->callid) != NULL)
676 call->protows = protows;
677 call->next = protows->calls;
678 protows->calls = call;
679 pthread_mutex_unlock(&protows->mutex);
681 /* creates the call message */
682 if (!writebuf_char(&wb, CHAR_FOR_CALL)
683 || !writebuf_uint32(&wb, call->callid)
684 || !writebuf_string(&wb, verb)
685 || !writebuf_string(&wb, sessionid)
686 || !writebuf_object(&wb, args)
687 || !writebuf_nullstring(&wb, user_creds)) {
693 pthread_mutex_lock(&protows->mutex);
694 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
695 pthread_mutex_unlock(&protows->mutex);
702 client_call_destroy(call);
707 /* get the description */
708 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
710 struct client_describe *desc, *d;
711 struct writebuf wb = { .count = 0 };
713 desc = malloc(sizeof *desc);
719 /* fill in stack the description of the task */
720 pthread_mutex_lock(&protows->mutex);
721 desc->descid = ptr2id(desc);
722 d = protows->describes;
724 if (d->descid != desc->descid)
728 d = protows->describes;
731 desc->callback = callback;
732 desc->closure = closure;
733 desc->protows = protows;
734 desc->next = protows->describes;
735 protows->describes = desc;
738 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
739 && writebuf_uint32(&wb, desc->descid)
740 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0) {
741 pthread_mutex_unlock(&protows->mutex);
745 d = protows->describes;
747 protows->describes = desc->next;
749 while(d && d->next != desc)
752 d->next = desc->next;
754 pthread_mutex_unlock(&protows->mutex);
757 /* TODO? callback(closure, NULL); */
761 /******************* client description part for server *****************************/
763 /* on call, propagate it to the ws service */
764 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
766 struct afb_proto_ws_call *call;
767 const char *uuid, *verb, *user_creds;
770 struct json_object *object;
772 afb_proto_ws_addref(protows);
774 /* reads the call message data */
775 if (!readbuf_uint32(rb, &callid)
776 || !readbuf_string(rb, &verb, &lenverb)
777 || !readbuf_string(rb, &uuid, NULL)
778 || !readbuf_object(rb, &object)
779 || !readbuf_nullstring(rb, &user_creds, NULL))
782 /* create the request */
783 call = malloc(sizeof *call);
787 call->protows = protows;
788 call->callid = callid;
790 call->buffer = rb->base;
791 rb->base = NULL; /* don't free the buffer */
793 protows->server_itf->on_call(protows->closure, call, verb, object, uuid, user_creds);
797 json_object_put(object);
800 afb_proto_ws_unref(protows);
803 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
806 struct writebuf wb = { .count = 0 };
808 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
809 && writebuf_uint32(&wb, descid)
810 && writebuf_object(&wb, descobj)) {
811 pthread_mutex_lock(&protows->mutex);
812 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
813 pthread_mutex_unlock(&protows->mutex);
820 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
822 int rc = server_send_description(describe->protows, describe->descid, description);
823 afb_proto_ws_unref(describe->protows);
828 /* on describe, propagate it to the ws service */
829 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
832 struct afb_proto_ws_describe *desc;
834 /* reads the descid */
835 if (readbuf_uint32(rb, &descid)) {
836 if (protows->server_itf->on_describe) {
837 /* create asynchronous job */
838 desc = malloc(sizeof *desc);
840 desc->descid = descid;
841 desc->protows = protows;
842 afb_proto_ws_addref(protows);
843 protows->server_itf->on_describe(protows->closure, desc);
847 server_send_description(protows, descid, NULL);
851 /* callback when receiving binary data */
852 static void server_on_binary_job(int sig, void *closure)
854 struct binary *binary = closure;
857 switch (*binary->rb.head++) {
859 server_on_call(binary->protows, &binary->rb);
861 case CHAR_FOR_DESCRIBE:
862 server_on_describe(binary->protows, &binary->rb);
864 default: /* unexpected message */
865 /* TODO: close the connection */
869 free(binary->rb.base);
873 static void server_on_binary(void *closure, char *data, size_t size)
876 struct binary *binary;
879 binary = malloc(sizeof *binary);
883 binary->protows = closure;
884 binary->rb.base = data;
885 binary->rb.head = data;
886 binary->rb.end = data + size;
887 rc = jobs_queue(NULL, 0, server_on_binary_job, binary);
896 /******************* server part: manage events **********************************/
898 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
900 struct writebuf wb = { .count = 0 };
903 if (writebuf_char(&wb, order)
904 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
905 && writebuf_string(&wb, event_name)
906 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))) {
907 pthread_mutex_lock(&protows->mutex);
908 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
909 pthread_mutex_unlock(&protows->mutex);
916 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
918 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
921 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
923 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
926 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
928 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
931 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
933 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
936 /*****************************************************/
938 /* callback when receiving a hangup */
939 static void on_hangup(void *closure)
941 struct afb_proto_ws *protows = closure;
942 struct client_describe *cd, *ncd;
944 ncd = protows->describes;
948 cd->callback(cd->closure, NULL);
953 fdev_unref(protows->fdev);
955 if (protows->on_hangup)
956 protows->on_hangup(protows->closure);
960 /*****************************************************/
962 static const struct afb_ws_itf proto_ws_client_ws_itf =
966 .on_binary = client_on_binary,
968 .on_hangup = on_hangup
971 static const struct afb_ws_itf server_ws_itf =
975 .on_binary = server_on_binary,
977 .on_hangup = on_hangup
980 /*****************************************************/
982 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)
984 struct afb_proto_ws *protows;
986 protows = calloc(1, sizeof *protows);
990 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
991 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
992 protows->ws = afb_ws_create(fdev, itf, protows);
993 if (protows->ws != NULL) {
994 protows->fdev = fdev;
995 protows->refcount = 1;
996 protows->closure = closure;
997 protows->server_itf = itfs;
998 protows->client_itf = itfc;
999 pthread_mutex_init(&protows->mutex, NULL);
1007 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1009 return afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1012 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1014 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1017 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1019 if (!__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1020 afb_proto_ws_hangup(protows);
1021 afb_ws_destroy(protows->ws);
1022 pthread_mutex_destroy(&protows->mutex);
1027 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1029 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1032 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1034 return !!protows->client_itf;
1037 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1039 return !!protows->server_itf;
1042 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1044 afb_ws_hangup(protows->ws);
1047 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1049 protows->on_hangup = on_hangup;