【问题标题】:how to use asio with device files如何将 asio 与设备文件一起使用
【发布时间】:2012-12-22 08:45:56
【问题描述】:

我在整个项目中都在使用 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);
    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 回调永远不会被调用。

标签: c++ linux boost boost-asio


【解决方案1】:

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

【讨论】:

  • 更改对 async_read_some 的调用后 并没有真正帮助任何人。您应该编写一些带有相应更改的代码。
猜你喜欢
  • 1970-01-01
  • 2020-07-11
  • 2021-05-06
  • 1970-01-01
  • 2012-10-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多