I am using Msgpack [1] to serialize data into packets. Your example could be
as simple as:
msgpack_sbuffer *buffer = msgpack_sbuffer_new();
msgpack_sbuffer_init(buffer);
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
/// pack
msgpack_pack_int(pk, s2.header);
msgpack_pack_raw(pk, 5);
msgpack_pack_raw_body(pk, s2.data, 5);
msgpack_packer_free(pk);
zmq_msg_t msg;
zmq_msg_init_data(&msg, buffer->data, buffer->size, free_msgpack_msg,
buffer);
zmq_send(socket_, &msg, 0);
The "free_msgpack_msg" argument is a function called to destroy the buffer:
void free_msgpack_msg(void *data, void *buffer) {
msgpack_sbuffer_free((msgpack_sbuffer*)buffer);
}
You can see a more complete example (encodes lua values) in Lubyk's source
code [2], usage with zmq [3].
[1] http://msgpack.org/
[2] (github) http://bit.ly/hnaGgR
[3] (github) http://bit.ly/gZhLSX
On Thu, Mar 3, 2011 at 11:14 AM, Sven Koebnick <[email protected]>wrote:
> you have to:
> - create a zmq msg with sufficient size
> - copy the struct into the msg buffer
> This means, that you need a func, that knows the struct and copies the data
> of the inner pointers etc.
> In short: you need to write a serialization function for your struct.
>
> ^5
> Sven
> ===================
> sent by the iPhone
>
> Am 03.03.2011 um 08:30 schrieb Rakesh Kumar Jha <[email protected]>:
>
>
> struct msg1
> {
> int header;
> char data[5];
> }msg1;
>
>
> int main(int argc, char *argv[])
> {
> void *context = zmq_init(1);
> struct msg1 s2;
>
> .......................
> ........................
> and s_send(sender,s2);
>
>
> .................
> ..............
> create a error
>
>
>
> so please is there any way to send msg which supports structure sending
> over a socket...
>
> --
> Thanks & Regards,
> Rakesh Kumar Jha
>
> _______________________________________________
>
> zeromq-dev mailing list
> [email protected]
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
> _______________________________________________
> zeromq-dev mailing list
> [email protected]
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev