From 51cd897b964d1ff387d6a875c13a516c8b4a6c03 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Sat, 13 Apr 2024 16:15:20 +0000 Subject: [PATCH] Create Facade for network --- include/cserver/components/work_guard.hpp | 5 +- include/cserver/engine/network.hpp | 118 ++++++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 include/cserver/engine/network.hpp diff --git a/include/cserver/components/work_guard.hpp b/include/cserver/components/work_guard.hpp index 5f7183a..032e3d1 100644 --- a/include/cserver/components/work_guard.hpp +++ b/include/cserver/components/work_guard.hpp @@ -3,10 +3,11 @@ namespace cserver { + struct StopBlocker { - boost::asio::io_service::work work; + boost::asio::io_context::work guard; inline constexpr StopBlocker(auto, auto& context) : - work(context.template FindComponent().ioContext) {}; + guard(context.template FindComponent().ioContext) {}; }; } // namespace cserver diff --git a/include/cserver/engine/network.hpp b/include/cserver/engine/network.hpp new file mode 100644 index 0000000..d597d01 --- /dev/null +++ b/include/cserver/engine/network.hpp @@ -0,0 +1,118 @@ +#pragma once +#include +namespace cserver::network { + +class DeadLineTimer; + +class TcpSocket; +class TcpResolver; + +class IoContext { + boost::asio::io_context impl; +public: + inline constexpr IoContext() : impl() {}; + class Work { + boost::asio::io_context::work impl; + public: + inline constexpr Work(IoContext& ioContext) : + impl(ioContext.impl) {}; + inline constexpr Work(Work&&) = default; + inline constexpr Work(const Work&) = default; + }; + + friend DeadLineTimer; + friend TcpSocket; + + friend TcpResolver; +}; + + +class DeadLineTimer { + boost::asio::deadline_timer impl; +public: + inline constexpr DeadLineTimer(IoContext& ioContext) : + impl(ioContext.impl) {}; + inline constexpr auto AsyncWait() -> Task<> { + return this->impl.async_wait(boost::asio::use_awaitable); + }; + inline constexpr auto Cancel() -> void { + this->impl.cancel(); + }; + inline constexpr auto CancelOne() -> void { + this->impl.cancel_one(); + }; +}; + +class TcpSocket { + boost::asio::ip::tcp::socket impl; +public: + inline constexpr TcpSocket(IoContext& ioContext) : + impl(ioContext.impl) {}; + inline constexpr TcpSocket(TcpSocket&& other) : + impl(std::move(other.impl)) {}; + + template + friend inline constexpr auto AsyncWrite(TcpSocket& socket, Buffer buffer, CompletionCondition completion); + template + friend inline constexpr auto AsyncWrite(TcpSocket& socket, Buffer buffer); + + template + friend inline constexpr auto AsyncRead(TcpSocket& socket, Buffer buffer, CompletionCondition completion); + template + friend inline constexpr auto AsyncRead(TcpSocket& socket, Buffer buffer); + + template + friend inline constexpr auto AsyncReadUntil(TcpSocket& socket, Buffer buffer, Match match); +}; + + + + +class TcpResolver { + boost::asio::ip::tcp::resolver impl; +public: + inline constexpr TcpResolver(IoContext& ioContext) : impl(ioContext.impl) {}; +}; + + +template +inline constexpr auto Buffer(const T* ptr, std::size_t size) { + return boost::asio::buffer(ptr, size); +}; + +template +inline constexpr auto DynamicBuffer(T&& arg) -> decltype(boost::asio::dynamic_buffer(std::forward(arg))) { + return boost::asio::dynamic_buffer(std::forward(arg)); +}; + + +template +inline constexpr auto AsyncWrite(TcpSocket& socket, Buffer buffer) -> Task<> { + return boost::asio::async_write(socket.impl, std::move(buffer), boost::asio::use_awaitable); +}; + +template +inline constexpr auto AsyncWrite(TcpSocket& socket, Buffer buffer, CompletionCondition completion) -> Task<> { + return boost::asio::async_write(socket.impl, std::move(buffer), std::move(completion), boost::asio::use_awaitable); +}; + +template +inline constexpr auto AsyncRead(TcpSocket& socket, Buffer buffer) -> Task<> { + return boost::asio::async_read(socket.impl, std::move(buffer), boost::asio::use_awaitable); +}; + +template +inline constexpr auto AsyncRead(TcpSocket& socket, Buffer buffer, CompletionCondition completion) -> Task<> { + return boost::asio::async_read(socket.impl, std::move(buffer), std::move(completion), boost::asio::use_awaitable); +}; + +template +inline constexpr auto AsyncReadUntil(TcpSocket& socket, Buffer buffer, Match match) -> Task<> { + return boost::asio::async_read_until(socket.impl, std::move(buffer), std::move(match), boost::asio::use_awaitable); +}; + +inline constexpr auto TransferAll() { + return boost::asio::transfer_all(); +}; + +} // namespace cserver::network