read from keyboard using boost async_read and posix::stream_descriptor

半世苍凉 提交于 2020-12-06 13:01:19

问题


I am trying to capture single keyboard inputs in a non blocking way inside a while loop using boost asio async_read. The handler is expected to display the read characters.

My code:

    #include <boost/asio/io_service.hpp>
    #include <boost/asio/posix/stream_descriptor.hpp>
    #include <boost/asio/read.hpp>
    #include <boost/system/error_code.hpp>
    #include <iostream>
    #include <unistd.h>
    #include <termios.h>

    using namespace boost::asio;

    void read_handler(const boost::system::error_code&, std::size_t)
    {   
        char c;
        std::cin>>c;

        std::cout << "keyinput=" << c << std::endl;
    }

    int main()
    {
      io_service ioservice;        
      posix::stream_descriptor stream(ioservice, STDIN_FILENO);

      char buf[1];
      while(1)
      {    
      async_read(stream, buffer(buf,sizeof(buf)), read_handler);
      ioservice.run();    
      }     
      return 0;    
    }

My output is not as expected(keyinput=char format):

a
key input
b
c
d
e

Where am I going wrong?

Also the program is very cpu intensive. How to rectify it?


回答1:


There's an important restriction on async IO with stdin: Strange exception throw - assign: Operation not permitted

Secondly, if you use async_read do not use std::cin at the same time (you will just do two reads). (Do look at async_wait instead).

That aside, you should be able to fix the high CPU load by using async IO properly:

#include <boost/asio.hpp>
#include <iostream>

using namespace boost::asio;

int main()
{
    io_service ioservice;        
    posix::stream_descriptor stream(ioservice, STDIN_FILENO);

    char buf[1] = {};

    std::function<void(boost::system::error_code, size_t)> read_handler;

    read_handler = [&](boost::system::error_code ec, size_t len) {   
            if (ec) {
                std::cerr << "exit with " << ec.message() << std::endl;
            } else {
                if (len == 1) {
                    std::cout << "keyinput=" << buf[0] << std::endl;
                }
                async_read(stream, buffer(buf), read_handler);
            }
        };


    async_read(stream, buffer(buf), read_handler);

    ioservice.run();    
}

As you can see the while loop has been replaced with a chain of async operations.



来源:https://stackoverflow.com/questions/52308998/read-from-keyboard-using-boost-async-read-and-posixstream-descriptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!