Merge issue-44-binary-session: binary subprocess session (closes #44)
This commit is contained in:
commit
7cfd30e550
4 files changed
+1222
No files matched your search
@@ -4,11 +4,13 @@
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -161,6 +163,160 @@ private:
|
||||
[[nodiscard]] Void::Result::Result<Child, Void::Result::Error>
|
||||
spawn(const SpawnOptions& options);
|
||||
|
||||
inline constexpr std::size_t kDefaultBinaryOutputBufferBytes = 1024ULL * 1024ULL;
|
||||
|
||||
/// Why a BinarySession operation stopped before completing.
|
||||
enum class BinaryFailureCode : std::int32_t {
|
||||
none = 0,
|
||||
timeout = 1,
|
||||
cancelled = 2,
|
||||
truncated = 3,
|
||||
closed = 4,
|
||||
buffer_full = 5,
|
||||
io_error = 6,
|
||||
};
|
||||
|
||||
/// Successful (possibly partial) BinarySession operation.
|
||||
struct BinaryTransfer {
|
||||
std::size_t bytes{0};
|
||||
bool eof{false}; ///< the stream reached end-of-file
|
||||
bool exited{false}; ///< the child exit status is known (see status)
|
||||
bool truncated{false}; ///< output was discarded because a buffer was full
|
||||
ExitStatus status{};
|
||||
};
|
||||
|
||||
/// Structured BinarySession failure. `bytes_transferred` lets callers resume
|
||||
/// from the exact offset that was not transferred.
|
||||
struct BinaryError {
|
||||
std::size_t bytes_transferred{0};
|
||||
bool eof{false};
|
||||
BinaryFailureCode failure{BinaryFailureCode::io_error};
|
||||
Void::Result::Error error{};
|
||||
};
|
||||
|
||||
struct BinarySessionOptions {
|
||||
std::size_t max_stdout_buffer{kDefaultBinaryOutputBufferBytes};
|
||||
std::size_t max_stderr_buffer{kDefaultBinaryOutputBufferBytes};
|
||||
/// When true, output beyond a buffer cap is drained and discarded (marked
|
||||
/// truncated) instead of applying backpressure to the child.
|
||||
bool truncate_output_on_overflow{false};
|
||||
};
|
||||
|
||||
/// Incremental binary I/O session over a spawned child's pipes.
|
||||
///
|
||||
/// Each call drives both output directions: while stdout is being read,
|
||||
/// stderr is drained into its bounded buffer (and vice versa), so a child
|
||||
/// flooding one stream cannot block the other as long as buffering allows.
|
||||
/// Pipes are non-blocking; operations are poll-driven and bounded by an
|
||||
/// optional timeout and a stop token (or cancel()).
|
||||
class BinarySession
|
||||
{
|
||||
public:
|
||||
BinarySession() noexcept = default;
|
||||
explicit BinarySession(Child child, BinarySessionOptions options = {});
|
||||
~BinarySession() noexcept;
|
||||
|
||||
BinarySession(const BinarySession&) = delete;
|
||||
BinarySession& operator=(const BinarySession&) = delete;
|
||||
BinarySession(BinarySession&& other) noexcept;
|
||||
BinarySession& operator=(BinarySession&& other) noexcept;
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<BinarySession, Void::Result::Error>
|
||||
spawn(const SpawnOptions& options, BinarySessionOptions session_options = {});
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept;
|
||||
[[nodiscard]] Child& child() noexcept { return child_; }
|
||||
[[nodiscard]] const Child& child() const noexcept { return child_; }
|
||||
[[nodiscard]] long long pid() const noexcept { return child_.pid(); }
|
||||
|
||||
/// Writes as much of `data` as the child accepts. Returns the transferred
|
||||
/// count; on timeout/cancellation/closure the BinaryError reports it.
|
||||
[[nodiscard]] Void::Result::Result<BinaryTransfer, BinaryError> write_stdin(
|
||||
std::span<const std::byte> data,
|
||||
std::chrono::milliseconds timeout = std::chrono::milliseconds{0},
|
||||
std::stop_token stop = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<BinaryTransfer, BinaryError> read_stdout(
|
||||
std::span<std::byte> out,
|
||||
std::chrono::milliseconds timeout = std::chrono::milliseconds{0},
|
||||
std::stop_token stop = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<BinaryTransfer, BinaryError> read_stderr(
|
||||
std::span<std::byte> out,
|
||||
std::chrono::milliseconds timeout = std::chrono::milliseconds{0},
|
||||
std::stop_token stop = {});
|
||||
|
||||
/// Closes the child's stdin (flush-free half-close). Idempotent.
|
||||
[[nodiscard]] Void::Result::Result<void, Void::Result::Error> close_stdin();
|
||||
|
||||
/// Waits for the child while continuing to drain both output streams.
|
||||
[[nodiscard]] Void::Result::Result<ExitStatus, Void::Result::Error> wait(
|
||||
std::chrono::milliseconds timeout = std::chrono::milliseconds{0},
|
||||
std::stop_token stop = {});
|
||||
|
||||
/// Terminates the child (and its process group) with the configured grace.
|
||||
[[nodiscard]] Void::Result::Result<ExitStatus, Void::Result::Error> terminate(
|
||||
std::chrono::milliseconds grace = std::chrono::milliseconds{-1});
|
||||
|
||||
/// Requests cancellation of the next/current operation. Safe to call from
|
||||
/// another thread.
|
||||
void cancel() noexcept { cancelled_.store(true, std::memory_order_release); }
|
||||
[[nodiscard]] bool cancelled() const noexcept
|
||||
{
|
||||
return cancelled_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool stdout_eof() const noexcept { return stdout_eof_; }
|
||||
[[nodiscard]] bool stderr_eof() const noexcept { return stderr_eof_; }
|
||||
[[nodiscard]] bool stdout_truncated() const noexcept { return stdout_truncated_; }
|
||||
[[nodiscard]] bool stderr_truncated() const noexcept { return stderr_truncated_; }
|
||||
[[nodiscard]] std::size_t buffered_stdout() const noexcept
|
||||
{
|
||||
return stdout_buffer_.size() - stdout_offset_;
|
||||
}
|
||||
[[nodiscard]] std::size_t buffered_stderr() const noexcept
|
||||
{
|
||||
return stderr_buffer_.size() - stderr_offset_;
|
||||
}
|
||||
|
||||
private:
|
||||
struct PollFill {
|
||||
bool stdin_broken{false};
|
||||
bool idle{false};
|
||||
BinaryFailureCode failure{BinaryFailureCode::none};
|
||||
Void::Result::Error error{};
|
||||
};
|
||||
|
||||
[[nodiscard]] PollFill poll_fill(int timeout_ms,
|
||||
std::span<const std::byte> stdin_data,
|
||||
std::size_t& stdin_written,
|
||||
bool discard_on_full);
|
||||
[[nodiscard]] Void::Result::Result<BinaryTransfer, BinaryError> read_stream(
|
||||
bool is_stdout,
|
||||
std::span<std::byte> out,
|
||||
std::chrono::milliseconds timeout,
|
||||
std::stop_token stop);
|
||||
void note_exit() noexcept;
|
||||
|
||||
Child child_{};
|
||||
Void::System::OwnedFd stdin_{};
|
||||
Void::System::OwnedFd stdout_{};
|
||||
Void::System::OwnedFd stderr_{};
|
||||
std::vector<std::byte> stdout_buffer_{};
|
||||
std::size_t stdout_offset_{0};
|
||||
std::vector<std::byte> stderr_buffer_{};
|
||||
std::size_t stderr_offset_{0};
|
||||
bool stdout_eof_{true};
|
||||
bool stderr_eof_{true};
|
||||
bool stdin_closed_{true};
|
||||
bool stdout_truncated_{false};
|
||||
bool stderr_truncated_{false};
|
||||
BinarySessionOptions options_{};
|
||||
std::atomic<bool> cancelled_{false};
|
||||
ExitStatus status_{};
|
||||
bool reaped_{false};
|
||||
};
|
||||
|
||||
[[nodiscard]] Void::Result::Result<CommunicateResult, Void::Result::Error>
|
||||
communicate_blocking(Child& child,
|
||||
std::string_view stdin_data = {},
|
||||
|
||||
Reference in new issue
Block a user