Add option to use Boost.Asio

This commit is contained in:
bugdea1er 2024-04-04 17:32:04 +03:00 committed by gittiver
parent 96e049666e
commit e3ff9238a4
8 changed files with 133 additions and 46 deletions

View File

@ -46,6 +46,7 @@ option(CROW_BUILD_EXAMPLES "Build the examples in the project" ${CROW_I
option(CROW_BUILD_TESTS "Build the tests in the project" ${CROW_IS_MAIN_PROJECT}) option(CROW_BUILD_TESTS "Build the tests in the project" ${CROW_IS_MAIN_PROJECT})
option(CROW_AMALGAMATE "Combine all headers into one" OFF) option(CROW_AMALGAMATE "Combine all headers into one" OFF)
option(CROW_INSTALL "Add install step for Crow" ON ) option(CROW_INSTALL "Add install step for Crow" ON )
option(CROW_USE_BOOST "Use Boost.Asio for Crow" OFF)
# Possible values: ssl, compression # Possible values: ssl, compression
option(CROW_FEATURES "Enable features extending Crow's abilities" "") option(CROW_FEATURES "Enable features extending Crow's abilities" "")
@ -62,12 +63,20 @@ target_include_directories(Crow
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>
) )
find_package(asio REQUIRED) if(CROW_USE_BOOST)
find_package(Boost 1.64 COMPONENTS system date_time REQUIRED)
target_link_libraries(Crow target_link_libraries(Crow
INTERFACE INTERFACE
asio::asio Boost::boost Boost::system Boost::date_time
) )
target_compile_definitions(Crow INTERFACE CROW_USE_BOOST)
else()
find_package(asio REQUIRED)
target_link_libraries(Crow
INTERFACE
asio::asio
)
endif()
target_compile_definitions(Crow INTERFACE "") target_compile_definitions(Crow INTERFACE "")

View File

@ -1,32 +1,43 @@
#pragma once #pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#else
#ifndef ASIO_STANDALONE #ifndef ASIO_STANDALONE
#define ASIO_STANDALONE #define ASIO_STANDALONE
#endif #endif
#include <asio.hpp> #include <asio.hpp>
#endif
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <vector>
#include <memory> #include <memory>
#include <vector>
#include "crow/http_parser_merged.h" #include "crow/http_parser_merged.h"
#include "crow/common.h" #include "crow/common.h"
#include "crow/parser.h" #include "crow/compression.h"
#include "crow/http_response.h" #include "crow/http_response.h"
#include "crow/logging.h" #include "crow/logging.h"
#include "crow/settings.h"
#include "crow/task_timer.h"
#include "crow/middleware_context.h"
#include "crow/middleware.h" #include "crow/middleware.h"
#include "crow/middleware_context.h"
#include "crow/parser.h"
#include "crow/settings.h"
#include "crow/socket_adaptors.h" #include "crow/socket_adaptors.h"
#include "crow/compression.h" #include "crow/task_timer.h"
#include "crow/utility.h" #include "crow/utility.h"
namespace crow namespace crow
{ {
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
using tcp = asio::ip::tcp; using tcp = asio::ip::tcp;
#ifdef CROW_ENABLE_DEBUG #ifdef CROW_ENABLE_DEBUG
static std::atomic<int> connectionCount; static std::atomic<int> connectionCount;
#endif #endif
@ -81,7 +92,7 @@ namespace crow
void start() void start()
{ {
auto self = this->shared_from_this(); auto self = this->shared_from_this();
adaptor_.start([self](const asio::error_code& ec) { adaptor_.start([self](const error_code& ec) {
if (!ec) if (!ec)
{ {
self->start_deadline(); self->start_deadline();
@ -474,7 +485,7 @@ namespace crow
auto self = this->shared_from_this(); auto self = this->shared_from_this();
adaptor_.socket().async_read_some( adaptor_.socket().async_read_some(
asio::buffer(buffer_), asio::buffer(buffer_),
[self](const asio::error_code& ec, std::size_t bytes_transferred) { [self](const error_code& ec, std::size_t bytes_transferred) {
bool error_while_reading = true; bool error_while_reading = true;
if (!ec) if (!ec)
{ {
@ -517,7 +528,7 @@ namespace crow
auto self = this->shared_from_this(); auto self = this->shared_from_this();
asio::async_write( asio::async_write(
adaptor_.socket(), buffers_, adaptor_.socket(), buffers_,
[self](const asio::error_code& ec, std::size_t /*bytes_transferred*/) { [self](const error_code& ec, std::size_t /*bytes_transferred*/) {
self->res.clear(); self->res.clear();
self->res_body_copy_.clear(); self->res_body_copy_.clear();
if (!self->continue_requested) if (!self->continue_requested)
@ -548,7 +559,7 @@ namespace crow
inline void do_write_sync(std::vector<asio::const_buffer>& buffers) inline void do_write_sync(std::vector<asio::const_buffer>& buffers)
{ {
asio::write(adaptor_.socket(), buffers, [&](asio::error_code ec, std::size_t) { asio::write(adaptor_.socket(), buffers, [&](error_code ec, std::size_t) {
if (!ec) if (!ec)
{ {
return false; return false;

View File

@ -1,9 +1,13 @@
#pragma once #pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#else
#ifndef ASIO_STANDALONE #ifndef ASIO_STANDALONE
#define ASIO_STANDALONE #define ASIO_STANDALONE
#endif #endif
#include <asio.hpp> #include <asio.hpp>
#endif
#include "crow/common.h" #include "crow/common.h"
#include "crow/ci_map.h" #include "crow/ci_map.h"
@ -11,6 +15,10 @@
namespace crow namespace crow
{ {
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
#endif
/// Find and return the value associated with the key. (returns an empty string if nothing is found) /// Find and return the value associated with the key. (returns an empty string if nothing is found)
template<typename T> template<typename T>
inline const std::string& get_header_value(const T& headers, const std::string& key) inline const std::string& get_header_value(const T& headers, const std::string& key)

View File

@ -1,6 +1,11 @@
#pragma once #pragma once
#include <chrono> #ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#ifdef CROW_ENABLE_SSL
#include <boost/asio/ssl.hpp>
#endif
#else
#ifndef ASIO_STANDALONE #ifndef ASIO_STANDALONE
#define ASIO_STANDALONE #define ASIO_STANDALONE
#endif #endif
@ -8,11 +13,14 @@
#ifdef CROW_ENABLE_SSL #ifdef CROW_ENABLE_SSL
#include <asio/ssl.hpp> #include <asio/ssl.hpp>
#endif #endif
#include <cstdint> #endif
#include <atomic> #include <atomic>
#include <chrono>
#include <cstdint>
#include <future> #include <future>
#include <vector>
#include <memory> #include <memory>
#include <vector>
#include "crow/version.h" #include "crow/version.h"
#include "crow/http_connection.h" #include "crow/http_connection.h"
@ -21,6 +29,12 @@
namespace crow namespace crow
{ {
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
using tcp = asio::ip::tcp; using tcp = asio::ip::tcp;
template<typename Handler, typename Adaptor = SocketAdaptor, typename... Middlewares> template<typename Handler, typename Adaptor = SocketAdaptor, typename... Middlewares>
@ -52,7 +66,7 @@ namespace crow
{ {
tick_function_(); tick_function_();
tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count())); tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count()));
tick_timer_.async_wait([this](const asio::error_code& ec) { tick_timer_.async_wait([this](const error_code& ec) {
if (ec) if (ec)
return; return;
on_tick(); on_tick();
@ -128,7 +142,7 @@ namespace crow
{ {
tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count())); tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count()));
tick_timer_.async_wait( tick_timer_.async_wait(
[this](const asio::error_code& ec) { [this](const error_code& ec) {
if (ec) if (ec)
return; return;
on_tick(); on_tick();
@ -143,7 +157,7 @@ namespace crow
CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs."; CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs.";
signals_.async_wait( signals_.async_wait(
[&](const asio::error_code& /*error*/, int /*signal_number*/) { [&](const error_code& /*error*/, int /*signal_number*/) {
stop(); stop();
}); });
@ -227,7 +241,7 @@ namespace crow
acceptor_.async_accept( acceptor_.async_accept(
p->socket(), p->socket(),
[this, p, &is, service_idx](asio::error_code ec) { [this, p, &is, service_idx](error_code ec) {
if (!ec) if (!ec)
{ {
is.post( is.post(

View File

@ -1,20 +1,41 @@
#pragma once #pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#include <boost/asio/version.hpp>
#ifdef CROW_ENABLE_SSL
#include <boost/asio/ssl.hpp>
#endif
#if BOOST_VERSION >= 107000
#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
#else
#define GET_IO_SERVICE(s) ((s).get_io_service())
#endif
#else
#ifndef ASIO_STANDALONE #ifndef ASIO_STANDALONE
#define ASIO_STANDALONE #define ASIO_STANDALONE
#endif #endif
#include <asio.hpp> #include <asio.hpp>
#include <asio/version.hpp>
#ifdef CROW_ENABLE_SSL #ifdef CROW_ENABLE_SSL
#include <asio/ssl.hpp> #include <asio/ssl.hpp>
#endif #endif
#include "crow/settings.h"
#include <asio/version.hpp>
#if ASIO_VERSION >= 101300 // 1.13.0 #if ASIO_VERSION >= 101300 // 1.13.0
#define GET_IO_SERVICE(s) ((asio::io_context&)(s).get_executor().context()) #define GET_IO_SERVICE(s) ((asio::io_context&)(s).get_executor().context())
#else #else
#define GET_IO_SERVICE(s) ((s).get_io_service()) #define GET_IO_SERVICE(s) ((s).get_io_service())
#endif #endif
#endif
#include "crow/settings.h"
namespace crow namespace crow
{ {
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
using tcp = asio::ip::tcp; using tcp = asio::ip::tcp;
/// A wrapper for the asio::ip::tcp::socket and asio::ssl::stream /// A wrapper for the asio::ip::tcp::socket and asio::ssl::stream
@ -54,32 +75,32 @@ namespace crow
void close() void close()
{ {
asio::error_code ec; error_code ec;
socket_.close(ec); socket_.close(ec);
} }
void shutdown_readwrite() void shutdown_readwrite()
{ {
asio::error_code ec; error_code ec;
socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec); socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
} }
void shutdown_write() void shutdown_write()
{ {
asio::error_code ec; error_code ec;
socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec); socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
} }
void shutdown_read() void shutdown_read()
{ {
asio::error_code ec; error_code ec;
socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec); socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
} }
template<typename F> template<typename F>
void start(F f) void start(F f)
{ {
f(asio::error_code()); f(error_code());
} }
tcp::socket socket_; tcp::socket socket_;
@ -119,7 +140,7 @@ namespace crow
{ {
if (is_open()) if (is_open())
{ {
asio::error_code ec; error_code ec;
raw_socket().close(ec); raw_socket().close(ec);
} }
} }
@ -128,7 +149,7 @@ namespace crow
{ {
if (is_open()) if (is_open())
{ {
asio::error_code ec; error_code ec;
raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec); raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
} }
} }
@ -137,7 +158,7 @@ namespace crow
{ {
if (is_open()) if (is_open())
{ {
asio::error_code ec; error_code ec;
raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send, ec); raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
} }
} }
@ -146,7 +167,7 @@ namespace crow
{ {
if (is_open()) if (is_open())
{ {
asio::error_code ec; error_code ec;
raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec); raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
} }
} }
@ -160,7 +181,7 @@ namespace crow
void start(F f) void start(F f)
{ {
ssl_socket_->async_handshake(asio::ssl::stream_base::server, ssl_socket_->async_handshake(asio::ssl::stream_base::server,
[f](const asio::error_code& ec) { [f](const error_code& ec) {
f(ec); f(ec);
}); });
} }

View File

@ -1,10 +1,15 @@
#pragma once #pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#include <boost/asio/basic_waitable_timer.hpp>
#else
#ifndef ASIO_STANDALONE #ifndef ASIO_STANDALONE
#define ASIO_STANDALONE #define ASIO_STANDALONE
#endif #endif
#include <asio.hpp> #include <asio.hpp>
#include <asio/basic_waitable_timer.hpp> #include <asio/basic_waitable_timer.hpp>
#endif
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@ -15,6 +20,12 @@
namespace crow namespace crow
{ {
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
namespace detail namespace detail
{ {
@ -112,7 +123,7 @@ namespace crow
if (tasks_.empty()) highest_id_ = 0; if (tasks_.empty()) highest_id_ = 0;
} }
void tick_handler(const asio::error_code& ec) void tick_handler(const error_code& ec)
{ {
if (ec) return; if (ec) return;

View File

@ -8,6 +8,12 @@
namespace crow namespace crow
{ {
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using error_code = boost::system::error_code;
#else
using error_code = asio::error_code;
#endif
namespace websocket namespace websocket
{ {
enum class WebSocketReadState enum class WebSocketReadState
@ -299,7 +305,7 @@ namespace crow
//asio::async_read(adaptor_.socket(), asio::buffer(&mini_header_, 1), //asio::async_read(adaptor_.socket(), asio::buffer(&mini_header_, 1),
adaptor_.socket().async_read_some( adaptor_.socket().async_read_some(
asio::buffer(&mini_header_, 2), asio::buffer(&mini_header_, 2),
[this](const asio::error_code& ec, std::size_t [this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG #ifdef CROW_ENABLE_DEBUG
bytes_transferred bytes_transferred
#endif #endif
@ -367,7 +373,7 @@ namespace crow
remaining_length16_ = 0; remaining_length16_ = 0;
asio::async_read( asio::async_read(
adaptor_.socket(), asio::buffer(&remaining_length16_, 2), adaptor_.socket(), asio::buffer(&remaining_length16_, 2),
[this](const asio::error_code& ec, std::size_t [this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG #ifdef CROW_ENABLE_DEBUG
bytes_transferred bytes_transferred
#endif #endif
@ -403,7 +409,7 @@ namespace crow
{ {
asio::async_read( asio::async_read(
adaptor_.socket(), asio::buffer(&remaining_length_, 8), adaptor_.socket(), asio::buffer(&remaining_length_, 8),
[this](const asio::error_code& ec, std::size_t [this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG #ifdef CROW_ENABLE_DEBUG
bytes_transferred bytes_transferred
#endif #endif
@ -447,7 +453,7 @@ namespace crow
{ {
asio::async_read( asio::async_read(
adaptor_.socket(), asio::buffer((char*)&mask_, 4), adaptor_.socket(), asio::buffer((char*)&mask_, 4),
[this](const asio::error_code& ec, std::size_t [this](const error_code& ec, std::size_t
#ifdef CROW_ENABLE_DEBUG #ifdef CROW_ENABLE_DEBUG
bytes_transferred bytes_transferred
#endif #endif
@ -489,7 +495,7 @@ namespace crow
to_read = remaining_length_; to_read = remaining_length_;
adaptor_.socket().async_read_some( adaptor_.socket().async_read_some(
asio::buffer(buffer_, static_cast<std::size_t>(to_read)), asio::buffer(buffer_, static_cast<std::size_t>(to_read)),
[this](const asio::error_code& ec, std::size_t bytes_transferred) { [this](const error_code& ec, std::size_t bytes_transferred) {
is_reading = false; is_reading = false;
if (!ec) if (!ec)
@ -641,7 +647,7 @@ namespace crow
auto watch = std::weak_ptr<void>{anchor_}; auto watch = std::weak_ptr<void>{anchor_};
asio::async_write( asio::async_write(
adaptor_.socket(), buffers, adaptor_.socket(), buffers,
[&, watch](const asio::error_code& ec, std::size_t /*bytes_transferred*/) { [&, watch](const error_code& ec, std::size_t /*bytes_transferred*/) {
if (!ec && !close_connection_) if (!ec && !close_connection_)
{ {
sending_buffers_.clear(); sending_buffers_.clear();

View File

@ -19,6 +19,13 @@
using namespace std; using namespace std;
using namespace crow; using namespace crow;
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
using asio_error_code = boost::system::error_code;
#else
using asio_error_code = asio::error_code;
#endif
#define LOCALHOST_ADDRESS "127.0.0.1" #define LOCALHOST_ADDRESS "127.0.0.1"
TEST_CASE("Rule") TEST_CASE("Rule")
@ -2925,7 +2932,7 @@ TEST_CASE("websocket_max_payload")
} }
} }
asio::error_code ec; asio_error_code ec;
c.lowest_layer().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec); c.lowest_layer().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
app.stop(); app.stop();
@ -3510,7 +3517,7 @@ TEST_CASE("timeout")
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451));
auto receive_future = async(launch::async, [&]() { auto receive_future = async(launch::async, [&]() {
asio::error_code ec; asio_error_code ec;
c.receive(asio::buffer(buf, 2048), 0, ec); c.receive(asio::buffer(buf, 2048), 0, ec);
return ec; return ec;
}); });
@ -3530,7 +3537,7 @@ TEST_CASE("timeout")
size_t received; size_t received;
auto receive_future = async(launch::async, [&]() { auto receive_future = async(launch::async, [&]() {
asio::error_code ec; asio_error_code ec;
received = c.receive(asio::buffer(buf, 2048), 0, ec); received = c.receive(asio::buffer(buf, 2048), 0, ec);
return ec; return ec;
}); });