Boost logo

Boost :

From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2006-02-09 08:30:10


Hi Peter,

--- Peter Dimov <pdimov_at_[hidden]> wrote:
<snip>
> >> Is the handler executed?
> >
> > No (according to above rules).
>
> Not good. If you want the library to support this low-level
> style, the above snippet should be exception-safe;

As I said in my previous reply, the tutorial code is simply
broken with respect to exception safety. It could be made
exception safe by putting the pointer in an auto_ptr and only
releasing it after the async_accept function call has returned
successfully. However, I do plan to redo the tutorial to use a
different style anyway, so this is a moot point.

> this could be accomplished by calling the handler with an
> appropriate asio error code.

This would not be appropriate as it may break other important
behaviour of the interface, namely:

- the handler must not be invoked inside the async_accept call;
  and

- the handler must only be invoked from a thread that is calling
  demuxer::run()

> This aside, why would one need shared_from_this?

Because the tutorial may be misleading due to being too simple,
and I plan to change it to use an approach that will also fit
more complex problems better. After all, you did say that
whatever is used in the tutorial will effectively become the
recommended approach.

I am thinking along the lines of the HTTP server example, where
you have a per-connection class, e.g.:

  class tcp_connection
    : public enable_shared_from_this<tcp_connection>
  {
    ...
    void send_reply()
    {
      asio::async_send(socket_, asio::buffer(reply_),
          boost::bind(&tcp_connection::handle_send,
            shared_from_this(), asio::placeholders::error));
    }

    void handle_send(const asio::error& e)
    {
       ...
    }
    ...
    asio::stream_socket socket_;
    std::string reply_;
  };

Cheers,
Chris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk