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);
Boost.Asio是一个强大的C++库,用于网络和系统I/O的非阻塞操作,包括串口通信(也称为UART或COM)。在使用Boost.Asio进行串口异步读写时,它提供了一种事件驱动的方式来处理串口数据的传输。 串口异步读写通常是通过`boost::asio::serial_port`类实现的。下面是一个基本的例子: ```cpp #include <boost/asio.hpp> using boost::asio::ip::tcp; using boost::asio::read; using boost::asio::write; // 创建一个异步操作的模板函数 template<typename Stream> void async_read_write(Stream& socket, std::string data) { // 使用异步套接字读取 boost::asio::async_read(socket, boost::asio::buffer(data), [socket](auto error, auto read_size) { if (!error) std::cout << "Read " << read_size << " bytes.\n"; else std::cerr << "Error reading: " << error.message() << "\n"; // 然后可以继续写入或者处理其他操作 // ... // 异步写入数据 write(socket, data); }); } int main() { boost::asio::io_context io_context; boost::asio::serial_port serial(io_context, "/dev/ttyS0"); // 替换为你设备的实际路径 // 进行串口连接并开始异步读写 serial.open(); if (serial.is_open()) { std::string message = "Hello from Boost.Asio!"; async_read_write(serial, message); } io_context.run(); // 主循环等待所有异步操作完成 return 0; } ``` 在这个例子中,当数据读取完成后,会自动触发回调函数,你在其中可以获取到读取的数据,并进行进一步的操作,比如立即回写,或者处理其他任务。这种方式使得程序可以在等待IO操作的同时执行其他计算密集型的任务,提高效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值