Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6231373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:55:26+00:00 2026-05-24T09:55:26+00:00

I am trying to use Boost::asio and async_read to implement a timeout in protocol

  • 0

I am trying to use Boost::asio and async_read to implement a timeout in protocol which uses the serial port.

I have done a test implementation using synchronous read, which works just find, but my implementation with time out does not work.

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;
    }
}

When I run this code I got the following outputs:

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

As it appears from the output, the timer is set must faster than 5000ms, in this case less than 1 ms.

Further more, by looking at the actual bytes I does receives it seems like I am missing the first byte.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:55:27+00:00Added an answer on May 24, 2026 at 9:55 am

    Hmm… The problem here is subtle, basically when you cancel the previous timer – another event is triggered on the io_service (a call to the handler responsible for that timer) to indicate it has been cancelled (with the appropriate error_code). Because you are only calling poll_one, the read is good to go, so you handle the read, then you cancel the timer (which sets up the subsequent event on the io_service – but for this run, it does not get called) – and then when your receive() method is called a second time, and you call poll_one – this event is triggered (and because you don’t check for error condition in your set_result method), the state of the optional is good (it contains an error) and you treat this as your timeout…

    Follow?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use boost::signal to implement a callback mechanism, and I'm getting a
I have been trying to set the TTL on ICMP packets using the boost::asio::ip::unicast::hops
I'm trying to use boost::exception and have condensed my prototype code down to the
I am trying to use boost-test, and in particular boost unit testing. I clearly
I am trying to use Boost Test to add some much needed unit tests
I am relatively new to CMake, and I'm trying use the boost asio library
I am trying to use BOOST ASIO library asynchronous sockets. My platform is Linux.
I am trying to receive data from a server application using boost asio's async_read()
I'm trying to create a library that uses Boost ASIO (UDP multicast, asynchronous) and
I'm trying to use boost iterator facade to implement an iterator for a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.