2 * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #define NO_PLUGIN_VERBOSE_MACRO
30 #include <sys/types.h>
31 #include <sys/socket.h>
35 #include <json-c/json.h>
38 #include "afb-msg-json.h"
39 #include "afb-proto-ws.h"
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
64 - subscribe or unsubscribe an event
66 For the purpose of handling events the server can:
68 - create/destroy an event
70 - push or brodcast data as an event
73 /************** constants for protocol definition *************************/
75 #define CHAR_FOR_CALL 'C'
76 #define CHAR_FOR_ANSWER_SUCCESS 'T'
77 #define CHAR_FOR_ANSWER_FAIL 'F'
78 #define CHAR_FOR_EVT_BROADCAST '*'
79 #define CHAR_FOR_EVT_ADD '+'
80 #define CHAR_FOR_EVT_DEL '-'
81 #define CHAR_FOR_EVT_PUSH '!'
82 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
83 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
84 #define CHAR_FOR_SUBCALL_CALL 'B'
85 #define CHAR_FOR_SUBCALL_REPLY 'R'
86 #define CHAR_FOR_DESCRIBE 'D'
87 #define CHAR_FOR_DESCRIPTION 'd'
89 /******************* handling subcalls *****************************/
92 * Structure on server side for recording pending
97 struct server_subcall *next; /**< next subcall for the client */
98 uint32_t subcallid; /**< the subcallid */
99 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
100 void *closure; /**< closure of the callback */
104 * Structure for sending back replies on client side
106 struct afb_proto_ws_subcall
108 struct afb_proto_ws *protows; /**< proto descriptor */
110 uint32_t subcallid; /**< subcallid for the reply */
114 * structure for recording calls on client side
117 struct client_call *next; /* the next call */
118 struct afb_proto_ws *protows; /* the proto_ws */
120 uint32_t callid; /* the message identifier */
124 * structure for a ws request
126 struct afb_proto_ws_call {
127 struct client_call *next; /* the next call */
128 struct afb_proto_ws *protows; /* the client of the request */
129 uint32_t refcount; /* reference count */
130 uint32_t callid; /* the incoming request callid */
131 char *buffer; /* the incoming buffer */
135 * structure for recording describe requests
137 struct client_describe
139 struct client_describe *next;
140 struct afb_proto_ws *protows;
141 void (*callback)(void*, struct json_object*);
147 * structure for jobs of describing
149 struct afb_proto_ws_describe
151 struct afb_proto_ws *protows;
155 /******************* proto description for client or servers ******************/
159 /* count of references */
162 /* file descriptor */
165 /* resource control */
166 pthread_mutex_t mutex;
171 /* the client closure */
174 /* the client side interface */
175 const struct afb_proto_ws_client_itf *client_itf;
177 /* the server side interface */
178 const struct afb_proto_ws_server_itf *server_itf;
180 /* emitted calls (client side) */
181 struct client_call *calls;
183 /* pending subcalls (server side) */
184 struct server_subcall *subcalls;
186 /* pending description (client side) */
187 struct client_describe *describes;
189 /* on hangup callback */
190 void (*on_hangup)(void *closure);
193 /******************* common useful tools **********************************/
196 * translate a pointer to some integer
197 * @param ptr the pointer to translate
200 static inline uint32_t ptr2id(void *ptr)
202 return (uint32_t)(((intptr_t)ptr) >> 6);
205 /******************* serialisation part **********************************/
209 char *base, *head, *end;
212 #define WRITEBUF_COUNT_MAX 32
215 struct iovec iovec[WRITEBUF_COUNT_MAX];
216 uint32_t uints[WRITEBUF_COUNT_MAX];
220 static char *readbuf_get(struct readbuf *rb, uint32_t length)
222 char *before = rb->head;
223 char *after = before + length;
230 static int readbuf_char(struct readbuf *rb, char *value)
232 if (rb->head >= rb->end)
234 *value = *rb->head++;
238 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
240 char *after = rb->head + sizeof *value;
243 memcpy(value, rb->head, sizeof *value);
245 *value = le32toh(*value);
249 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
252 if (!readbuf_uint32(rb, &len) || !len)
255 *length = (size_t)(len - 1);
256 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
259 static int readbuf_object(struct readbuf *rb, struct json_object **object)
262 struct json_object *o;
263 int rc = readbuf_string(rb, &string, NULL);
265 o = json_tokener_parse(string);
266 if (o == NULL && strcmp(string, "null"))
267 o = json_object_new_string(string);
273 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
276 if (i == WRITEBUF_COUNT_MAX)
278 wb->iovec[i].iov_base = (void*)value;
279 wb->iovec[i].iov_len = length;
284 static int writebuf_char(struct writebuf *wb, char value)
287 if (i == WRITEBUF_COUNT_MAX)
289 *(char*)&wb->uints[i] = value;
290 wb->iovec[i].iov_base = &wb->uints[i];
291 wb->iovec[i].iov_len = 1;
296 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
299 if (i == WRITEBUF_COUNT_MAX)
301 wb->uints[i] = htole32(value);
302 wb->iovec[i].iov_base = &wb->uints[i];
303 wb->iovec[i].iov_len = sizeof wb->uints[i];
308 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
310 uint32_t len = (uint32_t)++length;
311 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
314 static int writebuf_string(struct writebuf *wb, const char *value)
316 return writebuf_string_length(wb, value, strlen(value));
319 static int writebuf_object(struct writebuf *wb, struct json_object *object)
321 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
322 return string != NULL && writebuf_string(wb, string);
325 /******************* ws request part for server *****************/
327 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
329 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
332 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
334 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
337 afb_proto_ws_unref(call->protows);
342 int afb_proto_ws_call_success(struct afb_proto_ws_call *call, struct json_object *obj, const char *info)
345 struct writebuf wb = { .count = 0 };
347 if (writebuf_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
348 && writebuf_uint32(&wb, call->callid)
349 && writebuf_string(&wb, info ?: "")
350 && writebuf_object(&wb, obj)) {
351 rc = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
361 int afb_proto_ws_call_fail(struct afb_proto_ws_call *call, const char *status, const char *info)
364 struct writebuf wb = { .count = 0 };
366 if (writebuf_char(&wb, CHAR_FOR_ANSWER_FAIL)
367 && writebuf_uint32(&wb, call->callid)
368 && writebuf_string(&wb, status)
369 && writebuf_string(&wb, info ? : "")) {
370 rc = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
380 int afb_proto_ws_call_subcall(struct afb_proto_ws_call *call, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
383 struct writebuf wb = { .count = 0 };
384 struct server_subcall *sc, *osc;
385 struct afb_proto_ws *protows = call->protows;
387 sc = malloc(sizeof *sc);
391 sc->callback = callback;
392 sc->closure = cb_closure;
394 pthread_mutex_unlock(&protows->mutex);
395 sc->subcallid = ptr2id(sc);
398 osc = protows->subcalls;
399 while(osc && osc->subcallid != sc->subcallid)
402 sc->next = protows->subcalls;
403 protows->subcalls = sc;
404 pthread_mutex_unlock(&protows->mutex);
406 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_CALL)
407 && writebuf_uint32(&wb, sc->subcallid)
408 && writebuf_uint32(&wb, call->callid)
409 && writebuf_string(&wb, api)
410 && writebuf_string(&wb, verb)
411 && writebuf_object(&wb, args)) {
412 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
423 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
426 struct writebuf wb = { .count = 0 };
428 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
429 && writebuf_uint32(&wb, call->callid)
430 && writebuf_uint32(&wb, (uint32_t)event_id)
431 && writebuf_string(&wb, event_name)) {
432 rc = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
442 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
445 struct writebuf wb = { .count = 0 };
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 = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
461 /******************* client part **********************************/
463 /* search a memorized call */
464 static struct client_call *client_call_search(struct afb_proto_ws *protows, uint32_t callid)
466 struct client_call *call;
468 call = protows->calls;
469 while (call != NULL && call->callid != callid)
475 /* free and release the memorizing call */
476 static void client_call_destroy(struct client_call *call)
478 struct client_call **prv;
480 prv = &call->protows->calls;
481 while (*prv != NULL) {
491 /* get event data from the message */
492 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
494 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
497 /* get event from the message */
498 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
502 /* get event data from the message */
503 if (!readbuf_uint32(rb, &callid)) {
508 *call = client_call_search(protows, callid);
517 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
519 const char *event_name;
522 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
523 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
526 /* removes an event */
527 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
529 const char *event_name;
532 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
533 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
536 /* subscribes an event */
537 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
539 const char *event_name;
541 struct client_call *call;
543 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
544 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
547 /* unsubscribes an event */
548 static void client_on_event_unsubscribe(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_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
555 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
558 /* receives broadcasted events */
559 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
561 const char *event_name;
562 struct json_object *object;
564 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
565 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
569 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
571 const char *event_name;
573 struct json_object *object;
575 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
576 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
579 static void client_on_reply_success(struct afb_proto_ws *protows, struct readbuf *rb)
581 struct client_call *call;
582 struct json_object *object;
585 if (!client_msg_call_get(protows, rb, &call))
588 if (readbuf_string(rb, &info, NULL) && readbuf_object(rb, &object)) {
589 protows->client_itf->on_reply_success(protows->closure, call->request, object, info);
591 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process success");
593 client_call_destroy(call);
596 static void client_on_reply_fail(struct afb_proto_ws *protows, struct readbuf *rb)
598 struct client_call *call;
599 const char *info, *status;
601 if (!client_msg_call_get(protows, rb, &call))
604 if (readbuf_string(rb, &status, NULL) && readbuf_string(rb, &info, NULL)) {
605 protows->client_itf->on_reply_fail(protows->closure, call->request, status, info);
607 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process fail");
609 client_call_destroy(call);
612 /* send a subcall reply */
613 static int client_send_subcall_reply(struct afb_proto_ws *protows, uint32_t subcallid, int status, json_object *object)
615 struct writebuf wb = { .count = 0 };
616 char ie = status < 0;
618 return -!(writebuf_char(&wb, CHAR_FOR_SUBCALL_REPLY)
619 && writebuf_uint32(&wb, subcallid)
620 && writebuf_char(&wb, ie)
621 && writebuf_object(&wb, object)
622 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0);
625 /* callback for subcall reply */
626 int afb_proto_ws_subcall_reply(struct afb_proto_ws_subcall *subcall, int status, struct json_object *result)
628 int rc = client_send_subcall_reply(subcall->protows, subcall->subcallid, status, result);
629 afb_proto_ws_unref(subcall->protows);
630 free(subcall->buffer);
635 /* received a subcall request */
636 static void client_on_subcall(struct afb_proto_ws *protows, struct readbuf *rb)
638 struct afb_proto_ws_subcall *subcall;
639 struct client_call *call;
640 const char *api, *verb;
642 struct json_object *object;
644 /* get the subcallid */
645 if (!readbuf_uint32(rb, &subcallid))
648 /* if not expected drop it */
649 if (!protows->client_itf->on_subcall)
652 /* retrieve the message data */
653 if (!client_msg_call_get(protows, rb, &call))
656 /* allocation of the subcall */
657 subcall = malloc(sizeof *subcall);
662 if (readbuf_string(rb, &api, NULL)
663 && readbuf_string(rb, &verb, NULL)
664 && readbuf_object(rb, &object)) {
665 afb_proto_ws_addref(protows);
666 subcall->protows = protows;
667 subcall->subcallid = subcallid;
668 subcall->buffer = rb->base;
670 protows->client_itf->on_subcall(protows->closure, subcall, call->request, api, verb, object);
675 client_send_subcall_reply(protows, subcallid, 1, NULL);
678 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
681 struct client_describe *desc, **prv;
682 struct json_object *object;
684 if (readbuf_uint32(rb, &descid)) {
685 prv = &protows->describes;
686 while ((desc = *prv) && desc->descid != descid)
690 if (!readbuf_object(rb, &object))
692 desc->callback(desc->closure, object);
698 /* callback when receiving binary data */
699 static void client_on_binary(void *closure, char *data, size_t size)
701 struct afb_proto_ws *protows;
707 rb.end = data + size;
710 pthread_mutex_lock(&protows->mutex);
711 switch (*rb.head++) {
712 case CHAR_FOR_ANSWER_SUCCESS: /* success */
713 client_on_reply_success(protows, &rb);
715 case CHAR_FOR_ANSWER_FAIL: /* fail */
716 client_on_reply_fail(protows, &rb);
718 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
719 client_on_event_broadcast(protows, &rb);
721 case CHAR_FOR_EVT_ADD: /* creates the event */
722 client_on_event_create(protows, &rb);
724 case CHAR_FOR_EVT_DEL: /* removes the event */
725 client_on_event_remove(protows, &rb);
727 case CHAR_FOR_EVT_PUSH: /* pushs the event */
728 client_on_event_push(protows, &rb);
730 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
731 client_on_event_subscribe(protows, &rb);
733 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
734 client_on_event_unsubscribe(protows, &rb);
736 case CHAR_FOR_SUBCALL_CALL: /* subcall */
737 client_on_subcall(protows, &rb);
739 case CHAR_FOR_DESCRIPTION: /* description */
740 client_on_description(protows, &rb);
742 default: /* unexpected message */
743 /* TODO: close the connection */
746 pthread_mutex_unlock(&protows->mutex);
751 int afb_proto_ws_client_call(
752 struct afb_proto_ws *protows,
754 struct json_object *args,
755 const char *sessionid,
760 struct client_call *call;
761 struct writebuf wb = { .count = 0 };
763 /* allocate call data */
764 call = malloc(sizeof *call);
769 call->request = request;
772 pthread_mutex_lock(&protows->mutex);
773 call->callid = ptr2id(call);
774 while(client_call_search(protows, call->callid) != NULL)
776 call->protows = protows;
777 call->next = protows->calls;
778 protows->calls = call;
780 /* creates the call message */
781 if (!writebuf_char(&wb, CHAR_FOR_CALL)
782 || !writebuf_uint32(&wb, call->callid)
783 || !writebuf_string(&wb, verb)
784 || !writebuf_string(&wb, sessionid)
785 || !writebuf_object(&wb, args)) {
791 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
798 client_call_destroy(call);
800 pthread_mutex_unlock(&protows->mutex);
804 /* get the description */
805 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
807 struct client_describe *desc, *d;
808 struct writebuf wb = { .count = 0 };
810 desc = malloc(sizeof *desc);
816 /* fill in stack the description of the task */
817 pthread_mutex_lock(&protows->mutex);
818 desc->descid = ptr2id(desc);
819 d = protows->describes;
821 if (d->descid != desc->descid)
825 d = protows->describes;
828 desc->callback = callback;
829 desc->closure = closure;
830 desc->protows = protows;
831 desc->next = protows->describes;
832 protows->describes = desc;
833 pthread_mutex_unlock(&protows->mutex);
836 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
837 && writebuf_uint32(&wb, desc->descid)
838 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0)
841 pthread_mutex_lock(&protows->mutex);
842 d = protows->describes;
844 protows->describes = desc->next;
846 while(d && d->next != desc)
849 d->next = desc->next;
852 pthread_mutex_unlock(&protows->mutex);
854 /* TODO? callback(closure, NULL); */
858 /******************* client description part for server *****************************/
860 /* on call, propagate it to the ws service */
861 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
863 struct afb_proto_ws_call *call;
864 const char *uuid, *verb;
867 struct json_object *object;
869 afb_proto_ws_addref(protows);
871 /* reads the call message data */
872 if (!readbuf_uint32(rb, &callid)
873 || !readbuf_string(rb, &verb, &lenverb)
874 || !readbuf_string(rb, &uuid, NULL)
875 || !readbuf_object(rb, &object))
878 /* create the request */
879 call = malloc(sizeof *call);
883 call->protows = protows;
884 call->callid = callid;
886 call->buffer = rb->base;
887 rb->base = NULL; /* don't free the buffer */
889 protows->server_itf->on_call(protows->closure, call, verb, object, uuid);
893 json_object_put(object);
896 afb_proto_ws_unref(protows);
899 /* on subcall reply */
900 static void server_on_subcall_reply(struct afb_proto_ws *protows, struct readbuf *rb)
904 struct json_object *object;
905 struct server_subcall *sc, **psc;
907 /* reads the call message data */
908 if (!readbuf_uint32(rb, &subcallid)
909 || !readbuf_char(rb, &ie)
910 || !readbuf_object(rb, &object)) {
911 /* TODO bad protocol */
915 /* search the subcall and unlink it */
916 pthread_mutex_lock(&protows->mutex);
917 psc = &protows->subcalls;
918 while ((sc = *psc) && sc->subcallid != subcallid)
921 pthread_mutex_unlock(&protows->mutex);
922 json_object_put(object);
923 /* TODO subcall not found */
926 pthread_mutex_unlock(&protows->mutex);
927 sc->callback(sc->closure, -(int)ie, object);
932 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
934 struct writebuf wb = { .count = 0 };
936 return -!(writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
937 && writebuf_uint32(&wb, descid)
938 && writebuf_object(&wb, descobj)
939 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0);
942 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
944 int rc = server_send_description(describe->protows, describe->descid, description);
945 afb_proto_ws_addref(describe->protows);
950 /* on describe, propagate it to the ws service */
951 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
954 struct afb_proto_ws_describe *desc;
956 /* reads the descid */
957 if (readbuf_uint32(rb, &descid)) {
958 if (protows->server_itf->on_describe) {
959 /* create asynchronous job */
960 desc = malloc(sizeof *desc);
962 desc->descid = descid;
963 desc->protows = protows;
964 afb_proto_ws_addref(protows);
965 protows->server_itf->on_describe(protows->closure, desc);
969 server_send_description(protows, descid, NULL);
973 /* callback when receiving binary data */
974 static void server_on_binary(void *closure, char *data, size_t size)
976 struct afb_proto_ws *protows;
982 rb.end = data + size;
985 switch (*rb.head++) {
987 server_on_call(protows, &rb);
989 case CHAR_FOR_SUBCALL_REPLY:
990 server_on_subcall_reply(protows, &rb);
992 case CHAR_FOR_DESCRIBE:
993 server_on_describe(protows, &rb);
995 default: /* unexpected message */
996 /* TODO: close the connection */
1003 /******************* server part: manage events **********************************/
1005 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
1007 struct writebuf wb = { .count = 0 };
1009 return -!(writebuf_char(&wb, order)
1010 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
1011 && writebuf_string(&wb, event_name)
1012 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))
1013 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0);
1016 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
1018 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
1021 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
1023 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
1026 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
1028 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
1031 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
1033 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
1036 /*****************************************************/
1038 /* callback when receiving a hangup */
1039 static void on_hangup(void *closure)
1041 struct afb_proto_ws *protows = closure;
1042 struct server_subcall *sc, *nsc;
1043 struct client_describe *cd, *ncd;
1045 nsc = protows->subcalls;
1049 sc->callback(sc->closure, 1, NULL);
1053 ncd = protows->describes;
1057 cd->callback(cd->closure, NULL);
1061 if (protows->fd >= 0) {
1064 if (protows->on_hangup)
1065 protows->on_hangup(protows->closure);
1069 /*****************************************************/
1071 static const struct afb_ws_itf proto_ws_client_ws_itf =
1075 .on_binary = client_on_binary,
1077 .on_hangup = on_hangup
1080 static const struct afb_ws_itf server_ws_itf =
1084 .on_binary = server_on_binary,
1086 .on_hangup = on_hangup
1089 /*****************************************************/
1091 static struct afb_proto_ws *afb_proto_ws_create(struct sd_event *eloop, int fd, const struct afb_proto_ws_server_itf *itfs, const struct afb_proto_ws_client_itf *itfc, void *closure, const struct afb_ws_itf *itf)
1093 struct afb_proto_ws *protows;
1095 protows = calloc(1, sizeof *protows);
1096 if (protows == NULL)
1099 fcntl(fd, F_SETFD, FD_CLOEXEC);
1100 fcntl(fd, F_SETFL, O_NONBLOCK);
1101 protows->ws = afb_ws_create(eloop, fd, itf, protows);
1102 if (protows->ws != NULL) {
1104 protows->refcount = 1;
1105 protows->subcalls = NULL;
1106 protows->closure = closure;
1107 protows->server_itf = itfs;
1108 protows->client_itf = itfc;
1109 pthread_mutex_init(&protows->mutex, NULL);
1117 struct afb_proto_ws *afb_proto_ws_create_client(struct sd_event *eloop, int fd, const struct afb_proto_ws_client_itf *itf, void *closure)
1119 return afb_proto_ws_create(eloop, fd, NULL, itf, closure, &proto_ws_client_ws_itf);
1122 struct afb_proto_ws *afb_proto_ws_create_server(struct sd_event *eloop, int fd, const struct afb_proto_ws_server_itf *itf, void *closure)
1124 return afb_proto_ws_create(eloop, fd, itf, NULL, closure, &server_ws_itf);
1127 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1129 if (!__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1130 afb_proto_ws_hangup(protows);
1131 afb_ws_destroy(protows->ws);
1132 pthread_mutex_destroy(&protows->mutex);
1137 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1139 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1142 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1144 return !!protows->client_itf;
1147 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1149 return !!protows->server_itf;
1152 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1154 afb_ws_hangup(protows->ws);
1157 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1159 protows->on_hangup = on_hangup;