cgv
Loading...
Searching...
No Matches
pipe_thread.h
1#pragma once
2
3#include <deque>
4#include <mutex>
5#include "thread.h"
6#include "lib_begin.h"
7
8namespace nes {
9 // forward declaration of pipe output stream to avoid inclusion of pipe header here
10 template<typename CharT, typename Traits> class basic_pipe_ostream;
11 template <typename CharT, typename Traits> class basic_pipe_istream;
12}
13
14namespace cgv {
15 namespace os {
20 {
22 bool all_data_sent = false;
23 protected:
27 bool connected = false;
31 std::deque<std::pair<char*, size_t> > blocks;
33 unsigned ms_to_wait;
35 virtual bool connect_to_child_process() = 0;
37 virtual void write_block_to_pipe(const char* data, size_t count) = 0;
39 virtual void close() = 0;
40 public:
42 queued_output_thread(bool is_binary = true, unsigned _ms_to_wait = 20);
44 void run();
46 bool has_connection() const;
48 bool send_block(const char* data, size_t count);
50 size_t get_nr_blocks() const;
52 size_t get_nr_bytes() const;
54 void done();
55 };
60 {
61 protected:
63 std::string pipe_name;
67 bool connect_to_child_process();
69 void write_block_to_pipe(const char* data, size_t count);
71 void close();
72 public:
74 named_pipe_output_thread(const std::string& _pipe_name, bool is_binary = true, unsigned _ms_to_wait = 20);
76 std::string get_pipe_path() const;
77 };
78
83 {
84 protected:
86 std::string cmd;
88 FILE* fp = 0;
90 int result = -1;
92 bool connect_to_child_process();
94 void write_block_to_pipe(const char* data, size_t count);
96 void close();
97 public:
99 pipe_output_thread(const std::string& _cmd, bool is_binary = true, unsigned _ms_to_wait = 20);
101 int get_result() const;
102 };
103
104
110 {
111 protected:
113 bool all_data_received = false;
123 mutable std::mutex mutex_packages;
125 mutable std::mutex mutex_data_blocks;
127 char* packages;
129 std::deque<std::vector<char>> data_blocks;
130
132 unsigned ms_to_wait;
134 virtual bool connect_to_source() = 0;
136 virtual size_t read_package_from_pipe(char* buffer, size_t package_size) = 0;
138 virtual void move_packages_to_data_blocks();
140 virtual void close() = 0;
141
142 public:
144 queued_input_thread(bool _is_binary, size_t _package_size,
145 size_t _packeges_per_block, unsigned _ms_to_wait);
146
149 void run();
152 bool pop_data_block(char* buffer);
154 size_t get_nr_blocks() const;
156 void done();
157 };
158
160 {
161 protected:
162 std::string pipe_name;
164 bool connect_to_source() override;
165 size_t read_package_from_pipe(char* buffer,size_t package_size) override;
166 void close() override;
167
168 public:
169 named_pipe_input_thread(const std::string& _pipe_name, bool is_binary, size_t _package_size,
170 size_t _packeges_per_block, unsigned _ms_to_wait);
171 std::string get_pipe_path() const;
172 };
173
175 {
176 protected:
177 std::string cmd;
178 FILE* fp = 0;
179
180 int result = -1;
181
182 bool connect_to_source() override;
183 size_t read_package_from_pipe(char* buffer, size_t package_size) override;
184 void close() override;
185
186 public:
187 pipe_input_thread(const std::string& _cmd, bool is_binary, size_t _package_size, size_t _packages_per_block,
188 unsigned _ms_to_wait);
189 int get_result() const;
190 };
191 }
192}
193
194#include <cgv/config/lib_end.h>
queued thread class that manages a named pipe
Definition pipe_thread.h:60
std::string pipe_name
based name of the
Definition pipe_thread.h:63
queued thread class that manages a child process connecting to its input pipe
Definition pipe_thread.h:83
std::string cmd
system command to be executed
Definition pipe_thread.h:86
Base class for system command output pipe or named pipe threads including a queue of data blocks,...
size_t package_size
size of an indiviual package
std::deque< std::vector< char > > data_blocks
deque to store data blocks that are formed by packages
size_t packages_per_block
amount of packeges needed to form one data block
virtual void close()=0
to be implemented in derived classes
virtual size_t read_package_from_pipe(char *buffer, size_t package_size)=0
to be implemented in derived classes
virtual bool connect_to_source()=0
to be implemented in derived classes
char * packages
allocated memory for smaller data packages
size_t package_index
index of the next package to be read
std::mutex mutex_packages
mutex used to protect access to packages
bool is_binary
whether binary mode should be used
std::mutex mutex_data_blocks
mutex used to protect access to data_blocks
unsigned ms_to_wait
time in milliseconds to wait while pipe is empty
base class for system command input pipe or named pipe threads including a queue of data blocks and a...
Definition pipe_thread.h:20
virtual void close()=0
to be implemented in derived classes
unsigned ms_to_wait
time in miliseconds to wait while queue is empty
Definition pipe_thread.h:33
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
Definition pipe_thread.h:31
bool is_binary
whether binary mode should be used
Definition pipe_thread.h:25
virtual bool connect_to_child_process()=0
to be implemented in derived classes
cgv::os::mutex m
mutex used to protect access to blocks
Definition pipe_thread.h:29
Thread class implementation that uses pthreads internally.
Definition thread.h:39
the cgv namespace
Definition print.h:11
A simple mutex (mutual exclusion) for solving thread synchronisation problems.
Definition mutex.h:14