【问题标题】:boost::asio and synchrounous read causing compile errorsboost::asio 和同步读取导致编译错误
【发布时间】:2020-03-26 08:04:24
【问题描述】:

我正在尝试让客户端和服务器进行对话,并且正在传递的数据是结构的向量

std::vector<element> elemvec

这是一个类的私有成员,但我们有方法可以使用bloom_db.getelem(unsigned int)检索该向量的各个元素。

在 boost 库代码中编译失败。

我了解我在流、缓冲区和序列化方面的知识存在很大缺陷,但我似乎需要一些帮助,因为我不想从 boost 示例中复制代码。

以下是我的代码:

typedef struct elem{        //This will be seen as a POD (Plain Old Data) in boost::asio::buffer
    bool bit;
    int  count;
    uint64_t  hashSum[2];
    uint64_t  idSum;

    template <typename Archive>
        void serialize(Archive &ar, const unsigned int version){
            ar & bit;
            ar & count;
            ar & hashSum[0];ar & hashSum[1];
            ar & idSum;
        }
    }element;

    void sendData(tcp::socket& socket, boost::asio::const_buffer& data)
    {
        boost::asio::write(socket,boost::asio::buffer(data));
    }


    void getData(tcp::socket& socket, boost::asio::mutable_buffer& data)
    {
        boost::asio::read(socket, data);
    }


    void netcom_client(int portn, string serverip, bloom_filter& bloom_db){
        io_service io_service;
        ip::tcp::socket client_socket(io_service);
        boost::asio::const_buffer snd_buf;
        boost::system::error_code ec;
        element snd_elem;
        string bufdata;
        std::ostringstream os;
        boost::archive::text_oarchive out_archive {os};   //archive is connected to global stringstream object
        unsigned int i;

        client_socket.connect(tcp::endpoint(address::from_string(serverip),portn));
        //client sends the data : this loop is to send the data
        for(i=0; i< bloom_db.size(); ++i){

            ::set_elemvec(snd_elem,bloom_db.getelem(i));
            out_archive << snd_elem;            //element inserted in archive
            bufdata = os.str();
            snd_buf = boost::asio::buffer(os.str());
            sendData(client_socket,snd_buf);
        }

        cout<<"Sent "<<i<<" elements from client"<<endl<<std::flush;

    }

void netcom_server(int portn, bloom_filter& bloom_db){
    io_service io_service;
    element recv_elem;
    char bufdata[sizeof(element)];
    boost::system::error_code ec;
    bloom_filter recvd_bloomdb(4,"rcvbloom_db",4096);
    std::istringstream is(bufdata);
    boost::archive::text_iarchive in_archive {is};
    unsigned int i;

    ip::tcp::socket server_socket(io_service);
    tcp::acceptor acceptor_server(io_service,tcp::endpoint(tcp::v4(), portn));
    acceptor_server.accept(server_socket);

    for(i=0; i<recvd_bloomdb.size(); ++i){
        boost::asio::mutable_buffer rcv_buf  = boost::asio::buffer((void*)bufdata,sizeof(element));
        getData(server_socket, rcv_buf);
        in_archive >> recv_elem;
        recvd_bloomdb.tailadd_elem(recv_elem);
    }
    cout<<"Received "<<i<<" elements at server"<<endl<<std::flush;

}

错误的第一行

/usr/include/boost/asio/detail/sumption_buffers.hpp:在'class boost::asio::detail::consuming_buffers&lt;boost::asio::mutable_buffer, boost::asio::mutable_buffer&gt;’:的实例化中 /usr/include/boost/asio/impl/read.hpp:45:44:‘std::size_t boost::asio::read(SyncReadStream&amp;, const MutableBufferSequence&amp;, CompletionCondition, boost::system::error_code&amp;) [with SyncReadStream = boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp&gt;; MutableBufferSequence = boost::asio::mutable_buffer; CompletionCondition = boost::asio::detail::transfer_all_t; std::size_t = long unsigned int]’ 需要 /usr/include/boost/asio/impl/read.hpp:64:39:‘std::size_t boost::asio::read(SyncReadStream&amp;, const MutableBufferSequence&amp;) [with SyncReadStream = boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp&gt;; MutableBufferSequence = boost::asio::mutable_buffer; std::size_t = long unsigned int]’ 需要 ibflt.cpp:309:35:从这里需要 /usr/include/boost/asio/detail/sumption_buffers.hpp:164:5: error: no type named ‘const_iterator’ in

‘class boost::asio::mutable_buffer’
     const_iterator;

/usr/include/boost/asio/detail/sumption_buffers.hpp:261:36: error: no type named ‘const_iterator’ in

‘class boost::asio::mutable_buffer’
   typename Buffers::const_iterator begin_remainder_;

【问题讨论】:

  • 您使用的是什么增强版本?是1.66?
  • Boost 版本(目前为 1.65)

标签: c++ boost boost-asio


【解决方案1】:

boost::asio::read 作为第二个参数缓冲区,它必须满足 Mutable Buffer Sequence requirements,在 1.65 类中 mutable_buffers_1 是这样做的,而不是 mutable_buffer(它将从 1.66 版本开始工作)。

所以改变:

void getData(tcp::socket& socket, boost::asio::mutable_buffers_1& data)
{
    boost::asio::read(socket, data);
}

boost::asio::mutable_buffers_1 rcv_buf = ...

    std::ostringstream os;
    //...
    snd_buf = boost::asio::buffer(os.str()); // [1]
    sendData(client_socket,snd_buf);

上面的代码不能工作,我的意思是它编译得很好,但它会给你带来不确定的行为。为什么?

boost::buffer 不会复制传递的数据。它只返回元组(指向数据的指针和数据的大小),不复制底层数据,buffer 只是包装它。

ostringstream::str() 通过 COPY 返回字符串 reference。所以缓冲区接受它,包装成元组,在完整表达式的末尾,这个临时字符串被破坏,因此你在buffer返回的元组中有悬空引用。

解决方案?

创建命名字符串:

std::string str = os.str();
snd_buf = boost::asio::buffer(str);

【讨论】:

  • 谢谢它的工作。老实说,我期待着受到一些责骂:),但你太客气了。在发布之前,我在代码中纠正了一些项目,这可能使我免于愤怒。我更新了我的提升,而不是更正到 mutable_buffers_1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多