Skip to content

Commit

Permalink
Improve coro server (qicosmos#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Oct 16, 2023
1 parent 712dab7 commit 8287f01
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ endif()
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++20")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread -std=c++20")
endif ()

# --------------------- Gcc
Expand Down
2 changes: 0 additions & 2 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ class coro_http_response {
void clear() {
head_.clear();
content_.clear();
head_.shrink_to_fit();
content_.shrink_to_fit();

resp_headers_.clear();
resp_headers_sv_.clear();
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if(ENABLE_FILE_IO_URING)
endif()

if (UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread")
endif()

## manual import
Expand Down
21 changes: 11 additions & 10 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,31 @@ using namespace std::chrono_literals;

TEST_CASE("coro_server example, will block") {
return; // remove this line when you run the coro server.
cinatra::coro_http_server server(1, 9001);
cinatra::coro_http_server server(std::thread::hardware_concurrency(), 9001);
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/", [](coro_http_request &req, coro_http_response &resp) {
// response in io thread.
std::cout << std::this_thread::get_id() << "\n";
resp.set_keepalive(true);
std::this_thread::sleep_for(10ms);
resp.set_status_and_content(cinatra::status_type::ok, "hello world");
});

server.set_http_handler<cinatra::GET>(
"/coro",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
std::cout << std::this_thread::get_id() << "\n";

co_await coro_io::post([&] {
co_await coro_io::post([&]() {
// coroutine in other thread.
std::cout << std::this_thread::get_id() << "\n";
resp.set_status(cinatra::status_type::ok);
resp.set_content("hello world in coro");
std::this_thread::sleep_for(10ms);
resp.set_status_and_content(cinatra::status_type::ok, "hello world");
});
std::cout << std::this_thread::get_id() << "\n";
co_return;
});

server.set_http_handler<cinatra::GET, cinatra::POST>(
"/echo", [](coro_http_request &req, coro_http_response &resp) {
// response in io thread.
resp.set_status_and_content(cinatra::status_type::ok, "hello world");
});
server.sync_start();
CHECK(server.port() > 0);
}
Expand Down

0 comments on commit 8287f01

Please sign in to comment.