From efe4b8f50f9f50d360e018dffc3d533a55a97a6d Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Sat, 8 Nov 2014 22:21:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=BA=D0=B8=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85,=20=D1=81=D1=87=D0=B8=D1=82=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=B8=D0=B7=20=D1=81=D0=BE=D0=BA=D0=B5=D1=82=D0=B0,?= =?UTF-8?q?=20=D0=B2=20=D0=BF=D1=83=D0=BB=D0=B5=20=D0=BF=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=BA=D0=BE=D0=B2=20=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D1=8C=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=20=D0=BE=D0=B1=D1=80=D0=B0=D1=82=D0=BD=D0=BE=20=D0=B2=20?= =?UTF-8?q?=D1=81=D0=BE=D0=BA=D0=B5=D1=82=20(=D0=B8=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=20=D1=8D=D1=82=D0=BE=20=D0=B2=20=D0=BF=D1=81=D0=B5=D0=B2=D0=B4?= =?UTF-8?q?=D0=BE=D0=BB=D0=B8=D0=BD=D0=B5=D0=B9=D0=BD=D0=BE=D0=BC=20=D1=81?= =?UTF-8?q?=D1=82=D0=B8=D0=BB=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/server/session.cpp | 22 +++++++++------------- test/server/session.h | 13 ++++++++++++- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/test/server/session.cpp b/test/server/session.cpp index ec67e21..1255d6b 100644 --- a/test/server/session.cpp +++ b/test/server/session.cpp @@ -7,28 +7,24 @@ session::session(boost::asio::io_service &io_service): boost::asio::coroutine(), { } -session::~session() -{ - std::cout << "session destructor" << std::endl; -} - void session::start(boost::system::error_code ec, std::size_t bytes_transferred) { if(!ec) { - reenter(this) for(;;) + reenter(this) for(;;) // псевдолинейный код { std::memset(_data, 0, 128); yield _socket.async_read_some(boost::asio::buffer(_data, 128), std::bind(&session::start, this, std::placeholders::_1, std::placeholders::_2)); - std::cout << "async_read " << bytes_transferred << " bytes" << std::endl; - std::cout << "number: " << std::atoi(_data) << std::endl; -// yield _pool.add_task([this]{ -// return 42; -// }); + yield _fut = call_async([this]{ + int n = std::atoi(_data); + return n*n; + }); - yield _socket.async_write_some(boost::asio::buffer(_data, 128), std::bind(&session::start, this, std::placeholders::_1, std::placeholders::_2)); - std::cout << "async_write" << std::endl; + yield { + std::string res = std::to_string(_fut.get()) + "\n"; + _socket.async_write_some(boost::asio::buffer(res), std::bind(&session::start, this, std::placeholders::_1, std::placeholders::_2)); + } } } else diff --git a/test/server/session.h b/test/server/session.h index d1267a4..3cead8d 100644 --- a/test/server/session.h +++ b/test/server/session.h @@ -15,12 +15,23 @@ private: char _data[128]; threadpool _pool; + // "local" variables + std::future _fut; + public: session() = delete; session(session&) = delete; session(boost::asio::io_service& io_service); void start(boost::system::error_code ec = boost::system::error_code(), std::size_t bytes_transferred = 0); - ~session(); + + template std::future call_async(std::function func) + { + return _pool.add_task([this, &func]{ + R result = func(); + _socket.get_io_service().dispatch(std::bind(&session::start, this, boost::system::error_code(), 0)); + return result; + }); + } };