Note that there are some explanatory texts on larger screens.

plurals
  1. POCancel pending task callback invocation when the task owner is destructed
    primarykey
    data
    text
    <p>I am implementing something looks like a HTTP server, the design is: for a already established connection, I want to reuse it for several requests, so I start another reading task with <code>async_read</code> on it when a request is finished, and also start a <code>deadline_timer</code>. If there is no input in 60 seconds, the timer will be triggered and the connection will be destructed. The thing that annoys me is that before the invocation of the connection's destructor, the callback we set to <code>async_read</code> will be invoked.</p> <p>So, my question is, is there any way to cancel the pending reading task, that is, destruct the connection without the callback function invoked?</p> <p>If the generic description above is not clear, the detail work flow is as below(code is attached at the bottom):</p> <ol> <li><code>cleanup()</code> is called when a request finished;</li> <li>start the timer and another reading task in <code>cleanup()</code>;</li> <li>if time is out, <code>HandleTimeout()</code> is called, and it calls <code>stop()</code>;</li> <li>in <code>stop()</code>, do the clean work, and after it, the connection instance will be destructed.</li> </ol> <p>but, after step 4, the <code>callback()</code> function will be called, which is registered in <code>AsyncRead()</code>, so, is there any way to cancel the invocation of <code>callback()</code>?</p> <p>code:</p> <pre><code>class Connection : public boost::enable_shared_from_this&lt;Connection&gt;, private boost::noncopyable { public: typedef Connection this_type; void cleanup() { timer_.expires_from_now(boost::posix_time::seconds(kDefaultTimeout)); timer_.async_wait(boost::bind(&amp;this_type::HandleTimeout, shared_from_this(), boost::asio::placeholders::error)); AsyncRead(); } void AsyncRead() { boost::asio::async_read(*socket_, in_, boost::asio::transfer_at_least(1), boost::bind(&amp;this_type::callback, shared_from_this(), boost::asio::placeholders::error)); } void callback(const boost::system::error_code&amp; e) { // ... } void HandleTimeout(const boost::system::error_code&amp; e) { if(e == boost::asio::error::operation_aborted) LDEBUG &lt;&lt; "The timeout timer is cancelled."; else if(e) LERROR &lt;&lt; "Error occurred with the timer, message: " &lt;&lt; e.message(); else if(timer_.expires_at() &lt;= boost::asio::deadline_timer::traits_type::now()) { LDEBUG &lt;&lt; "Connection timed out, close it."; stop(); } } virtual void stop() { connected_ = false; socket_-&gt;close(); connection_manager_.stop(shared_from_this()); } private: // ... boost::asio::deadline_timer timer_; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload