3

The functions for sending and receiving messages as follows:

int zmq_send (void *socket, void *buf, size_t len, int flags);
int zmq_recv (void *socket, void *buf, size_t len, int flags);

However, from documentation it's not clear when I have to use zmq_msg_t or my custom data directly. So in which cases I should consider using zmq_msg_t and in which case send my data directly?

Pablo
  • 28,133
  • 34
  • 125
  • 215

2 Answers2

2

Both of those functions will construct a zmq_msg_tinternally then use the zmq_msg_t variant of the function. This is convenient but if you want to send the same message multiple times it would be more efficient (less copying) to create a zmq_msg_t and use those variants.

David
  • 1,510
  • 14
  • 20
  • Yes, in case there is some remarkable amount of re-use of the once initialised structure, there is a reason to go this way. [+1 to David] – user3666197 May 23 '16 at 21:13
  • It's strange, because if I don't use `zmq_msg_t` to send message through `zmq_send`, then on receiving endpoint I am not getting `zmq_msg_t`'s internal data structure. If using it, I am getting 16 byte internal data at the beginning, which is useless for my receiving application. – Pablo May 23 '16 at 21:54
  • How is the data you're trying to sending formatted? (what data types etc?) – David May 23 '16 at 21:57
  • My other post have that all: http://stackoverflow.com/questions/37397992/how-to-extract-data-from-message-in-c-sharp-zeromq – Pablo May 23 '16 at 21:59
  • If internally it wraps my data into `zmq_msg_t`, then it appears I just don't have to wrap myself anymore and that will solve also the original question to be solved. – Pablo May 23 '16 at 22:04
1

Never TLDR

Be rather careful on how to setup, load/unload and consume/destroy the supporting low-level data structures that the cool and powerful ZeroMQ-library services rely on.

Otherwise, one has better to rather use the high-level companion functions.


Do not hesitate to read The Book - worth one's time

Pieter HINTJENS recommends in page 42, item 4:

To release (not destroy) a message, you call zmq_msg_close(). This drops a reference, and eventually 0MQ will destroy the message.

In API documentation you may find even stronger caution:

The zmq_msg_close() function shall inform the ØMQ infrastructure
that any resources associated with the message object
referenced by msg are no longer required and may be released.

Actual release of resources associated with the message object
shall be postponed by ØMQ until all users of the message or underlying
data buffer have indicated it is no longer required.

Applications should ensure that zmq_msg_close() is called
once a message is no longer required, otherwise memory leaks may occur.

Never access zmq_msg_t members directly, instead always use the zmq_msg family of functions.

Return value:
              zmq_msg_close() function shall return zero if successful.
                              Otherwise it shall return -1 and
                              set errno to one of the values defined below.
Errors
              EFAULT          Invalid message.
halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92