【发布时间】:2013-05-06 10:06:46
【问题描述】:
我正在写一个基于源码的监控文件程序:https://github.com/kvikas/file-monitor-service/blob/master/
我的程序使用 boost::asio::stream_descriptor::async_read_some() 异步读取 inotify 描述符 http://linux.die.net/man/7/inotify
我的代码如下:
构造函数:
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_)));
}
异步读取:
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));
}
处理程序:
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);
}
错误在于,首先调用了几次handle_monitor(多个事件,例如MODIFY,ACCESS,OPEN ...),以进行受监视文件的第一次更改。之后再次调用 async_read_some 方法,但我不再收到信号(不再调用 handle_monitor)
但是,当我尝试重置 inotify 描述并再次读取受监控的文件时 ==> 它起作用了,handle_monitor 被调用以在此类受监控的文件中进行新的更改。
修改代码:
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 the inotify desciptor
}
你们能帮我解释一下吗????我很想得到你的答案.....
【问题讨论】:
-
你能把
boost::asio和inotify结合起来吗?
标签: asynchronous boost-asio inotify file-monitoring