Boost logo

Boost Users :

Subject: Re: [Boost-users] HTTP server3 example question
From: Acer Yang (yangacer_at_[hidden])
Date: 2013-01-25 01:24:48


On Fri, Jan 25, 2013 at 4:42 AM, huvcbo
<christer.borgqvist10_at_[hidden]>wrote:

> Hi all,
>
> I have problem with HTTP Server3 example,
> in connection::handle_write i have a dedline_timer with a
> async_wait.
> but before the handle_timed execute the connection_ptr goes out of scope,
> why?
>
>
> Appriciate any help.
>
> Christer
>
> The boost HTTP Server 3 in file connection.cpp
> in server.hpp
> boost::shared_ptr<connection> connection_ptr;
> server.cpp
> void server::handle_accept(const boost::system::error_code& e)
> {
> if (!e)
> {
> new_connection_->start();
> new_connection_.reset(new connection(io_service_));
> acceptor_.async_accept(new_**connection_->socket(),
> boost::bind(&server::handle_**accept, this,
> boost::asio::placeholders::**error));
> }
> }
>
> in connection.cpp
>
> void connection::handle_write(const boost::system::error_code& e)
> {
> if (!e)
> {
> const boost::shared_ptr<boost::asio:**:deadline_timer> t(new
> boost::asio::deadline_timer(***io_service_,
> boost::posix_time::seconds(15)**));
> }
> }
>
> void connection::handle_timed(
> const boost::system::error_code& ec,
> const boost::shared_ptr<boost::asio:**:deadline_timer> t)
> {
> syslog(LOG_INFO, "handle_timed: %s", ec.message().c_str());
>
> }
>
>
> ______________________________**_________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/**mailman/listinfo.cgi/boost-**users>
>

Hi,

I usually use timer as follows

struct connection : enable_shared_from_this<connection>
{
  // ...
private:
  socket socket_;
  mutable_buffer buffer_;
  deadline_timer timer_; // According to your design, you may put timer and
buffer in some other classes. e.g. session
                                   // I put the timer here for brevity.
};

void connection::read(...)
{
  timer_.expires_from_now(posix_time::seconds(5));
  timer_.async_wait(bind(&connection::handle_timeout, shared_from_this(),
_1)); // bind will increase ref cnt of this connection
  socket_.async_read_some(buffer_, bind(&connection::handle_read, _1, _2));
}

void connection::handle_timeout(error_code const & err)
{
  if(!err) { // timeout
    socket_.close(); // cause handle_read receive error_code 'Operation
Canceled'
  }
}

Hope this helps.

Best,
Acer.



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