D-Busをアプリケーションと統合しようとしていboost::asio
ます。
D-Busには、監視対象の一連のUnixファイル記述子(主にソケットですが、FIFOの場合もあります)を列挙するAPIがあります。それらの記述子に読み取るものがある場合は、D-Bus APIに通知して、記述子を読み取って実行できるようにする必要があります。
現在、私はこれを行っています:
using boost::asio::posix::stream_descriptor;
void read_handle(stream_descriptor* desc, const boost::system::error_code& ec,
std::size_t bytes_read)
{
if (!ec) {
stream_descriptor::bytes_readable command(true);
descriptor->io_control(command);
std::size_t bytes_readable = command.get();
std::cout << "It thinks I should read" << bytes_readable
<< " bytes" << std::endl;
} else {
std::cout << "There was an error" << std::endl;
}
}
void watch_descriptor(boost::asio::io_service& ios, int file_descriptor)
{
// Create the asio representation of the descriptor
stream_descriptor* desc = new stream_descriptor(ios);
desc->assign(file_descriptor);
// Try to read 0 bytes just to be informed that there is something to be read
std::vector<char> buffer(0);
desc->async_read_some(boost::asio::buffer(buffer, 0),
boost::bind(read_handle, desc, _1, _2));
}
ただし、ハンドラーはすぐに呼び出され、読み取るバイト数が0であると通知されます。何か読むものがあるときだけ呼んでほしいのですが、boost::asioは読めません。それは栄光のように振る舞うべきselect()
です。それを行う簡単な方法はありますか?
PS:私はboost::asio
自分のソフトウェアで広く使用していますが、これはそのほんの一部にすぎないので、glib
他のメインループに依存したくありません。