Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用boost::asio::async\u read(无需在io\u服务上运行调用)实现超时_C++_Boost Asio - Fatal编程技术网

C++ 使用boost::asio::async\u read(无需在io\u服务上运行调用)实现超时

C++ 使用boost::asio::async\u read(无需在io\u服务上运行调用)实现超时,c++,boost-asio,C++,Boost Asio,我试图在超时的情况下读取输入源(在本例中为stdin)。由于现有应用程序的设计必须满足这一要求,因此无法在我的io_服务上调用run 以下是我迄今为止的尝试: #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/optional.hpp> void set_result( boost::optional<boost::system::error_code> * a

我试图在超时的情况下读取输入源(在本例中为stdin)。由于现有应用程序的设计必须满足这一要求,因此无法在我的io_服务上调用run

以下是我迄今为止的尝试:

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/optional.hpp>

void set_result( boost::optional<boost::system::error_code> * a, boost::system::error_code b ) {
    if( b == 0)
        a->reset( b );
}

void receive(boost::asio::io_service & io, boost::asio::posix::stream_descriptor & stream, boost::asio::streambuf & result){
    boost::optional<boost::system::error_code> timer_result;
    boost::optional<boost::system::error_code> read_result;

    boost::asio::deadline_timer timer( io );
    timer.expires_from_now( boost::posix_time::milliseconds(5000) );
    timer.async_wait( boost::bind(&set_result, &timer_result, _1) );

    boost::asio::async_read(
            stream,
            result,
            boost::asio::transfer_at_least(1),
            boost::bind( &set_result, &read_result, _1 ));
    boost::system::error_code ec;

    while(1) {
        io.reset();
        io.poll_one(ec);

        if ( read_result ) {
            timer.cancel();
            return;

        } else if ( timer_result )
            throw std::runtime_error("timeout");
    }
}

void receive(boost::asio::io_service & io, boost::asio::posix::stream_descriptor & stream, size_t size, boost::asio::streambuf & result){
    while( result.size() < size )
        receive(io, stream, result);
}

int main(int argc, const char *argv[])
{
    boost::asio::io_service io;
    boost::asio::posix::stream_descriptor in(io, ::dup(STDIN_FILENO));

    for(int i = 0; i < 5; i++){
        std::cout << i << " Type in 4 chareters and press enter" << std::endl << std::endl;
        std::cout << "in> ";
        std::cout.flush();
        try {
            boost::asio::streambuf buf;
            receive(io, in, 5, buf);
            std::cout << "out> ";
            std::copy(boost::asio::buffer_cast<const char *>(buf.data()), boost::asio::buffer_cast<const char *>(buf.data()) + buf.size(), std::ostream_iterator<char>(std::cout));

        } catch (const std::exception & e) {
            std::cout << e.what() << std::endl;
        }
    }

    return 0;
}
#包括
#包括
#包括
无效集合结果(boost::可选*a,boost::系统::错误\u代码b){
如果(b==0)
a->重置(b);
}
无效接收(boost::asio::io_服务和io,boost::asio::posix::stream_描述符和流,boost::asio::streambuf和结果){
boost::可选的定时器结果;
boost::可选的读取结果;
boost::asio::截止期计时器(io);
timer.expires_from_now(boost::posix_time::毫秒(5000));
异步等待(boost::bind(&set_result,&timer_result,&u 1));
boost::asio::异步读取(
流动
结果,,
boost::asio::传输至少(1),
boost::bind(&set_result,&read_result,_1));
boost::system::error_code ec;
而(1){
io.reset();
io.投票表决一(ec);
如果(读取结果){
timer.cancel();
返回;
}else if(计时器\u结果)
抛出std::运行时_错误(“超时”);
}
}
无效接收(boost::asio::io\u服务和io、boost::asio::posix::流描述符和流、大小、boost::asio::streambuf和结果){
while(result.size()std::cout上述实现中的问题是,异步_读取的超时从未取消。下面是如何执行此操作:

    while(1) {
        io.reset();
        io.poll_one(ec);

        if ( read_result ) {
            timer.cancel(); // cancel the timeout operation as it has not completed yet
            return;

        } else if ( timer_result ) {
            stream.cancel(); // cancel the read operation as it has not completed yet
            throw std::runtime_error("timeout");
        }
    }
    while(1) {
        io.reset();
        io.poll_one(ec);

        if ( read_result ) {
            timer.cancel(); // cancel the timeout operation as it has not completed yet
            return;

        } else if ( timer_result ) {
            stream.cancel(); // cancel the read operation as it has not completed yet
            throw std::runtime_error("timeout");
        }
    }