Send complex data structure via boost message queue

tanlccc picture tanlccc · Sep 10, 2012 · Viewed 10.2k times · Source

I have the following data structure:

typedef struct
{
    short id;
    string name;
    short age;
} person_struct;

Using boost message queue, I tried to send this data structure to the message queue receiver in another process. However, after receiving, I have segmentation fault when accessing the 'name' variable in the above structure.

Below is my sender function:

person_struct MyRec;
MyRec.id = 1;
MyRec.name = "ABC123";
MyRec.age = 20;   
message_queue mqSender(create_only, "MSG_Q", 100, sizeof(person_struct));
mqSender.send(&MyRec, sizeof(person_struct), MQ_PRIORITY);

Below is my receiver function:

message_queue myReceiver(open_only, "MSG_Q");
person_struct *recvMsg = new person_struct();
size_t msg_size;
unsigned int priority;
myReceiver.receive(recvMsg, sizeof(person_struct), msg_size, priority);
cout << "ID: " << (*recvMsg).id << endl;
cout << "Name: " << (*recvMsg).name << endl;
cout << "Age: " << (*recvMsg).age << endl;

The cout for (*recvMsg).id is okay, but segmentation fault occurred at cout for (*recvMsg).name. Read somewhere that I need to do serialization for the structure, but can't figure out how to do it. Can anyone suggest?

Answer

Vikas picture Vikas · Sep 10, 2012

From the boost doc for message queue:

A message queue just copies raw bytes between processes and does not send objects. This means that if we want to send an object using a message queue the object must be binary serializable. For example, we can send integers between processes but not a std::string. You should use Boost.Serialization or use advanced Boost.Interprocess mechanisms to send complex data between processes.

Use Boost.Serialization to serialize your object and de-serialize on receiving end.

Some quick, working code:

info.hpp

#include <boost/serialization/string.hpp>

#define MAX_SIZE 1000

class info
{
    public:
        info (int i = 0, std::string n = "")
            : id(i), name(n)
        {};

        int id;
        std::string name;

    private:
        friend class boost::serialization::access;

        template<class Archive>
            void serialize(Archive & ar, const unsigned int version)
            {  
                ar & id;
                ar & name;
            }
};

send.cpp

#include <string>
#include <sstream>

#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/archive/text_oarchive.hpp>

#include "info.hpp"

using namespace boost::interprocess;

int main ()
{
    try
    {  
        message_queue mq
            (
             open_or_create,
             "mq",
             100,
             MAX_SIZE
            );

        info me(1, "asdf");

        std::stringstream oss;

        boost::archive::text_oarchive oa(oss);
        oa << me;

        std::string serialized_string(oss.str());
        mq.send(serialized_string.data(), serialized_string.size(), 0);
    }
    catch(interprocess_exception &ex)
    {  
        std::cerr << ex.what() << std::endl;
    }
}

receive.cpp

#include <string>
#include <iostream>

#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/archive/text_iarchive.hpp>

#include "info.hpp"

using namespace boost::interprocess;

int main ()
{
    try
    {  
        message_queue mq
            (
             open_only,
             "mq"
            );
        message_queue::size_type recvd_size;
        unsigned int priority;

        info me;

        std::stringstream iss;
        std::string serialized_string;
        serialized_string.resize(MAX_SIZE);
        mq.receive(&serialized_string[0], MAX_SIZE, recvd_size, priority);
        iss << serialized_string;

        boost::archive::text_iarchive ia(iss);
        ia >> me;

        std::cout << me.id << std::endl;
        std::cout << me.name << std::endl;
    }
    catch(interprocess_exception &ex)
    {  
        std::cerr << ex.what() << std::endl;
    }

    message_queue::remove("mq");
}