]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add funcs to binary seralize/deserialize switch_events into a contiguous binary frame
authorAnthony Minessale <anthm@freeswitch.org>
Thu, 13 Sep 2012 22:46:13 +0000 (17:46 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Thu, 13 Sep 2012 22:46:28 +0000 (17:46 -0500)
src/include/switch_event.h
src/switch_event.c

index d0474a6929eba4688a6fef6c54957ad9df617500..cc78336655a34ad13c284f9dced992997f3b7b64 100644 (file)
@@ -102,6 +102,20 @@ struct switch_event {
        int flags;
 };
 
+typedef struct switch_serial_event_s {
+       int event_id;
+       int priority;
+       int flags;
+       char *owner;
+       char *subclass_name;
+       char *body;
+} switch_serial_event_t;
+
+typedef struct switch_serial_event_header_s {
+       char *name;
+       char *value;
+} switch_serial_event_header_t;
+
 typedef enum {
        EF_UNIQ_HEADERS = (1 << 0),
        EF_NO_CHAT_EXEC = (1 << 1),
@@ -295,6 +309,8 @@ SWITCH_DECLARE(switch_status_t) switch_event_free_subclass_detailed(const char *
   \return SWITCH_STATUS_SUCCESS if the operation was successful
   \note you must free the resulting string when you are finished with it
 */
+SWITCH_DECLARE(switch_status_t) switch_event_binary_deserialize(switch_event_t **eventp, void **data, switch_size_t len, switch_bool_t destroy);
+SWITCH_DECLARE(switch_status_t) switch_event_binary_serialize(switch_event_t *event, void **data, switch_size_t *len);
 SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, char **str, switch_bool_t encode);
 SWITCH_DECLARE(switch_status_t) switch_event_serialize_json(switch_event_t *event, char **str);
 SWITCH_DECLARE(switch_status_t) switch_event_create_json(switch_event_t **event, const char *json);
index 242165d69f77c7639d74b5d9fdd4d6f2b5b5cd6f..fdb397de0132df99bf9c04853243636d0d1c32d0 100644 (file)
@@ -34,7 +34,7 @@
 
 #include <switch.h>
 #include <switch_event.h>
-
+#include "tpl.h"
 
 //#define SWITCH_EVENT_RECYCLE
 #define DISPATCH_QUEUE_LEN 100
@@ -1284,6 +1284,97 @@ SWITCH_DECLARE(switch_status_t) switch_event_dup_reply(switch_event_t **event, s
        return SWITCH_STATUS_SUCCESS;
 }
 
+#define SWITCH_SERIALIZED_EVENT_MAP "S(iiisss)A(S(ss))"
+
+SWITCH_DECLARE(switch_status_t) switch_event_binary_deserialize(switch_event_t **eventp, void **data, switch_size_t len, switch_bool_t destroy)
+{
+       switch_event_t *event;
+       tpl_node *tn;
+       switch_serial_event_t e;
+       switch_serial_event_header_t sh;
+       int how = TPL_MEM;
+
+       switch_event_create(&event, SWITCH_EVENT_CLONE);
+       switch_assert(event);
+
+       tn = tpl_map(SWITCH_SERIALIZED_EVENT_MAP, &e, &sh);
+
+       if (!destroy) {
+               how |= TPL_EXCESS_OK;
+       }
+
+       tpl_load(tn, how, data, len);
+
+       tpl_unpack(tn, 0);
+
+       event->event_id = e.event_id;
+       event->priority = e.priority;
+       event->flags = e.flags;
+
+       event->owner = e.owner;
+       event->subclass_name = e.subclass_name;
+       event->body = e.body;
+
+
+       while(tpl_unpack(tn, 1)) {
+               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, sh.name, sh.value);
+       }
+
+       *eventp = event;
+
+       tpl_free(tn);
+
+       if (destroy) {
+               free(*data);
+       }
+
+       *data = NULL;
+
+       return SWITCH_STATUS_SUCCESS;
+
+}
+
+SWITCH_DECLARE(switch_status_t) switch_event_binary_serialize(switch_event_t *event, void **data, switch_size_t *len)
+{
+       tpl_node *tn;
+       switch_serial_event_t e;
+       switch_serial_event_header_t sh;
+       switch_event_header_t *eh;
+       int how = TPL_MEM;
+
+       e.event_id = event->event_id;
+       e.priority = event->priority;
+       e.flags = event->flags;
+
+       e.owner = event->owner;
+       e.subclass_name = event->subclass_name;
+       e.body = event->body;
+
+       tn = tpl_map(SWITCH_SERIALIZED_EVENT_MAP, &e, &sh);
+
+       tpl_pack(tn, 0);
+       
+       for (eh = event->headers; eh; eh = eh->next) {
+               if (eh->idx) continue;  // no arrays yet
+       
+               sh.name = eh->name;
+               sh.value = eh->value;
+               
+               tpl_pack(tn, 1);
+       }
+
+       if (*len > 0) {
+               how |= TPL_PREALLOCD;
+       }
+
+       tpl_dump(tn, how, data, len);
+
+       tpl_free(tn);
+
+       return SWITCH_STATUS_SUCCESS;
+}
+
+
 SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, char **str, switch_bool_t encode)
 {
        switch_size_t len = 0;