asio源码解析
基于1.57版本
基础组件
service
所有io服务的基类,service_base作为service的模板实现类,作为其它io服务的公共基类
id
id类用于表示service的唯一,key使用id或者typeinfo表示service的关键字,用于在service_registry注册
服务管理
服务管理是通过service_registry来实现的,包含添加,删除和是否有服务。使用链表来存储所有注册的服务
task_io_service
作为io_service的桥接,其是内部实现类,是整个io处理流程的框架核心
op_queue_:用于存放异步操作
task_operation_:表示对应task_的操作,也就是io复用相关的操作,如等待io事件就绪
task_:io事件驱动,不同平台不同实现
处理流程
win_iocp_io_service
是window下io_servivice的实现类
iocp基础
参考iocp基础
win_iocp_io_service的类结构为
completed_ops_:是用于在调用PostQueuedCompletionStatus失败时存放提交的操作
依赖关系

operation
是task_io_service_operation和win_iocp_operation的别名
异步操作时是将descriptor_state注册到内核中,同时将完成操作添加到descriptor_state的op_queue_队列中
当有读写事件时,会触发descriptor_state的do_complete,调用perform_io,遍历op_queue_中的读写队列执行操作的perform,在执行成功时,会将操作统一放到一个临时队列中。执行完后,从临时队列中取出第一个操作执行完成回调
void epoll_reactor::descriptor_state::do_complete(io_service_impl* owner, operation* base,const boost::system::error_code& ec, std::size_t bytes_transferred)
{if (owner){descriptor_state* descriptor_data = static_cast<descriptor_state*>(base);uint32_t events = static_cast<uint32_t>(bytes_transferred);if (operation* op = descriptor_data->perform_io(events)){op->complete(*owner, ec, 0);}}
}operation* epoll_reactor::descriptor_state::perform_io(uint32_t events)
{mutex_.lock();perform_io_cleanup_on_block_exit io_cleanup(reactor_);mutex::scoped_lock descriptor_lock(mutex_, mutex::scoped_lock::adopt_lock);// Exception operations must be processed first to ensure that any// out-of-band data is read before normal data.static const int flag[max_ops] = { EPOLLIN, EPOLLOUT, EPOLLPRI };for (int j = max_ops - 1; j >= 0; --j){if (events & (flag[j] | EPOLLERR | EPOLLHUP)){while (reactor_op* op = op_queue_[j].front()){if (op->perform()){op_queue_[j].pop();io_cleanup.ops_.push(op);}elsebreak;}}}// The first operation will be returned for completion now. The others will// be posted for later by the io_cleanup object's destructor.io_cleanup.first_op_ = io_cleanup.ops_.front();io_cleanup.ops_.pop();return io_cleanup.first_op_;
}
proactor
asio使用的是proactor模式

参考资料:
https://learn.microsoft.com/zh-cn/windows/win32/fileio/i-o-completion-ports
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
