#include <iostream>
#include <string>
namespace {
{
std::string_view path;
if (argc > 0) {
path = argv[0];
#ifdef FZ_WINDOWS
auto delim = path.find_last_of("/\\");
#else
auto delim = path.find_last_of("/");
#endif
if (delim == std::string::npos) {
path = std::string_view();
}
else {
path = path.substr(0, delim + 1);
}
}
}
#ifdef FZ_WINDOWS
auto suffix =
fzT(
".exe");
#else
#endif
}
{
public:
:
fz::event_handler(loop)
, process_(pool, *this)
{}
~runner()
{
process_.kill();
}
{
fz::dispatch<fz::process_event>(ev, this, &runner::on_process_event);
}
{
on_read();
}
}
void on_read()
{
std::string input;
while (!done_) {
char buf[100];
if (!r) {
return;
}
std::cerr << "Could not read from process" << std::endl;
exit(1);
}
std::cerr << "Unexpected EOF from process" << std::endl;
exit(1);
}
input += std::string(buf, r.
value_);
auto delim = input.find_first_of("\r\n");
while (delim != std::string::npos) {
std::string line = input.substr(0, delim);
input = input.substr(delim + 1);
delim = input.find_first_of("\r\n");
if (!line.empty()) {
std::cout << "Received line from process: " << line << std::endl;
if (line == "woof") {
done_ = true;
if (!process_.write("0\n")) {
std::cerr << "Sending data to the process failed. Looks like it could not be started or has quit early." << std::endl;
exit(1);
}
std::cout << "Told process to quit." << std::endl;
}
}
}
}
while (true) {
char buf[100];
if (!r) {
return;
}
std::cerr << "Could not read from process" << std::endl;
exit(1);
}
std::cerr << "Received the expected EOF from process" << std::endl;
break;
}
}
event_loop_.stop();
}
bool done_{};
};
int main(int argc, char *argv[])
{
runner h(pool, loop);
if (!h.process_.spawn(get_program_dir(argc, argv) +
fzT(
"timer_fizzbuzz") + suffix)) {
std::cerr << "Could not spawn process" << std::endl;
return 1;
}
std::cout << "Spawned process" << std::endl;
if (!r) {
std::cerr << "Sending data to the process failed. Looks like it could not be started or has quit early." << std::endl;
return 1;
}
}
std::cout << "Waiting on process to print woof..." << std::endl;
loop.run();
}
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition: buffer.hpp:27
void consume(size_t consumed)
Removes consumed bytes from the beginning of the buffer.
unsigned char const * get() const
Undefined if buffer is empty.
Definition: buffer.hpp:45
void append(unsigned char const *data, size_t len)
Appends the passed data to the buffer.
Common base class for all events.
Definition: event.hpp:23
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
void remove_handler()
Deactivates handler, removes all pending events and stops all timers for this handler.
virtual void operator()(event_base const &)=0
Called by the event loop in the worker thread with the event to process.
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:34
The process class manages an asynchronous process with redirected IO.
Definition: process.hpp:61
Holds the result of read/write operations.
Definition: fsresult.hpp:74
size_t value_
Undefined if error_ is not none.
Definition: fsresult.hpp:116
@ wouldblock
The operation would have blocked, but the file descriptor is marked non-blocking.
Definition: fsresult.hpp:92
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:64
Declares the event_handler class.
The namespace used by libfilezilla.
Definition: apply.hpp:17
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:34
process_event_flag
The type of a process event.
Definition: process.hpp:26
@ read
Data has become available.
native_string to_native(std::string_view const &in)
Converts std::string to native_string.
Header for the #process class.
#define fzT(x)
Macro for a string literal in system-native character type. Note: Macro definition changes depending...
Definition: string.hpp:257
Declares thread_pool and async_task.