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"
42 /******** implementation of internal binder protocol per api **************/
45 This protocol is asymetric: there is a client and a server
47 The client can require the following actions:
53 The server must reply to the previous actions by
55 - answering success or failure of the call
57 - answering the required description
59 The server can also within the context of a call
61 - subscribe or unsubscribe an event
63 For the purpose of handling events the server can:
65 - create/destroy an event
67 - push or brodcast data as an event
70 /************** constants for protocol definition *************************/
72 #define CHAR_FOR_CALL 'C'
73 #define CHAR_FOR_REPLY 'Y'
74 #define CHAR_FOR_EVT_BROADCAST '*'
75 #define CHAR_FOR_EVT_ADD '+'
76 #define CHAR_FOR_EVT_DEL '-'
77 #define CHAR_FOR_EVT_PUSH '!'
78 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
79 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
80 #define CHAR_FOR_DESCRIBE 'D'
81 #define CHAR_FOR_DESCRIPTION 'd'
83 /******************* handling calls *****************************/
86 * structure for recording calls on client side
89 struct client_call *next; /* the next call */
90 struct afb_proto_ws *protows; /* the proto_ws */
91 void *request; /* the request closure */
92 uint32_t callid; /* the message identifier */
96 * structure for a ws request
98 struct afb_proto_ws_call {
99 struct client_call *next; /* the next call */
100 struct afb_proto_ws *protows; /* the client of the request */
101 uint32_t refcount; /* reference count */
102 uint32_t callid; /* the incoming request callid */
103 char *buffer; /* the incoming buffer */
107 * structure for recording describe requests
109 struct client_describe
111 struct client_describe *next;
112 struct afb_proto_ws *protows;
113 void (*callback)(void*, struct json_object*);
119 * structure for jobs of describing
121 struct afb_proto_ws_describe
123 struct afb_proto_ws *protows;
127 /******************* proto description for client or servers ******************/
131 /* count of references */
134 /* file descriptor */
137 /* resource control */
138 pthread_mutex_t mutex;
143 /* the client closure */
146 /* the client side interface */
147 const struct afb_proto_ws_client_itf *client_itf;
149 /* the server side interface */
150 const struct afb_proto_ws_server_itf *server_itf;
152 /* emitted calls (client side) */
153 struct client_call *calls;
155 /* pending description (client side) */
156 struct client_describe *describes;
158 /* on hangup callback */
159 void (*on_hangup)(void *closure);
161 /* queuing facility for processing messages */
162 int (*queuing)(struct afb_proto_ws *proto, void (*process)(int s, void *c), 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 enum json_tokener_error jerr;
264 int rc = readbuf_string(rb, &string, NULL);
266 o = json_tokener_parse_verbose(string, &jerr);
267 if (jerr != json_tokener_success)
268 o = json_object_new_string(string);
274 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
277 if (i == WRITEBUF_COUNT_MAX)
279 wb->iovec[i].iov_base = (void*)value;
280 wb->iovec[i].iov_len = length;
285 static int writebuf_char(struct writebuf *wb, char value)
288 if (i == WRITEBUF_COUNT_MAX)
290 *(char*)&wb->uints[i] = value;
291 wb->iovec[i].iov_base = &wb->uints[i];
292 wb->iovec[i].iov_len = 1;
297 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
300 if (i == WRITEBUF_COUNT_MAX)
302 wb->uints[i] = htole32(value);
303 wb->iovec[i].iov_base = &wb->uints[i];
304 wb->iovec[i].iov_len = sizeof wb->uints[i];
309 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
311 uint32_t len = (uint32_t)++length;
312 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
315 static int writebuf_string(struct writebuf *wb, const char *value)
317 return writebuf_string_length(wb, value, strlen(value));
320 static int writebuf_nullstring(struct writebuf *wb, const char *value)
322 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
325 static int writebuf_object(struct writebuf *wb, struct json_object *object)
327 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
328 return string != NULL && writebuf_string(wb, string);
331 /******************* queuing of messages *****************/
333 /* queue the processing of the received message (except if size=0 cause it's not a valid message) */
334 static void queue_message_processing(struct afb_proto_ws *protows, char *data, size_t size, void (*processing)(int,void*))
336 struct binary *binary;
339 binary = malloc(sizeof *binary);
341 /* TODO process the problem */
344 binary->protows = protows;
345 binary->rb.base = data;
346 binary->rb.head = data;
347 binary->rb.end = data + size;
348 if (!protows->queuing
349 || protows->queuing(protows, processing, binary) < 0)
350 processing(0, binary);
357 /******************* ws request part for server *****************/
359 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
361 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
364 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
366 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
369 afb_proto_ws_unref(call->protows);
374 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
377 struct writebuf wb = { .count = 0 };
378 struct afb_proto_ws *protows = call->protows;
380 if (writebuf_char(&wb, CHAR_FOR_REPLY)
381 && writebuf_uint32(&wb, call->callid)
382 && writebuf_nullstring(&wb, error)
383 && writebuf_nullstring(&wb, info)
384 && writebuf_object(&wb, obj)) {
385 pthread_mutex_lock(&protows->mutex);
386 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
387 pthread_mutex_unlock(&protows->mutex);
397 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
400 struct writebuf wb = { .count = 0 };
401 struct afb_proto_ws *protows = call->protows;
403 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
404 && writebuf_uint32(&wb, call->callid)
405 && writebuf_uint32(&wb, (uint32_t)event_id)
406 && writebuf_string(&wb, event_name)) {
407 pthread_mutex_lock(&protows->mutex);
408 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
409 pthread_mutex_unlock(&protows->mutex);
419 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
422 struct writebuf wb = { .count = 0 };
423 struct afb_proto_ws *protows = call->protows;
425 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
426 && writebuf_uint32(&wb, call->callid)
427 && writebuf_uint32(&wb, (uint32_t)event_id)
428 && writebuf_string(&wb, event_name)) {
429 pthread_mutex_lock(&protows->mutex);
430 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
431 pthread_mutex_unlock(&protows->mutex);
441 /******************* client part **********************************/
443 /* search a memorized call */
444 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
446 struct client_call *call;
448 call = protows->calls;
449 while (call != NULL && call->callid != callid)
455 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
457 struct client_call *result;
459 pthread_mutex_lock(&protows->mutex);
460 result = client_call_search_locked(protows, callid);
461 pthread_mutex_unlock(&protows->mutex);
465 /* free and release the memorizing call */
466 static void client_call_destroy(struct client_call *call)
468 struct client_call **prv;
469 struct afb_proto_ws *protows = call->protows;
471 pthread_mutex_lock(&protows->mutex);
472 prv = &call->protows->calls;
473 while (*prv != NULL) {
480 pthread_mutex_unlock(&protows->mutex);
484 /* get event data from the message */
485 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
487 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
490 /* get event from the message */
491 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
495 /* get event data from the message */
496 if (!readbuf_uint32(rb, &callid)) {
501 *call = client_call_search_unlocked(protows, callid);
510 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
512 const char *event_name;
515 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
516 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
519 /* removes an event */
520 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
522 const char *event_name;
525 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
526 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
529 /* subscribes an event */
530 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
532 const char *event_name;
534 struct client_call *call;
536 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
537 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
540 /* unsubscribes an event */
541 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
543 const char *event_name;
545 struct client_call *call;
547 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
548 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
551 /* receives broadcasted events */
552 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
554 const char *event_name;
555 struct json_object *object;
557 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
558 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
562 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
564 const char *event_name;
566 struct json_object *object;
568 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
569 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
572 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
574 struct client_call *call;
575 struct json_object *object;
576 const char *error, *info;
578 if (!client_msg_call_get(protows, rb, &call))
581 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
582 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
584 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
586 client_call_destroy(call);
589 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
592 struct client_describe *desc, **prv;
593 struct json_object *object;
595 if (readbuf_uint32(rb, &descid)) {
596 pthread_mutex_lock(&protows->mutex);
597 prv = &protows->describes;
598 while ((desc = *prv) && desc->descid != descid)
601 pthread_mutex_unlock(&protows->mutex);
604 pthread_mutex_unlock(&protows->mutex);
605 if (!readbuf_object(rb, &object))
607 desc->callback(desc->closure, object);
613 /* callback when receiving binary data */
614 static void client_on_binary_job(int sig, void *closure)
616 struct binary *binary = closure;
619 switch (*binary->rb.head++) {
620 case CHAR_FOR_REPLY: /* reply */
621 client_on_reply(binary->protows, &binary->rb);
623 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
624 client_on_event_broadcast(binary->protows, &binary->rb);
626 case CHAR_FOR_EVT_ADD: /* creates the event */
627 client_on_event_create(binary->protows, &binary->rb);
629 case CHAR_FOR_EVT_DEL: /* removes the event */
630 client_on_event_remove(binary->protows, &binary->rb);
632 case CHAR_FOR_EVT_PUSH: /* pushs the event */
633 client_on_event_push(binary->protows, &binary->rb);
635 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
636 client_on_event_subscribe(binary->protows, &binary->rb);
638 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
639 client_on_event_unsubscribe(binary->protows, &binary->rb);
641 case CHAR_FOR_DESCRIPTION: /* description */
642 client_on_description(binary->protows, &binary->rb);
644 default: /* unexpected message */
645 /* TODO: close the connection */
649 free(binary->rb.base);
653 /* callback when receiving binary data */
654 static void client_on_binary(void *closure, char *data, size_t size)
656 struct afb_proto_ws *protows = closure;
658 queue_message_processing(protows, data, size, client_on_binary_job);
661 int afb_proto_ws_client_call(
662 struct afb_proto_ws *protows,
664 struct json_object *args,
665 const char *sessionid,
667 const char *user_creds
671 struct client_call *call;
672 struct writebuf wb = { .count = 0 };
674 /* allocate call data */
675 call = malloc(sizeof *call);
680 call->request = request;
683 pthread_mutex_lock(&protows->mutex);
684 call->callid = ptr2id(call);
685 while(client_call_search_locked(protows, call->callid) != NULL)
687 call->protows = protows;
688 call->next = protows->calls;
689 protows->calls = call;
690 pthread_mutex_unlock(&protows->mutex);
692 /* creates the call message */
693 if (!writebuf_char(&wb, CHAR_FOR_CALL)
694 || !writebuf_uint32(&wb, call->callid)
695 || !writebuf_string(&wb, verb)
696 || !writebuf_string(&wb, sessionid)
697 || !writebuf_object(&wb, args)
698 || !writebuf_nullstring(&wb, user_creds)) {
704 pthread_mutex_lock(&protows->mutex);
705 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
706 pthread_mutex_unlock(&protows->mutex);
713 client_call_destroy(call);
718 /* get the description */
719 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
721 struct client_describe *desc, *d;
722 struct writebuf wb = { .count = 0 };
724 desc = malloc(sizeof *desc);
730 /* fill in stack the description of the task */
731 pthread_mutex_lock(&protows->mutex);
732 desc->descid = ptr2id(desc);
733 d = protows->describes;
735 if (d->descid != desc->descid)
739 d = protows->describes;
742 desc->callback = callback;
743 desc->closure = closure;
744 desc->protows = protows;
745 desc->next = protows->describes;
746 protows->describes = desc;
749 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
750 && writebuf_uint32(&wb, desc->descid)
751 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0) {
752 pthread_mutex_unlock(&protows->mutex);
756 d = protows->describes;
758 protows->describes = desc->next;
760 while(d && d->next != desc)
763 d->next = desc->next;
765 pthread_mutex_unlock(&protows->mutex);
768 /* TODO? callback(closure, NULL); */
772 /******************* client description part for server *****************************/
774 /* on call, propagate it to the ws service */
775 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
777 struct afb_proto_ws_call *call;
778 const char *uuid, *verb, *user_creds;
781 struct json_object *object;
783 afb_proto_ws_addref(protows);
785 /* reads the call message data */
786 if (!readbuf_uint32(rb, &callid)
787 || !readbuf_string(rb, &verb, &lenverb)
788 || !readbuf_string(rb, &uuid, NULL)
789 || !readbuf_object(rb, &object)
790 || !readbuf_nullstring(rb, &user_creds, NULL))
793 /* create the request */
794 call = malloc(sizeof *call);
798 call->protows = protows;
799 call->callid = callid;
801 call->buffer = rb->base;
802 rb->base = NULL; /* don't free the buffer */
804 protows->server_itf->on_call(protows->closure, call, verb, object, uuid, user_creds);
808 json_object_put(object);
811 afb_proto_ws_unref(protows);
814 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
817 struct writebuf wb = { .count = 0 };
819 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
820 && writebuf_uint32(&wb, descid)
821 && writebuf_object(&wb, descobj)) {
822 pthread_mutex_lock(&protows->mutex);
823 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
824 pthread_mutex_unlock(&protows->mutex);
831 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
833 int rc = server_send_description(describe->protows, describe->descid, description);
834 afb_proto_ws_unref(describe->protows);
839 /* on describe, propagate it to the ws service */
840 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
843 struct afb_proto_ws_describe *desc;
845 /* reads the descid */
846 if (readbuf_uint32(rb, &descid)) {
847 if (protows->server_itf->on_describe) {
848 /* create asynchronous job */
849 desc = malloc(sizeof *desc);
851 desc->descid = descid;
852 desc->protows = protows;
853 afb_proto_ws_addref(protows);
854 protows->server_itf->on_describe(protows->closure, desc);
858 server_send_description(protows, descid, NULL);
862 /* callback when receiving binary data */
863 static void server_on_binary_job(int sig, void *closure)
865 struct binary *binary = closure;
868 switch (*binary->rb.head++) {
870 server_on_call(binary->protows, &binary->rb);
872 case CHAR_FOR_DESCRIBE:
873 server_on_describe(binary->protows, &binary->rb);
875 default: /* unexpected message */
876 /* TODO: close the connection */
880 free(binary->rb.base);
884 static void server_on_binary(void *closure, char *data, size_t size)
886 struct afb_proto_ws *protows = closure;
888 queue_message_processing(protows, data, size, server_on_binary_job);
891 /******************* server part: manage events **********************************/
893 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
895 struct writebuf wb = { .count = 0 };
898 if (writebuf_char(&wb, order)
899 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
900 && writebuf_string(&wb, event_name)
901 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))) {
902 pthread_mutex_lock(&protows->mutex);
903 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
904 pthread_mutex_unlock(&protows->mutex);
911 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
913 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
916 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
918 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
921 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
923 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
926 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
928 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
931 /*****************************************************/
933 /* callback when receiving a hangup */
934 static void on_hangup(void *closure)
936 struct afb_proto_ws *protows = closure;
937 struct client_describe *cd, *ncd;
938 struct client_call *call, *ncall;
940 ncd = __atomic_exchange_n(&protows->describes, NULL, __ATOMIC_RELAXED);
941 ncall = __atomic_exchange_n(&protows->calls, NULL, __ATOMIC_RELAXED);
946 protows->client_itf->on_reply(protows->closure, call->request, NULL, "disconnected", "server hung up");
953 cd->callback(cd->closure, NULL);
958 fdev_unref(protows->fdev);
960 if (protows->on_hangup)
961 protows->on_hangup(protows->closure);
965 /*****************************************************/
967 static const struct afb_ws_itf proto_ws_client_ws_itf =
971 .on_binary = client_on_binary,
973 .on_hangup = on_hangup
976 static const struct afb_ws_itf server_ws_itf =
980 .on_binary = server_on_binary,
982 .on_hangup = on_hangup
985 /*****************************************************/
987 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)
989 struct afb_proto_ws *protows;
991 protows = calloc(1, sizeof *protows);
995 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
996 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
997 protows->ws = afb_ws_create(fdev, itf, protows);
998 if (protows->ws != NULL) {
999 protows->fdev = fdev;
1000 protows->refcount = 1;
1001 protows->closure = closure;
1002 protows->server_itf = itfs;
1003 protows->client_itf = itfc;
1004 pthread_mutex_init(&protows->mutex, NULL);
1012 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1014 return afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1017 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1019 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1022 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1024 if (protows && !__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1025 afb_proto_ws_hangup(protows);
1026 afb_ws_destroy(protows->ws);
1027 pthread_mutex_destroy(&protows->mutex);
1032 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1034 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1037 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1039 return !!protows->client_itf;
1042 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1044 return !!protows->server_itf;
1047 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1049 afb_ws_hangup(protows->ws);
1052 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1054 protows->on_hangup = on_hangup;
1057 void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(struct afb_proto_ws*, void (*)(int,void*), void*))
1059 protows->queuing = queuing;