examples: Fix compile errors for asio server examples
This commit is contained in:
parent
51e474c097
commit
36a2023310
|
@ -36,12 +36,25 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include <nghttp2/asio_http2_server.h>
|
#include <nghttp2/asio_http2_server.h>
|
||||||
|
|
||||||
using namespace nghttp2::asio_http2;
|
using namespace nghttp2::asio_http2;
|
||||||
using namespace nghttp2::asio_http2::server;
|
using namespace nghttp2::asio_http2::server;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
void run_forever(boost::asio::io_service &io_service, size_t num_threads) {
|
||||||
|
std::vector<std::thread> 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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
try {
|
try {
|
||||||
// Check command line arguments.
|
// Check command line arguments.
|
||||||
|
@ -58,9 +71,9 @@ int main(int argc, char *argv[]) {
|
||||||
std::string port = argv[2];
|
std::string port = argv[2];
|
||||||
std::size_t num_threads = std::stoi(argv[3]);
|
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) {
|
server.handle("/", [](const request &req, const response &res) {
|
||||||
res.write_head(200, {{"foo", {"bar"}}});
|
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)) {
|
if (server.listen_and_serve(ec, tls, addr, port)) {
|
||||||
std::cerr << "error: " << ec.message() << std::endl;
|
std::cerr << "error: " << ec.message() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run_forever(io_service, num_threads);
|
||||||
} else {
|
} else {
|
||||||
if (server.listen_and_serve(ec, addr, port)) {
|
if (server.listen_and_serve(ec, addr, port)) {
|
||||||
std::cerr << "error: " << ec.message() << std::endl;
|
std::cerr << "error: " << ec.message() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run_forever(io_service, num_threads);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
std::cerr << "exception: " << e.what() << "\n";
|
std::cerr << "exception: " << e.what() << "\n";
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,12 +43,25 @@
|
||||||
#endif // HAVE_FCNTL_H
|
#endif // HAVE_FCNTL_H
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include <nghttp2/asio_http2_server.h>
|
#include <nghttp2/asio_http2_server.h>
|
||||||
|
|
||||||
using namespace nghttp2::asio_http2;
|
using namespace nghttp2::asio_http2;
|
||||||
using namespace nghttp2::asio_http2::server;
|
using namespace nghttp2::asio_http2::server;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
void run_forever(boost::asio::io_service &io_service, size_t num_threads) {
|
||||||
|
std::vector<std::thread> 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[]) {
|
int main(int argc, char *argv[]) {
|
||||||
try {
|
try {
|
||||||
// Check command line arguments.
|
// Check command line arguments.
|
||||||
|
@ -65,9 +78,9 @@ int main(int argc, char *argv[]) {
|
||||||
std::size_t num_threads = std::stoi(argv[3]);
|
std::size_t num_threads = std::stoi(argv[3]);
|
||||||
std::string docroot = argv[4];
|
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) {
|
server.handle("/", [&docroot](const request &req, const response &res) {
|
||||||
auto path = percent_decode(req.uri().path);
|
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)) {
|
if (server.listen_and_serve(ec, tls, addr, port)) {
|
||||||
std::cerr << "error: " << ec.message() << std::endl;
|
std::cerr << "error: " << ec.message() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run_forever(io_service, num_threads);
|
||||||
} else {
|
} else {
|
||||||
if (server.listen_and_serve(ec, addr, port)) {
|
if (server.listen_and_serve(ec, addr, port)) {
|
||||||
std::cerr << "error: " << ec.message() << std::endl;
|
std::cerr << "error: " << ec.message() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
run_forever(io_service, num_threads);
|
||||||
}
|
}
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
std::cerr << "exception: " << e.what() << "\n";
|
std::cerr << "exception: " << e.what() << "\n";
|
||||||
|
|
Loading…
Reference in New Issue