如何使用asio与设备文件

时间:2022-04-03 23:15:59

I'm using boost asio throughout my project. I now want to read a device file (/dev/input/eventX). In the boost asio documentation it states that normal file IO is not possible but device files or pipes are supported by using asio::posix::stream_descriptor.

我在整个项目中都使用了boost asio。我现在想要读取一个设备文件(/ dev / input / eventX)。在boost asio文档中,它声明不能使用普通文件IO,但使用asio :: posix :: stream_descriptor支持设备文件或管道。

I opened the file descriptor via open and assigned it to the stream_descriptor. I now issue a async_read() call which never returns.

我通过open打开文件描述符并将其分配给stream_descriptor。我现在发出一个永远不会返回的async_read()调用。

Is it possible to use boost asio with input events? Do I need to configure the file handle before using it with asio via ioctl?

是否可以在输入事件中使用boost asio?在通过ioctl与asio一起使用之前,是否需要配置文件句柄?

Edit: Add some example code -> added some example code.

编辑:添加一些示例代码 - >添加了一些示例代码。

The following code opens /dev/input/event12 and the run method on the io_service object is called.

以下代码打开/ 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);
    stream_descriptor m_InputStream;
};

FileReader::FileReaderWeakPtr FileReader::Create(asio::io_service& io_service,
                                                 const std::string& path){
    FileReaderPtr ptr(new FileReader(io_service, path));
    StreambufPtr sb(new boost::asio::streambuf());

    asio::async_read(ptr->m_InputStream, *sb,
            boost::bind(&FileReader::HandleRead, ptr.get(),
            ptr, sb, asio::placeholders::error));
    return ptr;
}

FileReader::FileReader(asio::io_service& io_service, const std::string& path)
    :m_InputStream(io_service)
{
    int dev = open(path.c_str(), O_RDONLY);
    if (dev == -1) {
        throw std::runtime_error("failed to open device " + path);
    }

    m_InputStream.assign(dev);
}

void FileReader::HandleRead(FileReaderPtr me, StreambufPtr sb,
                    const boost::system::error_code &error) {
    if(!error) {
        //Inform all of a sucessfull read
        std::istream is(sb.get());
        size_t data_length = sb->size();
        asio::async_read(m_InputStream, *sb,
            boost::bind(&FileReader::HandleRead, this, me, sb, asio::placeholders::error));
    }
}

1 个解决方案

#1


3  

The problem was that I was using async_read without any complete condition. Therefore the callback was never invoked. After changing the call to async_read_some everything works as expected.

问题是我在没有任何完整条件的情况下使用async_read。因此从不调用回调。将调用更改为async_read_some后,一切都按预期工作。

#1


3  

The problem was that I was using async_read without any complete condition. Therefore the callback was never invoked. After changing the call to async_read_some everything works as expected.

问题是我在没有任何完整条件的情况下使用async_read。因此从不调用回调。将调用更改为async_read_some后,一切都按预期工作。