gpt4 book ai didi

c++ - boost::asio 中异步操作的处理程序要求

转载 作者:行者123 更新时间:2023-11-30 03:04:30 24 4
gpt4 key购买 nike

boost::asio document 中指定async_accept() 的处理程序必须满足以下函数签名:

void accept_handler(
const boost::system::error_code& ec)
{
...
}

但是,在 Daytime.3 示例中,使用 boost::bind,可以根据需要为处理程序指定任意多的参数,只要不超过 boost 的限制::bind(最多 9 个参数):

class tcp_server
{
public:
tcp_server(boost::asio::io_service& io_service)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
{
start_accept();
}

private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());

acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}

void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error)
{
if (!error)
{
new_connection->start();
}

start_accept();
}

tcp::acceptor acceptor_;
};

为什么这是可能的?我认为即使使用 boost::bind,仍然需要匹配确切的函数签名。

注意 handle_accept() 函数及其在 async_accept() 中的使用方式。完整的代码 list 是 here .

最佳答案

我在这里找到了真正的答案:http://blog.think-async.com/2010/04/bind-illustrated.html基本上,真正的函数在函数调用运算符 () 的基础上被调用。 boost::bind 创建一个函数对象,伪装成作为其他一些函数的参数所需的函数签名。使用 boost::bind,可以将附加信息传递到处理程序中。

关于c++ - boost::asio 中异步操作的处理程序要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8559516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com