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>
36 #include <systemd/sd-event.h>
38 #include "afb-common.h"
41 #include "afb-msg-json.h"
42 #include "afb-proto-ws.h"
47 /******** implementation of internal binder protocol per api **************/
50 This protocol is asymetric: there is a client and a server
52 The client can require the following actions:
58 The server must reply to the previous actions by
60 - answering success or failure of the call
62 - answering the required description
64 The server can also within the context of a call
68 - subscribe or unsubscribe an event
70 For the purpose of handling events the server can:
72 - create/destroy an event
74 - push or brodcast data as an event
77 /************** constants for protocol definition *************************/
79 #define CHAR_FOR_CALL 'C'
80 #define CHAR_FOR_ANSWER_SUCCESS 'T'
81 #define CHAR_FOR_ANSWER_FAIL 'F'
82 #define CHAR_FOR_EVT_BROADCAST '*'
83 #define CHAR_FOR_EVT_ADD '+'
84 #define CHAR_FOR_EVT_DEL '-'
85 #define CHAR_FOR_EVT_PUSH '!'
86 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
87 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
88 #define CHAR_FOR_SUBCALL_CALL 'B'
89 #define CHAR_FOR_SUBCALL_REPLY 'R'
90 #define CHAR_FOR_DESCRIBE 'D'
91 #define CHAR_FOR_DESCRIPTION 'd'
93 /******************* handling subcalls *****************************/
96 * Structure on server side for recording pending
101 struct server_subcall *next; /**< next subcall for the client */
102 uint32_t subcallid; /**< the subcallid */
103 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
104 void *closure; /**< closure of the callback */
108 * Structure for sending back replies on client side
110 struct afb_proto_ws_subcall
112 struct afb_proto_ws *protows; /**< proto descriptor */
114 uint32_t subcallid; /**< subcallid for the reply */
118 * structure for recording calls on client side
121 struct client_call *next; /* the next call */
122 struct afb_proto_ws *protows; /* the proto_ws */
124 uint32_t callid; /* the message identifier */
128 * structure for a ws request
130 struct afb_proto_ws_call {
131 struct client_call *next; /* the next call */
132 struct afb_proto_ws *protows; /* the client of the request */
133 uint32_t refcount; /* reference count */
134 uint32_t callid; /* the incoming request callid */
135 char *buffer; /* the incoming buffer */
139 * structure for recording describe requests
141 struct client_describe
143 struct client_describe *next;
144 struct afb_proto_ws *protows;
145 void (*callback)(void*, struct json_object*);
151 * structure for jobs of describing
153 struct afb_proto_ws_describe
155 struct afb_proto_ws *protows;
159 /******************* proto description for client or servers ******************/
163 /* count of references */
166 /* file descriptor */
169 /* resource control */
170 pthread_mutex_t mutex;
175 /* the client closure */
178 /* the client side interface */
179 const struct afb_proto_ws_client_itf *client_itf;
181 /* the server side interface */
182 const struct afb_proto_ws_server_itf *server_itf;
184 /* emitted calls (client side) */
185 struct client_call *calls;
187 /* pending subcalls (server side) */
188 struct server_subcall *subcalls;
190 /* pending description (client side) */
191 struct client_describe *describes;
193 /* on hangup callback */
194 void (*on_hangup)(void *closure);
197 /******************* common useful tools **********************************/
200 * translate a pointer to some integer
201 * @param ptr the pointer to translate
204 static inline uint32_t ptr2id(void *ptr)
206 return (uint32_t)(((intptr_t)ptr) >> 6);
209 /******************* serialisation part **********************************/
213 char *base, *head, *end;
216 #define WRITEBUF_COUNT_MAX 32
219 struct iovec iovec[WRITEBUF_COUNT_MAX];
220 uint32_t uints[WRITEBUF_COUNT_MAX];
224 static char *readbuf_get(struct readbuf *rb, uint32_t length)
226 char *before = rb->head;
227 char *after = before + length;
234 static int readbuf_char(struct readbuf *rb, char *value)
236 if (rb->head >= rb->end)
238 *value = *rb->head++;
242 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
244 char *after = rb->head + sizeof *value;
247 memcpy(value, rb->head, sizeof *value);
249 *value = le32toh(*value);
253 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
256 if (!readbuf_uint32(rb, &len) || !len)
259 *length = (size_t)(len - 1);
260 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
263 static int readbuf_object(struct readbuf *rb, struct json_object **object)
266 struct json_object *o;
267 int rc = readbuf_string(rb, &string, NULL);
269 o = json_tokener_parse(string);
270 if (o == NULL && strcmp(string, "null"))
271 o = json_object_new_string(string);
277 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
280 if (i == WRITEBUF_COUNT_MAX)
282 wb->iovec[i].iov_base = (void*)value;
283 wb->iovec[i].iov_len = length;
288 static int writebuf_char(struct writebuf *wb, char value)
291 if (i == WRITEBUF_COUNT_MAX)
293 *(char*)&wb->uints[i] = value;
294 wb->iovec[i].iov_base = &wb->uints[i];
295 wb->iovec[i].iov_len = 1;
300 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
303 if (i == WRITEBUF_COUNT_MAX)
305 wb->uints[i] = htole32(value);
306 wb->iovec[i].iov_base = &wb->uints[i];
307 wb->iovec[i].iov_len = sizeof wb->uints[i];
312 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
314 uint32_t len = (uint32_t)++length;
315 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
318 static int writebuf_string(struct writebuf *wb, const char *value)
320 return writebuf_string_length(wb, value, strlen(value));
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 /******************* ws request part for server *****************/
331 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
333 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
336 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
338 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
341 afb_proto_ws_unref(call->protows);
346 int afb_proto_ws_call_success(struct afb_proto_ws_call *call, struct json_object *obj, const char *info)
349 struct writebuf wb = { .count = 0 };
351 if (writebuf_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
352 && writebuf_uint32(&wb, call->callid)
353 && writebuf_string(&wb, info ?: "")
354 && writebuf_object(&wb, obj)) {
355 rc = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
361 ERROR("error while sending success");
366 int afb_proto_ws_call_fail(struct afb_proto_ws_call *call, const char *status, const char *info)
369 struct writebuf wb = { .count = 0 };
371 if (writebuf_char(&wb, CHAR_FOR_ANSWER_FAIL)
372 && writebuf_uint32(&wb, call->callid)
373 && writebuf_string(&wb, status)
374 && writebuf_string(&wb, info ? : "")) {
375 rc = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
381 ERROR("error while sending fail");
386 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)
389 struct writebuf wb = { .count = 0 };
390 struct server_subcall *sc, *osc;
391 struct afb_proto_ws *protows = call->protows;
393 sc = malloc(sizeof *sc);
397 sc->callback = callback;
398 sc->closure = cb_closure;
400 pthread_mutex_unlock(&protows->mutex);
401 sc->subcallid = ptr2id(sc);
404 osc = protows->subcalls;
405 while(osc && osc->subcallid != sc->subcallid)
408 sc->next = protows->subcalls;
409 protows->subcalls = sc;
410 pthread_mutex_unlock(&protows->mutex);
412 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_CALL)
413 && writebuf_uint32(&wb, sc->subcallid)
414 && writebuf_uint32(&wb, call->callid)
415 && writebuf_string(&wb, api)
416 && writebuf_string(&wb, verb)
417 && writebuf_object(&wb, args)) {
418 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
425 ERROR("error while sending subcall");
430 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
433 struct writebuf wb = { .count = 0 };
435 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
436 && writebuf_uint32(&wb, call->callid)
437 && writebuf_uint32(&wb, (uint32_t)event_id)
438 && writebuf_string(&wb, event_name)) {
439 rc = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
445 ERROR("error while subscribing event");
450 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
453 struct writebuf wb = { .count = 0 };
455 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
456 && writebuf_uint32(&wb, call->callid)
457 && writebuf_uint32(&wb, (uint32_t)event_id)
458 && writebuf_string(&wb, event_name)) {
459 rc = afb_ws_binary_v(call->protows->ws, wb.iovec, wb.count);
465 ERROR("error while subscribing event");
470 /******************* client part **********************************/
472 /* search a memorized call */
473 static struct client_call *client_call_search(struct afb_proto_ws *protows, uint32_t callid)
475 struct client_call *call;
477 call = protows->calls;
478 while (call != NULL && call->callid != callid)
484 /* free and release the memorizing call */
485 static void client_call_destroy(struct client_call *call)
487 struct client_call **prv;
489 prv = &call->protows->calls;
490 while (*prv != NULL) {
500 /* get event data from the message */
501 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
503 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
506 /* get event from the message */
507 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
511 /* get event data from the message */
512 if (!readbuf_uint32(rb, &callid)) {
513 ERROR("Invalid message");
518 *call = client_call_search(protows, callid);
520 ERROR("message not found");
528 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
530 const char *event_name;
533 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
534 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
537 /* removes an event */
538 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
540 const char *event_name;
543 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
544 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
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);
558 /* unsubscribes an event */
559 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
561 const char *event_name;
563 struct client_call *call;
565 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
566 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
569 /* receives broadcasted events */
570 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
572 const char *event_name;
573 struct json_object *object;
575 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
576 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
580 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
582 const char *event_name;
584 struct json_object *object;
586 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
587 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
590 static void client_on_reply_success(struct afb_proto_ws *protows, struct readbuf *rb)
592 struct client_call *call;
593 struct json_object *object;
596 if (!client_msg_call_get(protows, rb, &call))
599 if (readbuf_string(rb, &info, NULL) && readbuf_object(rb, &object)) {
600 protows->client_itf->on_reply_success(protows->closure, call->request, object, info);
602 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process success");
604 client_call_destroy(call);
607 static void client_on_reply_fail(struct afb_proto_ws *protows, struct readbuf *rb)
609 struct client_call *call;
610 const char *info, *status;
612 if (!client_msg_call_get(protows, rb, &call))
615 if (readbuf_string(rb, &status, NULL) && readbuf_string(rb, &info, NULL)) {
616 protows->client_itf->on_reply_fail(protows->closure, call->request, status, info);
618 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process fail");
620 client_call_destroy(call);
623 /* send a subcall reply */
624 static int client_send_subcall_reply(struct afb_proto_ws *protows, uint32_t subcallid, int status, json_object *object)
626 struct writebuf wb = { .count = 0 };
627 char ie = status < 0;
629 return -!(writebuf_char(&wb, CHAR_FOR_SUBCALL_REPLY)
630 && writebuf_uint32(&wb, subcallid)
631 && writebuf_char(&wb, ie)
632 && writebuf_object(&wb, object)
633 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0);
636 /* callback for subcall reply */
637 int afb_proto_ws_subcall_reply(struct afb_proto_ws_subcall *subcall, int status, struct json_object *result)
639 int rc = client_send_subcall_reply(subcall->protows, subcall->subcallid, status, result);
640 afb_proto_ws_unref(subcall->protows);
641 free(subcall->buffer);
646 /* received a subcall request */
647 static void client_on_subcall(struct afb_proto_ws *protows, struct readbuf *rb)
649 struct afb_proto_ws_subcall *subcall;
650 struct client_call *call;
651 const char *api, *verb;
653 struct json_object *object;
655 /* get the subcallid */
656 if (!readbuf_uint32(rb, &subcallid))
659 /* if not expected drop it */
660 if (!protows->client_itf->on_subcall)
663 /* retrieve the message data */
664 if (!client_msg_call_get(protows, rb, &call))
667 /* allocation of the subcall */
668 subcall = malloc(sizeof *subcall);
673 if (readbuf_string(rb, &api, NULL)
674 && readbuf_string(rb, &verb, NULL)
675 && readbuf_object(rb, &object)) {
676 afb_proto_ws_addref(protows);
677 subcall->protows = protows;
678 subcall->subcallid = subcallid;
679 subcall->buffer = rb->base;
681 protows->client_itf->on_subcall(protows->closure, subcall, call->request, api, verb, object);
686 client_send_subcall_reply(protows, subcallid, 1, NULL);
689 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
692 struct client_describe *desc, **prv;
693 struct json_object *object;
695 if (readbuf_uint32(rb, &descid)) {
696 prv = &protows->describes;
697 while ((desc = *prv) && desc->descid != descid)
701 if (!readbuf_object(rb, &object))
703 desc->callback(desc->closure, object);
709 /* callback when receiving binary data */
710 static void client_on_binary(void *closure, char *data, size_t size)
712 struct afb_proto_ws *protows;
718 rb.end = data + size;
721 pthread_mutex_lock(&protows->mutex);
722 switch (*rb.head++) {
723 case CHAR_FOR_ANSWER_SUCCESS: /* success */
724 client_on_reply_success(protows, &rb);
726 case CHAR_FOR_ANSWER_FAIL: /* fail */
727 client_on_reply_fail(protows, &rb);
729 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
730 client_on_event_broadcast(protows, &rb);
732 case CHAR_FOR_EVT_ADD: /* creates the event */
733 client_on_event_create(protows, &rb);
735 case CHAR_FOR_EVT_DEL: /* removes the event */
736 client_on_event_remove(protows, &rb);
738 case CHAR_FOR_EVT_PUSH: /* pushs the event */
739 client_on_event_push(protows, &rb);
741 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
742 client_on_event_subscribe(protows, &rb);
744 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
745 client_on_event_unsubscribe(protows, &rb);
747 case CHAR_FOR_SUBCALL_CALL: /* subcall */
748 client_on_subcall(protows, &rb);
750 case CHAR_FOR_DESCRIPTION: /* description */
751 client_on_description(protows, &rb);
753 default: /* unexpected message */
754 /* TODO: close the connection */
757 pthread_mutex_unlock(&protows->mutex);
762 int afb_proto_ws_client_call(
763 struct afb_proto_ws *protows,
765 struct json_object *args,
766 const char *sessionid,
771 struct client_call *call;
772 struct writebuf wb = { .count = 0 };
774 /* allocate call data */
775 call = malloc(sizeof *call);
780 call->request = request;
783 pthread_mutex_lock(&protows->mutex);
784 call->callid = ptr2id(call);
785 while(client_call_search(protows, call->callid) != NULL)
787 call->protows = protows;
788 call->next = protows->calls;
789 protows->calls = call;
791 /* creates the call message */
792 if (!writebuf_char(&wb, CHAR_FOR_CALL)
793 || !writebuf_uint32(&wb, call->callid)
794 || !writebuf_string(&wb, verb)
795 || !writebuf_string(&wb, sessionid)
796 || !writebuf_object(&wb, args)) {
802 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
809 client_call_destroy(call);
811 pthread_mutex_unlock(&protows->mutex);
815 /* get the description */
816 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
818 struct client_describe *desc, *d;
819 struct writebuf wb = { .count = 0 };
821 desc = malloc(sizeof *desc);
827 /* fill in stack the description of the task */
828 pthread_mutex_lock(&protows->mutex);
829 desc->descid = ptr2id(desc);
830 d = protows->describes;
832 if (d->descid != desc->descid)
836 d = protows->describes;
839 desc->callback = callback;
840 desc->closure = closure;
841 desc->protows = protows;
842 desc->next = protows->describes;
843 protows->describes = desc;
844 pthread_mutex_unlock(&protows->mutex);
847 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
848 && writebuf_uint32(&wb, desc->descid)
849 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0)
852 pthread_mutex_lock(&protows->mutex);
853 d = protows->describes;
855 protows->describes = desc->next;
857 while(d && d->next != desc)
860 d->next = desc->next;
863 pthread_mutex_unlock(&protows->mutex);
865 /* TODO? callback(closure, NULL); */
869 /******************* client description part for server *****************************/
871 /* on call, propagate it to the ws service */
872 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
874 struct afb_proto_ws_call *call;
875 const char *uuid, *verb;
878 struct json_object *object;
880 afb_proto_ws_addref(protows);
882 /* reads the call message data */
883 if (!readbuf_uint32(rb, &callid)
884 || !readbuf_string(rb, &verb, &lenverb)
885 || !readbuf_string(rb, &uuid, NULL)
886 || !readbuf_object(rb, &object))
889 /* create the request */
890 call = malloc(sizeof *call);
894 call->protows = protows;
895 call->callid = callid;
897 call->buffer = rb->base;
898 rb->base = NULL; /* don't free the buffer */
900 protows->server_itf->on_call(protows->closure, call, verb, object, uuid);
904 json_object_put(object);
907 afb_proto_ws_unref(protows);
910 /* on subcall reply */
911 static void server_on_subcall_reply(struct afb_proto_ws *protows, struct readbuf *rb)
915 struct json_object *object;
916 struct server_subcall *sc, **psc;
918 /* reads the call message data */
919 if (!readbuf_uint32(rb, &subcallid)
920 || !readbuf_char(rb, &ie)
921 || !readbuf_object(rb, &object)) {
922 /* TODO bad protocol */
926 /* search the subcall and unlink it */
927 pthread_mutex_lock(&protows->mutex);
928 psc = &protows->subcalls;
929 while ((sc = *psc) && sc->subcallid != subcallid)
932 pthread_mutex_unlock(&protows->mutex);
933 json_object_put(object);
934 /* TODO subcall not found */
937 pthread_mutex_unlock(&protows->mutex);
938 sc->callback(sc->closure, -(int)ie, object);
943 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
945 struct writebuf wb = { .count = 0 };
947 return -!(writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
948 && writebuf_uint32(&wb, descid)
949 && writebuf_object(&wb, descobj)
950 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0);
953 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
955 int rc = server_send_description(describe->protows, describe->descid, description);
956 afb_proto_ws_addref(describe->protows);
961 /* on describe, propagate it to the ws service */
962 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
965 struct afb_proto_ws_describe *desc;
967 /* reads the descid */
968 if (readbuf_uint32(rb, &descid)) {
969 if (protows->server_itf->on_describe) {
970 /* create asynchronous job */
971 desc = malloc(sizeof *desc);
973 desc->descid = descid;
974 desc->protows = protows;
975 afb_proto_ws_addref(protows);
976 protows->server_itf->on_describe(protows->closure, desc);
980 server_send_description(protows, descid, NULL);
984 /* callback when receiving binary data */
985 static void server_on_binary(void *closure, char *data, size_t size)
987 struct afb_proto_ws *protows;
993 rb.end = data + size;
996 switch (*rb.head++) {
998 server_on_call(protows, &rb);
1000 case CHAR_FOR_SUBCALL_REPLY:
1001 server_on_subcall_reply(protows, &rb);
1003 case CHAR_FOR_DESCRIBE:
1004 server_on_describe(protows, &rb);
1006 default: /* unexpected message */
1007 /* TODO: close the connection */
1014 /******************* server part: manage events **********************************/
1016 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
1018 struct writebuf wb = { .count = 0 };
1020 return -!(writebuf_char(&wb, order)
1021 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
1022 && writebuf_string(&wb, event_name)
1023 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))
1024 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0);
1027 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
1029 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
1032 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
1034 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
1037 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
1039 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
1042 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
1044 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
1047 /*****************************************************/
1049 /* callback when receiving a hangup */
1050 static void on_hangup(void *closure)
1052 struct afb_proto_ws *protows = closure;
1053 struct server_subcall *sc, *nsc;
1054 struct client_describe *cd, *ncd;
1056 nsc = protows->subcalls;
1060 sc->callback(sc->closure, 1, NULL);
1064 ncd = protows->describes;
1068 cd->callback(cd->closure, NULL);
1072 if (protows->fd >= 0) {
1074 if (protows->on_hangup)
1075 protows->on_hangup(protows->closure);
1079 /*****************************************************/
1081 static const struct afb_ws_itf proto_ws_client_ws_itf =
1085 .on_binary = client_on_binary,
1087 .on_hangup = on_hangup
1090 static const struct afb_ws_itf server_ws_itf =
1094 .on_binary = server_on_binary,
1096 .on_hangup = on_hangup
1099 /*****************************************************/
1101 static struct afb_proto_ws *afb_proto_ws_create(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)
1103 struct afb_proto_ws *protows;
1105 protows = calloc(1, sizeof *protows);
1106 if (protows == NULL)
1109 fcntl(fd, F_SETFD, FD_CLOEXEC);
1110 fcntl(fd, F_SETFL, O_NONBLOCK);
1111 protows->ws = afb_ws_create(afb_common_get_event_loop(), fd, itf, protows);
1112 if (protows->ws != NULL) {
1114 protows->refcount = 1;
1115 protows->subcalls = NULL;
1116 protows->closure = closure;
1117 protows->server_itf = itfs;
1118 protows->client_itf = itfc;
1119 pthread_mutex_init(&protows->mutex, NULL);
1127 struct afb_proto_ws *afb_proto_ws_create_client(int fd, const struct afb_proto_ws_client_itf *itf, void *closure)
1129 return afb_proto_ws_create(fd, NULL, itf, closure, &proto_ws_client_ws_itf);
1132 struct afb_proto_ws *afb_proto_ws_create_server(int fd, const struct afb_proto_ws_server_itf *itf, void *closure)
1134 return afb_proto_ws_create(fd, itf, NULL, closure, &server_ws_itf);
1137 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1139 if (!__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1140 afb_proto_ws_hangup(protows);
1141 afb_ws_destroy(protows->ws);
1142 pthread_mutex_destroy(&protows->mutex);
1147 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1149 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1152 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1154 return !!protows->client_itf;
1157 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1159 return !!protows->server_itf;
1162 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1164 afb_ws_hangup(protows->ws);
1167 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1169 protows->on_hangup = on_hangup;