czeromqnanomsg

After doing nn_send, the nn_socket will forbid to send the next piece of data immediately in a Req/Rep mode


I'm using nanomsg to transfer pieces of my data from Windows to CentOS.

At the moment, I'm using Req/Rep mode.

The CentOS will send a Req-request, and "Windows" will respond with a Rep to it.

It looks alright.

But the pieces of data I respond to have mixed several structs. A data head following with several data frames.

I want to send them one by one.

But according to nanomsg, Req/Rep should just send a request, and receive a response one at a time. That's all.

So if I send the head, the nn_socket will forbid me to send the data frame.

What I can do is to make a big buffer, and cement the head and the data frames, and send them together.

Is there a way to send them one by one instead of sending together?

Thanks!


Solution

  • Something like Google Protocol Buffers will be your friend here.

    nanomsg sends complete messages. It's then simply a matter of what a "message" is. Essentially, it's just a set of bytes that it guarantees to deliver. It's up to you what those bytes actually represent.

    This is where Google Protocol Buffers (or JSON, or XML, or ASN.1 [my personal favourite], and there's many others to choose from) comes in. It allows you to define message structures that meet your needs, and it then generates code that serialises and deserialises those messages. You write your code to serialise / deserialise to buffers (byte arrays, strings, whatever), which you pass as messages using nanomsg.

    The nice thing is that you then don't have to worry about endianness, structure packing in different compilers, etc, or what programming language is being used at the other end, or whether the message in its deserialised form is contiguous in memory.

    I mention GPB because it is free, fairly straightforward