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 /* resource control */
136 pthread_mutex_t mutex;
141 /* the client closure */
144 /* the client side interface */
145 const struct afb_proto_ws_client_itf *client_itf;
147 /* the server side interface */
148 const struct afb_proto_ws_server_itf *server_itf;
150 /* emitted calls (client side) */
151 struct client_call *calls;
153 /* pending description (client side) */
154 struct client_describe *describes;
156 /* on hangup callback */
157 void (*on_hangup)(void *closure);
159 /* queuing facility for processing messages */
160 int (*queuing)(struct afb_proto_ws *proto, void (*process)(int s, void *c), 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 enum json_tokener_error jerr;
262 int rc = readbuf_string(rb, &string, NULL);
264 o = json_tokener_parse_verbose(string, &jerr);
265 if (jerr != json_tokener_success)
266 o = json_object_new_string(string);
272 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
275 if (i == WRITEBUF_COUNT_MAX)
277 wb->iovec[i].iov_base = (void*)value;
278 wb->iovec[i].iov_len = length;
283 static int writebuf_char(struct writebuf *wb, char value)
286 if (i == WRITEBUF_COUNT_MAX)
288 *(char*)&wb->uints[i] = value;
289 wb->iovec[i].iov_base = &wb->uints[i];
290 wb->iovec[i].iov_len = 1;
295 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
298 if (i == WRITEBUF_COUNT_MAX)
300 wb->uints[i] = htole32(value);
301 wb->iovec[i].iov_base = &wb->uints[i];
302 wb->iovec[i].iov_len = sizeof wb->uints[i];
307 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
309 uint32_t len = (uint32_t)++length;
310 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
313 static int writebuf_string(struct writebuf *wb, const char *value)
315 return writebuf_string_length(wb, value, strlen(value));
318 static int writebuf_nullstring(struct writebuf *wb, const char *value)
320 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
323 static int writebuf_object(struct writebuf *wb, struct json_object *object)
325 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
326 return string != NULL && writebuf_string(wb, string);
329 /******************* queuing of messages *****************/
331 /* queue the processing of the received message (except if size=0 cause it's not a valid message) */
332 static void queue_message_processing(struct afb_proto_ws *protows, char *data, size_t size, void (*processing)(int,void*))
334 struct binary *binary;
337 binary = malloc(sizeof *binary);
339 /* TODO process the problem */
342 binary->protows = protows;
343 binary->rb.base = data;
344 binary->rb.head = data;
345 binary->rb.end = data + size;
346 if (!protows->queuing
347 || protows->queuing(protows, processing, binary) < 0)
348 processing(0, binary);
355 /******************* ws request part for server *****************/
357 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
359 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
362 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
364 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
367 afb_proto_ws_unref(call->protows);
372 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
375 struct writebuf wb = { .count = 0 };
376 struct afb_proto_ws *protows = call->protows;
378 if (writebuf_char(&wb, CHAR_FOR_REPLY)
379 && writebuf_uint32(&wb, call->callid)
380 && writebuf_nullstring(&wb, error)
381 && writebuf_nullstring(&wb, info)
382 && writebuf_object(&wb, obj)) {
383 pthread_mutex_lock(&protows->mutex);
384 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
385 pthread_mutex_unlock(&protows->mutex);
395 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
398 struct writebuf wb = { .count = 0 };
399 struct afb_proto_ws *protows = call->protows;
401 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
402 && writebuf_uint32(&wb, call->callid)
403 && writebuf_uint32(&wb, (uint32_t)event_id)
404 && writebuf_string(&wb, event_name)) {
405 pthread_mutex_lock(&protows->mutex);
406 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
407 pthread_mutex_unlock(&protows->mutex);
417 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
420 struct writebuf wb = { .count = 0 };
421 struct afb_proto_ws *protows = call->protows;
423 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
424 && writebuf_uint32(&wb, call->callid)
425 && writebuf_uint32(&wb, (uint32_t)event_id)
426 && writebuf_string(&wb, event_name)) {
427 pthread_mutex_lock(&protows->mutex);
428 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
429 pthread_mutex_unlock(&protows->mutex);
439 /******************* client part **********************************/
441 /* search a memorized call */
442 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
444 struct client_call *call;
446 call = protows->calls;
447 while (call != NULL && call->callid != callid)
453 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
455 struct client_call *result;
457 pthread_mutex_lock(&protows->mutex);
458 result = client_call_search_locked(protows, callid);
459 pthread_mutex_unlock(&protows->mutex);
463 /* free and release the memorizing call */
464 static void client_call_destroy(struct client_call *call)
466 struct client_call **prv;
467 struct afb_proto_ws *protows = call->protows;
469 pthread_mutex_lock(&protows->mutex);
470 prv = &call->protows->calls;
471 while (*prv != NULL) {
478 pthread_mutex_unlock(&protows->mutex);
482 /* get event data from the message */
483 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
485 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
488 /* get event from the message */
489 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
493 /* get event data from the message */
494 if (!readbuf_uint32(rb, &callid)) {
499 *call = client_call_search_unlocked(protows, callid);
508 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
510 const char *event_name;
513 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
514 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
516 ERROR("Ignoring creation of event");
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);
528 ERROR("Ignoring deletion of event");
531 /* subscribes an event */
532 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
534 const char *event_name;
536 struct client_call *call;
538 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
539 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
541 ERROR("Ignoring subscription to event");
544 /* unsubscribes an event */
545 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
547 const char *event_name;
549 struct client_call *call;
551 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
552 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
554 ERROR("Ignoring unsubscription to event");
557 /* receives broadcasted events */
558 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
560 const char *event_name, *uuid;
562 struct json_object *object;
564 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object) && (uuid = readbuf_get(rb, 16)) && readbuf_char(rb, &hop))
565 protows->client_itf->on_event_broadcast(protows->closure, event_name, object, (unsigned char*)uuid, (uint8_t)hop);
567 ERROR("Ignoring broadcast of event");
571 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
573 const char *event_name;
575 struct json_object *object;
577 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
578 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
580 ERROR("Ignoring push of event");
583 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
585 struct client_call *call;
586 struct json_object *object;
587 const char *error, *info;
589 if (!client_msg_call_get(protows, rb, &call))
592 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
593 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
595 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
597 client_call_destroy(call);
600 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
603 struct client_describe *desc, **prv;
604 struct json_object *object;
606 if (readbuf_uint32(rb, &descid)) {
607 pthread_mutex_lock(&protows->mutex);
608 prv = &protows->describes;
609 while ((desc = *prv) && desc->descid != descid)
612 pthread_mutex_unlock(&protows->mutex);
615 pthread_mutex_unlock(&protows->mutex);
616 if (!readbuf_object(rb, &object))
618 desc->callback(desc->closure, object);
624 /* callback when receiving binary data */
625 static void client_on_binary_job(int sig, void *closure)
627 struct binary *binary = closure;
630 switch (*binary->rb.head++) {
631 case CHAR_FOR_REPLY: /* reply */
632 client_on_reply(binary->protows, &binary->rb);
634 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
635 client_on_event_broadcast(binary->protows, &binary->rb);
637 case CHAR_FOR_EVT_ADD: /* creates the event */
638 client_on_event_create(binary->protows, &binary->rb);
640 case CHAR_FOR_EVT_DEL: /* removes the event */
641 client_on_event_remove(binary->protows, &binary->rb);
643 case CHAR_FOR_EVT_PUSH: /* pushs the event */
644 client_on_event_push(binary->protows, &binary->rb);
646 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
647 client_on_event_subscribe(binary->protows, &binary->rb);
649 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
650 client_on_event_unsubscribe(binary->protows, &binary->rb);
652 case CHAR_FOR_DESCRIPTION: /* description */
653 client_on_description(binary->protows, &binary->rb);
655 default: /* unexpected message */
656 /* TODO: close the connection */
660 free(binary->rb.base);
664 /* callback when receiving binary data */
665 static void client_on_binary(void *closure, char *data, size_t size)
667 struct afb_proto_ws *protows = closure;
669 queue_message_processing(protows, data, size, client_on_binary_job);
672 int afb_proto_ws_client_call(
673 struct afb_proto_ws *protows,
675 struct json_object *args,
676 const char *sessionid,
678 const char *user_creds
682 struct client_call *call;
683 struct writebuf wb = { .count = 0 };
685 /* allocate call data */
686 call = malloc(sizeof *call);
691 call->request = request;
694 pthread_mutex_lock(&protows->mutex);
695 call->callid = ptr2id(call);
696 while(client_call_search_locked(protows, call->callid) != NULL)
698 call->protows = protows;
699 call->next = protows->calls;
700 protows->calls = call;
701 pthread_mutex_unlock(&protows->mutex);
703 /* creates the call message */
704 if (!writebuf_char(&wb, CHAR_FOR_CALL)
705 || !writebuf_uint32(&wb, call->callid)
706 || !writebuf_string(&wb, verb)
707 || !writebuf_string(&wb, sessionid)
708 || !writebuf_object(&wb, args)
709 || !writebuf_nullstring(&wb, user_creds)) {
715 pthread_mutex_lock(&protows->mutex);
716 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
717 pthread_mutex_unlock(&protows->mutex);
724 client_call_destroy(call);
729 /* get the description */
730 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
732 struct client_describe *desc, *d;
733 struct writebuf wb = { .count = 0 };
735 desc = malloc(sizeof *desc);
741 /* fill in stack the description of the task */
742 pthread_mutex_lock(&protows->mutex);
743 desc->descid = ptr2id(desc);
744 d = protows->describes;
746 if (d->descid != desc->descid)
750 d = protows->describes;
753 desc->callback = callback;
754 desc->closure = closure;
755 desc->protows = protows;
756 desc->next = protows->describes;
757 protows->describes = desc;
760 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
761 && writebuf_uint32(&wb, desc->descid)
762 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0) {
763 pthread_mutex_unlock(&protows->mutex);
767 d = protows->describes;
769 protows->describes = desc->next;
771 while(d && d->next != desc)
774 d->next = desc->next;
776 pthread_mutex_unlock(&protows->mutex);
779 /* TODO? callback(closure, NULL); */
783 /******************* client description part for server *****************************/
785 /* on call, propagate it to the ws service */
786 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
788 struct afb_proto_ws_call *call;
789 const char *uuid, *verb, *user_creds;
792 struct json_object *object;
794 afb_proto_ws_addref(protows);
796 /* reads the call message data */
797 if (!readbuf_uint32(rb, &callid)
798 || !readbuf_string(rb, &verb, &lenverb)
799 || !readbuf_string(rb, &uuid, NULL)
800 || !readbuf_object(rb, &object)
801 || !readbuf_nullstring(rb, &user_creds, NULL))
804 /* create the request */
805 call = malloc(sizeof *call);
809 call->protows = protows;
810 call->callid = callid;
812 call->buffer = rb->base;
813 rb->base = NULL; /* don't free the buffer */
815 protows->server_itf->on_call(protows->closure, call, verb, object, uuid, user_creds);
819 json_object_put(object);
822 afb_proto_ws_unref(protows);
825 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
828 struct writebuf wb = { .count = 0 };
830 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
831 && writebuf_uint32(&wb, descid)
832 && writebuf_object(&wb, descobj)) {
833 pthread_mutex_lock(&protows->mutex);
834 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
835 pthread_mutex_unlock(&protows->mutex);
842 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
844 int rc = server_send_description(describe->protows, describe->descid, description);
845 afb_proto_ws_unref(describe->protows);
850 /* on describe, propagate it to the ws service */
851 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
854 struct afb_proto_ws_describe *desc;
856 /* reads the descid */
857 if (readbuf_uint32(rb, &descid)) {
858 if (protows->server_itf->on_describe) {
859 /* create asynchronous job */
860 desc = malloc(sizeof *desc);
862 desc->descid = descid;
863 desc->protows = protows;
864 afb_proto_ws_addref(protows);
865 protows->server_itf->on_describe(protows->closure, desc);
869 server_send_description(protows, descid, NULL);
873 /* callback when receiving binary data */
874 static void server_on_binary_job(int sig, void *closure)
876 struct binary *binary = closure;
879 switch (*binary->rb.head++) {
881 server_on_call(binary->protows, &binary->rb);
883 case CHAR_FOR_DESCRIBE:
884 server_on_describe(binary->protows, &binary->rb);
886 default: /* unexpected message */
887 /* TODO: close the connection */
891 free(binary->rb.base);
895 static void server_on_binary(void *closure, char *data, size_t size)
897 struct afb_proto_ws *protows = closure;
899 queue_message_processing(protows, data, size, server_on_binary_job);
902 /******************* server part: manage events **********************************/
904 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
906 struct writebuf wb = { .count = 0 };
909 if (writebuf_char(&wb, order)
910 && writebuf_uint32(&wb, event_id)
911 && writebuf_string(&wb, event_name)
912 && (order != CHAR_FOR_EVT_PUSH || writebuf_object(&wb, data))) {
913 pthread_mutex_lock(&protows->mutex);
914 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
915 pthread_mutex_unlock(&protows->mutex);
922 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
924 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
927 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
929 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
932 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
934 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
937 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data, const unsigned char uuid[16], uint8_t hop)
939 struct writebuf wb = { .count = 0 };
945 if (writebuf_char(&wb, CHAR_FOR_EVT_BROADCAST)
946 && writebuf_string(&wb, event_name)
947 && writebuf_object(&wb, data)
948 && writebuf_put(&wb, uuid, 16)
949 && writebuf_char(&wb, (char)hop)) {
950 pthread_mutex_lock(&protows->mutex);
951 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
952 pthread_mutex_unlock(&protows->mutex);
959 /*****************************************************/
961 /* callback when receiving a hangup */
962 static void on_hangup(void *closure)
964 struct afb_proto_ws *protows = closure;
965 struct client_describe *cd, *ncd;
966 struct client_call *call, *ncall;
968 ncd = __atomic_exchange_n(&protows->describes, NULL, __ATOMIC_RELAXED);
969 ncall = __atomic_exchange_n(&protows->calls, NULL, __ATOMIC_RELAXED);
974 protows->client_itf->on_reply(protows->closure, call->request, NULL, "disconnected", "server hung up");
981 cd->callback(cd->closure, NULL);
986 afb_ws_destroy(protows->ws);
988 if (protows->on_hangup)
989 protows->on_hangup(protows->closure);
993 /*****************************************************/
995 static const struct afb_ws_itf proto_ws_client_ws_itf =
999 .on_binary = client_on_binary,
1001 .on_hangup = on_hangup
1004 static const struct afb_ws_itf server_ws_itf =
1008 .on_binary = server_on_binary,
1010 .on_hangup = on_hangup
1013 /*****************************************************/
1015 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)
1017 struct afb_proto_ws *protows;
1019 protows = calloc(1, sizeof *protows);
1020 if (protows == NULL)
1023 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
1024 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
1025 protows->ws = afb_ws_create(fdev, itf, protows);
1026 if (protows->ws != NULL) {
1027 protows->refcount = 1;
1028 protows->closure = closure;
1029 protows->server_itf = itfs;
1030 protows->client_itf = itfc;
1031 pthread_mutex_init(&protows->mutex, NULL);
1039 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1041 return afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1044 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1046 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1049 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1051 if (protows && !__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1052 afb_proto_ws_hangup(protows);
1053 pthread_mutex_destroy(&protows->mutex);
1058 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1060 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1063 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1065 return !!protows->client_itf;
1068 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1070 return !!protows->server_itf;
1073 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1076 afb_ws_hangup(protows->ws);
1079 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1081 protows->on_hangup = on_hangup;
1084 void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(struct afb_proto_ws*, void (*)(int,void*), void*))
1086 protows->queuing = queuing;