boost asio - Asynchronous reading on the inotify descriptor failed -


i'm writing monitoring-file program based on source code: https://github.com/kvikas/file-monitor-service/blob/master/

my program uses boost::asio::stream_descriptor::async_read_some() asynchronous reading inotify descriptor http://linux.die.net/man/7/inotify

my code following:

constructor:

void init(){     int fd = inotify_init1(in_nonblock);     int wd = inotify_add_watch(fd_, "./test.txt", in_all_events);     stream_.reset(new boost::asio::posix::stream_descriptor(io_service_, fd_))); } 

the asynchronous reading:

template<typename monitor_handler> void async_monitor(monitor_handler handler) {     stream_->async_read_some(boost::asio::buffer(buffer_),             boost::bind(&monitor::handle_monitor<monitor_handler>,                     shared_from_this(), boost::asio::placeholders::error,                     boost::asio::placeholders::bytes_transferred, handler)); }    

the handler:

template<typename monitor_handler> void handle_monitor(const boost::system::error_code &ec,         std::size_t bytes_transferred, monitor_handler handler) {            //process buffer         async_monitor(handler);  } 

the error @ first handle_monitor invoked several times (multiple events such modify, access, open...) first change in monitored file. after async_read_some method called again, got no signal anymore (the handle_monitor not called anymore)

however, when tried reset inotify description, , readd monitored files again ==> worked, handle_monitor called new changes in such monitored files.

modified code:

template<typename monitor_handler> void handle_monitor(const boost::system::error_code &ec,         std::size_t bytes_transferred, monitor_handler handler) {            //process buffer         async_monitor(handler);     init();//for resetting inotify desciptor  } 

could guys me explain this???? i'm dying answer.....

this looks suspicious me

void init(){     int fd = inotify_init1(in_nonblock);     int wd = inotify_add_watch(fd_, "./test.txt", in_all_events);     stream_.reset(new boost::asio::posix::stream_descriptor(io_service_, fd_))); } 

you should create stream_descriptor value returned notify_init1(), fd instead of fd_. assume fd_ class member, , uninitialized or initialized 0.


Comments