本文介绍了如何使用设备文件ASIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的整个项目推动ASIO。
我现在想读一个设备文件(的/ dev /输入/ eventX )。
在升压ASIO文件它指出正常的文件IO是不可能的,但设备文件或管道通过使用支持 ASIO :: POSIX :: stream_descriptor。

我通过开放打开了文件描述符,并分配给stream_descriptor。我现在发出 async_read()调用它永远不会返回。

是否有可能使用boost ASIO与输入事件?我是否需要通过ioctl与ASIO在使用它之前配置文件句柄?

编辑:添加一些例如code - >添加了一些例如code

以下code打开的/ dev /输入/ event12和io_service对象的run方法被调用。

 的#include<升压/ asio.hpp>
#包括LT&;串GT;
#包括LT&;&iostream的GT;
#包括LT&;升压/ bind.hpp>
#包括LT&; Linux的/ input.h>命名空间ASIO =提振:: ASIO;
#IFDEF BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
typedef的ASIO :: POSIX :: stream_descriptor stream_descriptor;
#else伪// BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR
typedef的ASIO:视窗:stream_handle stream_descriptor;
#ENDIF // BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR一流的FileReader
{
    TYPEDEF提高:: shared_ptr的< ASIO ::流缓冲> StreambufPtr;
    TYPEDEF提高:: shared_ptr的<&的FileReader GT; FileReaderPtr;
    TYPEDEF的boost :: weak_ptr的<&的FileReader GT; FileReaderWeakPtr;
    上市:
    静态FileReaderWeakPtr创建(ASIO :: io_service对象和放大器; io_service对象,常量标准::字符串&放大器;路径);
    虚拟〜的FileReader();    无效HandleRead(FileReaderPtr我,StreambufPtr SB,
                    常量的boost ::系统::错误_ code&安培;错误);
私人的:
    的FileReader(ASIO :: io_service对象和放大器; io_service对象,常量标准::字符串&放大器;路径);
    stream_descriptor m_InputStream;
};::的FileReader FileReaderWeakPtr的FileReader ::创建(ASIO :: io_service对象和放大器; io_service对象,
                                                 常量标准::字符串&安培;路径){
    FileReaderPtr PTR(新的FileReader(io_service对象,路径));
    StreambufPtr某人(新的boost :: ASIO ::流缓冲());    ASIO :: async_read(ptr-> m_InputStream,* SB,
            提高::绑定(安培;的FileReader :: HandleRead,ptr.get()
            PTR,SB,ASIO ::占位符::错误));
    返回PTR;
}::的FileReader的FileReader(ASIO :: io_service对象和放大器; io_service对象,常量标准::字符串&放大器;路径)
    :m_InputStream(io_service对象)
{
    INT开发=开(path.c_str(),O_RDONLY);
    如果(DEV == -1){
        抛出std :: runtime_error(无法打开设备+路径);
    }    m_InputStream.assign(DEV);
}无效的FileReader :: HandleRead(FileReaderPtr我,StreambufPtr SB,
                    常量的boost ::系统::错误_ code&安培;错误){
    如果(!错误){
        //通知所有一sucessfull读
        的std :: istream的是(sb.get());
        为size_t数据长度= SB-GT&;尺寸();
        ASIO :: async_read(m_InputStream,* SB,
            提高::绑定(安培;的FileReader :: HandleRead,这个,我,SB,ASIO ::占位符::错误));
    }
}


解决方案

的问题是,我使用async_read没有任何完整的状态。因此回调从未调用。改变呼叫async_read_some一切后,按预期工作。

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.

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

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?

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.

#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));
    }
}
解决方案

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.

这篇关于如何使用设备文件ASIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:07