I am currently trying to use msgpack in a project written in C. I am using msgpack to serialize the contents of a structure, which then needs to be sent over the network and deserialized back to the corresponding struct on the other side.
A compressed version of what I'm trying to do:
#include <stdio.h> #include <msgpack.h> #include <stdbool.h> typedef someStruct{ uint32_t a; uint32_t b; float c; } someStruct; int main (void){ someStruct data; /* ... Fill 'data' with some data for test purposes ...*/ msgpack_sbuffer* buff = msgpack_sbuffer_new(); msgpack_packer* pck = msgpack_packer_new(buff, msgpack_sbuffer_write); someStruct* structs = malloc(sizeof(someStruct) * 10); /* ... Fill 'structs' with members containing test data ... */ // Serialize msgpack_pack_array (pck, 10); int i; for(i = 0 ; i < 10 ; i++){ msgpack_pack_array (pck, 3); msgpack_pack_uint32 (pck, structs[i].a); msgpack_pack_uint32 (pck, structs[i].b); msgpack_pack_float (pck, structs[i].c); } free(structs); msgpack_packer_free(pck); // Deserialize msgpack_unpacked msg; msgpack_unpacked_init(&msg); bool deserialize_success = msgpack_unpack_next (&msg, buff->data, buff->size, NULL); if(!deserialize_success) /* Error */ msgpack_object obj = msg.data; msgpack_object_print(stdout,obj); // This seems to work perfectly, indicating serialize / deserialize works as intended... someStruct deserialized_data; /* Insert code to extract and cast deserialized data to 'deserialized_data */ // Clean msgpack_sbuffer_free(buff); msgpack_packer_free(pck); return 0; }
The specified code is more or less torn right from here , which seems to be one of the few msgpack-c resources.
Can someone point me in the right direction to a way to "recreate" the original structure on the other side of the wire? The only way to find deserialized data is to use the msgpack_object_print () call to print from the messagepack_object file. However, this works, so I'm sure there is data.
Do I need to somehow iterate over serialized data and use msgpack_unpack_next () with an offset to extract each element of someStruct? Using memcpy for local byte buffer?
Any help is much appreciated!
source share