Boost logo

Boost Users :

Subject: [Boost-users] sending a serialized object using message_queue
From: Luciano Afranllie (listas.luafran_at_[hidden])
Date: 2009-12-10 22:04:02


I am trying to send a serialized object using message_queue but I am
getting a boost::interprocess_exception::library_error in the sender
I think the problem is me but after a lot of google I could not find a
way to make it work

This is the method where I am trying to send the message:

int RemoteMgr::sendMsgToPeers(const IPCMsg &msg)
    std::cout << "RemoteMgr::sendMsgToPeers()\n";

    std::ostringstream archive_stream;
    boost::archive::text_oarchive archive(archive_stream);
    archive << msg;
    std::string outbound_data = archive_stream.str();
    std::vector<char> outbound(outbound_data.begin(), outbound_data.end());

    std::cout << "outbound_data = [" << outbound_data << "]\n";
    std::cout << "size = " << outbound.size() << "\n";

    // TODO: Not working yet. How to send a string with serialized data?
    message_queue mq_master(open_only, "master_queue");
    mq_master.send(&outbound, outbound.size(), 0);

Where IPCMsg is

class IPCMsg
    IPCMsg() {}
    IPCMsg(const std::string &type, const std::string &payload);

    std::string getSender() { return m_sender; }
    std::string getType() { return m_type; }
    std::string getPayload() { return m_payload; }

    template<class Archive>
    void serialize(Archive & ar, const unsigned int /* file_version */)
        ar & BOOST_SERIALIZATION_NVP(m_sender)
           & BOOST_SERIALIZATION_NVP(m_type)
           & BOOST_SERIALIZATION_NVP(m_payload);

    std::string m_sender;
    std::string m_type;
    std::string m_payload;

Receiver code is like this:

        unsigned int priority;
        std::size_t recvd_size;
        IPCMsg msg;
        std::vector<char> inbound_data(100);

        std::cout << "Waiting for peers" << std::endl;

        while (!terminate)
            if (mq.try_receive(&inbound_data, inbound_data.size(),
recvd_size, priority))
                std::cout << "New message received.\n";
                std::string archive_data(&inbound_data[0], inbound_data.size());
                std::istringstream archive_stream(archive_data);
                boost::archive::text_iarchive archive(archive_stream);
                archive >> msg;
                std::cout << "msg type = " << msg.getType() << "\n";

Hope you can help. Thanks!

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at