简体   繁体   English

使用boost :: asio :: ip :: tcp :: socket :: cancel()和socket :: close()

[英]Using boost::asio::ip::tcp::socket::cancel() and socket::close()

If I use close and not cancel, there are some problems. 如果我使用close而不取消,则存在一些问题。

The close function can close the socket, and any outstanding asynchronous operations is stopped by returning boost::asio::error::operation_aborted error. close函数可以关闭套接字,并通过返回boost::asio::error::operation_aborted错误来停止任何未完成的异步操作。

Why should I use cancel instead of close ? 我为什么要使用cancel而不是close

I worry if some asynchronous operations is executing, the cancel could not cancel it, yes? 我担心如果某些异步操作正在执行, cancel无法取消它,是吗?

Like asio::ip::tcp::resolve::cancel , I try many times to cancel the resolve_handler after calling async_resolve , but resolve_handler always returns with no boost::asio::error::operation_aborted error. asio::ip::tcp::resolve::cancel ,我在调用async_resolve后尝试多次取消resolve_handler ,但是resolve_handler总是返回而没有boost::asio::error::operation_aborted错误。

I think resolve_handler is being executed? 我认为resolve_handler正在执行?

Yes? 是?

Cancel is useful if you want to stop pending operations without closing down the socket. 如果要在不关闭套接字的情况下停止挂起操作,则取消非常有用。

Note that the Boost documentation recommends using close for greater portability (from doc page): 请注意, Boost文档建议使用close以获得更高的可移植性(来自doc页面):

... For portable cancellation, consider using one of the following alternatives: ...对于便携式取消,请考虑使用以下备选方案之一:

  • Disable asio's I/O completion port backend by defining BOOST_ASIO_DISABLE_IOCP. 通过定义BOOST_ASIO_DISABLE_IOCP来禁用asio的I / O完成端口后端。
  • Use the close() function to simultaneously cancel the outstanding operations and close the socket. 使用close()函数同时取消未完成的操作并关闭套接字。

cancel won't close the socket, so use cancel if you intend to continue using the socket object. cancel不会关闭套接字,因此如果您打算继续使用套接字对象,请使用cancel In particular, if you have code in asynchronous handler methods that references the socket's member functions, you may not want to close the socket until you are guaranteed that your currently executing asynchronous handlers have completed. 特别是,如果在异步处理程序方法中有代码引用套接字的成员函数,则可能不希望在确保当前正在执行的异步处理程序已完成之前关闭套接字。

cancel doesn't guarantee anything about currently executing asynchronous handlers, it only guarantees (per the boost documentation) that "This function causes all outstanding asynchronous connect, send and receive operations to finish immediately" in the case of the socket::cancel() call, or "This function forces the completion of any pending asynchronous operations on the host resolver" in the case of the resolver::cancel() call. cancel不保证当前正在执行的异步处理程序,它只保证(根据boost文档)“这个函数导致所有未完成的异步连接,发送和接收操作立即完成”在socket::cancel()的情况下在resolver::cancel()调用的情况下,调用或“此函数强制完成主机解析器上的任何挂起的异步操作”。 This "completion" means that boost will call your asynchronous handler method, it has no jurisdiction to inject any cancellation logic into your asynchronous handler (not to mention it doesn't know about the handler's implementation to begin with). 这个“完成”意味着boost将调用你的异步处理程序方法,它没有权限将任何取消逻辑注入你的异步处理程序(更不用说它不知道处理程序的实现开始)。

I would suggest adding your own logic into your asynchronous handler method to handle the case where the socket/resolver/etc. 我建议将自己的逻辑添加到异步处理程序方法中,以处理socket / resolver / etc的情况。 is canceled. 被取消了。 If you are calling the cancel method, then you likely have the ability to communicate this cancellation to the asynchronous handler method. 如果您正在调用cancel方法,那么您可能能够将此取消传递给异步处理程序方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM