Skip to content

Instantly share code, notes, and snippets.

@soumyakantiroychowdhury
Created April 10, 2018 06:31
Show Gist options
  • Save soumyakantiroychowdhury/d7104c4c83ad2da19c22416460582dc1 to your computer and use it in GitHub Desktop.
Save soumyakantiroychowdhury/d7104c4c83ad2da19c22416460582dc1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <nghttp2/asio_http2_server.h>
using namespace nghttp2::asio_http2;
using namespace nghttp2::asio_http2::server;
int main(int argc, char *argv[]) {
std::thread::id this_id = std::this_thread::get_id();
std::cout << "Main thread running at " << this_id << std::endl;
boost::system::error_code ec;
http2 server;
server.num_threads(4);
server.handle("/api/ping", [](const request &req, const response &res) {
std::thread::id this_id = std::this_thread::get_id();
std::cout << "Ping Request received at " << this_id << std::endl;
req.on_data([](const uint8_t *data, std::size_t len){
std::cerr.write(reinterpret_cast<const char *>(data), len);
std::cerr << std::endl;
});
res.write_head(200);
res.end("hello, ping");
});
server.handle("/api/wait", [](const request &req, const response &res) {
std::thread::id this_id = std::this_thread::get_id();
std::cout << "Wait Request received at " << this_id << std::endl;
req.on_data([](const uint8_t *data, std::size_t len){
std::cerr.write(reinterpret_cast<const char *>(data), len);
std::cerr << std::endl;
});
for(int j = 0; j < 5; j++)
for(int i = 0; i < INT_MAX; i++);
res.write_head(200);
res.end("hello, wait");
});
if (server.listen_and_serve(ec, "0.0.0.0", "8080")) {
std::cerr << "error: " << ec.message() << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment