如何在设备文件中使用ASIO

10

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

我通过打开并将文件描述符分配给stream_descriptor来打开该文件描述符。然后我发出一个async_read()调用,但该调用从未返回。

是否可以使用boost asio处理输入事件?在使用ioctl之前,我需要配置文件句柄吗?

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

下面的代码打开/dev/input/event12,并调用io_service对象上的运行方法。

#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));
    }
}

async_read()的调用没有返回,或者你是指回调函数没有被调用?没有代码很难诊断。你记得调用io_service::run()了吗?值得一提的是,我确实做过这个,它绝对可行。 - Sean
@sean 回调从未被调用。 - David Feurle
1个回答

4
问题在于我使用了async_read而没有任何完成条件,因此回调从未被调用。将调用更改为async_read_some后,一切都按预期工作了。

4
将调用更改为async_read_some并不能真正帮助任何人。您应该编写一些包含相应更改的代码。 - Victor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接