title: how to use asio with device files
body: <p>I'm using boost asio throughout my project. 
  I now want to read a device file (<code>/dev/input/eventX</code>). 
  In the boost asio documentation it states that normal file IO is not possible but   device files or pipes are supported by using <code>asio::posix::stream_descriptor.</code></p>
  
  <p>I opened the file descriptor via open and assigned it to the stream_descriptor. I now issue a <code>async_read()</code> call which never returns.</p>
  
  <p>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?</p>
  
  <p>Edit: Add some example code -> added some example code.</p>
  
  <p>The following code opens /dev/input/event12 and the run method on the io_service object is called.</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;linux/input.h&gt;
  
  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&lt;asio::streambuf&gt; StreambufPtr;
      typedef boost::shared_ptr&lt;FileReader&gt; FileReaderPtr;
      typedef boost::weak_ptr&lt;FileReader&gt; FileReaderWeakPtr;
      public:
      static FileReaderWeakPtr Create(asio::io_service&amp; io_service, const std::string&amp; path);
      virtual ~FileReader();
  
      void HandleRead(FileReaderPtr me, StreambufPtr sb,
                      const boost::system::error_code &amp;error);
  private:
      FileReader(asio::io_service&amp; io_service, const std::string&amp; path);
      stream_descriptor m_InputStream;
  };
  
  FileReader::FileReaderWeakPtr FileReader::Create(asio::io_service&amp; io_service,
                                                   const std::string&amp; path){
      FileReaderPtr ptr(new FileReader(io_service, path));
      StreambufPtr sb(new boost::asio::streambuf());
  
      asio::async_read(ptr-&gt;m_InputStream, *sb,
              boost::bind(&amp;FileReader::HandleRead, ptr.get(),
              ptr, sb, asio::placeholders::error));
      return ptr;
  }
  
  FileReader::FileReader(asio::io_service&amp; io_service, const std::string&amp; 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 &amp;error) {
      if(!error) {
          //Inform all of a sucessfull read
          std::istream is(sb.get());
          size_t data_length = sb-&gt;size();
          asio::async_read(m_InputStream, *sb,
              boost::bind(&amp;FileReader::HandleRead, this, me, sb, asio::placeholders::error));
      }
  }
  </code></pre>