Boost :: asio和async_read

时间:2011-08-08 15:31:06

标签: c++ boost-asio

我正在尝试使用Boost :: asio和async_read来实现使用串口的协议超时。

我已经使用同步读取完成了一个测试实现,它只能查找,但我的超时实现不起作用。

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

void receive(boost::asio::io_service & io, boost::asio::serial_port & cctalk_port, boost::asio::streambuf & result){
#if 1
    // Non-working implementation with timeout
    boost::optional<boost::system::error_code> timer_result;
    boost::optional<boost::system::error_code> read_result;

    boost::asio::deadline_timer timer( io );
    LOG(INFO) << "Timer at 5000ms starts here";
    timer.expires_from_now( boost::posix_time::milliseconds(5000) ); // allow up to 50ms of timeout for every char
    timer.async_wait( boost::bind(&set_result, &timer_result, _1) );

    LOG(INFO) << "Async read starts here (result.size() == " << result.size() << ")";
    boost::asio::async_read(
            cctalk_port,
            result,
            boost::asio::transfer_at_least(1),
            boost::bind( &set_result, &read_result, _1 ));
    boost::system::error_code ec;

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

        if( ec != 0 || read_result != 0 || timer_result != 0)
            LOG(INFO) << "Error code: " << ec << " read_result: " << read_result << " timer_result: " << timer_result;

        if ( read_result ) {
            timer.cancel();
            LOG(INFO) << "Result ready (" << result.size() << ")";
            return;

        } else if ( timer_result ) {
            LOG(INFO) << "Timeout";
            throw runtime_error("timeout");
        }
    }

    LOG(INFO) << "Not suppose to happen";

#else
    // Working implementation without timeout
    boost::asio::read(cctalk_port, result, boost::asio::transfer_at_least(1));
#endif
}

void receive(boost::asio::io_service & io, boost::asio::serial_port & cctalk_port, size_t size, boost::asio::streambuf & result){
    LOG(INFO) << "Fetch at least " << size << " has allready: " << result.size();
    while( result.size() < size ) {
        receive(io, cctalk_port, result);
        LOG(INFO) << "Buffer size: " << result.size() << "/" << size;
    }
}

当我运行此代码时,我得到以下输出:

I0808 17:25:40.809615  3682 ccTalkScan.cxx:137] Fetch at least 5 has allready: 0
I0808 17:25:40.809672  3682 ccTalkScan.cxx:99] Timer at 5000ms starts here
I0808 17:25:40.809686  3682 ccTalkScan.cxx:103] Async read starts here (result.size() == 0)
I0808 17:25:40.809731  3682 ccTalkScan.cxx:115] Error code: system:0 read_result: 1 timer_result: 0
I0808 17:25:40.809738  3682 ccTalkScan.cxx:119] Result ready (12)
I0808 17:25:40.809742  3682 ccTalkScan.cxx:140] Buffer size: 12/5
I0808 17:25:40.809778  3682 ccTalkScan.cxx:137] Fetch at least 5 has allready: 7
I0808 17:25:40.809783  3682 ccTalkScan.cxx:137] Fetch at least 9 has allready: 7
I0808 17:25:40.809788  3682 ccTalkScan.cxx:99] Timer at 5000ms starts here
I0808 17:25:40.809797  3682 ccTalkScan.cxx:103] Async read starts here (result.size() == 7)
I0808 17:25:40.809808  3682 ccTalkScan.cxx:115] Error code: system:0 read_result: 0 timer_result: 1
I0808 17:25:40.809811  3682 ccTalkScan.cxx:123] Timeout

从输出中看,定时器设置必须快于5000ms,在这种情况下小于1 ms。

此外,通过查看我收到的实际字节,似乎我错过了第一个字节。

1 个答案:

答案 0 :(得分:6)

嗯......这里的问题是微妙的,基本上当你取消前一个计时器时 - 在io_service上触发另一个事件(对负责该计时器的处理程序的调用)以指示它已被取消(具有相应的error_code) )。因为你只是调用poll_one,所以读取是好的,所以你处理读取,然后取消定时器(它在io_service上设置后续事件 - 但是对于这次运行,它不会被调用) - 然后当您第二次调用receive()方法,然后调用poll_one时 - 会触发此事件(并且因为您未在set_result方法中检查错误情况),可选的状态是好的(它包含一个错误),你把它当作你的超时......

请?