From 6864e2969003c68e606e731788a011dcb22c11b6 Mon Sep 17 00:00:00 2001 From: uchenily Date: Wed, 22 May 2024 15:55:18 +0800 Subject: [PATCH] Remove origin TcpBufReader/TcpBufWriter --- tests/test_buffered2.cpp | 9 ++----- uvio/net.hpp | 2 ++ uvio/net/tcp_reader.hpp | 48 ++++++------------------------------- uvio/net/tcp_writer.hpp | 51 ++++++---------------------------------- 4 files changed, 18 insertions(+), 92 deletions(-) diff --git a/tests/test_buffered2.cpp b/tests/test_buffered2.cpp index 03590bd..ded384c 100644 --- a/tests/test_buffered2.cpp +++ b/tests/test_buffered2.cpp @@ -2,9 +2,6 @@ #include "uvio/io.hpp" #include "uvio/net.hpp" -#include "uvio/net/tcp_reader.hpp" -#include "uvio/net/tcp_writer.hpp" - using namespace uvio; using namespace uvio::net; using namespace uvio::io; @@ -57,10 +54,8 @@ using namespace uvio::io; auto process(TcpStream stream) -> Task<> { auto [reader, writer] = stream.into_split(); - TcpBufReader, 1024> buffered_reader( - std::move(reader)); - TcpBufWriter, 1024> buffered_writer( - std::move(writer)); + TcpReader<1024> buffered_reader(std::move(reader)); + TcpWriter<1024> buffered_writer(std::move(writer)); while (true) { std::string buf; diff --git a/uvio/net.hpp b/uvio/net.hpp index 5b2cd3a..41c3b7f 100644 --- a/uvio/net.hpp +++ b/uvio/net.hpp @@ -1,4 +1,6 @@ #pragma once #include "uvio/net/tcp_listener.hpp" +#include "uvio/net/tcp_reader.hpp" #include "uvio/net/tcp_stream.hpp" +#include "uvio/net/tcp_writer.hpp" diff --git a/uvio/net/tcp_reader.hpp b/uvio/net/tcp_reader.hpp index 9a85e5c..01b72c3 100644 --- a/uvio/net/tcp_reader.hpp +++ b/uvio/net/tcp_reader.hpp @@ -1,46 +1,12 @@ #pragma once -#include "uvio/io/buffer.hpp" -#include "uvio/io/impl/buf_reader.hpp" +#include "uvio/io/reader.hpp" +#include "uvio/io/split.hpp" +#include "uvio/net/tcp_stream.hpp" -namespace uvio::io { +namespace uvio::net { -template - requires requires(IO io, std::span buf) { - { io.read(buf) }; - } -// class TcpBufReader : public detail::ImplBufRead { // wrong -class TcpBufReader : public detail::ImplBufRead> { - friend class detail::ImplBufRead; +template +using TcpReader = io::BufReader, RBUF_SIZE>; -public: - TcpBufReader(IO &&io) - : io_{std::move(io)} {} - - TcpBufReader(TcpBufReader &&other) noexcept - : io_{std::move(other.io_)} - , r_stream_{std::move(other.r_stream_)} {} - - auto operator=(TcpBufReader &&other) noexcept -> TcpBufReader & { - if (std::addressof(other) != this) { - io_ = std::move(other.io_); - r_stream_ = std::move(other.r_stream_); - } - return *this; - } - -public: - auto inner() noexcept -> IO & { - return io_; - } - - auto into_inner() noexcept -> IO { - r_stream_.disable(); - return std::move(io_); - } - -private: - IO io_; - detail::StreamBuffer r_stream_; -}; -} // namespace uvio::io +} // namespace uvio::net diff --git a/uvio/net/tcp_writer.hpp b/uvio/net/tcp_writer.hpp index 57c1917..43641d2 100644 --- a/uvio/net/tcp_writer.hpp +++ b/uvio/net/tcp_writer.hpp @@ -1,48 +1,11 @@ #pragma once +#include "uvio/io/split.hpp" +#include "uvio/io/writer.hpp" +#include "uvio/net/tcp_stream.hpp" -#include "uvio/io/buffer.hpp" -#include "uvio/io/impl/buf_writer.hpp" +namespace uvio::net { -namespace uvio::io { +template +using TcpWriter = io::BufWriter, WBUF_SIZE>; -template - requires requires(IO io, std::span buf) { - { io.write(buf) }; - } -class TcpBufWriter : public detail::ImplBufWrite> { - friend class detail::ImplBufWrite>; - -public: - TcpBufWriter(IO &&io) - : io_{std::move(io)} {} - - TcpBufWriter(TcpBufWriter &&other) noexcept - : io_{std::move(other.io_)} - , w_stream_{std::move(other.w_stream_)} {} - - auto operator=(TcpBufWriter &&other) noexcept -> TcpBufWriter & { - if (std::addressof(other) != this) { - io_ = std::move(other.io_); - w_stream_ = std::move(other.w_stream_); - } - return *this; - } - -public: - [[nodiscard]] - auto inner() noexcept -> IO & { - return io_; - } - - [[nodiscard]] - auto into_inner() noexcept -> IO { - w_stream_.disable(); - return std::move(io_); - } - -private: - IO io_; - detail::StreamBuffer w_stream_; -}; - -} // namespace uvio::io +} // namespace uvio::net