当前位置: 首页 > 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);
http://www.xdnf.cn/news/972523.html

相关文章:

  • 路径规划算法概论:从理论到实践
  • switch选择语句
  • ABB UNITROL 6000 X-power 3BH022294R0103 GFD233A103
  • Python 3.6/3.8版本切换脚本
  • 调用支付宝接口响应40004 SYSTEM_ERROR问题排查
  • Python模块全解析:从入门到精通
  • MySQL学习之---索引
  • Lighttpd 配置选项介绍
  • 谷歌趋势自动报告系统(Pipedream + Scrapeless + Discord)
  • 电脑一段时间没用就变成登陆的界面
  • 5G+边缘计算推动下的商品详情API低延迟高效率新方案
  • 【Linux Learning】SSH连线出现警告:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  • 超火的开源项目(Github热点)
  • 交叉编译笔记
  • Docker部署Nginx-UI
  • 【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例
  • 安装 PyCharm
  • Open3D 点云处理笔记
  • 城市照明深夜全亮太浪费?智能分时调光方案落地贵州某市
  • threadlocal的实现说明
  • python46
  • 端到端自动驾驶研究:通过强化学习与世界模型的协同作用向VLA范式演进
  • 曼昆《经济学原理》第九版 第十三章生产成本
  • 智能呼入系统助力酒店客服服务
  • 使用mpu6500/6050, PID,互补滤波实现一个简单的飞行自稳控制系统
  • 2025.6.10【ZR NOI模拟赛 T3】 过啥题 题解(Lucas 定理, 数位dp, 组合意义)
  • Java设计模式基础问答
  • 通过Wrangler CLI在worker中创建数据库和表
  • QFuture的使用方式
  • vue的created和mounted区别