Skip to content

Commit

Permalink
Remove origin TcpBufReader/TcpBufWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
uchenily committed May 22, 2024
1 parent ed6b747 commit 6864e29
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 92 deletions.
9 changes: 2 additions & 7 deletions tests/test_buffered2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,10 +54,8 @@ using namespace uvio::io;

auto process(TcpStream stream) -> Task<> {
auto [reader, writer] = stream.into_split();
TcpBufReader<OwnedReadHalf<TcpStream>, 1024> buffered_reader(
std::move(reader));
TcpBufWriter<OwnedWriteHalf<TcpStream>, 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;

Expand Down
2 changes: 2 additions & 0 deletions uvio/net.hpp
Original file line number Diff line number Diff line change
@@ -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"
48 changes: 7 additions & 41 deletions uvio/net/tcp_reader.hpp
Original file line number Diff line number Diff line change
@@ -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 <typename IO, int RBUF_SIZE>
requires requires(IO io, std::span<char> buf) {
{ io.read(buf) };
}
// class TcpBufReader : public detail::ImplBufRead<IO> { // wrong
class TcpBufReader : public detail::ImplBufRead<TcpBufReader<IO, RBUF_SIZE>> {
friend class detail::ImplBufRead<TcpBufReader>;
template <int RBUF_SIZE>
using TcpReader = io::BufReader<io::OwnedReadHalf<TcpStream>, 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<RBUF_SIZE> r_stream_;
};
} // namespace uvio::io
} // namespace uvio::net
51 changes: 7 additions & 44 deletions uvio/net/tcp_writer.hpp
Original file line number Diff line number Diff line change
@@ -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 <int WBUF_SIZE>
using TcpWriter = io::BufWriter<io::OwnedWriteHalf<TcpStream>, WBUF_SIZE>;

template <class IO, int WBUF_SIZE>
requires requires(IO io, std::span<const char> buf) {
{ io.write(buf) };
}
class TcpBufWriter : public detail::ImplBufWrite<TcpBufWriter<IO, WBUF_SIZE>> {
friend class detail::ImplBufWrite<TcpBufWriter<IO, WBUF_SIZE>>;

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<WBUF_SIZE> w_stream_;
};

} // namespace uvio::io
} // namespace uvio::net

0 comments on commit 6864e29

Please sign in to comment.