我正在尝试将D-Bus与我的boost::asio应用程序集成.
D-Bus有一个API,可以枚举一组要监视的Unix文件描述符(主要是套接字,但也可以是FIFO).当那些描述符有东西需要阅读时,我应该通知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 …Run Code Online (Sandbox Code Playgroud) 我在整个项目中都使用了boost asio.我现在想要读取一个设备文件(/dev/input/eventX).在boost asio文档中,它声明不能使用普通文件IO,但使用支持设备文件或管道asio::posix::stream_descriptor.
我通过open打开文件描述符并将其分配给stream_descriptor.我现在发出一个async_read()永不回复的电话.
是否可以在输入事件中使用boost asio?在通过ioctl与asio一起使用之前,是否需要配置文件句柄?
编辑:添加一些示例代码 - >添加了一些示例代码.
以下代码打开/ dev/input/event12,并调用io_service对象上的run方法.
#include <boost/asio.hpp>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <linux/input.h>
namespace asio = boost::asio;
#ifdef BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
typedef asio::posix::stream_descriptor stream_descriptor;
#else // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
typedef asio::windows::stream_handle stream_descriptor;
#endif // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
class FileReader
{
typedef boost::shared_ptr<asio::streambuf> StreambufPtr;
typedef boost::shared_ptr<FileReader> FileReaderPtr;
typedef boost::weak_ptr<FileReader> FileReaderWeakPtr;
public:
static FileReaderWeakPtr Create(asio::io_service& io_service, const std::string& path);
virtual ~FileReader();
void HandleRead(FileReaderPtr me, StreambufPtr sb,
const boost::system::error_code &error);
private:
FileReader(asio::io_service& io_service, const std::string& path); …Run Code Online (Sandbox Code Playgroud) 我正在搜索如何测试是否按下了某个键。测试不应阻止程序。如果不是太重的话,我可以使用一个小库,但不幸的是 ncurses 的依赖性太大,无法引入。