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 */
92 void *request; /* the request closure */
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);
162 /* queuing facility for processing messages */
163 int (*queuing)(void (*process)(int s, void *c), void *closure);
166 /******************* streaming objects **********************************/
168 #define WRITEBUF_COUNT_MAX 32
171 struct iovec iovec[WRITEBUF_COUNT_MAX];
172 uint32_t uints[WRITEBUF_COUNT_MAX];
178 char *base, *head, *end;
183 struct afb_proto_ws *protows;
187 /******************* common useful tools **********************************/
190 * translate a pointer to some integer
191 * @param ptr the pointer to translate
194 static inline uint32_t ptr2id(void *ptr)
196 return (uint32_t)(((intptr_t)ptr) >> 6);
199 /******************* serialisation part **********************************/
201 static char *readbuf_get(struct readbuf *rb, uint32_t length)
203 char *before = rb->head;
204 char *after = before + length;
211 __attribute__((unused))
212 static int readbuf_char(struct readbuf *rb, char *value)
214 if (rb->head >= rb->end)
216 *value = *rb->head++;
220 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
222 char *after = rb->head + sizeof *value;
225 memcpy(value, rb->head, sizeof *value);
227 *value = le32toh(*value);
231 static int _readbuf_string_(struct readbuf *rb, const char **value, size_t *length, int nulok)
234 if (!readbuf_uint32(rb, &len))
245 *length = (size_t)(len - 1);
246 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
250 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
252 return _readbuf_string_(rb, value, length, 0);
255 static int readbuf_nullstring(struct readbuf *rb, const char **value, size_t *length)
257 return _readbuf_string_(rb, value, length, 1);
260 static int readbuf_object(struct readbuf *rb, struct json_object **object)
263 struct json_object *o;
264 enum json_tokener_error jerr;
265 int rc = readbuf_string(rb, &string, NULL);
267 o = json_tokener_parse_verbose(string, &jerr);
268 if (jerr != json_tokener_success)
269 o = json_object_new_string(string);
275 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
278 if (i == WRITEBUF_COUNT_MAX)
280 wb->iovec[i].iov_base = (void*)value;
281 wb->iovec[i].iov_len = length;
286 static int writebuf_char(struct writebuf *wb, char value)
289 if (i == WRITEBUF_COUNT_MAX)
291 *(char*)&wb->uints[i] = value;
292 wb->iovec[i].iov_base = &wb->uints[i];
293 wb->iovec[i].iov_len = 1;
298 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
301 if (i == WRITEBUF_COUNT_MAX)
303 wb->uints[i] = htole32(value);
304 wb->iovec[i].iov_base = &wb->uints[i];
305 wb->iovec[i].iov_len = sizeof wb->uints[i];
310 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
312 uint32_t len = (uint32_t)++length;
313 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
316 static int writebuf_string(struct writebuf *wb, const char *value)
318 return writebuf_string_length(wb, value, strlen(value));
321 static int writebuf_nullstring(struct writebuf *wb, const char *value)
323 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
326 static int writebuf_object(struct writebuf *wb, struct json_object *object)
328 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
329 return string != NULL && writebuf_string(wb, string);
332 /******************* queuing of messages *****************/
334 /* queue the processing of the received message (except if size=0 cause it's not a valid message) */
335 static void queue_message_processing(struct afb_proto_ws *protows, char *data, size_t size, void (*processing)(int,void*))
337 struct binary *binary;
340 binary = malloc(sizeof *binary);
342 /* TODO process the problem */
345 binary->protows = protows;
346 binary->rb.base = data;
347 binary->rb.head = data;
348 binary->rb.end = data + size;
349 if (!protows->queuing
350 || protows->queuing(processing, binary) < 0)
351 processing(0, binary);
358 /******************* ws request part for server *****************/
360 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
362 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
365 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
367 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
370 afb_proto_ws_unref(call->protows);
375 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
378 struct writebuf wb = { .count = 0 };
379 struct afb_proto_ws *protows = call->protows;
381 if (writebuf_char(&wb, CHAR_FOR_REPLY)
382 && writebuf_uint32(&wb, call->callid)
383 && writebuf_nullstring(&wb, error)
384 && writebuf_nullstring(&wb, info)
385 && writebuf_object(&wb, obj)) {
386 pthread_mutex_lock(&protows->mutex);
387 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
388 pthread_mutex_unlock(&protows->mutex);
398 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
401 struct writebuf wb = { .count = 0 };
402 struct afb_proto_ws *protows = call->protows;
404 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
405 && writebuf_uint32(&wb, call->callid)
406 && writebuf_uint32(&wb, (uint32_t)event_id)
407 && writebuf_string(&wb, event_name)) {
408 pthread_mutex_lock(&protows->mutex);
409 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
410 pthread_mutex_unlock(&protows->mutex);
420 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
423 struct writebuf wb = { .count = 0 };
424 struct afb_proto_ws *protows = call->protows;
426 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
427 && writebuf_uint32(&wb, call->callid)
428 && writebuf_uint32(&wb, (uint32_t)event_id)
429 && writebuf_string(&wb, event_name)) {
430 pthread_mutex_lock(&protows->mutex);
431 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
432 pthread_mutex_unlock(&protows->mutex);
442 /******************* client part **********************************/
444 /* search a memorized call */
445 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
447 struct client_call *call;
449 call = protows->calls;
450 while (call != NULL && call->callid != callid)
456 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
458 struct client_call *result;
460 pthread_mutex_lock(&protows->mutex);
461 result = client_call_search_locked(protows, callid);
462 pthread_mutex_unlock(&protows->mutex);
466 /* free and release the memorizing call */
467 static void client_call_destroy(struct client_call *call)
469 struct client_call **prv;
470 struct afb_proto_ws *protows = call->protows;
472 pthread_mutex_lock(&protows->mutex);
473 prv = &call->protows->calls;
474 while (*prv != NULL) {
481 pthread_mutex_unlock(&protows->mutex);
485 /* get event data from the message */
486 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
488 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
491 /* get event from the message */
492 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
496 /* get event data from the message */
497 if (!readbuf_uint32(rb, &callid)) {
502 *call = client_call_search_unlocked(protows, callid);
511 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
513 const char *event_name;
516 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
517 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
520 /* removes an event */
521 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
523 const char *event_name;
526 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
527 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
530 /* subscribes an event */
531 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
533 const char *event_name;
535 struct client_call *call;
537 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
538 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
541 /* unsubscribes an event */
542 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
544 const char *event_name;
546 struct client_call *call;
548 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
549 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
552 /* receives broadcasted events */
553 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
555 const char *event_name;
556 struct json_object *object;
558 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
559 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
563 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
565 const char *event_name;
567 struct json_object *object;
569 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
570 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
573 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
575 struct client_call *call;
576 struct json_object *object;
577 const char *error, *info;
579 if (!client_msg_call_get(protows, rb, &call))
582 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
583 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
585 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
587 client_call_destroy(call);
590 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
593 struct client_describe *desc, **prv;
594 struct json_object *object;
596 if (readbuf_uint32(rb, &descid)) {
597 pthread_mutex_lock(&protows->mutex);
598 prv = &protows->describes;
599 while ((desc = *prv) && desc->descid != descid)
602 pthread_mutex_unlock(&protows->mutex);
605 pthread_mutex_unlock(&protows->mutex);
606 if (!readbuf_object(rb, &object))
608 desc->callback(desc->closure, object);
614 /* callback when receiving binary data */
615 static void client_on_binary_job(int sig, void *closure)
617 struct binary *binary = closure;
620 switch (*binary->rb.head++) {
621 case CHAR_FOR_REPLY: /* reply */
622 client_on_reply(binary->protows, &binary->rb);
624 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
625 client_on_event_broadcast(binary->protows, &binary->rb);
627 case CHAR_FOR_EVT_ADD: /* creates the event */
628 client_on_event_create(binary->protows, &binary->rb);
630 case CHAR_FOR_EVT_DEL: /* removes the event */
631 client_on_event_remove(binary->protows, &binary->rb);
633 case CHAR_FOR_EVT_PUSH: /* pushs the event */
634 client_on_event_push(binary->protows, &binary->rb);
636 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
637 client_on_event_subscribe(binary->protows, &binary->rb);
639 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
640 client_on_event_unsubscribe(binary->protows, &binary->rb);
642 case CHAR_FOR_DESCRIPTION: /* description */
643 client_on_description(binary->protows, &binary->rb);
645 default: /* unexpected message */
646 /* TODO: close the connection */
650 free(binary->rb.base);
654 /* callback when receiving binary data */
655 static void client_on_binary(void *closure, char *data, size_t size)
657 struct afb_proto_ws *protows = closure;
659 queue_message_processing(protows, data, size, client_on_binary_job);
662 int afb_proto_ws_client_call(
663 struct afb_proto_ws *protows,
665 struct json_object *args,
666 const char *sessionid,
668 const char *user_creds
672 struct client_call *call;
673 struct writebuf wb = { .count = 0 };
675 /* allocate call data */
676 call = malloc(sizeof *call);
681 call->request = request;
684 pthread_mutex_lock(&protows->mutex);
685 call->callid = ptr2id(call);
686 while(client_call_search_locked(protows, call->callid) != NULL)
688 call->protows = protows;
689 call->next = protows->calls;
690 protows->calls = call;
691 pthread_mutex_unlock(&protows->mutex);
693 /* creates the call message */
694 if (!writebuf_char(&wb, CHAR_FOR_CALL)
695 || !writebuf_uint32(&wb, call->callid)
696 || !writebuf_string(&wb, verb)
697 || !writebuf_string(&wb, sessionid)
698 || !writebuf_object(&wb, args)
699 || !writebuf_nullstring(&wb, user_creds)) {
705 pthread_mutex_lock(&protows->mutex);
706 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
707 pthread_mutex_unlock(&protows->mutex);
714 client_call_destroy(call);
719 /* get the description */
720 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
722 struct client_describe *desc, *d;
723 struct writebuf wb = { .count = 0 };
725 desc = malloc(sizeof *desc);
731 /* fill in stack the description of the task */
732 pthread_mutex_lock(&protows->mutex);
733 desc->descid = ptr2id(desc);
734 d = protows->describes;
736 if (d->descid != desc->descid)
740 d = protows->describes;
743 desc->callback = callback;
744 desc->closure = closure;
745 desc->protows = protows;
746 desc->next = protows->describes;
747 protows->describes = desc;
750 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
751 && writebuf_uint32(&wb, desc->descid)
752 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0) {
753 pthread_mutex_unlock(&protows->mutex);
757 d = protows->describes;
759 protows->describes = desc->next;
761 while(d && d->next != desc)
764 d->next = desc->next;
766 pthread_mutex_unlock(&protows->mutex);
769 /* TODO? callback(closure, NULL); */
773 /******************* client description part for server *****************************/
775 /* on call, propagate it to the ws service */
776 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
778 struct afb_proto_ws_call *call;
779 const char *uuid, *verb, *user_creds;
782 struct json_object *object;
784 afb_proto_ws_addref(protows);
786 /* reads the call message data */
787 if (!readbuf_uint32(rb, &callid)
788 || !readbuf_string(rb, &verb, &lenverb)
789 || !readbuf_string(rb, &uuid, NULL)
790 || !readbuf_object(rb, &object)
791 || !readbuf_nullstring(rb, &user_creds, NULL))
794 /* create the request */
795 call = malloc(sizeof *call);
799 call->protows = protows;
800 call->callid = callid;
802 call->buffer = rb->base;
803 rb->base = NULL; /* don't free the buffer */
805 protows->server_itf->on_call(protows->closure, call, verb, object, uuid, user_creds);
809 json_object_put(object);
812 afb_proto_ws_unref(protows);
815 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
818 struct writebuf wb = { .count = 0 };
820 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
821 && writebuf_uint32(&wb, descid)
822 && writebuf_object(&wb, descobj)) {
823 pthread_mutex_lock(&protows->mutex);
824 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
825 pthread_mutex_unlock(&protows->mutex);
832 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
834 int rc = server_send_description(describe->protows, describe->descid, description);
835 afb_proto_ws_unref(describe->protows);
840 /* on describe, propagate it to the ws service */
841 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
844 struct afb_proto_ws_describe *desc;
846 /* reads the descid */
847 if (readbuf_uint32(rb, &descid)) {
848 if (protows->server_itf->on_describe) {
849 /* create asynchronous job */
850 desc = malloc(sizeof *desc);
852 desc->descid = descid;
853 desc->protows = protows;
854 afb_proto_ws_addref(protows);
855 protows->server_itf->on_describe(protows->closure, desc);
859 server_send_description(protows, descid, NULL);
863 /* callback when receiving binary data */
864 static void server_on_binary_job(int sig, void *closure)
866 struct binary *binary = closure;
869 switch (*binary->rb.head++) {
871 server_on_call(binary->protows, &binary->rb);
873 case CHAR_FOR_DESCRIBE:
874 server_on_describe(binary->protows, &binary->rb);
876 default: /* unexpected message */
877 /* TODO: close the connection */
881 free(binary->rb.base);
885 static void server_on_binary(void *closure, char *data, size_t size)
887 struct afb_proto_ws *protows = closure;
889 queue_message_processing(protows, data, size, server_on_binary_job);
892 /******************* server part: manage events **********************************/
894 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
896 struct writebuf wb = { .count = 0 };
899 if (writebuf_char(&wb, order)
900 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
901 && writebuf_string(&wb, event_name)
902 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))) {
903 pthread_mutex_lock(&protows->mutex);
904 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
905 pthread_mutex_unlock(&protows->mutex);
912 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
914 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
917 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
919 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
922 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
924 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
927 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
929 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
932 /*****************************************************/
934 /* callback when receiving a hangup */
935 static void on_hangup(void *closure)
937 struct afb_proto_ws *protows = closure;
938 struct client_describe *cd, *ncd;
939 struct client_call *call, *ncall;
941 ncd = __atomic_exchange_n(&protows->describes, NULL, __ATOMIC_RELAXED);
942 ncall = __atomic_exchange_n(&protows->calls, NULL, __ATOMIC_RELAXED);
947 protows->client_itf->on_reply(protows->closure, call->request, NULL, "disconnected", "server hung up");
954 cd->callback(cd->closure, NULL);
959 fdev_unref(protows->fdev);
961 if (protows->on_hangup)
962 protows->on_hangup(protows->closure);
966 /*****************************************************/
968 static const struct afb_ws_itf proto_ws_client_ws_itf =
972 .on_binary = client_on_binary,
974 .on_hangup = on_hangup
977 static const struct afb_ws_itf server_ws_itf =
981 .on_binary = server_on_binary,
983 .on_hangup = on_hangup
986 /*****************************************************/
988 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)
990 struct afb_proto_ws *protows;
992 protows = calloc(1, sizeof *protows);
996 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
997 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
998 protows->ws = afb_ws_create(fdev, itf, protows);
999 if (protows->ws != NULL) {
1000 protows->fdev = fdev;
1001 protows->refcount = 1;
1002 protows->closure = closure;
1003 protows->server_itf = itfs;
1004 protows->client_itf = itfc;
1005 pthread_mutex_init(&protows->mutex, NULL);
1013 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1015 return afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1018 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1020 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1023 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1025 if (protows && !__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1026 afb_proto_ws_hangup(protows);
1027 afb_ws_destroy(protows->ws);
1028 pthread_mutex_destroy(&protows->mutex);
1033 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1035 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1038 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1040 return !!protows->client_itf;
1043 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1045 return !!protows->server_itf;
1048 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1050 afb_ws_hangup(protows->ws);
1053 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1055 protows->on_hangup = on_hangup;
1058 void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(void (*)(int,void*), void*))
1060 protows->queuing = queuing;