I am trying to listen for input on a named pipe. I am using Boost.Asio stream_descriptor and async_read on Linux. The problem is that the io_service :: run () call is blocked, as I want, until the first read. After that, it simply calls the handler immediately with the "End of file" error, although I am trying to connect more async_reads to it. The code I have is equivalent to the following:
boost::asio::io_service io_service;
int fifo_d = open("/tmp/fifo", O_RDONLY);
boost::asio::posix::stream_descriptor fifo(io_service, fifo_d);
while (true)
{
boost::asio::async_read(fifo, buffer, handler);
io_service.run();
}
Only the first async_read works as I expect. Subsequent async_reads are returned immediately. The only way I worked as I want is to close and reopen the named pipe, but it looks like a hack:
boost::asio::io_service io_service;
while (true)
{
int fifo_d = open("/tmp/fifo", O_RDONLY);
boost::asio::posix::stream_descriptor fifo(io_service, fifo_d);
boost::asio::async_read(fifo, buffer, handler);
io_service.run();
close(fifo_d);
}
Can someone tell me what I am doing wrong?
UPDATE: "" , , :
int fifo_d = open("/tmp/fifo", O_RDONLY);
boost::asio::posix::stream_descriptor fifo(io_service, fifo_d);
while (true) {
try {
boost::asio::read(fifo, boost::asio::buffer(buffer));
}
catch (boost::system::system_error& err) {
std::cout << err.what() << std::endl;
}
}