Boost logo

Boost Users :

Subject: Re: [Boost-users] Asio problems with async_read
From: Allan Nielsen (a_at_[hidden])
Date: 2011-08-08 10:33:14


Hi

> Note that if poll_one() exits due to "out of work", you have to call
> io_service::reset()

I am not sure that this is my problem...

I have added some more debug code prints:

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){
    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) );
    timer.async_wait( boost::bind(&set_result, &timer_result, _1) );

    LOG(INFO) << "Async read starts here";
    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";
            return;

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

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

When this function is called twice I got the following output:

I0808 16:27:31.632927 31531 ccTalkScan.cxx:92] Timer at 5000ms starts here
I0808 16:27:31.632993 31531 ccTalkScan.cxx:96] Async read starts here
I0808 16:27:31.633018 31531 ccTalkScan.cxx:108] Error code: system:0
read_result: 1 timer_result: 0
I0808 16:27:31.633026 31531 ccTalkScan.cxx:112] Result ready
I0808 16:27:31.633080 31531 ccTalkScan.cxx:92] Timer at 5000ms starts here
I0808 16:27:31.633095 31531 ccTalkScan.cxx:96] Async read starts here
I0808 16:27:31.633112 31531 ccTalkScan.cxx:108] Error code: system:0
read_result: 0 timer_result: 1
I0808 16:27:31.633121 31531 ccTalkScan.cxx:116] Timeout

So, what I do not understand is why does the timeout event occur after
32 ms, when I have specified 5000?

--
Allan W. Nielsen

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net