boost::asio 中异步操作的处理程序要求
它在 boost::asio 文档 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()
中的使用方式。完整的代码清单位于此处。
It is specified in boost::asio document that a handler for async_accept()
must satisfy the following function signature:
void accept_handler(
const boost::system::error_code& ec)
{
...
}
However, in the Daytime.3
example, using boost::bind, the handler can be specified as much parameter as desired, as long as it does not exceed the limit of boost::bind
(which is 9 arguments at maximum):
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_;
};
Why is that possible? I thought even with boost::bind
, the exact function signature still has to be matched.
Notice the handle_accept()
funciton and how it is used in async_accept()
. The full code listing is here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这里找到了真正的答案:http://blog.think-async。 com/2010/04/bind-illusterated.html
基本上,真正的函数是在函数调用运算符
()
的基础上调用的。 boost::bind 创建一个函数对象,假装是某些其他函数所需的参数的函数签名。使用 boost::bind,可以将附加信息传递到处理程序中。I found the real answer here: http://blog.think-async.com/2010/04/bind-illustrated.html
Basically, the real function is called underlying the function call operator
()
. boost::bind creates a function object, pretend to be the function signature required to be a parameter for some other functions. Using boost::bind, additional information can be passed into the handler.这个概念在计时器教程 3,这就是
bind
的工作原理。针对
async_accept
的问题,error_code
参数作为命名参数传递给绑定函数对象。但是,您不需要使用该参数。正如上一段所示,它可以被省略。还可以提供其他参数,这可以为您的处理程序提供有关触发它的异步操作的更多信息。This concept is described in the timer tutorial 3, it's how
bind
works.Specific to your question with
async_accept
, theerror_code
parameter is passed to the bound function object as a named parameter. However, you are not required to use that parameter. As the above paragraph indicates, it may be elided. Additional parameters may be provided as well, this can give your handler more information about the asynchronous operation that triggered it.