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