From 36a2023310f68010fb08b0c4c760b9029c0c864e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 5 Feb 2017 18:08:31 +0900 Subject: [PATCH] examples: Fix compile errors for asio server examples --- examples/asio-sv.cc | 22 ++++++++++++++++++++-- examples/asio-sv2.cc | 21 +++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/examples/asio-sv.cc b/examples/asio-sv.cc index 47a1d847..2c1032b4 100644 --- a/examples/asio-sv.cc +++ b/examples/asio-sv.cc @@ -36,12 +36,25 @@ #include #include +#include #include using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; +namespace { +void run_forever(boost::asio::io_service &io_service, size_t num_threads) { + std::vector ts; + for (size_t i = 0; i < num_threads; ++i) { + ts.emplace_back([&io_service]() { io_service.run(); }); + } + for (auto &t : ts) { + t.join(); + } +} +} // namespace + int main(int argc, char *argv[]) { try { // Check command line arguments. @@ -58,9 +71,9 @@ int main(int argc, char *argv[]) { std::string port = argv[2]; std::size_t num_threads = std::stoi(argv[3]); - http2 server; + boost::asio::io_service io_service; - server.num_threads(num_threads); + http2 server(io_service); server.handle("/", [](const request &req, const response &res) { res.write_head(200, {{"foo", {"bar"}}}); @@ -136,11 +149,16 @@ int main(int argc, char *argv[]) { if (server.listen_and_serve(ec, tls, addr, port)) { std::cerr << "error: " << ec.message() << std::endl; } + + run_forever(io_service, num_threads); } else { if (server.listen_and_serve(ec, addr, port)) { std::cerr << "error: " << ec.message() << std::endl; } + + run_forever(io_service, num_threads); } + } catch (std::exception &e) { std::cerr << "exception: " << e.what() << "\n"; } diff --git a/examples/asio-sv2.cc b/examples/asio-sv2.cc index 8d2580ed..455f0399 100644 --- a/examples/asio-sv2.cc +++ b/examples/asio-sv2.cc @@ -43,12 +43,25 @@ #endif // HAVE_FCNTL_H #include #include +#include #include using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; +namespace { +void run_forever(boost::asio::io_service &io_service, size_t num_threads) { + std::vector ts; + for (size_t i = 0; i < num_threads; ++i) { + ts.emplace_back([&io_service]() { io_service.run(); }); + } + for (auto &t : ts) { + t.join(); + } +} +} // namespace + int main(int argc, char *argv[]) { try { // Check command line arguments. @@ -65,9 +78,9 @@ int main(int argc, char *argv[]) { std::size_t num_threads = std::stoi(argv[3]); std::string docroot = argv[4]; - http2 server; + boost::asio::io_service io_service; - server.num_threads(num_threads); + http2 server(io_service); server.handle("/", [&docroot](const request &req, const response &res) { auto path = percent_decode(req.uri().path); @@ -112,10 +125,14 @@ int main(int argc, char *argv[]) { if (server.listen_and_serve(ec, tls, addr, port)) { std::cerr << "error: " << ec.message() << std::endl; } + + run_forever(io_service, num_threads); } else { if (server.listen_and_serve(ec, addr, port)) { std::cerr << "error: " << ec.message() << std::endl; } + + run_forever(io_service, num_threads); } } catch (std::exception &e) { std::cerr << "exception: " << e.what() << "\n";