/// \warning call only from event loop thread, otherwise the behavior is undefined.
    void on_connect(task<std::error_code>::future_move_type future, std::shared_ptr<task<void>::promise_type> promise) {
        const auto ec = future.get();

        if (ec) {
            switch (ec.value()) {
            case asio::error::already_started:
                queue->push_back(promise);
                break;
            case asio::error::already_connected:
                promise->set_value();
                break;
            default:
                promise->set_exception(std::system_error(ec));
                queue.apply([&](queue_type& queue) {
                    for (auto it = queue.begin(); it != queue.end(); ++it) {
                        (*it)->set_exception(std::system_error(ec));
                    }
                    queue.clear();
                });
            }
        } else {
            promise->set_value();
            queue.apply([&](queue_type& queue) {
                for (auto it = queue.begin(); it != queue.end(); ++it) {
                    (*it)->set_value();
                }
                queue.clear();
            });
        }
    }
Beispiel #2
0
    void
    spool() {
        state.apply([&](state_type& state) {
            if (!state->stopped()) {
                throw std::logic_error("invalid state");
            }

            COCAINE_LOG_TRACE(log, "app is spooling");
            state.reset(new state::spooling_t(
                context,
                *loop,
                manifest(),
                profile,
                std::bind(&app_state_t::on_spool, shared_from_this(), ph::_1, ph::_2)
            ));
        });
    }