0
votes

I am learning to use Boost ASIO. Here is my code adapted from the 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));
}

When compiling, I got an error that says ‘async_connect’ is not a member of ‘boost::asio’. Clearly, async_connect is not a member of boost asio, but of socket, as far as what the IDE tells me. I'm using libasiodev 1.41.3.

I tried to modify the call to this

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> > > >’

UPDATE: After changing the handle_connect signature to include the iterator parameter, I succesfully compiled that line. However, the compiler is now stuck at the include to 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 (*)()]’

For some reason, I can't start a new thread with the call boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)); , even though it works if I replace it by a simple call to io_service.run();

1
Why you trying to send endpoint_iterator in handle_connect if this function should get only error_code? - ForEveR
You mean in the last call? sorry, it was my bad copying and pasting. Can you add an answer so that I can accept (after compiling and running)? Thanks. - Son Do Lenh
So, i give you answer, if it doesn't compiles - give your new code. - ForEveR
hi @ForEveR, I updated my question, please consider and help! Thank you. - Son Do Lenh

1 Answers

2
votes

Since your cb has signature

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

this call is incorrect.

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

Your last par is wrong, so, right bind is

boost::bind(&AsioCommunicationService::handle_connect, this,
        boost::asio::placeholders::error));