libnick 2025.3.5
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1
22
23#ifndef PROCESS_H
24#define PROCESS_H
25
26#include <cstdio>
27#include <filesystem>
28#include <memory>
29#include <mutex>
30#include <string>
31#include <thread>
32#include <vector>
33#include "events/event.h"
35#ifdef _WIN32
36#include <windows.h>
37#else
38#include <sys/types.h>
39#endif
40
41namespace Nickvision::System
42{
46 class Process
47 {
48 public:
56 Process(const std::filesystem::path& path, const std::vector<std::string>& args = {}, const std::filesystem::path& workingDir = {});
66 const std::filesystem::path& getPath() const;
76 bool isRunning() const;
81 bool hasCompleted() const;
86 int getExitCode() const;
91 const std::string& getOutput() const;
96 unsigned long long getRAMUsage() const;
101 double getCPUUsage() const;
106 bool start();
111 bool kill();
124 bool send(const std::string& s);
130 bool sendCommand(std::string s);
131
132 private:
136 void watch();
137 mutable std::mutex m_mutex;
138 std::filesystem::path m_path;
139 std::vector<std::string> m_args;
140 std::filesystem::path m_workingDirectory;
142 bool m_running;
143 bool m_completed;
144 int m_exitCode;
145 std::string m_output;
146 std::thread m_watchThread;
147#ifdef _WIN32
148 HANDLE m_childOutRead;
149 HANDLE m_childOutWrite;
150 HANDLE m_childInRead;
151 HANDLE m_childInWrite;
152 PROCESS_INFORMATION m_pi;
153 HANDLE m_job;
154 mutable unsigned long long m_lastProcKernelTime;
155 mutable unsigned long long m_lastProcUserTime;
156 mutable unsigned long long m_lastSysKernelTime;
157 mutable unsigned long long m_lastSysUserTime;
158#else
159 int m_childOutPipes[2];
160 int m_childInPipes[2];
161 pid_t m_pid;
162 mutable unsigned long long m_lastUserTime;
163 mutable unsigned long long m_lastSystemTime;
164#endif
165 };
166}
167
168#endif //PROCESS_H
An event that can have handlers subscribe to it, which in turn will be called when the event is invok...
Definition event.h:49
bool isRunning() const
Gets whether or not the process is running.
double getCPUUsage() const
Gets the percent of the CPU being used by the process.
~Process()
Destructs a Process.
bool sendCommand(std::string s)
Send text to the process's console and adds the return characters.
bool start()
Starts the process.
unsigned long long getRAMUsage() const
Gets the amount of RAM being used by the process in bytes.
int getExitCode() const
Gets the exit code of the process.
Process(const std::filesystem::path &path, const std::vector< std::string > &args={}, const std::filesystem::path &workingDir={})
Constructs a Process.
bool kill()
Kills the process.
const std::string & getOutput() const
Gets the console output of the process.
bool send(const std::string &s)
Send text to the process's console.
Events::Event< ProcessExitedEventArgs > & exited()
Gets the event for when the process has exited.
int waitForExit()
Waits for the process to exit.
const std::filesystem::path & getPath() const
Gets the path of the process.
bool hasCompleted() const
Gets whether or not the process has completed.
Definition deploymentmode.h:27