CptanPanic
CptanPanic

Reputation: 639

Trying to use deadline_timer to add timeout to read_until

Maybe I am misunderstanding how things work, but I am trying to add a timeout to a read_until call, so I created a deadline_timer and started it before calling read_until, but the read_until still blocks everything, and the timer never gets activated. Am I doing it wrong? Below are some snippets from my code.

void MyClass::handle_timeout(const boost::system::error_code& error)
{
    // Our deadline timer went off.
    std::cout << "Deadline Timer was triggered." << std::endl;
    Disconnect();
}

// Read some data.
void MyClass::ReadData(){
            boost::asio::streambuf response;
            deadline_.expires_from_now(boost::posix_time::seconds(DEFAULT_TIMEOUT));
            deadline_.async_wait(boost::bind(&MyClass::handle_timeout, this, _1));

            boost::asio::read_until(socket_,response,asString);
}

Upvotes: 2

Views: 325

Answers (1)

Sam Miller
Sam Miller

Reputation: 24174

you're misunderstanding how things work. If you desire cancelability, you need to use the asynchronous methods such as

boost::asio::async_read_until(...);

instead of

boost::asio::read_until(socket_,response,asString);

Upvotes: 1

Related Questions