Skip to content

Commit

Permalink
[new lib]coro_http_server (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Oct 17, 2023
1 parent 71692db commit a682d7e
Show file tree
Hide file tree
Showing 14 changed files with 1,441 additions and 72 deletions.
37 changes: 37 additions & 0 deletions include/ylt/coro_http/coro_http_server.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#ifdef YLT_ENABLE_SSL
#define CINATRA_ENABLE_SSL
#endif
#include <ylt/easylog.hpp>
#define CINATRA_LOG_ERROR ELOG_ERROR
#define CINATRA_LOG_WARNING ELOG_WARN
#define CINATRA_LOG_INFO ELOG_INFO
#define CINATRA_LOG_DEBUG ELOG_DEBUG
#define CINATRA_LOG_TRACE ELOG_TRACE

#include <cinatra/coro_http_server.hpp>

namespace coro_http {
using coro_http_server = cinatra::coro_http_server;
using coro_http_request = cinatra::coro_http_request;
using coro_http_response = cinatra::coro_http_response;
using status_type = cinatra::status_type;
using http_method = cinatra::http_method;
constexpr auto GET = cinatra::GET;
constexpr auto POST = cinatra::POST;
} // namespace coro_http
18 changes: 17 additions & 1 deletion include/ylt/coro_io/coro_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ inline async_simple::coro::Lazy<std::error_code> async_connect(
template <typename Socket>
inline async_simple::coro::Lazy<void> async_close(Socket &socket) noexcept {
callback_awaitor<void> awaitor;
auto &executor = socket.get_executor();
auto executor = socket.get_executor();
co_return co_await awaitor.await_resume([&](auto handler) {
asio::post(executor, [&, handler]() {
asio::error_code ignored_ec;
Expand Down Expand Up @@ -282,6 +282,22 @@ inline async_simple::coro::Lazy<void> sleep_for(Duration d) {
}
}

template <typename Func>
inline async_simple::coro::Lazy<void> post(
Func func,
coro_io::ExecutorWrapper<> *e = coro_io::get_global_block_executor()) {
callback_awaitor<void> awaitor;

co_return co_await awaitor.await_resume(
[e, func = std::move(func)](auto handler) {
auto executor = e->get_asio_executor();
asio::post(executor, [=, func = std::move(func)]() {
func();
handler.resume();
});
});
}

template <typename Socket, typename AsioBuffer>
std::pair<asio::error_code, size_t> read_some(Socket &sock,
AsioBuffer &&buffer) {
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/coro_io/io_context_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class io_context_pool {

easylog::logger<>::instance();
for (std::size_t i = 0; i < pool_size; ++i) {
io_context_ptr io_context(new asio::io_context);
io_context_ptr io_context(new asio::io_context(1));
work_ptr work(new asio::io_context::work(*io_context));
io_contexts_.push_back(io_context);
auto executor = std::make_unique<coro_io::ExecutorWrapper<>>(
Expand Down
Loading

0 comments on commit a682d7e

Please sign in to comment.