|
VLink 2.0.0
A high-performance communication middleware
|
Cross-platform child process with async I/O and state notification. 更多...
#include <process.h>
Public 类型 | |
| enum | State : uint8_t { kNotRunningState = 0 , kStartingState = 1 , kRunningState = 2 } |
| Lifecycle state of the child process. 更多... | |
| enum | ExitStatus : uint8_t { kNormalExitStatus = 0 , kCrashExitStatus = 1 } |
| How the child process exited. 更多... | |
| enum | Error : uint8_t { kNoError = 0 , kUnknownError = 1 , kStartError = 2 , kCrashedError = 3 , kTimedOutError = 4 , kWriteError = 5 , kReadError = 6 , kBufferOverflowError = 7 } |
| Error codes set on failure. 更多... | |
| enum | Mode : uint8_t { kSeparateMode = 0 , kMergedMode = 1 , kForwardedMode = 2 , kForwardedOutputMode = 3 , kForwardedErrorMode = 4 } |
| I/O channel routing mode. 更多... | |
| using | EnvironmentMap = std::unordered_map<std::string, std::string> |
Environment variable map type for set_environment(). | |
| using | ErrorCallback = std::function<void(Error)> |
| Callback type invoked when an error occurs. | |
| using | FinishedCallback = std::function<void(int, ExitStatus)> |
| Callback type invoked when the process exits. | |
| using | ReadyReadCallback = std::function<void()> |
| Callback type invoked when new data is available on a pipe. | |
| using | StateChangedCallback = std::function<void(State)> |
| Callback type invoked when the process state changes. | |
Public 成员函数 | |
| Process () | |
Constructs a Process object. No child is started yet. | |
| ~Process () | |
Destructor. Closes the process and waits up to kDestructorWaitTimeoutMs. | |
| Process (Process &&other) noexcept=delete | |
| Process & | operator= (Process &&other) noexcept=delete |
| State | get_state () const |
| Returns the current state of the child process. | |
| Error | get_error () const |
| Returns the last error code. | |
| int | get_exit_code () const |
| Returns the exit code of the child process. | |
| ExitStatus | get_exit_status () const |
| Returns how the child process exited. | |
| bool | is_running () const |
Returns true if the child process is currently running. | |
| int64_t | get_process_id () const |
| Returns the operating-system process ID of the child. | |
| void | set_max_buffer_size (size_t size) |
| Sets the maximum buffer size for stdout and stderr capture. | |
| size_t | get_max_buffer_size () const |
| Returns the configured maximum buffer size. | |
| void | set_environment (const EnvironmentMap &env_map) |
| Sets the environment variables for the child process. | |
| EnvironmentMap | get_environment () const |
| Returns the configured environment map. | |
| void | set_process_mode (Mode mode) |
| Sets the I/O channel routing mode. | |
| Mode | get_process_mode () const |
| Returns the configured I/O channel mode. | |
| void | set_inherit_environment (bool inherit) |
| Controls whether the child inherits the parent's environment. | |
| bool | get_inherit_environment () const |
| Returns whether the child inherits the parent environment. | |
| void | set_working_directory (const std::string &dir) |
| Sets the working directory for the child process. | |
| std::string | get_working_directory () const |
| Returns the configured working directory. | |
| void | register_error_callback (ErrorCallback &&callback) |
| Registers a callback for error events. | |
| void | register_finished_callback (FinishedCallback &&callback) |
| Registers a callback invoked when the child exits. | |
| void | register_ready_read_stdout_callback (ReadyReadCallback &&callback) |
| Registers a callback invoked when new stdout data is available. | |
| void | register_ready_read_stderr_callback (ReadyReadCallback &&callback) |
| Registers a callback invoked when new stderr data is available. | |
| void | register_state_changed_callback (StateChangedCallback &&callback) |
| Registers a callback invoked when the process state changes. | |
| void | start (const std::string &program, const std::vector< std::string > &arguments={}) |
| Launches the child process. | |
| void | start_command (const std::string &command) |
| Parses and launches a shell command string. | |
| bool | wait_for_started (int msecs=kDefaultWaitTimeoutMs) |
Blocks until the child process enters kRunningState. | |
| bool | wait_for_finished (int msecs=kDefaultWaitTimeoutMs) |
| Blocks until the child process exits. | |
| bool | wait_for_ready_read (int msecs=kDefaultWaitTimeoutMs) |
| Blocks until new data is available on any pipe. | |
| void | terminate () |
| Requests child termination. | |
| void | kill () |
| Forcefully kills the child process (SIGKILL / TerminateProcess). | |
| void | close (bool force_kill_on_timeout=false) |
Calls terminate() and optionally force-kills after a timeout. | |
| size_t | bytes_available_stdout () const |
| Returns the number of bytes available to read from stdout. | |
| size_t | bytes_available_stderr () const |
| Returns the number of bytes available to read from stderr. | |
| bool | can_read_line_stdout () const |
Returns true if a complete newline-terminated line is available on stdout. | |
| bool | can_read_line_stderr () const |
Returns true if a complete newline-terminated line is available on stderr. | |
| bool | read_line_stdout (std::string &line) |
Reads one line from stdout into line. | |
| bool | read_line_stderr (std::string &line) |
Reads one line from stderr into line. | |
| size_t | read_stdout (std::vector< uint8_t > &buffer, size_t max_size) |
Reads up to max_size bytes from stdout into buffer. | |
| size_t | read_stderr (std::vector< uint8_t > &buffer, size_t max_size) |
Reads up to max_size bytes from stderr into buffer. | |
| bool | read_all_output (std::vector< uint8_t > &buffer) |
Reads all available stdout data into buffer (byte vector overload). | |
| bool | read_all_error (std::vector< uint8_t > &buffer) |
Reads all available stderr data into buffer (byte vector overload). | |
| bool | read_all (std::vector< uint8_t > &buffer) |
Reads all available stdout and stderr data into buffer (byte vector overload). | |
| bool | read_all_output (std::string &str) |
Reads all available stdout data into str. | |
| bool | read_all_error (std::string &str) |
Reads all available stderr data into str. | |
| bool | read_all (std::string &str) |
Reads all available stdout and stderr data into str. | |
| size_t | write (const std::vector< uint8_t > &buffer, int timeout_ms=kDefaultWriteTimeoutMs) |
Writes buffer to the child's stdin. | |
| size_t | write (const std::string &str, int timeout_ms=kDefaultWriteTimeoutMs) |
| Writes a string to the child's stdin. | |
| void | close_write_channel () |
| Closes the write channel (stdin pipe), signalling EOF to the child. | |
静态 Public 成员函数 | |
| static int | execute (const std::string &program, const std::vector< std::string > &arguments={}, int timeout_ms=kDefaultExecuteTimeoutMs) |
| Synchronously executes a program and waits for it to finish. | |
| static bool | start_detached (const std::string &program, const std::vector< std::string > &arguments={}) |
| Starts a program in the background and returns immediately. | |
静态 Public 属性 | |
| static constexpr int | kInfinite {-1} |
| Sentinel wait timeout meaning wait indefinitely. | |
| static constexpr int | kDefaultWaitTimeoutMs {3000} |
Default timeout for wait_for_started() and wait_for_finished() in milliseconds. | |
| static constexpr int | kDefaultWriteTimeoutMs {5000} |
Default timeout for write() in milliseconds. | |
| static constexpr int | kDefaultExecuteTimeoutMs {30000} |
Default timeout for the synchronous execute() helper in milliseconds. | |
| static constexpr int | kDestructorWaitTimeoutMs {5000} |
| Time the destructor waits for the child to exit before force-killing in milliseconds. | |
Cross-platform child process with async I/O and state notification.
A single Process object manages one child process at a time. It is non-copyable and non-moveable.
| using vlink::Process::EnvironmentMap = std::unordered_map<std::string, std::string> |
Environment variable map type for set_environment().
| using vlink::Process::ErrorCallback = std::function<void(Error)> |
Callback type invoked when an error occurs.
| using vlink::Process::FinishedCallback = std::function<void(int, ExitStatus)> |
Callback type invoked when the process exits.
First argument is the exit code; second is kNormalExitStatus or kCrashExitStatus.
| using vlink::Process::ReadyReadCallback = std::function<void()> |
Callback type invoked when new data is available on a pipe.
| using vlink::Process::StateChangedCallback = std::function<void(State)> |
Callback type invoked when the process state changes.
| enum vlink::Process::Error : uint8_t |
Error codes set on failure.
| 枚举值 | |
|---|---|
| kNoError | No error |
| kUnknownError | Unknown error |
| kStartError | Failed to start the process (e.g., file not found) |
| kCrashedError | Process crashed |
| kTimedOutError | A wait operation timed out |
| kWriteError | Write to stdin failed |
| kReadError | Read from stdout/stderr failed |
| kBufferOverflowError | Output exceeded |
| enum vlink::Process::ExitStatus : uint8_t |
| enum vlink::Process::Mode : uint8_t |
I/O channel routing mode.
| enum vlink::Process::State : uint8_t |
Lifecycle state of the child process.
| 枚举值 | |
|---|---|
| kNotRunningState | Not started or has exited |
| kStartingState |
|
| kRunningState | Successfully running |
| vlink::Process::Process | ( | ) |
| vlink::Process::~Process | ( | ) |
Destructor. Closes the process and waits up to kDestructorWaitTimeoutMs.
|
deletenoexcept |
|
nodiscard |
Returns the number of bytes available to read from stderr.
|
nodiscard |
Returns the number of bytes available to read from stdout.
|
nodiscard |
Returns true if a complete newline-terminated line is available on stderr.
true if at least one line can be read.
|
nodiscard |
Returns true if a complete newline-terminated line is available on stdout.
true if at least one line can be read. | void vlink::Process::close | ( | bool | force_kill_on_timeout = false | ) |
Calls terminate() and optionally force-kills after a timeout.
| force_kill_on_timeout | If true, calls kill() if the process has not exited after kDestructorWaitTimeoutMs. If false and the child does not exit in time, the process remains running. On Windows, terminate() is already forceful. Default: false. |
| void vlink::Process::close_write_channel | ( | ) |
Closes the write channel (stdin pipe), signalling EOF to the child.
|
static |
Synchronously executes a program and waits for it to finish.
Blocks until the program exits or timeout_ms elapses. Returns the exit code. Stdout and stderr are discarded.
| program | Path to the executable. |
| arguments | Command-line arguments. Default: empty. |
| timeout_ms | Maximum wait time. Default: kDefaultExecuteTimeoutMs (30 s). |
|
nodiscard |
Returns the configured environment map.
|
nodiscard |
Returns the last error code.
kNoError if no error has occurred.
|
nodiscard |
Returns the exit code of the child process.
Valid only after get_state() returns kNotRunningState.
|
nodiscard |
Returns how the child process exited.
kNormalExitStatus or kCrashExitStatus.
|
nodiscard |
Returns whether the child inherits the parent environment.
true if inheriting.
|
nodiscard |
Returns the configured maximum buffer size.
|
nodiscard |
Returns the operating-system process ID of the child.
|
nodiscard |
Returns the configured I/O channel mode.
|
nodiscard |
Returns the current state of the child process.
State value.
|
nodiscard |
Returns the configured working directory.
|
nodiscard |
Returns true if the child process is currently running.
true if state is kRunningState. | void vlink::Process::kill | ( | ) |
Forcefully kills the child process (SIGKILL / TerminateProcess).
| bool vlink::Process::read_all | ( | std::string & | str | ) |
Reads all available stdout and stderr data into str.
| str | Destination string. |
true if any data was available. | bool vlink::Process::read_all | ( | std::vector< uint8_t > & | buffer | ) |
Reads all available stdout and stderr data into buffer (byte vector overload).
| buffer | Destination vector; existing content is replaced. |
true if any data was available. | bool vlink::Process::read_all_error | ( | std::string & | str | ) |
Reads all available stderr data into str.
| str | Destination string. |
true if any data was available. | bool vlink::Process::read_all_error | ( | std::vector< uint8_t > & | buffer | ) |
Reads all available stderr data into buffer (byte vector overload).
| buffer | Destination vector; existing content is replaced. |
true if any data was available. | bool vlink::Process::read_all_output | ( | std::string & | str | ) |
Reads all available stdout data into str.
| str | Destination string. |
true if any data was available. | bool vlink::Process::read_all_output | ( | std::vector< uint8_t > & | buffer | ) |
Reads all available stdout data into buffer (byte vector overload).
| buffer | Destination vector; existing content is replaced. |
true if any data was available. | bool vlink::Process::read_line_stderr | ( | std::string & | line | ) |
Reads one line from stderr into line.
| line | Output string. Includes the trailing newline when one is buffered. |
true if a line was read. | bool vlink::Process::read_line_stdout | ( | std::string & | line | ) |
Reads one line from stdout into line.
| line | Output string. Includes the trailing newline when one is buffered. |
true if a line was read. | size_t vlink::Process::read_stderr | ( | std::vector< uint8_t > & | buffer, |
| size_t | max_size ) |
Reads up to max_size bytes from stderr into buffer.
| buffer | Destination vector. |
| max_size | Maximum bytes to read. |
| size_t vlink::Process::read_stdout | ( | std::vector< uint8_t > & | buffer, |
| size_t | max_size ) |
Reads up to max_size bytes from stdout into buffer.
| buffer | Destination vector. |
| max_size | Maximum bytes to read. |
| void vlink::Process::register_error_callback | ( | ErrorCallback && | callback | ) |
Registers a callback for error events.
| callback | Invoked with the Error code when an error occurs. |
| void vlink::Process::register_finished_callback | ( | FinishedCallback && | callback | ) |
Registers a callback invoked when the child exits.
| callback | Invoked with (exit_code, exit_status). |
| void vlink::Process::register_ready_read_stderr_callback | ( | ReadyReadCallback && | callback | ) |
Registers a callback invoked when new stderr data is available.
| callback | Invoked from the monitor thread when stderr has data. |
| void vlink::Process::register_ready_read_stdout_callback | ( | ReadyReadCallback && | callback | ) |
Registers a callback invoked when new stdout data is available.
| callback | Invoked from the monitor thread when stdout has data. |
| void vlink::Process::register_state_changed_callback | ( | StateChangedCallback && | callback | ) |
Registers a callback invoked when the process state changes.
| callback | Invoked with the new State value. |
| void vlink::Process::set_environment | ( | const EnvironmentMap & | env_map | ) |
Sets the environment variables for the child process.
Replaces (or supplements, depending on set_inherit_environment) the child's environment with env_map.
| env_map | Map of variable name to value. |
| void vlink::Process::set_inherit_environment | ( | bool | inherit | ) |
Controls whether the child inherits the parent's environment.
| inherit | If true, the child inherits all parent environment variables. If false, only the variables in the EnvironmentMap are set. |
| void vlink::Process::set_max_buffer_size | ( | size_t | size | ) |
Sets the maximum buffer size for stdout and stderr capture.
If the child produces more output than size bytes, kBufferOverflowError is set. Passing 0 resets the limit to the default (16 MB).
| size | Maximum buffer size in bytes. |
| void vlink::Process::set_process_mode | ( | Mode | mode | ) |
Sets the I/O channel routing mode.
| mode | Channel mode. Must be set before start(). |
| void vlink::Process::set_working_directory | ( | const std::string & | dir | ) |
Sets the working directory for the child process.
| dir | Absolute path to the working directory. |
| void vlink::Process::start | ( | const std::string & | program, |
| const std::vector< std::string > & | arguments = {} ) |
Launches the child process.
The child is started asynchronously. Call wait_for_started() to block until the child is in kRunningState.
| program | Path to the executable. |
| arguments | Command-line arguments. Default: empty. |
| void vlink::Process::start_command | ( | const std::string & | command | ) |
Parses and launches a shell command string.
Splits command by whitespace into a program and argument list, then calls start().
| command | Shell command string. |
|
static |
Starts a program in the background and returns immediately.
The started process is completely detached from the calling process. No handle is returned; the process runs until it exits on its own.
| program | Path to the executable. |
| arguments | Command-line arguments. Default: empty. |
true if the process was successfully started. | void vlink::Process::terminate | ( | ) |
Requests child termination.
On POSIX this sends SIGTERM, which the child may handle or ignore. On Windows this uses TerminateProcess, so termination is immediate. Use kill() for an explicit forceful stop.
| bool vlink::Process::wait_for_finished | ( | int | msecs = kDefaultWaitTimeoutMs | ) |
Blocks until the child process exits.
| msecs | Timeout in milliseconds. Default: kDefaultWaitTimeoutMs (3000 ms). |
true if the process exited within the timeout. | bool vlink::Process::wait_for_ready_read | ( | int | msecs = kDefaultWaitTimeoutMs | ) |
Blocks until new data is available on any pipe.
| msecs | Timeout in milliseconds. Default: kDefaultWaitTimeoutMs (3000 ms). |
true if data became available within the timeout. | bool vlink::Process::wait_for_started | ( | int | msecs = kDefaultWaitTimeoutMs | ) |
Blocks until the child process enters kRunningState.
| msecs | Timeout in milliseconds. Default: kDefaultWaitTimeoutMs (3000 ms). |
true if the process is running within the timeout. | size_t vlink::Process::write | ( | const std::string & | str, |
| int | timeout_ms = kDefaultWriteTimeoutMs ) |
Writes a string to the child's stdin.
| str | String to write. |
| timeout_ms | Maximum time to wait. Default: kDefaultWriteTimeoutMs. |
| size_t vlink::Process::write | ( | const std::vector< uint8_t > & | buffer, |
| int | timeout_ms = kDefaultWriteTimeoutMs ) |
Writes buffer to the child's stdin.
| buffer | Data to write. |
| timeout_ms | Maximum time to wait for the write to complete. Default: kDefaultWriteTimeoutMs. |
|
staticconstexpr |
Default timeout for the synchronous execute() helper in milliseconds.
|
staticconstexpr |
Default timeout for wait_for_started() and wait_for_finished() in milliseconds.
|
staticconstexpr |
Default timeout for write() in milliseconds.
|
staticconstexpr |
Time the destructor waits for the child to exit before force-killing in milliseconds.
|
staticconstexpr |
Sentinel wait timeout meaning wait indefinitely.