有没有可能访问boost asio异步完成处理程序中的套接字句柄?我查看了boost asio占位符,但没有存储套接字句柄的变量。
发布于 2014-02-27 02:16:31
您可以只安排它,无论如何,您将在boost或asio之外。
要绑定一个函数,该函数需要一个套接字来公开一个void()函数,您可以使用bind
int foo(std::string const& s, int);
std::function<void()> adapted = std::bind(foo, "hello world", 42);所以,通常你会有类似下面这样的代码:
boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator,
boost::bind(&client::handle_connect, this, boost::asio::placeholders::error));注意,通过使用绑定和this,我们已经将一个成员函数绑定到完成处理程序:
struct client
{
// ....
void handle_connect(boost::system::error_code err)
{
// you can just use `this->socket_` here
// ...
}
};这意味着在handle_connect中,我们只能使用socket_成员变量。
然而,如果你想让事情变得复杂,你也可以使用免费函数
boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator,
boost::bind(&free_handle_connect, boost::ref(socket_), boost::asio::placeholders::error));现在,隐含的处理程序函数如下所示
static void free_handle_connect(
boost::asio::ip::tcp::socket& socket_,
boost::system::error_code err)
{
// using `socket_` as it was passed in
int fd = _socket.native_handle_type();
}https://stackoverflow.com/questions/22049526
复制相似问题