// protected:
void session::register_channel(channel::ptr channel,
    result_handler handle_started, result_handler handle_stopped)
{
    result_handler stop_handler =
        BIND_3(do_remove, _1, channel, handle_stopped);

    result_handler start_handler =
        BIND_4(handle_start, _1, channel, handle_started, stop_handler);

    if (stopped())
    {
        start_handler(error::service_stopped);
        return;
    }

    if (incoming_)
    {
        handle_pend(error::success, channel, start_handler);
        return;
    }

    channel->set_notify(notify_);
    channel->set_nonce(nonzero_pseudo_random());

    result_handler unpend_handler =
        BIND_3(do_unpend, _1, channel, start_handler);

    pending_.store(channel,
        BIND_3(handle_pend, _1, channel, unpend_handler));
}
void session::do_unpend(const code& ec, channel::ptr channel,
    result_handler handle_started)
{
    channel->set_nonce(0);
    pending_.remove(channel, BIND_1(handle_unpend, _1));
    handle_started(ec);
}
// protected:
void session::start_channel(channel::ptr channel,
    result_handler handle_started)
{
    channel->set_notify(notify_on_connect_);
    channel->set_nonce(pseudo_random(1, max_uint64));

    // The channel starts, invokes the handler, then starts the read cycle.
    channel->start(
        BIND_3(handle_starting, _1, channel, handle_started));
}