TSDuck v3.40-3963
MPEG Transport Stream Toolkit
|
Fork a process and create an optional pipe to its standard input. More...
#include <tsForkPipe.h>
Public Types | |
enum | InputMode { STDIN_PARENT , STDIN_PIPE , STDIN_NONE } |
How to standard input in the created process. More... | |
enum | OutputMode { KEEP_BOTH , STDOUT_ONLY , STDERR_ONLY , STDOUT_PIPE , STDOUTERR_PIPE } |
How to merge standard output and standard error in the created process. More... | |
using | SuperClass = std::basic_ostream< char > |
Explicit reference to the public superclass. | |
enum | WaitMode { ASYNCHRONOUS , SYNCHRONOUS , EXIT_PROCESS } |
How to wait for the created process when close() is invoked. More... | |
Public Member Functions | |
ForkPipe () | |
Default constructor. | |
virtual | ~ForkPipe () override |
Destructor. | |
void | abortPipeReadWrite () |
Abort any currenly input/output operation in the pipe. | |
bool | close (Report &report) |
Close the pipe. | |
virtual bool | endOfStream () override |
Check if the end of stream was reached. | |
bool | getIgnoreAbort () const |
Get "ignore abort". | |
bool | isBroken () const |
Check if the pipe was broken. | |
bool | isOpen () const |
Check if the process is running and the pipe is open (when used). | |
bool | isSynchronous () const |
Check if synchronous mode is active (ie. | |
bool | open (const UString &command, WaitMode wait_mode, size_t buffer_size, Report &report, OutputMode out_mode, InputMode in_mode) |
Create the process, open the optional pipe. | |
virtual bool | readStreamChunks (void *addr, size_t max_size, size_t chunk_size, size_t &ret_size, Report &report) |
Read chunks of data from the stream. | |
virtual bool | readStreamComplete (void *addr, size_t max_size, size_t &ret_size, Report &report) |
Read complete data from the stream. | |
virtual bool | readStreamPartial (void *addr, size_t max_size, size_t &ret_size, Report &report) override |
Read partial data from the stream. | |
void | setIgnoreAbort (bool on) |
Set "ignore abort". | |
virtual bool | writeStream (const void *addr, size_t size, size_t &written_size, Report &report) override |
Write data to the stream. | |
Static Public Member Functions | |
static bool | Launch (const UString &command, Report &report, OutputMode out_mode=KEEP_BOTH, InputMode in_mode=STDIN_PARENT, WaitMode wait_mode=ASYNCHRONOUS) |
This static method launches a command, without pipe, optionally without waiting for the completion of the command process. | |
Static Public Attributes | |
static constexpr size_t | DEFAULT_STREAM_BUFFER_SIZE = 1024 |
Default stream buffer size in bytes. | |
Protected Member Functions | |
virtual bool | writeStreamBuffer (const void *addr, size_t size) override |
Write buffered data to underlying output devicen whatever it is. | |
Fork a process and create an optional pipe to its standard input.
This class can be used as any output stream when the output is a pipe.
How to wait for the created process when close() is invoked.
No pipe can be used with EXIT_PROCESS because there would be nobody on the other end of the pipe.
Enumerator | |
---|---|
ASYNCHRONOUS | Don't wait, close() will return immediately. |
SYNCHRONOUS | Wait for process completion during close(). |
EXIT_PROCESS | Exit parent process during open(). UNIX: call exec(), Windows: call exit() after process creation. |
How to standard input in the created process.
The pipe can be used either on input or output, but not both. So, STDIN_PIPE is also forbidden with output mode is either STDOUT_PIPE or STDOUTERR_PIPE.
Enumerator | |
---|---|
STDIN_PARENT | Keep same stdin as current (parent) process. |
STDIN_PIPE | Use the pipe as stdin. |
STDIN_NONE | No standard input (the null device in fact). |
How to merge standard output and standard error in the created process.
bool ts::ForkPipe::open | ( | const UString & | command, |
WaitMode | wait_mode, | ||
size_t | buffer_size, | ||
Report & | report, | ||
OutputMode | out_mode, | ||
InputMode | in_mode | ||
) |
Create the process, open the optional pipe.
[in] | command | The command to execute. |
[in] | wait_mode | How to wait for process termination in close(). |
[in] | buffer_size | The pipe buffer size in bytes. Used on Windows only. Zero means default. |
[in,out] | report | Where to report errors. |
[in] | out_mode | How to handle stdout and stderr. |
[in] | in_mode | How to handle stdin. Use the pipe by default. When set to KEEP_STDIN, no pipe is created. |
bool ts::ForkPipe::close | ( | Report & | report | ) |
Close the pipe.
Optionally wait for process termination if wait_mode was SYNCHRONOUS on open().
[in,out] | report | Where to report errors. |
|
inline |
Check if the process is running and the pipe is open (when used).
|
inline |
Check if the pipe was broken.
|
inline |
Check if synchronous mode is active (ie.
will wait for process termination).
|
inline |
Set "ignore abort".
[in] | on | If true and the process aborts, do not report error when writing data. when writing data. |
|
inline |
Get "ignore abort".
void ts::ForkPipe::abortPipeReadWrite | ( | ) |
Abort any currenly input/output operation in the pipe.
The pipe is left in a broken state and can be only closed.
|
static |
This static method launches a command, without pipe, optionally without waiting for the completion of the command process.
[in] | command | The command to execute. |
[in,out] | report | Where to report errors. |
[in] | out_mode | How to handle stdout and stderr. Must be KEEP_BOTH (default), STDOUT_ONLY or STDERR_ONLY. Output modes using pipes are forbidden. |
[in] | in_mode | How to handle stdin. Must be STDIN_PARENT (default) or STDIN_NONE. Input modes using pipes are forbidden. |
[in] | wait_mode | How to wait for the command process. Must be ASYNCHRONOUS (default) or SYNCHRONOUS. |
|
overridevirtual |
Check if the end of stream was reached.
Implements ts::AbstractReadStreamInterface.
|
overridevirtual |
Read partial data from the stream.
Wait and read at least one byte. Don't try to read exactly max_size bytes. If ret_size is less than max_bytes, it is possible to read more.
[out] | addr | Address of the buffer for the incoming data. |
[in] | max_size | Maximum size in bytes of the buffer. |
[out] | ret_size | Returned input size in bytes. If zero, end of file has been reached or an error occurred. |
[in,out] | report | Where to report errors. |
Implements ts::AbstractReadStreamInterface.
|
overridevirtual |
Write data to the stream.
All bytes are written to the stream, blocking or retrying when necessary.
[in] | addr | Address of the data to write. |
[in] | size | Size in bytes of the data to write. |
[out] | written_size | Actually written size in bytes. Can be less than size in case of error in the middle of the write. |
[in,out] | report | Where to report errors. |
Implements ts::AbstractWriteStreamInterface.
|
overrideprotectedvirtual |
Write buffered data to underlying output devicen whatever it is.
Must be implemented by subclasses.
[in] | addr | Buffered data address. |
[in] | size | Buffered data size in bytes. |
Implements ts::AbstractOutputStream.
|
virtualinherited |
Read complete data from the stream.
Wait and read exactly max_size bytes. If ret_size is less than max_bytes, it is not possible to read more. End of file has probably been reached.
[out] | addr | Address of the buffer for the incoming data. |
[in] | max_size | Maximum size in bytes of the buffer. |
[out] | ret_size | Returned input size in bytes. |
[in,out] | report | Where to report errors. |
|
virtualinherited |
Read chunks of data from the stream.
[out] | addr | Address of the buffer for the incoming data. |
[in] | max_size | Maximum size in bytes of the buffer. |
[in] | chunk_size | If not zero, make sure that the input size is always a multiple of chunk_size. If the initial read ends in the middle of a chunk, read again and again, up to the end of the current chunk or end of file. |
[out] | ret_size | Returned input size in bytes. |
[in,out] | report | Where to report errors. |