Boost.Asioから切り離したAsioライブラリがあります。
名前空間が違うぐらいでほとんどBoost.Asioと同じように使えます。Boost.AsioがBoost.SystemErrorやBoost.DateTimeに依存しているのに対し、独立して動くという点が嬉しいです。
試しに非同期にhttp通信するコードを書いてみました。以前の記事との違いは非同期になった点です。以前の記事は同期処理になっています。
AsioのExapmlesには非同期のHttp通信はありませんが、 Boost.AsioのC++03のExamplesに非同期Http通信クライアントのサンプルコードが有るため、それの名前空間を修正して使ってみました。
サンプルそのままだと動かない部分があったので修正を加えています。またエラー処理は一番外側のtry-catch以外全く書いていませんのでご注意下さい。
動作環境はVisual Studio 2015とNuGetで取れる最新のAsio 1.10.2です。2016/4/8現在のAsio自体の最新は1.10.6です。
javascriptなどでは見慣れたコールバック地獄になっています。今後の目標はfutureやコルーチンを使った実装でコールバック地獄を緩和することです。
#define ASIO_ERROR_CATEGORY_NOEXCEPT noexcept #include <asio.hpp> #include <iostream> #include <thread> #include <sstream> #include <memory> int main() { try { asio::io_service io; asio::ip::tcp::resolver resolver{io}; asio::ip::tcp::resolver::query query{"www.boost.org", "http"}; asio::ip::tcp::socket socket{io}; resolver.async_resolve(query, [&](auto const&, asio::ip::tcp::resolver::iterator endpoint) { std::cout << "resolve" << std::endl; asio::async_connect(socket, endpoint, [&](auto const&, auto) { std::cout << "connect" << std::endl; std::ostringstream os; os << "GET " << "/" << " HTTP/1.0\r\n"; os << "Host: " << "www.boost.org" << "\r\n"; os << "Accept: */*\r\n"; os << "Connection: close\r\n\r\n"; asio::async_write(socket, asio::buffer(os.str().data(), os.str().size()), [&](auto error_code, auto) { std::cout << "write" << std::endl; auto response = std::make_shared<asio::streambuf>(); asio::async_read(socket, *response, [&, response](auto, auto) { std::cout << "read" << std::endl; std::istream iss(response.get()); std::string buf; while (iss && std::getline(iss, buf)) { std::cout << buf << std::endl; } }); }); }); }); std::thread thread{[&io] { io.run(); }}; thread.join(); } catch (std::exception const& e) { std::cout << e.what() << std::endl; } }
実行結果例
resolve connect write read HTTP/1.1 200 OK Date: Thu, 07 Apr 2016 20:17:18 GMT Server: Apache/2.2.15 (Red Hat) Accept-Ranges: bytes Connection: close Content-Type: text/html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta name="generator" content= "HTML Tidy for Windows (vers 1st November 2003), see www.w3.org" /> <title>Boost C++ Libraries</title> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> <link rel="icon" href="/favicon.ico" type="image/ico" /> <link rel="stylesheet" type="text/css" href= "/style-v2/section-welcome.css" /> <!--[if IE 7]> <style type="text/css"> body { behavior: url(/style-v2/csshover3.htc); } </style> <![endif]--> <meta name="google-site-verification" content= "mpr2HgFpodnbF_8fv4qXd9roIClVwtX3C-Kd3F6r61w" /> </head><!-- Note: Editing website content is documented at: http://www.boost.org/development/website_updating.html --> <body> <div id="heading"> <div class="heading-inner"> <div class="heading-placard"></div> <h1 class="heading-title"> <a href="/"> <img src="/gfx/space.png" alt= "Boost C++ Libraries" class="heading-logo" /> <span class="heading-boost">Boost</span> <span class="heading-cpplibraries">C++ Libraries</span> </a></h1> <p class="heading-quote"> <q>...one of the most highly regarded and expertly designed C++ library projects in the world.</q> <span class="heading-attribution">— <a href= "http://www.gotw.ca/" class="external">Herb Sutter</a> and <a href= "http://en.wikipedia.org/wiki/Andrei_Alexandrescu" class="external">Andrei Alexandrescu</a>, <a href= "http://safari.awprofessional.com/?XmlId=0321113586" class="external">C++ Coding Standards</a></span></p> </div> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-33761719-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </div> <div id="body"> <div id="body-inner"> <div id="content"> <div class="section" id="intro"> <div class="section-0"> <div class="section-body"> <div class="directory"> <div class="directory-item" id="welcome"> <h2>Welcome to Boost.org!</h2> <p>Boost provides free peer-reviewed portable C++ source libraries.</p> <p>We emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. The <a href="/users/license.html">Boost license</a> encourages both commercial and non-commercial use.</p> <p>We aim to establish "existing practice" and provide reference implementations so that Boost libraries are suitable for eventual standardization. Ten Boost libraries are included in the <a href= "http://www.open-std.org/jtc1/sc22/wg21/" class= "external">C++ Standards Committee's</a> Library Technical Report (<a href= "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1745.pdf" class="external">TR1</a>) and in the new C++11 Standard. C++11 also includes several more Boost libraries in addition to those from TR1. More Boost libraries are proposed for standardization in C++17.</p> <p>Since 2006 an intimate week long annual conference related to Boost called <a href="http://cppnow.org/" class= "external">C++ Now</a> has been held in Aspen, Colorado each May. Boost has been a participant in the <a href= "https://developers.google.com/open-source/soc/?csw=1" class="external">annual Google Summer of Code</a> since 2007.</p> <h3 class="note">Getting Started</h3> <p class="note"><span class="note-body">Boost works on almost any modern operating system, including UNIX and Windows variants. Follow the <a href= "/doc/libs/release/more/getting_started/index.html">Getting Started Guide</a> to download and install Boost. Popular Linux and Unix distributions such as <a href= "http://fedoraproject.org/" class="external">Fedora</a>, <a href="http://www.debian.org/" class= "external">Debian</a>, and <a href="http://www.netbsd.org/" class="external">NetBSD</a> include pre-built Boost packages. Boost may also already be available on your organization's internal web server.</span></p> <h3 class="note">Background</h3> <p class="note"><span class="note-body">Read on with the <a href="/users/">introductory material</a> to help you understand what Boost is about and to help in educating your organization about Boost.</span></p> <h3 class="note">Community</h3> <p class="note"><span class="note-body">Boost welcomes and thrives on participation from a variety of individuals and organizations. Many avenues for participation are available in the <a href="/community/">Boost Community</a>.</span></p> </div><div class="directory-item" id="important-downloads"> <h2>Downloads</h2> <div id="downloads"> <h3>Current Release</h3> <ul> <li><div class="news-title"><a href="/users/history/version_1_60_0.html">Version 1.60.0</a></div><div class="news-date"><a href="/users/history/version_1_60_0.html">Release Notes</a> | <a href="http://sourceforge.net/projects/boost/files/boost/1.60.0/">Download</a> | <a href="/doc/libs/1_60_0/">Documentation</a></div><div class="news-date">December 17th, 2015 15:52 GMT</div></li> </ul> <h3>Beta Release</h3> <ul> <li><div class="news-title"><a href="/users/history/version_1_61_0.html">Version 1.61.0 beta</a></div><div class="news-date"><a href="/users/history/version_1_61_0.html">Release Notes</a> | <a href="https://sourceforge.net/projects/boost/files/boost/1.61.0.beta.1/">Download</a> | <a href="/doc/libs/1_61_0_b1/">Documentation</a></div><div class="news-date">In Progress</div></li> </ul> </div> <p><a href="/users/download/">More Downloads...</a> (<a href="feed/downloads.rss">RSS</a>)</p> </div> <div class="directory-item" id="important-news"> <h2>News</h2> <ul id="news"> <li><span class= "news-title"><a href="/users/history/version_1_60_0.html">Version 1.60.0</a></span> <span class= "news-description"><span class="brief"><span class="purpose"> New Libraries: VMD Updated Libraries: Atomic, Chrono, Container, Context, Core, Filesystem, Flyweight, Fusion, Interprocess, Intrusive, Lexical Cast, Locale, Log, Move, Multi-index Containers, odeint, Optional, Predef, Test, Thread, UUID. </span></span></span> <span class= "news-date">December 17th, 2015 15:52 GMT</span></li> <li><span class= "news-title"><a href="/users/history/version_1_59_0.html">Version 1.59.0</a></span> <span class= "news-description"><span class="brief"><span class="purpose"> New Libraries: Convert, Coroutine2. Updated Libraries: Container, Context, Coroutine, Fusion, Geometry, Interprocess, Intrusive, Lexical Cast, Log, Move, Multi-index Containers, Predef, Program Options, Property Tree, Test, TypeIndex, Variant. </span></span></span> <span class= "news-date">August 13th, 2015 15:23 GMT</span></li> <li><span class= "news-title"><a href="/users/history/version_1_58_0.html">Version 1.58.0</a></span> <span class= "news-description"><span class="brief"><span class="purpose"> New Libraries: Endian, Sort. Updated Libraries: Asio, Chrono, Container, Context, Conversion, DateTime, Flyweight, Function, Functional/Factory, Fusion, Geometry, Hash, Interprocess, Intrusive, Lexical Cast, Log, Math, Move, Multi-index Containers, Multiprecision, Optional, Phoenix, Predef, Random, Thread, TypeErasure, TypeIndex, Units, Unordered, Variant. </span></span></span> <span class= "news-date">April 17th, 2015 07:53 GMT</span></li></ul> <p><a href="/users/news/">More News...</a> (<a href="feed/news.rss">RSS</a>)</p> </div> <div class="clear"></div> <div class="clear"></div> </div> </div> </div> </div> </div> <div id="sidebar"> <div id="get-boost"> <h2><a href="/users/download/"><img src="/gfx/space.png" alt= "Get Boost" /></a></h2> </div> <div id="search"> <!-- Created at http://www.google.com/cse/ --> <div id="cse-search-form" style="width: 100%;">Loading</div> <script src="http://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"> google.load('search', '1', {language : 'en'}); google.setOnLoadCallback(function() { var customSearchControl = new google.search.CustomSearchControl('011577717147771266991:jigzgqluebe'); customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); var options = new google.search.DrawOptions(); options.enableSearchboxOnly("http://google.com/cse?cx=011577717147771266991:jigzgqluebe"); customSearchControl.draw('cse-search-form', options); }, true); </script> <link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" /> </div> <div id="donatelink"> <a href="/donate" class=""><img src= "https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" alt= "Donate to Boost" /></a> </div> <div id="menu"> <ul> <li id="welcome-section-menu"> <h4><a href="/" class="internal">Welcome <span class= "link">></span></a></h4> <ul> <li><a href="/doc/libs/release/more/getting_started/index.html" class= "internal">Getting Started <span class="link">></span></a></li> <li><a href="/users/download/" class="internal">Download <span class= "link">></span></a></li> <li><a href="/doc/libs/">Libraries <span class= "link">></span></a></li> <li><a href="/community/groups.html">Mailing Lists <span class= "link">></span></a></li> <li><a href="/development/bugs.html" class="internal">Reporting and Fixing Bugs<span class="link">></span></a></li> <li><a href="https://svn.boost.org/trac/boost/" class= "external">Wiki<span class="link">></span></a></li> </ul> </li> <li id="boost-section-menu"> <h4><a href="/users/index.html" class="internal">Introduction <span class= "link">></span></a></h4> <ul> <li><a href="/users/download/" class="internal">Download <span class= "link">></span></a></li> <li><a href="/users/news/" class="internal">News <span class= "link">></span></a></li> <li><a href="/users/history/" class="internal">Version History <span class="link">></span></a></li> <li><a href="/users/license.html" class="internal">License <span class= "link">></span></a></li> <li><a href="/users/people.html" class="internal">People <span class= "link">></span></a></li> <li><a href="/users/bibliography.html" class="internal">Bibliography <span class="link">></span></a></li> <li><a href="/users/uses.html" class="internal">Who's Using Boost <span class="link">></span></a></li> <li><a href="/users/faq.html" class="internal">FAQ <span class= "link">></span></a></li> <li><a href="/users/proposal.pdf" class="internal">How Boost Started <span class= "link">></span></a></li> <li><a href="/map.html" class="internal">Index <span class= "link">></span></a></li> </ul> </li> <li id="community-section-menu"> <h4><a href="/community/index.html">Community <span class= "link">></span></a></h4> <ul> <li><a href="/community/groups.html">Discussion Groups <span class= "link">></span></a></li> <li><a href="/community/policy.html">Discussion Policy <span class= "link">></span></a></li> <li> <a href="/community/cpp.html">C++ <span class="link">></span></a> <ul> <li><a href="/community/committee.html">Committee Meetings <span class="link">></span></a></li> <li><a href="/community/generic_programming.html">Generic Progamming Techniques <span class="link">></span></a></li> <li><a href="/community/error_handling.html">Error and Exception Handling <span class="link">></span></a></li> <li><a href="/community/exception_safety.html">Exception-Safety in Generic Components <span class="link">></span></a></li> <li><a href="/community/counted_body.html">Counted Body Techniques <span class="link">></span></a></li> <li><a href= "/community/implementation_variations.html">Implementation Variations <span class="link">></span></a></li> <li><a href="/community/feature_model_diagrams.html">Feature Model Diagrams <span class="link">></span></a></li> </ul> </li> <li><a href="/community/acknowledgements.html">Acknowledgements <span class= "link">></span></a></li> <li><a href="/community/groups.html">Mailing Lists <span class= "link">></span></a></li> <li><a href="/community/moderators.html">Moderators <span class= "link">></span></a></li> <li><a href="https://sites.google.com/a/boost.org/steering/">Steering Committee <span class="link">></span></a></li> <li><a href="/community/requests.html">Requesting a New Feature <span class="link">></span></a></li> <li> <a href="/community/reviews.html">Reviews <span class= "link">></span></a> <ul> <li><a href="/community/review_schedule.html">Schedule <span class= "link">></span></a></li> </ul> </li> <li><a href="/community/sandbox.html">Sandbox <span class= "link">></span></a></li> <li> <a href="/community/irc.html">IRC <span class="link">></span></a> </li> <li> <a href="/community/gsoc.html">Google Summer of Code <span class= "link">></span></a> <ul> <li><a href="/community/gsoc_2006_boost_overview.html">2006 Overview <span class="link">></span></a></li> </ul> </li> <li><a href="http://cppnow.org" target="_blank">C++Now <span class= "link">></span></a></li> </ul> </li> <li id="development-section-menu"> <h4><a href="/development/index.html">Development <span class= "link">></span></a></h4> <ul> <li> <a href="/development/submissions.html">Submissions <span class= "link">></span></a> </li> <li><a href="/development/bugs.html" class="internal">Reporting and Fixing Bugs<span class="link">></span></a></li> <li><a href="/development/pull_requests.php" class="internal">Open Pull Requests Report<span class="link">></span></a></li> <li> <a href="/development/testing.html">Testing <span class= "link">></span></a> <ul> <li><a href= "/development/tests/master/developer/summary.html">Master Summary <span class="link">></span></a></li> <li><a href= "/development/tests/master/developer/issues.html">Master Issues <span class="link">></span></a></li> <li><a href="/development/tests/develop/developer/summary.html">Develop Summary <span class="link">></span></a></li> <li><a href="/development/tests/develop/developer/issues.html">Develop Issues <span class="link">></span></a></li> <li><a href="/development/running_regression_tests.html">Running Regression Tests <span class="link">></span></a></li> </ul> </li> <li><a href="/development/requirements.html">Requirements and Guidelines <span class="link">></span></a> <ul> <li><a href="/development/test.html">Test policy <span class= "link">></span></a></li> <li><a href="/development/header.html">Header policy <span class= "link">></span></a></li> <li><a href="/development/separate_compilation.html">Separate Source <span class="link">></span></a></li> <li><a href="/development/reuse.html">Library Reuse <span class= "link">></span></a></li> <li><a href="/development/int_const_guidelines.html">Coding Guidelines for Integral Constant Expressions <span class= "link">></span></a></li> <li><a href="/development/library_metadata.html">Library Metadata <span class="link">></span></a></li> </ul> </li> <li> <h3 class="heading">Website</h3> <ul> <li><a href="/development/website_updating.html">Updating <span class="link">></span></a></li> <li><a href="/development/exemplar.html">Exemplar <span class= "link">></span></a></li> <li><a href="/development/design_faq.html">Design FAQ <span class= "link">></span></a></li> <li><a href="/development/webcheck/index.html">Self Check <span class= "link">></span></a></li> </ul> </li> </ul> </li> <li id="documentation-section-menu"> <h4><a href="/doc/" class="internal">Documentation <span class= "link">></span></a></h4> <ul> <li><a href="/doc/libs/release/more/getting_started/">Getting Started <span class="link">></span></a></li> <li> <a href="/doc/libs">Libraries <span class="link">></span></a> <ul> <li><a href="/doc/libs/1_60_0/" rel="nofollow">1.60.0 - Current Release <span class="link">></span></a></li> <li><a href="/doc/libs/1_59_0/" rel="nofollow">1.59.0<span class= "link">></span></a></li> <li><a href="/doc/libs/1_58_0/" rel="nofollow">1.58.0<span class= "link">></span></a></li> <li><a href="/doc/libs/1_57_0/" rel="nofollow">1.57.0<span class= "link">></span></a></li> <li><a href="/doc/libs/1_56_0/" rel="nofollow">1.56.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_55_0/" rel="nofollow">1.55.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_54_0/" rel="nofollow">1.54.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_53_0/" rel="nofollow">1.53.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_52_0/" rel="nofollow">1.52.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_51_0/" rel="nofollow">1.51.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_50_0/" rel="nofollow">1.50.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_49_0/" rel="nofollow">1.49.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_48_0/" rel="nofollow">1.48.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_47_0/" rel="nofollow">1.47.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_46_1/" rel="nofollow">1.46.1 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_45_0/" rel="nofollow">1.45.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_44_0/" rel="nofollow">1.44.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_43_0/" rel="nofollow">1.43.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_42_0/" rel="nofollow">1.42.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_41_0/" rel="nofollow">1.41.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_40_0/" rel="nofollow">1.40.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_39_0/" rel="nofollow">1.39.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_38_0/" rel="nofollow">1.38.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_37_0/" rel="nofollow">1.37.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_36_0/" rel="nofollow">1.36.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_35_0/" rel="nofollow">1.35.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_34_1/" rel="nofollow">1.34.1 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_34_0/" rel="nofollow">1.34.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_33_1/" rel="nofollow">1.33.1 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_32_0/" rel="nofollow">1.32.0 <span class= "link">></span></a></li> <li><a href="/doc/libs/1_31_0/" rel="nofollow">1.31.0 <span class= "link">></span></a></li> </ul> </li> <li> <a href="/doc/tools.html">Tools <span class="link">></span></a> <ul> <li><a href="/build/">Boost Build <span class= "link">></span></a></li> <li><a href="/tools/regression/">Regression <span class= "link">></span></a></li> <li><a href="/tools/inspect/">Inspect <span class= "link">></span></a></li> <li><a href="/doc/html/boostbook.html">BoostBook <span class= "link">></span></a></li> <li><a href="/tools/quickbook/">QuickBook <span class= "link">></span></a></li> <li><a href="/tools/bcp/">bcp <span class= "link">></span></a></li> <li><a href="/libs/wave/doc/wave_driver.html">Wave <span class= "link">></span></a></li> <li><a href="/tools/auto_index/">AutoIndex <span class= "link">></span></a></li> </ul> </li> </ul> </li> </ul> <div class="box-bottom"></div> </div> </div> <div class="clear"></div> </div> </div> <div id="footer"> <div id="footer-left"> <div id="copyright"> <p>Copyright Beman Dawes, David Abrahams, 1998-2005.</p> <p>Copyright Rene Rivera 2004-2007.</p> </div> <div id="license"> <p>Distributed under the <a href="/LICENSE_1_0.txt" class= "internal">Boost Software License, Version 1.0</a>.</p> </div> </div> <div id="footer-right"> <div id="banners"> <p id="banner-xhtml"><a href="http://validator.w3.org/check?uri=referer" class="external">XHTML 1.0</a></p> <p id="banner-css"><a href= "http://jigsaw.w3.org/css-validator/check/referer" class= "external">CSS</a></p> <p id="banner-osi"><a href= "http://www.opensource.org/docs/definition.php" class="external">OSI Certified</a></p> </div> </div> <div class="clear"></div> </div> </body> </html> 続行するには何かキーを押してください . . .