boost::asio::async_read 在命名管道上不断返回 EOF
boost::asio::async_read keeps returning eof on named pipe
这是我在阅读模式下打开管道的示例代码。它使用boost::asio
从管道读取。当数据(假设 X 字节)写入管道时,它会调用缓冲区中具有ec=EOF
、bytes=X
和 X 字节数据的on_read
。
发送 EOF 是因为编写器在完成对管道的写入后将其关闭。我想继续阅读。这就是为什么我在on_read
中称pipe.async_wait()
.但是,即使没有准备好从管道中读取任何内容,on_read
调用my_pipe::async_read()
再次调用带有bytes = 0
和ec=EOF
的on_read()
。这在一个无限循环中持续下去。
为什么它一直反复阅读EOF
?
#include <boost/asio/io_service.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <unistd.h>
#include <chrono>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <boost/bind.hpp>
class my_pipe final
{
public:
explicit my_pipe(boost::asio::io_service& io_service);
~my_pipe();
private:
boost::asio::posix::stream_descriptor pipe;
std::vector<char> _buf{};
void async_read(const boost::system::error_code& ec);
void on_read(const boost::system::error_code& ec, std::size_t bytes_transferred);
};
my_pipe::my_pipe(boost::asio::io_service& io_service) : pipe(io_service)
{
int fd = open("/tmp/pipe1", O_RDONLY | O_NONBLOCK);
if (fd == -1)
return;
_buf.resize(8192);
pipe.assign(fd);
pipe.async_wait(boost::asio::posix::stream_descriptor::wait_read,
boost::bind(&my_pipe::async_read, this, boost::asio::placeholders::error));
}
my_pipe::~my_pipe()
{
pipe.close();
}
void my_pipe::async_read(const boost::system::error_code& ec)
{
std::cout << "async_readn";
if (ec)
return;
boost::asio::async_read(pipe, boost::asio::buffer(_buf),
boost::bind(&my_pipe::on_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void my_pipe::on_read(const boost::system::error_code& ec, std::size_t bytes)
{
std::cout << "on_read. bytes=" << bytes << "n";
if (!ec || ec == boost::asio::error::eof) {
if (ec == boost::asio::error::eof)
std::cout << "eofn";
std::cout << "call async_readn";
pipe.async_wait(boost::asio::posix::stream_descriptor::wait_read,
boost::bind(&my_pipe::async_read, this, boost::asio::placeholders::error));
} else {
std::cout << "on_read error: " << ec.message() << "n";
}
}
int main()
{
boost::asio::io_service ios;
my_pipe obj(ios);
ios.run();
}
感谢您的帮助。
当一端的所有手柄都关闭时,管道是"寡妇"的。
在这种情况下,在获得 EOF 后,应关闭管道手柄,然后重新打开管道。然后,您可以在新描述符上发出async_read()
以等待更多数据。
如果您有多个编写器,还要考虑写入只能保证最多PIPE_BUF
个字节的原子写入。
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- boost::asio::async_read 在命名管道上不断返回 EOF
- 从 python 到C++并返回的管道图像
- 命名管道:连接命名管道返回ERROR_BROKEN_PIPE后的读取文件
- 为什么 ReadFile() 不返回 0 ?程序尝试永远从管道读取数据
- pclose() 返回管道的终止状态是否在所有平台上都向左移动了 8 位?
- 命名管道上的 WriteFile 有时会返回ERROR_NO_DATA