[英]async_read_until: insufficient buffer space or queue is full
I'm converting an application from using Juce asynchronous i/o to asio. 我正在将应用程序从使用Juce异步I / O转换为Asio。 The first part is to rewrite the code that receives traffic from another application on the same machine (it's a Lightroom Lua plugin that sends \\n
delimited messages on port 58764). 第一部分是重写从同一台机器上的另一个应用程序接收流量的代码(这是一个Lightroom Lua插件,可在端口58764上发送\\n
分隔的消息)。 Whenever I successfully connect to that port with my C++ program, I get a series of error codes, all the same: 每当我使用C ++程序成功连接到该端口时,都会得到一系列错误代码,都相同:
An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full. 由于系统缺少足够的缓冲区空间或队列已满,无法对套接字执行操作。
Can someone point out my error? 有人可以指出我的错误吗? I can see that the socket is successfully opened. 我可以看到套接字已成功打开。 I've reduced this from my full program to a minimal example. 我已将其从整个程序中简化为一个最小的示例。 I also tried it with connect
instead of async_connect
and had the same problem. 我也尝试使用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();
}
You are trying to start an infinite number of asynchronous read operations at the same time. 您试图同时启动无限数量的异步读取操作。 You shouldn't start a new asynchronous read until the previous one finished. 在上一个异步读取完成之前,您不应该开始新的异步读取。
async_read_until
returns immediately, even though the data hasn't been received yet. 即使尚未收到数据, async_read_until
立即返回。 That's the point of "async". 这就是“异步”的意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.