解析令牌是一个 完成令牌,其完成签名是 void(error_code, ip::basic_resolver_results<InternetProtocol>)
, 针对指定的 InternetProtocol
。
作为解析令牌的自由函数
void resolve_handler( const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::results_type results) { ... }
解析令牌函数对象
struct resolve_handler { ... void operator()( const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::results_type results) { ... } ... };
作为解析令牌的 Lambda 表达式
resolver.async_resolve(..., [](const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::results_type results) { ... });
使用 std::bind()
改编为解析令牌的非静态类成员函数
void my_class::resolve_handler( const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::results_type results) { ... } ... resolver.async_resolve(..., std::bind(&my_class::resolve_handler, this, std::placeholders::_1, std::placeholders::_2));
使用 boost::bind()
改编为解析令牌的非静态类成员函数
void my_class::resolve_handler( const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::results_type results) { ... } ... resolver.async_resolve(..., boost::bind(&my_class::resolve_handler, this, boost::asio::placeholders::error, boost::asio::placeholders::results));
使用 use_future 作为解析令牌
std::future<boost::asio::ip::tcp::resolver::results_type> f = resolver.async_resolve(..., boost::asio::use_future); ... try { boost::asio::ip::tcp::resolver::results_type r = f.get(); ... } catch (const system_error& e) { ... }
使用 use_awaitable 作为解析令牌
boost::asio::awaitable<void> my_coroutine() { try { ... boost::asio::ip::tcp::resolver::results_type r = co_await resolver.async_resolve( ..., boost::asio::use_awaitable); ... } catch (const system_error& e) { ... } }