开发者

Bind resolve_handler to resolver async_resolve using boost::asio

I have this code. How can I bind my method resolve_handler with the expected iterator and error parameters? Is it the correct way to break down the connection logic?

void FileClient::start() 
{ 
    try {
        boost::asio::ip::tcp::resolver::query query("ip", "port");
        resolver_.async_resolve(query, boost::bind(
            &FileClient::resolve_handler, this
        ));
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}

void FileClient::resolve开发者_JAVA百科_handler(const boost::system::error_code &_error,
                                 boost::asio::ip::tcp::resolver::iterator _it)
{ 
    if (!_error) 
        socket_.async_connect(*_it, boost::bind(
            &FileClient::connect_handler, this, boost::asio::placeholders::error
        ));
    else 
        std::cerr << "resolve_handler error: " << _error << std::endl; 
}


There are examples in boost.asio tutorials, for example, from this HTTP async client

tcp::resolver::query query(server, "http");
    resolver_.async_resolve(query,
        boost::bind(&client::handle_resolve, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::iterator));

...

void handle_resolve(const boost::system::error_code& err,
                    tcp::resolver::iterator endpoint_iterator)
{
    if (!err)
    {
        // Attempt a connection to the first endpoint in the list. Each endpoint
        // will be tried until we successfully establish a connection.
         tcp::endpoint endpoint = *endpoint_iterator;
         socket_.async_connect(endpoint,
                         boost::bind(&client::handle_connect, this,
                         boost::asio::placeholders::error, ++endpoint_iterator));
    }
    else
    {
        std::cout << "Error: " << err.message() << "\n";
    }
}

(their handle_connect continues to increment endpoint_iterator as necessary)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜