0

我做了一些测试与提升进程和ptree结构,我有段错误,当我尝试阅读发送的消息(或当我尝试解析它在json中)。boost json序列化和message_queue段错误

我在debian linux上使用boost1.49。

我正在序列化它在json中以备后用,并且因为我没有找到任何好的文档来直接序列化boost属性三。

这是我使用的测试代码(commed说得清的段错误的):

recv.cc

#include <boost/property_tree/ptree.hpp> 
#include <boost/property_tree/json_parser.hpp> 
#include <boost/interprocess/ipc/message_queue.hpp> 
#include <sstream> 



struct test_data{ 
    std::string action; 
    std::string name; 
    int faceID; 
    uint32_t Flags; 
    uint32_t freshness; 
}; 

test_data recvData() 
{ 
    boost::interprocess::message_queue::remove("queue"); 
    boost::property_tree::ptree pt; 
    test_data data; 
    std::istringstream buffer; 
    boost::interprocess::message_queue mq(boost::interprocess::open_or_create,"queue", 1, sizeof(buffer) 
    boost::interprocess::message_queue::size_type recvd_size; 
    unsigned int pri; 
    mq.receive(&buffer,sizeof(buffer),recvd_size,pri); 
    std::cout << buffer.str() << std::endl; //the segfault is there 
    boost::property_tree::read_json(buffer,pt); 
    data.action = pt.get<std::string>("action"); 
    data.name = pt.get<std::string>("name"); 
    data.faceID = pt.get<int>("face"); 
    data.Flags = pt.get<uint32_t>("flags"); 
    data.freshness = pt.get<uint32_t>("freshness"); 
    boost::interprocess::message_queue::remove("queue"); 
    return data; 
} 

int main() 
{ 
    test_data test; 
    test = recvData(); 
    std::cout << test.action << test.name << test.faceID << test.Flags << test.freshness << std::endl; 
} 

sender.cc

#include <boost/property_tree/ptree.hpp> 
#include <boost/property_tree/json_parser.hpp> 
#include <boost/interprocess/ipc/message_queue.hpp> 
#include <sstream> 


struct test_data{ 
    std::string action; 
    std::string name; 
    int faceID; 
    uint32_t Flags; 
    uint32_t freshness; 
}; 


int sendData(test_data data) 
{ 
    boost::property_tree::ptree pt; 
    pt.put("action",data.action); 
    pt.put("name",data.name); 
    pt.put("face",data.faceID); 
    pt.put("flags",data.Flags); 
    pt.put("freshness",data.freshness); 
    std::ostringstream buffer; 
    boost::property_tree::write_json(buffer,pt,false); 
    boost::interprocess::message_queue mq(boost::interprocess::open_only,"chiappen") 
    std::cout << sizeof(buffer) << std::endl; 
    mq.send(&buffer,sizeof(buffer),0); 
    return 0; 
} 


int main() 
{ 
    test_data prova; 
    prova.action = "registration"; 
    prova.name = "prefix"; 
    prova.Flags = 0; 
    prova.freshness = 52; 
    sendData(prova); 
} 
+0

我相信这是我不明白std:stringstream对象的东西 – kurojishi

回答

3

我知道现在对于答案有点迟,但无论如何.. 您无法将istringstream作为缓冲区接收。 Boost消息队列只处理原始字节,不处理std类似的对象。

要使其工作,您必须使用char数组或任何先前用malloc保留的缓冲区。

例:

char buffer [1024]; 
mq.receive(buffer, sizeof(buffer), recvd_size, pri); 

对于发送这是相同的,你只能发送原始字节,所以你不能使用ostringstream。

希望它有帮助。

+0

是这是答案,我忘了autoreply的问题谢谢:) – kurojishi