Re: [Cppcms-users] async_flush_output and multipart/x_mixed_replace
Brought to you by:
artyom-beilis
From: Artyom B. <art...@ya...> - 2012-02-07 11:31:10
|
You are mixing calls from the boost::thread and the mail event loop thread and you should not. Slightly changed example that works (without boost::thread) Read this: http://cppcms.com/wikipp/en/page/thread_safety Also you can't access context_->response().out() from the thread, you can't call async_flush or async_complete_response from different thread. Artyom Beilis ------------- Support CppCMS by donating money: https://sourceforge.net/donate/index.php?group_id=209965 ------------------- #include <cppcms/service.h> #include <cppcms/application.h> #include <cppcms/http_context.h> #include <cppcms/http_response.h> #include <cppcms/applications_pool.h> #include <booster/aio/deadline_timer.h> #include <boost/bind.hpp> class Application : public cppcms::application { int counter_; booster::aio::deadline_timer timer_; booster::shared_ptr<cppcms::http::context> context_; public: Application(cppcms::service &srv) : cppcms::application(srv) { timer_.set_io_service(service().get_io_service()); } void async_handler(cppcms::http::context::completion_type t) { timer_.expires_from_now(booster::ptime::seconds(1)); timer_.async_wait(boost::bind(&Application::compute_thread, booster::intrusive_ptr<Application>(this))); } void flush_output() { context_->async_flush_output(boost::bind(&Application::async_handler, booster::intrusive_ptr<Application>(this), _1)); } void compute_thread() { std::ostream& o = context_->response().out(); if (counter_++ < 5) { o << "Content-type: text/plain\n\n"; o << "count: " << counter_ << std::endl; o << "--progress-token\n"; flush_output(); return; } o << "Content-type: text/plain\n\n"; o << "Computation finished\n"; o << "--progress-token\n"; context_->async_complete_response(); } void main(std::string) { context_ = release_context(); context_->response().set_content_header("multipart/x-mixed-replace; boundary=progress-token"); context_->response().out() << "--progress-token\n"; counter_ = 0; compute_thread(); } }; int main(int argc, char** argv) { try { cppcms::service srv(argc, argv); booster::intrusive_ptr<Application> app = new Application(srv); srv.applications_pool().mount(app); srv.run(); } catch(std::exception const& e) { std::cerr << e.what() << std::endl; } } |