Useful ZMQ Message Helper Classes

If you use ZMQ in C/C++, be sure to check this out.

https://github.com/pijyoi/msg_parts

Why use this, u say?The advantage is that msg_single_t/msg_multi_t objects on the stack get cleaned up automatically. It helps to clean up a lot of boilerplate code.

Previously,

zmq_msg_t msg; 
zmq_msg_init(&msg); 
zmq_msg_init_size(&msg, sizeof(int)); 
int payload = zmq_msg_recv(&msg, zpipe, 0); 
zmq_msg_close(&msg); 

Can now be replaced by

msg_single_t msg(sizeof(int)); 
int payload = msg.recv(zpipe, 0); 

Closing the msg is done for you when msg goes out of scope. This helps to reduce memory leaks / zmq errors due to not closing the message properly. Pretty, pretty code!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: