2 * Copyright (C) 2015-2019 "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
166 #define WRITEBUF_BUFSZ (WRITEBUF_COUNT_MAX * sizeof(uint32_t))
170 int iovcount, bufcount;
171 struct iovec iovec[WRITEBUF_COUNT_MAX];
172 char buf[WRITEBUF_BUFSZ];
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)
276 int i = wb->iovcount;
277 if (i == WRITEBUF_COUNT_MAX)
279 wb->iovec[i].iov_base = (void*)value;
280 wb->iovec[i].iov_len = length;
281 wb->iovcount = i + 1;
285 static int writebuf_putbuf(struct writebuf *wb, const void *value, int length)
288 int i = wb->iovcount, n = wb->bufcount, nafter;
290 /* check enough length */
292 if (nafter > WRITEBUF_BUFSZ)
295 /* get where to store */
297 if (i && p == (((char*)wb->iovec[i - 1].iov_base) + wb->iovec[i - 1].iov_len))
298 /* increase previous iovec */
299 wb->iovec[i - 1].iov_len += (size_t)length;
300 else if (i == WRITEBUF_COUNT_MAX)
305 wb->iovec[i].iov_base = p;
306 wb->iovec[i].iov_len = (size_t)length;
307 wb->iovcount = i + 1;
310 memcpy(p, value, (size_t)length);
311 wb->bufcount = nafter;
315 static int writebuf_char(struct writebuf *wb, char value)
317 return writebuf_putbuf(wb, &value, 1);
320 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
322 value = htole32(value);
323 return writebuf_putbuf(wb, &value, (int)sizeof value);
326 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
328 uint32_t len = (uint32_t)++length;
329 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
332 static int writebuf_string(struct writebuf *wb, const char *value)
334 return writebuf_string_length(wb, value, strlen(value));
337 static int writebuf_nullstring(struct writebuf *wb, const char *value)
339 return value ? writebuf_string_length(wb, value, strlen(value)) : writebuf_uint32(wb, 0);
342 static int writebuf_object(struct writebuf *wb, struct json_object *object)
344 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
345 return string != NULL && writebuf_string(wb, string);
348 /******************* queuing of messages *****************/
350 /* queue the processing of the received message (except if size=0 cause it's not a valid message) */
351 static void queue_message_processing(struct afb_proto_ws *protows, char *data, size_t size, void (*processing)(int,void*))
353 struct binary *binary;
356 binary = malloc(sizeof *binary);
358 /* TODO process the problem */
361 binary->protows = protows;
362 binary->rb.base = data;
363 binary->rb.head = data;
364 binary->rb.end = data + size;
365 if (!protows->queuing
366 || protows->queuing(protows, processing, binary) < 0)
367 processing(0, binary);
374 /******************* sending messages *****************/
376 static int proto_write(struct afb_proto_ws *protows, struct writebuf *wb)
381 pthread_mutex_lock(&protows->mutex);
387 rc = afb_ws_binary_v(ws, wb->iovec, wb->iovcount);
391 pthread_mutex_unlock(&protows->mutex);
395 /******************* ws request part for server *****************/
397 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
399 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
402 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
404 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
407 afb_proto_ws_unref(call->protows);
412 int afb_proto_ws_call_reply(struct afb_proto_ws_call *call, struct json_object *obj, const char *error, const char *info)
415 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
416 struct afb_proto_ws *protows = call->protows;
418 if (writebuf_char(&wb, CHAR_FOR_REPLY)
419 && writebuf_uint32(&wb, call->callid)
420 && writebuf_nullstring(&wb, error)
421 && writebuf_nullstring(&wb, info)
422 && writebuf_object(&wb, obj))
423 rc = proto_write(protows, &wb);
427 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
430 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
431 struct afb_proto_ws *protows = call->protows;
433 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
434 && writebuf_uint32(&wb, call->callid)
435 && writebuf_uint32(&wb, (uint32_t)event_id)
436 && writebuf_string(&wb, event_name))
437 rc = proto_write(protows, &wb);
441 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
444 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
445 struct afb_proto_ws *protows = call->protows;
447 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
448 && writebuf_uint32(&wb, call->callid)
449 && writebuf_uint32(&wb, (uint32_t)event_id)
450 && writebuf_string(&wb, event_name))
451 rc = proto_write(protows, &wb);
455 /******************* client part **********************************/
457 /* search a memorized call */
458 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
460 struct client_call *call;
462 call = protows->calls;
463 while (call != NULL && call->callid != callid)
469 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
471 struct client_call *result;
473 pthread_mutex_lock(&protows->mutex);
474 result = client_call_search_locked(protows, callid);
475 pthread_mutex_unlock(&protows->mutex);
479 /* free and release the memorizing call */
480 static void client_call_destroy(struct client_call *call)
482 struct client_call **prv;
483 struct afb_proto_ws *protows = call->protows;
485 pthread_mutex_lock(&protows->mutex);
486 prv = &call->protows->calls;
487 while (*prv != NULL) {
494 pthread_mutex_unlock(&protows->mutex);
498 /* get event data from the message */
499 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
501 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
504 /* get event from the message */
505 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
509 /* get event data from the message */
510 if (!readbuf_uint32(rb, &callid)) {
515 *call = client_call_search_unlocked(protows, callid);
524 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
526 const char *event_name;
529 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
530 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
532 ERROR("Ignoring creation of event");
535 /* removes an event */
536 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
538 const char *event_name;
541 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
542 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
544 ERROR("Ignoring deletion of event");
547 /* subscribes an event */
548 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
550 const char *event_name;
552 struct client_call *call;
554 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
555 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
557 ERROR("Ignoring subscription to event");
560 /* unsubscribes an event */
561 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
563 const char *event_name;
565 struct client_call *call;
567 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
568 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
570 ERROR("Ignoring unsubscription to event");
573 /* receives broadcasted events */
574 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
576 const char *event_name, *uuid;
578 struct json_object *object;
580 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))
581 protows->client_itf->on_event_broadcast(protows->closure, event_name, object, (unsigned char*)uuid, (uint8_t)hop);
583 ERROR("Ignoring broadcast of event");
587 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
589 const char *event_name;
591 struct json_object *object;
593 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
594 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
596 ERROR("Ignoring push of event");
599 static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb)
601 struct client_call *call;
602 struct json_object *object;
603 const char *error, *info;
605 if (!client_msg_call_get(protows, rb, &call))
608 if (readbuf_nullstring(rb, &error, NULL) && readbuf_nullstring(rb, &info, NULL) && readbuf_object(rb, &object)) {
609 protows->client_itf->on_reply(protows->closure, call->request, object, error, info);
611 protows->client_itf->on_reply(protows->closure, call->request, NULL, "proto-error", "can't process success");
613 client_call_destroy(call);
616 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
619 struct client_describe *desc, **prv;
620 struct json_object *object;
622 if (readbuf_uint32(rb, &descid)) {
623 pthread_mutex_lock(&protows->mutex);
624 prv = &protows->describes;
625 while ((desc = *prv) && desc->descid != descid)
628 pthread_mutex_unlock(&protows->mutex);
631 pthread_mutex_unlock(&protows->mutex);
632 if (!readbuf_object(rb, &object))
634 desc->callback(desc->closure, object);
640 /* callback when receiving binary data */
641 static void client_on_binary_job(int sig, void *closure)
643 struct binary *binary = closure;
646 switch (*binary->rb.head++) {
647 case CHAR_FOR_REPLY: /* reply */
648 client_on_reply(binary->protows, &binary->rb);
650 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
651 client_on_event_broadcast(binary->protows, &binary->rb);
653 case CHAR_FOR_EVT_ADD: /* creates the event */
654 client_on_event_create(binary->protows, &binary->rb);
656 case CHAR_FOR_EVT_DEL: /* removes the event */
657 client_on_event_remove(binary->protows, &binary->rb);
659 case CHAR_FOR_EVT_PUSH: /* pushs the event */
660 client_on_event_push(binary->protows, &binary->rb);
662 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
663 client_on_event_subscribe(binary->protows, &binary->rb);
665 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
666 client_on_event_unsubscribe(binary->protows, &binary->rb);
668 case CHAR_FOR_DESCRIPTION: /* description */
669 client_on_description(binary->protows, &binary->rb);
671 default: /* unexpected message */
672 /* TODO: close the connection */
676 free(binary->rb.base);
680 /* callback when receiving binary data */
681 static void client_on_binary(void *closure, char *data, size_t size)
683 struct afb_proto_ws *protows = closure;
685 queue_message_processing(protows, data, size, client_on_binary_job);
688 int afb_proto_ws_client_call(
689 struct afb_proto_ws *protows,
691 struct json_object *args,
692 const char *sessionid,
694 const char *user_creds
698 struct client_call *call;
699 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
701 /* allocate call data */
702 call = malloc(sizeof *call);
707 call->request = request;
710 pthread_mutex_lock(&protows->mutex);
711 call->callid = ptr2id(call);
712 while(client_call_search_locked(protows, call->callid) != NULL)
714 call->protows = protows;
715 call->next = protows->calls;
716 protows->calls = call;
717 pthread_mutex_unlock(&protows->mutex);
719 /* creates the call message */
720 if (!writebuf_char(&wb, CHAR_FOR_CALL)
721 || !writebuf_uint32(&wb, call->callid)
722 || !writebuf_string(&wb, verb)
723 || !writebuf_string(&wb, sessionid)
724 || !writebuf_object(&wb, args)
725 || !writebuf_nullstring(&wb, user_creds)) {
731 rc = proto_write(protows, &wb);
736 client_call_destroy(call);
741 /* get the description */
742 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
744 struct client_describe *desc, *d;
745 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
747 desc = malloc(sizeof *desc);
753 /* fill in stack the description of the task */
754 pthread_mutex_lock(&protows->mutex);
755 desc->descid = ptr2id(desc);
756 d = protows->describes;
758 if (d->descid != desc->descid)
762 d = protows->describes;
765 desc->callback = callback;
766 desc->closure = closure;
767 desc->protows = protows;
768 desc->next = protows->describes;
769 protows->describes = desc;
772 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
773 && writebuf_uint32(&wb, desc->descid)
774 && protows->ws != NULL
775 && afb_ws_binary_v(protows->ws, wb.iovec, wb.iovcount) >= 0) {
776 pthread_mutex_unlock(&protows->mutex);
780 d = protows->describes;
782 protows->describes = desc->next;
784 while(d && d->next != desc)
787 d->next = desc->next;
789 pthread_mutex_unlock(&protows->mutex);
792 /* TODO? callback(closure, NULL); */
796 /******************* client description part for server *****************************/
798 /* on call, propagate it to the ws service */
799 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
801 struct afb_proto_ws_call *call;
802 const char *uuid, *verb, *user_creds;
805 struct json_object *object;
807 afb_proto_ws_addref(protows);
809 /* reads the call message data */
810 if (!readbuf_uint32(rb, &callid)
811 || !readbuf_string(rb, &verb, &lenverb)
812 || !readbuf_string(rb, &uuid, NULL)
813 || !readbuf_object(rb, &object)
814 || !readbuf_nullstring(rb, &user_creds, NULL))
817 /* create the request */
818 call = malloc(sizeof *call);
822 call->protows = protows;
823 call->callid = callid;
825 call->buffer = rb->base;
826 rb->base = NULL; /* don't free the buffer */
828 protows->server_itf->on_call(protows->closure, call, verb, object, uuid, user_creds);
832 json_object_put(object);
835 afb_proto_ws_unref(protows);
838 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
841 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
843 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
844 && writebuf_uint32(&wb, descid)
845 && writebuf_object(&wb, descobj))
846 rc = proto_write(protows, &wb);
850 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
852 int rc = server_send_description(describe->protows, describe->descid, description);
853 afb_proto_ws_unref(describe->protows);
858 /* on describe, propagate it to the ws service */
859 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
862 struct afb_proto_ws_describe *desc;
864 /* reads the descid */
865 if (readbuf_uint32(rb, &descid)) {
866 if (protows->server_itf->on_describe) {
867 /* create asynchronous job */
868 desc = malloc(sizeof *desc);
870 desc->descid = descid;
871 desc->protows = protows;
872 afb_proto_ws_addref(protows);
873 protows->server_itf->on_describe(protows->closure, desc);
877 server_send_description(protows, descid, NULL);
881 /* callback when receiving binary data */
882 static void server_on_binary_job(int sig, void *closure)
884 struct binary *binary = closure;
887 switch (*binary->rb.head++) {
889 server_on_call(binary->protows, &binary->rb);
891 case CHAR_FOR_DESCRIBE:
892 server_on_describe(binary->protows, &binary->rb);
894 default: /* unexpected message */
895 /* TODO: close the connection */
899 free(binary->rb.base);
903 static void server_on_binary(void *closure, char *data, size_t size)
905 struct afb_proto_ws *protows = closure;
907 queue_message_processing(protows, data, size, server_on_binary_job);
910 /******************* server part: manage events **********************************/
912 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
914 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
917 if (writebuf_char(&wb, order)
918 && writebuf_uint32(&wb, event_id)
919 && writebuf_string(&wb, event_name)
920 && (order != CHAR_FOR_EVT_PUSH || writebuf_object(&wb, data)))
921 rc = proto_write(protows, &wb);
925 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
927 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
930 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
932 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
935 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
937 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
940 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)
942 struct writebuf wb = { .iovcount = 0, .bufcount = 0 };
948 if (writebuf_char(&wb, CHAR_FOR_EVT_BROADCAST)
949 && writebuf_string(&wb, event_name)
950 && writebuf_object(&wb, data)
951 && writebuf_put(&wb, uuid, 16)
952 && writebuf_char(&wb, (char)(hop - 1)))
953 rc = proto_write(protows, &wb);
957 /*****************************************************/
959 /* callback when receiving a hangup */
960 static void on_hangup(void *closure)
962 struct afb_proto_ws *protows = closure;
963 struct client_describe *cd, *ncd;
964 struct client_call *call, *ncall;
967 pthread_mutex_lock(&protows->mutex);
968 ncd = protows->describes;
969 protows->describes = NULL;
970 ncall = protows->calls;
971 protows->calls = NULL;
974 pthread_mutex_unlock(&protows->mutex);
979 protows->client_itf->on_reply(protows->closure, call->request, NULL, "disconnected", "server hung up");
986 cd->callback(cd->closure, NULL);
992 if (protows->on_hangup)
993 protows->on_hangup(protows->closure);
997 /*****************************************************/
999 static const struct afb_ws_itf proto_ws_client_ws_itf =
1003 .on_binary = client_on_binary,
1005 .on_hangup = on_hangup
1008 static const struct afb_ws_itf server_ws_itf =
1012 .on_binary = server_on_binary,
1014 .on_hangup = on_hangup
1017 /*****************************************************/
1019 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)
1021 struct afb_proto_ws *protows;
1023 protows = calloc(1, sizeof *protows);
1024 if (protows == NULL)
1027 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
1028 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
1029 protows->ws = afb_ws_create(fdev, itf, protows);
1030 if (protows->ws != NULL) {
1031 protows->refcount = 1;
1032 protows->closure = closure;
1033 protows->server_itf = itfs;
1034 protows->client_itf = itfc;
1035 pthread_mutex_init(&protows->mutex, NULL);
1043 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1045 return afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1048 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1050 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1053 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1055 if (protows && !__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1056 afb_proto_ws_hangup(protows);
1057 pthread_mutex_destroy(&protows->mutex);
1062 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1064 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1067 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1069 return !!protows->client_itf;
1072 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1074 return !!protows->server_itf;
1077 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1080 afb_ws_hangup(protows->ws);
1083 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1085 protows->on_hangup = on_hangup;
1088 void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(struct afb_proto_ws*, void (*)(int,void*), void*))
1090 protows->queuing = queuing;