Spawns a child process and exposes its stdin/stdout/stderr as async handles. More...
#include <pipe.hpp>
Public Member Functions | |
| template<typename TPoller> | |
| TPipe (TPoller &poller, const std::string &exe, const std::vector< std::string > &args, bool stderrToStdout=false) | |
| Spawns exe with args and wires up async I/O handles. | |
| int | Pid () const |
| Returns the child's process ID. | |
| void | CloseRead () |
| Closes the read end of the child's stdout pipe. | |
| void | CloseWrite () |
| Closes the write end of the child's stdin pipe, sending EOF to the child. | |
| void | CloseErr () |
| Closes the read end of the child's stderr pipe. | |
| int | Wait () |
| Waits for the child process to exit and returns its exit status. | |
| TFuture< int > | AsyncWait () |
| Waits for the child process to exit without blocking the event loop. | |
| TFuture< ssize_t > | ReadSome (void *buffer, size_t size) |
| Reads up to size bytes from the child's stdout. | |
| TFuture< ssize_t > | ReadSomeErr (void *buffer, size_t size) |
| Reads up to size bytes from the child's stderr. | |
| TFuture< ssize_t > | WriteSome (const void *buffer, size_t size) |
| Writes up to size bytes to the child's stdin. | |
Spawns a child process and exposes its stdin/stdout/stderr as async handles.
Linux/macOS only (#ifndef _WIN32). The constructor forks immediately; the child runs exe with args. The parent side owns three async file handles:
Call CloseWrite() to send EOF to the child, then AsyncWait() to reap it. The Close*() calls take effect in the poller's next iteration, so call the blocking Wait() only once the child's output was read to EOF.
|
inline |
Spawns exe with args and wires up async I/O handles.
| poller | Event-loop poller used for async I/O on the pipe fds. |
| exe | Absolute path to the executable. |
| args | Arguments passed to the executable (argv[1..]). |
| stderrToStdout | If true, child stderr is redirected into stdout (ReadSomeErr becomes unavailable). |
| TFuture< int > NNet::TPipe::AsyncWait | ( | ) |
Waits for the child process to exit without blocking the event loop.
Polls waitpid(WNOHANG) and sleeps on the poller between attempts with a backoff growing from 1 ms to 32 ms, so it works with every poller backend. Once it completes the child is reaped and Pid() returns -1.
The returned future refers to this TPipe, so the pipe must outlive the future: destroying it while the future is suspended is a use-after-free.
| TFuture< ssize_t > NNet::TPipe::ReadSome | ( | void * | buffer, |
| size_t | size ) |
Reads up to size bytes from the child's stdout.
Returns bytes read (>0), 0 on EOF (child closed stdout), or a negative retry hint on transient errors.
| TFuture< ssize_t > NNet::TPipe::ReadSomeErr | ( | void * | buffer, |
| size_t | size ) |
Reads up to size bytes from the child's stderr.
Only valid when stderrToStdout = false. Returns bytes read (>0), 0 on EOF, or a negative retry hint.
| int NNet::TPipe::Wait | ( | ) |
Waits for the child process to exit and returns its exit status.
Blocks the calling thread (via waitpid) without running the loop. Call after the child has finished or after sending it a signal; see the class description.
Once it returns the child is reaped and Pid() returns -1.
| TFuture< ssize_t > NNet::TPipe::WriteSome | ( | const void * | buffer, |
| size_t | size ) |
Writes up to size bytes to the child's stdin.
Returns bytes written (>0), 0 if the child closed stdin, or a negative retry hint on transient errors.