【问题标题】:How to pass complex objects ( std::string ) through boost::interprocess::message queue如何通过 boost::interprocess::message 队列传递复杂对象( std::string )
【发布时间】:2011-03-26 08:11:44
【问题描述】:

是否有人有一些示例代码显示序列化 std::string 通过 boost::interprocess::message_queue 发送它并再次将其返回的管道?

【问题讨论】:

    标签: c++ boost message-queue


    【解决方案1】:

    您需要对数据进行序列化,因为 boost::interprocess::message_queue 对字节数组进行操作。如果你所有的消息都是字符串,那就这样做:

    size_t const max_msg_size = 0x100;
    boost::interprocess::message_queue q(..., max_msg_size);
    
    // sending
    std::string s(...);
    q.send(s.data(), s.size(), 0);
    
    // receiving
    std::string s;
    s.resize(max_msg_size);
    size_t msg_size;
    unsigned msg_prio;
    q.receive(&s[0], s.size(), msg_size, msg_prio);
    s.resize(msg_size);
    

    【讨论】:

      【解决方案2】:

      一种解决方案可以是编写一个函数,该函数将要发送的对象(例如,可变大小的字符串)作为输入,并将固定大小的对象容器作为输出。

      类似:

      int Tokenize(std::vector<MessageToken>&, const Message&) const;
      int Merge(Message&, const std::vector<MessageToken>&) const;                                    
      

      然后可以将这些固定大小的对象发送到message_queue中/从message_queue中提取。

      与Maxim的方案相比的优势在于不需要指定max_msg_size参数。

      【讨论】:

        猜你喜欢
        • 2011-09-17
        • 2011-11-18
        • 2013-06-25
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 2020-11-17
        • 2018-07-24
        • 1970-01-01
        相关资源
        最近更新 更多