当前位置: 首页 > news >正文

asio之读写

简介

asio读写支持同步和异步

读操作

主要在read.hpp,read_at.hpp和read_until.hpp文件中
异步读函数对象有

  • read_until_delim_op
  • read_until_delim_string_op
  • read_until_expr_op
  • read_until_match_op
  • read_streambuf_op
  • read_op
  • read_at_op
  • read_at_streambuf_op

写操作

主要在write.hpp,write_at.hpp文件中
异步写函数对象有

  • write_at_op
  • write_at_streambuf_op
  • write_op
  • write_streambuf_handler

异步操作分析

异步操作可能在调用线程和io线程中调用,以read_until_delim_op为例,其第三个参数为默认参数,区分不同线程的调用。在switch语句中,使用循环,同时循环中使用了default分支。

void operator()(const boost::system::error_code& ec,std::size_t bytes_transferred, int start = 0){const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();std::size_t bytes_to_read;switch (start_ = start){case 1:for (;;){{// Determine the range of the data to be searched.typedef typename boost::asio::basic_streambuf<Allocator>::const_buffers_type const_buffers_type;typedef boost::asio::buffers_iterator<const_buffers_type> iterator;const_buffers_type buffers = streambuf_.data();iterator begin = iterator::begin(buffers);iterator start_pos = begin + search_position_;iterator end = iterator::end(buffers);// Look for a match.iterator iter = std::find(start_pos, end, delim_);if (iter != end){// Found a match. We're done.search_position_ = iter - begin + 1;bytes_to_read = 0;}// No match yet. Check if buffer is full.else if (streambuf_.size() == streambuf_.max_size()){search_position_ = not_found;bytes_to_read = 0;}// Need to read some more data.else{// Next search can start with the new data.search_position_ = end - begin;bytes_to_read = read_size_helper(streambuf_, 65536);}}// Check if we're done.if (!start && bytes_to_read == 0)break;// Start a new asynchronous read operation to obtain more data.stream_.async_read_some(streambuf_.prepare(bytes_to_read),BOOST_ASIO_MOVE_CAST(read_until_delim_op)(*this));return; default:streambuf_.commit(bytes_transferred);if (ec || bytes_transferred == 0)break;}const boost::system::error_code result_ec =(search_position_ == not_found)? error::not_found : ec;const std::size_t result_n =(ec || search_position_ == not_found)? 0 : search_position_;handler_(result_ec, result_n);}}

在发起异步调用时直接return了

stream_.async_read_some(streambuf_.prepare(bytes_to_read),BOOST_ASIO_MOVE_CAST(read_until_delim_op)(*this));
return; default:

当在io线程调用时,会进入default分支,如果数据没有读完,会进入for循环,继续发起异步读取返回。直到将数据读取完毕, 才会调用自定义的handler

handler_(result_ec, result_n);

相关文章:

  • 解决克隆Github源码库时的Permission denied 问题
  • JAVA中的多线程
  • 对DOM操作 与 jQuery的简单理解(通俗
  • 【数组和二分查找】
  • 鹰盾播放器禁止录屏操作的深度技术解析与全栈实现方案
  • 《高等数学》(同济大学·第7版)第三章第五节“函数的极值与最大值最小值“
  • SpringDoc集成到Springboot
  • 【PhysUnits】17.5 实现常量除法(div.rs)
  • git clone 时报错超时的问题解决方案
  • windows mysql zip部署
  • 国产 AI 绘画新标杆:HiDream-I1 的技术突破与创作
  • Python入门手册:常用的Python标准库
  • 企业中使用 MCP Server 实现业务打通
  • 全国大学生计算机应用能力与数字素养大赛 C语言程序设计赛项——本科组练习
  • 人工智能增强入侵检测系统以对抗高级持续性杀伤链
  • 《信号与系统》第 7 章 采样
  • 1.一起学习仓颉-编译环境,ide,输出hello,world
  • 鹰盾加密器基于AI的视频个性化压缩技术深度解析:从智能分析到无损压缩实践
  • Pytest断言全解析:掌握测试验证的核心艺术
  • Spring Boot 4.0.0 新特性详解:深入解读 Spring Framework 7.0.0
  • tlbb3官方网站慕容神器做的步骤/做一个个人网站
  • o2o网站建设渠道/南昌seo网站排名
  • 建设部网站怎么查询企业业绩/推广形式
  • 惠州专业的免费建站/百度客服电话
  • 个人备案能公司网站/关键词是网站seo的核心工作
  • 成品网站源码1688版本号/学seo推广