0

I'm working on inherited codebase that uses POSIX Message Queues to pass data between processes.

Because of the nature of mq, the messages have to be passed in form of bytes and the numbytes passed along so when the messages are reconstructed, there's a lot of ugly static_cast<PVOID>-ing.

QUESTION

Is there an interprocess message passing interface in Linux that maintains the datatype of the content?

Or is there a better way of doing this?

Bob
  • 4,576
  • 7
  • 39
  • 107
  • Look for 'serialization'. There are libraries which simplify this task. – SergeyA Sep 12 '16 at 18:58
  • @SergeyA how will that help if there can be different types in the same queue? – Bob Sep 12 '16 at 18:59
  • @Adrian You can have a type as part of serialized message. You can use for example [TLV](https://cs.wikipedia.org/wiki/Type-length-value) to encoding (used for example in SNMP) or JSON, ... – KIIV Sep 12 '16 at 19:13
  • @Adrian, this is the very task serialization libraries try to accomplish. – SergeyA Sep 12 '16 at 19:20
  • Looks here there are several good suggestions, http://stackoverflow.com/questions/234724/is-it-possible-to-serialize-and-deserialize-a-class-in-c – Matthew Fisher Sep 12 '16 at 19:48
  • Be careful about using complex serialization in performance sensitive code. Getting bytes off the wire and slamming them into a POD struct is much faster than a complex serialization model which include factories, type safety, inheritance, etc. – Matthew Fisher Sep 12 '16 at 19:50
  • @SergeyA is it possible to "register a typedef" with the OS? – Bob Sep 12 '16 at 20:56
  • @Adrian, how do you mean? – SergeyA Sep 13 '16 at 14:41
  • @SergeyA if you register a type then maybe you wouldn't have to serialize it? idk. just spitballing. – Bob Sep 13 '16 at 14:42
  • @Adrian, no. OS is agnostic to any types your program might introduce. – SergeyA Sep 13 '16 at 14:44
  • @SergeyA what I mean is: is there any way to do an interprocess message transfer of type `my_type` and have it be no different than a function call passing a parameter of type `my_type`? – Bob Sep 13 '16 at 14:47
  • 1
    @Adrian, and I have been trying to explain you this - there is no such way out of the box in C++ (there is in other languages). You will have to serialize your messages and deserialize them on the other end. There are frameworks/libraries which assist in this task with various degrees of success. – SergeyA Sep 13 '16 at 14:49
  • @SergeyA ok I believe you. Just wanted to be really really sure there was no other way. – Bob Sep 13 '16 at 14:50
  • 1
    @Adrian, Google Protocol Buffers isn't a bad bet for this kind of thing. You can use a oneof as a means of sending many different types of object through a message queue, and the receiving end can tell what type of object has just arrived. The problem of using message queues is that you have to know in advance how big the serialised message is going to be. Using something like ZeroMQ instead is an alternative way of shifting bytes around with much more flexibility. – bazza Sep 24 '16 at 07:52

0 Answers0