182 lines
5.9 KiB
C++
182 lines
5.9 KiB
C++
#pragma once
|
|
|
|
#include <Void/Async/Task.hpp>
|
|
#include <Void/IO/Detail/Backend.hpp>
|
|
#include <Void/IO/Types.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <span>
|
|
#include <utility>
|
|
|
|
namespace Void::IO
|
|
{
|
|
|
|
class ByteStream
|
|
{
|
|
public:
|
|
ByteStream() noexcept = default;
|
|
|
|
explicit ByteStream(std::shared_ptr<detail::StreamState> state) noexcept
|
|
: state_(std::move(state))
|
|
{
|
|
}
|
|
|
|
ByteStream(ByteStream&&) noexcept = default;
|
|
ByteStream& operator=(ByteStream&&) noexcept = default;
|
|
ByteStream(const ByteStream&) = delete;
|
|
ByteStream& operator=(const ByteStream&) = delete;
|
|
|
|
[[nodiscard]] bool valid() const noexcept { return state_ != nullptr; }
|
|
|
|
[[nodiscard]] DescriptorId id() const noexcept
|
|
{
|
|
return state_ ? state_->id() : DescriptorId{};
|
|
}
|
|
|
|
[[nodiscard]] Coro::Task<Void::Result::Result<Transfer>> read_some(
|
|
std::span<std::byte> buf, Async::OperationOptions options = {})
|
|
{
|
|
if (buf.empty()) {
|
|
co_return Transfer{0, false};
|
|
}
|
|
auto state = state_;
|
|
if (!state) {
|
|
co_return std::unexpected{make_io_error(
|
|
IoErrorCode::closed, "ByteStream is closed or moved-from")};
|
|
}
|
|
auto backend = state->lock_backend();
|
|
if (!backend) {
|
|
co_return std::unexpected{make_io_error(
|
|
IoErrorCode::closed, "IO Driver no longer alive")};
|
|
}
|
|
auto submitted = backend->submit_read(state, buf, std::move(options));
|
|
if (!submitted.has_value()) {
|
|
co_return std::unexpected{std::move(submitted.error())};
|
|
}
|
|
auto op = std::move(*submitted);
|
|
co_return co_await op->awaitable(&backend->executor());
|
|
}
|
|
|
|
[[nodiscard]] Coro::Task<Void::Result::Result<Transfer>> write_some(
|
|
std::span<const std::byte> buf, Async::OperationOptions options = {})
|
|
{
|
|
if (buf.empty()) {
|
|
co_return Transfer{0, false};
|
|
}
|
|
auto state = state_;
|
|
if (!state) {
|
|
co_return std::unexpected{make_io_error(
|
|
IoErrorCode::closed, "ByteStream is closed or moved-from")};
|
|
}
|
|
auto backend = state->lock_backend();
|
|
if (!backend) {
|
|
co_return std::unexpected{make_io_error(
|
|
IoErrorCode::closed, "IO Driver no longer alive")};
|
|
}
|
|
auto submitted = backend->submit_write(state, buf, std::move(options));
|
|
if (!submitted.has_value()) {
|
|
co_return std::unexpected{std::move(submitted.error())};
|
|
}
|
|
auto op = std::move(*submitted);
|
|
co_return co_await op->awaitable(&backend->executor());
|
|
}
|
|
|
|
[[nodiscard]] Coro::Task<Void::Result::Result<Transfer>> read_some_owned(
|
|
BufferLease& lease, Async::OperationOptions options = {})
|
|
{
|
|
auto res = co_await read_some(lease.writable(), std::move(options));
|
|
if (res.has_value()) {
|
|
lease.set_valid_bytes(res->bytes);
|
|
}
|
|
co_return res;
|
|
}
|
|
|
|
[[nodiscard]] Coro::Task<Void::Result::Result<Transfer>> write_some_owned(
|
|
const BufferLease& lease, Async::OperationOptions options = {})
|
|
{
|
|
co_return co_await write_some(lease.readable(), std::move(options));
|
|
}
|
|
|
|
[[nodiscard]] Coro::Task<Void::Result::Result<void>> close_and_join(
|
|
Async::OperationOptions options = {})
|
|
{
|
|
auto state = std::move(state_);
|
|
if (!state) {
|
|
co_return {};
|
|
}
|
|
state->mark_explicit_closed();
|
|
auto backend = state->lock_backend();
|
|
if (!backend) {
|
|
co_return {};
|
|
}
|
|
auto submitted = backend->submit_close(state, std::move(options));
|
|
if (!submitted.has_value()) {
|
|
co_return std::unexpected{std::move(submitted.error())};
|
|
}
|
|
auto op = std::move(*submitted);
|
|
co_return co_await op->awaitable(&backend->executor());
|
|
}
|
|
|
|
private:
|
|
std::shared_ptr<detail::StreamState> state_{};
|
|
};
|
|
|
|
[[nodiscard]] inline Coro::Task<std::expected<std::size_t, ComposedTransferError>>
|
|
read_exact(ByteStream& stream,
|
|
std::span<std::byte> buf,
|
|
Async::OperationOptions options = {})
|
|
{
|
|
std::size_t total = 0;
|
|
while (total < buf.size()) {
|
|
if (options.stop.stop_requested()) {
|
|
co_return std::unexpected{ComposedTransferError{
|
|
total,
|
|
false,
|
|
make_io_error(IoErrorCode::cancelled, "read_exact stopped before completion")}};
|
|
}
|
|
const auto chunk = co_await stream.read_some(buf.subspan(total), options);
|
|
if (!chunk.has_value()) {
|
|
co_return std::unexpected{ComposedTransferError{
|
|
total,
|
|
false,
|
|
chunk.error()}};
|
|
}
|
|
total += chunk->bytes;
|
|
if (total < buf.size() && chunk->eof) {
|
|
co_return std::unexpected{ComposedTransferError{
|
|
total,
|
|
true,
|
|
make_io_error(IoErrorCode::eof, "Unexpected EOF before read_exact finished")}};
|
|
}
|
|
}
|
|
co_return total;
|
|
}
|
|
|
|
[[nodiscard]] inline Coro::Task<std::expected<std::size_t, ComposedTransferError>>
|
|
write_all(ByteStream& stream,
|
|
std::span<const std::byte> buf,
|
|
Async::OperationOptions options = {})
|
|
{
|
|
std::size_t total = 0;
|
|
while (total < buf.size()) {
|
|
if (options.stop.stop_requested()) {
|
|
co_return std::unexpected{ComposedTransferError{
|
|
total,
|
|
false,
|
|
make_io_error(IoErrorCode::cancelled, "write_all stopped before completion")}};
|
|
}
|
|
const auto chunk = co_await stream.write_some(buf.subspan(total), options);
|
|
if (!chunk.has_value()) {
|
|
co_return std::unexpected{ComposedTransferError{
|
|
total,
|
|
false,
|
|
chunk.error()}};
|
|
}
|
|
total += chunk->bytes;
|
|
}
|
|
co_return total;
|
|
}
|
|
|
|
} // namespace Void::IO
|