Skip to content

Commit

Permalink
Big Update for io: Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
8sileus committed Mar 17, 2024
1 parent 4e53f20 commit 83a4d45
Show file tree
Hide file tree
Showing 68 changed files with 2,499 additions and 1,856 deletions.
4 changes: 2 additions & 2 deletions tests/file_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ auto test_file_create() -> Task<void> {
ret.value().seek(0, SEEK_SET);
{
std::string buf = "hahah\n";
co_await ret.value().read_to_string(buf);
co_await ret.value().read_to_end(buf);
console.info("{} {}", buf, buf.size());
}
if (auto fsync_ret = co_await ret.value().fsync_data(); !fsync_ret) {
Expand All @@ -51,7 +51,7 @@ auto test_file_open() -> Task<void> {
console.error("{} {}", meta.error().value(), meta.error().message());
}
std::string buf = "nono\n";
co_await ret.value().read_to_string(buf);
co_await ret.value().read_to_end(buf);
console.info("{}", buf);
co_return;
}
Expand Down
14 changes: 8 additions & 6 deletions tests/io_buf.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "zedio/core.hpp"
#include "zedio/fs/file.hpp"
#include "zedio/io/buf_reader.hpp"
#include "zedio/io/buf_writer.hpp"
#include "zedio/io/buf/reader.hpp"
#include "zedio/io/buf/stream.hpp"
#include "zedio/io/buf/writer.hpp"
#include "zedio/log.hpp"

using namespace zedio::async;
Expand All @@ -19,9 +20,10 @@ auto create_file() -> Task<void> {
.open("read_line_test.txt");
std::string n1 = "\n";
std::string n2 = "\r\n";
auto writer_test_move = io::BufWriter(std::move(ret.value()));
LOG_DEBUG("{}", writer_test_move.capacity());
auto writer = std::move(writer_test_move);
// auto writer_test_move = io::BufWriter(std::move(ret.value()));
// LOG_DEBUG("{}", writer_test_move.capacity());
// auto writer = std::move(writer_test_move);
auto writer = io::BufStream(std::move(ret.value()));
for (int i = 0; i <= 10000; i += 1) {
if (i & 1) {
co_await writer.write_all(std::to_string(i) + n1);
Expand Down Expand Up @@ -52,7 +54,7 @@ auto read_line() -> Task<void> {
} else {
LOG_ERROR("{} {}", meta.error().value(), meta.error().message());
}
auto reader = io::BufReader(std::move(ret.value()));
auto reader = io::BufReader(std::move(ret.value()));
std::vector<char> line;
while (true) {
if (auto ret = co_await reader.read_line(line); !ret || ret.value() == 0) {
Expand Down
4 changes: 2 additions & 2 deletions tests/socket_addr_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#define BOOST_TEST_MODULE address_test

#include "zedio/net/addr.hpp"
#include "zedio/socket/net/addr.hpp"

#include <boost/test/included/unit_test.hpp>

using namespace zedio::net;
using namespace zedio::socket::net;

BOOST_AUTO_TEST_SUITE(address_test)

Expand Down
4 changes: 2 additions & 2 deletions tests/tcp_socket_test.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#define BOOST_TEST_MODULE socket_test

#include "zedio/net/tcp/socket.hpp"
#include "zedio/socket/net/socket.hpp"

#include <boost/test/included/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(tcp_socket_test)

using namespace zedio::net;
using namespace zedio::socket::net;
using namespace std::chrono_literals;

#define X_HAS_VAL(F) \
Expand Down
11 changes: 5 additions & 6 deletions tests/unix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
#include "zedio/net.hpp"

using namespace zedio::async;
using namespace zedio::net;
using namespace zedio;
using namespace zedio::log;

auto client(const UnixSocketAddr &addr) -> Task<void> {
auto ret = co_await UnixStream::connect(addr);
auto client(const unix_domian::SocketAddr &addr) -> Task<void> {
auto ret = co_await unix_domian::TcpStream::connect(addr);
if (!ret) {
console.error("{}", ret.error().message());
co_return;
Expand All @@ -30,8 +29,8 @@ auto client(const UnixSocketAddr &addr) -> Task<void> {
}
}

auto server(const UnixSocketAddr &addr) -> Task<void> {
auto ret = UnixListener::bind(addr);
auto server(const unix_domian::SocketAddr &addr) -> Task<void> {
auto ret = unix_domian::TcpListener::bind(addr);
if (!ret) {
console.error("{}", ret.error().message());
co_return;
Expand All @@ -58,7 +57,7 @@ auto server(const UnixSocketAddr &addr) -> Task<void> {
}

auto test() -> Task<void> {
auto addr = UnixSocketAddr::parse("tmp_test");
auto addr = unix_domian::SocketAddr::parse("tmp_test");
if (!addr) {
co_return;
}
Expand Down
3 changes: 1 addition & 2 deletions zedio/common/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// C++
#include <concepts>
#include <coroutine>
#include <ranges>
#include <span>
// Linux
#include <sys/socket.h>
Expand All @@ -24,6 +23,6 @@ concept is_awaiter = requires(IOAwaiter awaiter) {
};

template <typename C>
concept constructible_to_char_splice = requires(C c) { std::span<char>{c}; };
concept constructible_to_char_splice = requires(C c) { std::span<const char>{c}; };

} // namespace zedio
3 changes: 3 additions & 0 deletions zedio/common/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Error {
WriteZero,
TooLongTime,
PassedTime,
InvalidSocketType,
};

public:
Expand Down Expand Up @@ -50,6 +51,8 @@ class Error {
return "Time is too long";
case PassedTime:
return "Time has passed";
case InvalidSocketType:
return "Invalid socket type";
default:
return strerror(err_code_);
}
Expand Down
29 changes: 26 additions & 3 deletions zedio/fs/builder.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "zedio/fs/io.hpp"
#include "zedio/io/base/registrator.hpp"

namespace zedio::fs::detail {

template <class FileType>
template <class F>
class Builder {
public:
auto read(bool on) noexcept -> Builder & {
Expand Down Expand Up @@ -44,7 +44,30 @@ class Builder {
[[REMEMBER_CO_AWAIT]]
auto open(std::string_view path) {
adjust_flags();
return FileIO::open<FileType>(path, flags_, permission_);

class Open : public io::detail::IORegistrator<Open> {
private:
using Super = io::detail::IORegistrator<Open>;

public:
Open(std::string_view path, int flags, mode_t mode)
: Super{io_uring_prep_openat, AT_FDCWD, nullptr, flags, mode}
, path_{path} {
this->sqe_->addr = (unsigned long)path.data();
}

auto await_resume() const noexcept -> Result<F> {
if (this->cb_.result_ >= 0) [[likely]] {
return F{this->cb_.result_};
} else {
return std::unexpected{make_sys_error(-this->cb_.result_)};
}
}

private:
std::string path_;
};
return Open(path, flags_, permission_);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion zedio/fs/dir.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "zedio/fs/io.hpp"
#include "zedio/io/io.hpp"

namespace zedio::fs {

Expand Down
123 changes: 56 additions & 67 deletions zedio/fs/file.hpp
Original file line number Diff line number Diff line change
@@ -1,97 +1,81 @@
#pragma once

#include "zedio/common/concepts.hpp"
#include "zedio/common/error.hpp"
#include "zedio/fs/builder.hpp"
#include "zedio/fs/io.hpp"
#include "zedio/fs/impl/impl_async_fsync.hpp"
#include "zedio/fs/impl/impl_async_metadata.hpp"
#include "zedio/io/impl/impl_async_read.hpp"
#include "zedio/io/impl/impl_async_write.hpp"
#include "zedio/io/io.hpp"
// Linux
#include <fcntl.h>

namespace zedio::fs {

class File {
friend detail::FileIO;
class File : public io::detail::Fd,
public io::detail::ImplAsyncRead<File>,
public io::detail::ImplAsyncWrite<File>,
public detail::ImplAsyncFsync<File>,
public detail::ImplAsyncMetadata<File> {
private:
friend class detail::Builder<File>;

private:
File(detail::FileIO &&io)
: io_{std::move(io)} {}
explicit File(const int fd)
: Fd{fd} {}

public:
template <class T>
requires constructible_to_char_splice<T>
[[REMEMBER_CO_AWAIT]]
auto write_all(std::span<const char> buf) noexcept {
return io_.write_all(buf);
}

[[REMEMBER_CO_AWAIT]]
auto write(std::span<const char> buf) noexcept {
return io_.write(buf);
}

template <typename... Ts>
[[REMEMBER_CO_AWAIT]]
auto write_vectored(Ts &...bufs) noexcept {
return io_.write_vectored(bufs...);
}

[[REMEMBER_CO_AWAIT]]
auto read(std::span<char> buf) const noexcept {
return io_.read(buf);
}

[[REMEMBER_CO_AWAIT]]
auto read_to_string(std::string &buf) {
return io_.read_to_end(buf);
}

[[REMEMBER_CO_AWAIT]]
auto read_to_end(std::vector<char> &buf) {
return io_.read_to_end(buf);
auto read_to_end(T &buf) const noexcept -> zedio::async::Task<Result<void>> {
auto old_len = buf.size();
{
auto ret = co_await this->metadata();
if (!ret) {
co_return std::unexpected{ret.error()};
}
buf.resize(old_len + ret.value().stx_size - lseek64(fd_, 0, SEEK_CUR));
}
auto span = std::span<char>{buf}.subspan(old_len);
auto ret = Result<std::size_t>{};
while (true) {
ret = co_await this->read(span);
if (!ret) [[unlikely]] {
co_return std::unexpected{ret.error()};
}
if (ret.value() == 0) {
break;
}
span = span.subspan(ret.value());
if (span.empty()) {
break;
}
}
co_return Result<void>{};
}

[[nodiscard]]
auto seek(off64_t offset, int whence) noexcept {
::lseek64(io_.fd(), offset, whence);
}

[[REMEMBER_CO_AWAIT]]
auto metadata() const noexcept {
return io_.metadata();
}

[[REMEMBER_CO_AWAIT]]
auto fsync_all() noexcept {
return io_.fsync(0);
}

[[REMEMBER_CO_AWAIT]]
auto fsync_data() noexcept {
return io_.fsync(IORING_FSYNC_DATASYNC);
}

[[REMEMBER_CO_AWAIT]]
auto close() noexcept {
return io_.close();
}

[[nodiscard]]
auto fd() const noexcept {
return io_.fd();
::lseek64(fd_, offset, whence);
}

public:
[[REMEMBER_CO_AWAIT]] auto static open(std::string_view path) {
[[REMEMBER_CO_AWAIT]]
static auto open(std::string_view path) {
return options().read(true).open(path);
}

[[REMEMBER_CO_AWAIT]] auto static create(std::string_view path, mode_t permission = 0666) {
[[REMEMBER_CO_AWAIT]]
static auto create(std::string_view path, mode_t permission = 0666) {
return options().write(true).create(true).truncate(true).permission(permission).open(path);
}

[[nodiscard]] auto static options() -> detail::Builder<File> {
[[nodiscard]]
static auto options() -> detail::Builder<File> {
return detail::Builder<File>{};
}

private:
detail::FileIO io_;
};

[[REMEMBER_CO_AWAIT]]
Expand All @@ -108,13 +92,13 @@ static inline auto read(std::string path) -> async::Task<Result<std::vector<char
}

[[REMEMBER_CO_AWAIT]]
static inline auto read_to_string(std::string path) -> async::Task<Result<std::string>> {
static inline auto read_to_end(std::string_view path) -> async::Task<Result<std::string>> {
auto file = co_await File::open(path);
if (!file) [[unlikely]] {
co_return std::unexpected{file.error()};
}
std::string res;
if (auto ret = co_await file.value().read_to_string(res); !ret) [[unlikely]] {
if (auto ret = co_await file.value().read_to_end(res); !ret) [[unlikely]] {
co_return std::unexpected{ret.error()};
}
co_return res;
Expand All @@ -140,4 +124,9 @@ static inline auto sym_link(std::string_view src_path, std::string_view dst_path
return io::symlink(src_path.data(), dst_path.data());
}

[[REMEMBER_CO_AWAIT]]
static inline auto metadata(std::string_view dir_path) {
return detail::Metadata(dir_path);
}

} // namespace zedio::fs
Loading

0 comments on commit 83a4d45

Please sign in to comment.