diff --git a/src/include/switch_event.h b/src/include/switch_event.h index d0474a6929..cc78336655 100644 --- a/src/include/switch_event.h +++ b/src/include/switch_event.h @@ -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); diff --git a/src/switch_event.c b/src/switch_event.c index 242165d69f..fdb397de01 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -34,7 +34,7 @@ #include #include - +#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;