繁体   English   中英

async_read_until:缓冲区空间不足或队列已满

[英]async_read_until: insufficient buffer space or queue is full

我正在将应用程序从使用Juce异步I / O转换为Asio。 第一部分是重写从同一台机器上的另一个应用程序接收流量的代码(这是一个Lightroom Lua插件,可在端口58764上发送\\n分隔的消息)。 每当我使用C ++程序成功连接到该端口时,都会得到一系列错误代码,都相同:

由于系统缺少足够的缓冲区空间或队列已满,无法对套接字执行操作。

有人可以指出我的错误吗? 我可以看到套接字已成功打开。 我已将其从整个程序中简化为一个最小的示例。 我也尝试使用connect而不是async_connect尝试,并且遇到了同样的问题。

#include <iostream>
#include "asio.hpp"

asio::io_context io_context_;
asio::ip::tcp::socket socket_{io_context_};

void loop_me()
{
   asio::streambuf streambuf{};
   while (true) {
      if (!socket_.is_open()) {
         return;
      }
      else {
         asio::async_read_until(socket_, streambuf, '\n',
             [&streambuf](const asio::error_code& error_code, std::size_t bytes_transferred) {
                if (error_code) {
                   std::cerr << "Socket error " << error_code.message() << std::endl;
                   return;
                }
                // Extract up to the first delimiter.
                std::string command{buffers_begin(streambuf.data()),
                    buffers_begin(streambuf.data()) + bytes_transferred};
                std::cout << command << std::endl;
                streambuf.consume(bytes_transferred);
             });

      }
   }
}

int main()
{
   auto work_{asio::make_work_guard(io_context_)};
   std::thread io_thread_;
   std::thread run_thread_;
   io_thread_ = std::thread([] { io_context_.run(); });
   socket_.async_connect(asio::ip::tcp::endpoint(asio::ip::address_v4::loopback(), 58764),
       [&run_thread_](const asio::error_code& error) {
          if (!error) {
             std::cout << "Socket connected in LR_IPC_In\n";
             run_thread_ = std::thread(loop_me);
          }
          else {
             std::cerr << "LR_IPC_In socket connect failed " << error.message() << std::endl;
          }
       });
   std::this_thread::sleep_for(std::chrono::seconds(1));
   socket_.close();
   io_context_.stop();
   if (io_thread_.joinable())
      io_thread_.join();
   if (run_thread_.joinable())
      run_thread_.join();
}

您试图同时启动无限数量的异步读取操作。 在上一个异步读取完成之前,您不应该开始新的异步读取。

即使尚未收到数据, async_read_until立即返回。 这就是“异步”的意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM