做增强asio套接字有正确的RAII清理 [英] Do boost asio sockets have proper RAII cleanup

查看:96
本文介绍了做增强asio套接字有正确的RAII清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过源代码,但我不能导航那么多的模板代码。
基本上:这是文档说的( close()):

I tried looking through source but I cant navigate that much of a template code. Basically: this is what documentation says (for close()):


Remarks    
For portable behaviour with respect to graceful 
closure  of a connected socket, call shutdown() before closing the socket.


我可以手动进行,但如果可能,

I can do that manually, but if possible it would be nice to rely on RAII.

所以如果我有socket超出范围,我需要调用 shutdown() close()上,还是会自动完成?

So if I have socket going out of scope do I need to call shutdown() and close() on it, or it will be done automatically?

推荐答案

可以依赖套接字使用RAII进行适当的清理。

One can rely on the socket performing proper cleanup with RAII.

当销毁IO对象(如套接字)时,其 destructor 将在IO对象的服务上调用 destroy(),传递 implementation_type ,IO对象的服务将在其上操作。 SocketService 要求 destroy()将通过调用隐式地取消异步操作> close()在服务上有一个条件 is_open()返回false。此外,服务的 close()将导致尽快完成异步操作。取消操作的处理程序将传递错误代码 boost :: asio :: error :: operation_aborted ,并计划在 io_service 。如果这些处理程序是从处理事件循环的线程中调用,或者 io_service io_service a href =http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_service/_io_service.html =nofollow>已销毁。

When an IO object, such as socket, is destroyed, its destructor will invoke destroy() on the IO object's service, passing in an instance of the implementation_type on which the IO object's service will operate. The SocketService requirements state that destroy() will implicitly cancel asynchronous operations as-if by calling the close() on the service, which has a post condition that is_open() returns false. Furthermore, the service's close() will cause outstanding asynchronous operations to complete as soon as possible. Handlers for cancelled operations will be passed the error code boost::asio::error::operation_aborted, and scheduled for deferred invocation within the io_service. These handlers are removed from the io_service if they are either invoked from a thread processing the event loop or the io_service is destroyed.

这篇关于做增强asio套接字有正确的RAII清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆