0

我正在学习使用 Boost ASIO。这是我改编自 Boost doc's chat example的代码。

class AsioCommunicationService {
AsioCommunicationService::AsioCommunicationService(
        boost::asio::io_service& io_service,
        tcp::resolver::iterator endpoint_iterator)
    : io_service_(io_service),
      socket_(io_service)
{
    boost::asio::async_connect(socket_, endpoint_iterator,
        boost::bind(&AsioCommunicationService::handle_connect, this,
        boost::asio::placeholders::error));
}



void AsioCommunicationService::handle_connect(const boost::system::error_code& error)
{
    if (!error)
    {
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.data(), LampMessage::header_length),
          boost::bind(&AsioCommunicationService::handle_read_header, this,
          boost::asio::placeholders::error));
    }
}
}


class Connection
{
    m_session = std::shared_ptr<AsioCommunicationService>(
            new AsioCommunicationService(io_service, iterator));
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
}

编译时,我收到一条错误消息,提示“async_connect”不是“boost::asio”的成员。显然,就 IDE 告诉我的内容而言,async_connect 不是 boost asio 的成员,而是 socket 的成员。我正在使用 libasiodev 1.41.3。

我试图修改对此的调用

tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
    boost::bind(&AsioCommunicationService::handle_connect, this,
    boost::asio::placeholders::error, ++endpoint_iterator));
//Got this error: instantiated from ‘boost::_bi::bind_t<boost::_bi::unspecified, void (networkService::AsioCommunicationService::*)(const boost::system::error_code&), boost::_bi::list3<boost::_bi::value<networkService::AsioCommunicationService*>, boost::arg<1> (*)(), boost::_bi::value<boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> > > >’

更新:更改 handle_connect 签名以包含迭代器参数后,我成功编译了该行。但是,编译器现在停留在对 thread.hpp 的包含

#include <boost/thread.hpp>
//ERROR: In function ‘boost::thread&& boost::move(boost::thread&&)’:
///usr/include/boost/thread/detail/thread.hpp:349:16: error: invalid initialization of reference of type ‘boost::thread&&’ from expression of type ‘boost::thread’
//In file included from /usr/include/boost/thread/detail/thread_heap_alloc.hpp:17:0,
//             from /usr/include/boost/thread/detail/thread.hpp:13,
//             from /usr/include/boost/thread/thread.hpp:22,
//             from /usr/include/boost/thread.hpp:13,
//             from /home/son/dev/logistics/src/frameworks/networkService/NetworkConnection.cpp:13:
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp: In function ‘T* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<void (*)()>, A1 = void (*&)()]’:
///usr/include/boost/thread/detail/thread.hpp:130:95:   instantiated from here
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp:24:47: error: cannot bind ‘void (*)()’ lvalue to ‘void (*&&)()’
///usr/include/boost/thread/detail/thread.hpp:43:13: error:   initializing argument 1 of ‘boost::detail::thread_data<F>::thread_data(F&&) [with F = void (*)()]’

出于某种原因,我无法通过调用 boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)); 启动一个新线程。,即使如果我通过对 io_service.run(); 的简单调用来替换它,它也可以工作。

4

1 回答 1

2

因为你的 cb 有签名

void AsioCommunicationService::handle_connect(const boost::system::error_code& error)

这个调用是不正确的。

tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
    boost::bind(&AsioCommunicationService::handle_connect, this,
    boost::asio::placeholders::error, ++endpoint_iterator));

你的最后一个标准是错误的,所以,正确的绑定是

boost::bind(&AsioCommunicationService::handle_connect, this,
        boost::asio::placeholders::error));
于 2012-07-30T10:13:40.463 回答