2#include "pipe_thread.h"
3#include "cmdline_tools.h"
25 std::pair<char*, size_t> block = { 0,0 };
42 all_sent = all_data_sent;
64 all_sent = all_data_sent;
68 bool out_of_memory =
false;
71 block =
new char[count];
73 catch (
const std::bad_alloc&) {
78 std::copy(data, data + count, block);
80 blocks.push_back({ block, count });
104 all_data_sent =
true;
119 auto mode =
is_binary ? (std::ios_base::out | std::ios_base::binary) : std::ios_base::out;
151 fwrite(data, 1, count,
fp);
155 result = cgv::os::close_system_input(
fp);
163 unsigned _ms_to_wait)
185 if (bytes_read > 0) {
261 auto mode =
is_binary ? (std::ios_base::in | std::ios_base::binary) : std::ios_base::in;
262 pipe_ptr =
new nes::pipe_istream(pipe_name, mode);
263 if (pipe_ptr->fail()) {
277 return pipe_ptr->gcount();
287named_pipe_input_thread::named_pipe_input_thread(
const std::string& _pipe_name,
bool is_binary,
288 size_t _package_size,
size_t _packeges_per_block,
unsigned _ms_to_wait)
291 pipe_name = _pipe_name;
293std::string named_pipe_input_thread::get_pipe_path()
const
295 return nes::pipe_root + pipe_name;
298 fp = cgv::os::open_system_output(cmd,
is_binary);
313 result = cgv::os::close_system_input(fp);
315pipe_input_thread::pipe_input_thread(
const std::string& _cmd,
bool is_binary,
size_t _package_size,
size_t _packages_per_block,
unsigned _ms_to_wait)
320int pipe_input_thread::get_result()
const {
void write_block_to_pipe(const char *data, size_t count)
write block to named pipe
std::string get_pipe_path() const
return path of pipe that can be used in command line arguments to child/client processes
bool connect_to_child_process()
creates pipe and waits for connection
std::string pipe_name
based name of the
named_pipe_output_thread(const std::string &_pipe_name, bool is_binary=true, unsigned _ms_to_wait=20)
construct pipe output thread from pipe name, whether to use binary mode and wait time in ms used when...
void close()
closes named pipe
nes::basic_pipe_ostream< char, std::char_traits< char > > * pipe_ptr
pointer to the named pipe output stream
void write_block_to_pipe(const char *data, size_t count)
write block to stdin pipe
int result
result value of child process
pipe_output_thread(const std::string &_cmd, bool is_binary=true, unsigned _ms_to_wait=20)
construct pipe output thread from system command, whether to use binary mode and wait time in ms used...
void close()
closes stdin pipe and stores result value
FILE * fp
file handle of stdin of child process
int get_result() const
return the result value returned from child process which is available only after termination of thre...
std::string cmd
system command to be executed
bool connect_to_child_process()
starts child process and connects to its stdin
base class for system command input pipe or named pipe threads including a queue of data blocks and a...
bool connected
flag that tells whether the pipe has been connected to from the other side
size_t get_nr_blocks() const
returns the number of blocks in the queue of not yet written data
virtual void close()=0
to be implemented in derived classes
unsigned ms_to_wait
time in miliseconds to wait while queue is empty
bool send_block(const char *data, size_t count)
if done() had not been called, insert a data block into the queue; can fail if done() or out of memor...
virtual void write_block_to_pipe(const char *data, size_t count)=0
to be implemented in derived classes
std::deque< std::pair< char *, size_t > > blocks
deque used to queue the data blocks that should be written to the pipe by the thread
void run()
connect to child process and continuously write queue content to pipe; if empty wait in intervals of ...
bool is_binary
whether binary mode should be used
void done()
call this to announce the all data has been sent
virtual bool connect_to_child_process()=0
to be implemented in derived classes
queued_output_thread(bool is_binary=true, unsigned _ms_to_wait=20)
construct queued output thread from flag, whether to use binary mode and wait time in ms used when qu...
cgv::os::mutex m
mutex used to protect access to blocks
size_t get_nr_bytes() const
returns the number of bytes in the queue of not yet written data, what is more time consuming than ge...
bool has_connection() const
returns true as soon as child process has connected to pipe
static void wait(unsigned millisec)
wait the given number of milliseconds
bool have_stop_request()
check if there is a stop request
void unlock()
unlock the mutex
void lock()
lock the mutex (if the mutex is already locked, the caller is blocked until the mutex becomes availab...