add funcs to binary seralize/deserialize switch_events into a contiguous binary frame

This commit is contained in:
Anthony Minessale committed 2012-09-13 17:46:28 -05:00
1 parent c28c99c394
commit d5e5fb032a
2 files changed
+108 -1

No files matched your search

+92 -1
View 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;