libnick 2025.9.1
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#include "processstate.h"
36#ifdef _WIN32
37#include <windows.h>
38#else
39#include <sys/types.h>
40#endif
41
42namespace Nickvision::System
43{
47 class Process
48 {
49 public:
57 Process(const std::filesystem::path& path, const std::vector<std::string>& args = {}, const std::filesystem::path& workingDir = {});
62 ~Process() noexcept;
72 const std::filesystem::path& getPath() const noexcept;
77 ProcessState getState() const noexcept;
82 int getExitCode() const noexcept;
87 const std::string& getOutput() const noexcept;
92 double getCPUUsage() const noexcept;
97 unsigned long long getRAMUsage() const noexcept;
103 bool start() noexcept;
108 bool kill() noexcept;
113 bool resume() noexcept;
118 bool pause() noexcept;
125 int waitForExit() noexcept;
131 bool send(const std::string& s) noexcept;
137 bool sendCommand(std::string s) noexcept;
138
139 private:
143 void watch() noexcept;
144 mutable std::mutex m_mutex;
145 std::filesystem::path m_path;
146 std::vector<std::string> m_args;
147 std::filesystem::path m_workingDirectory;
148 Events::Event<ProcessExitedEventArgs> m_exited;
149 ProcessState m_state;
150 int m_exitCode;
151 std::string m_output;
152 std::thread m_watchThread;
153#ifdef _WIN32
154 HANDLE m_childOutRead;
155 HANDLE m_childOutWrite;
156 HANDLE m_childInRead;
157 HANDLE m_childInWrite;
158 PROCESS_INFORMATION m_pi;
159 HANDLE m_job;
160 mutable unsigned long long m_lastProcKernelTime;
161 mutable unsigned long long m_lastProcUserTime;
162 mutable unsigned long long m_lastSysKernelTime;
163 mutable unsigned long long m_lastSysUserTime;
164#else
165 int m_childOutPipes[2];
166 int m_childInPipes[2];
167 pid_t m_pid;
168 mutable unsigned long long m_lastUserTime;
169 mutable unsigned long long m_lastSystemTime;
170#endif
171 };
172}
173
174#endif //PROCESS_H
An event argument for when a process has exited.
Definition processexitedeventargs.h:35
double getCPUUsage() const noexcept
Gets the percent of the CPU being used by the process.
const std::string & getOutput() const noexcept
Gets the console output of the process.
bool start() noexcept
Starts the process.
const std::filesystem::path & getPath() const noexcept
Gets the path of the process.
~Process() noexcept
Destructs a Process.
bool sendCommand(std::string s) noexcept
Sends text to the process' console and adds the return characters.
bool kill() noexcept
Kills the process.
Process(const std::filesystem::path &path, const std::vector< std::string > &args={}, const std::filesystem::path &workingDir={})
Constructs a Process.
bool pause() noexcept
Pauses the process.
unsigned long long getRAMUsage() const noexcept
Gets the amount of RAM being used by the process in bytes.
bool resume() noexcept
Resumes the process.
Events::Event< ProcessExitedEventArgs > & exited() noexcept
Gets the event for when the process has exited.
ProcessState getState() const noexcept
Gets the state of the proicess.
int getExitCode() const noexcept
Gets the exit code of the process.
int waitForExit() noexcept
Waits for the process to exit.
bool send(const std::string &s) noexcept
Sends text to the process' console.
Definition event.h:33
Definition credentials.h:31
ProcessState
States of a process.
Definition processstate.h:10