From 694ced4d258045101d9aa0fdd40e89f2452b8d48 Mon Sep 17 00:00:00 2001 From: "dmytro.bogovych" Date: Thu, 31 Jan 2019 10:11:16 +0300 Subject: [PATCH] - fixes & improvements to helper libraries --- src/engine/helper/HL_Process.cpp | 30 +++++++++++++++++++++++++++++- src/engine/helper/HL_Process.h | 4 ++++ src/engine/helper/HL_String.cpp | 5 +++-- src/engine/helper/HL_Sync.cpp | 1 + 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/engine/helper/HL_Process.cpp b/src/engine/helper/HL_Process.cpp index 3645b3a8..c573746e 100644 --- a/src/engine/helper/HL_Process.cpp +++ b/src/engine/helper/HL_Process.cpp @@ -218,6 +218,7 @@ std::string OsProcess::execCommand(const std::string& cmd) #include #include #include "helper/HL_String.h" +#include "helper/HL_Sync.h" std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdline, std::function line_callback, @@ -226,6 +227,7 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl { std::shared_ptr t = std::make_shared([cmdline, line_callback, finished_callback, &finish_flag]() { + ThreadHelper::setName("OsProcess::asyncExecCommand"); std::string cp = cmdline; FILE* pipe = popen(cp.c_str(), "r"); if (!pipe) @@ -258,7 +260,7 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl int r; do { - r = (int)read(fno, buffer, sizeof(buffer) - 1); + r = static_cast(read(fno, buffer, sizeof(buffer) - 1)); if (r > 0) { buffer[r] = 0; @@ -285,6 +287,10 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl } } + if (finish_flag) + { + // Send SIGINT to process + } if (pipe) pclose(pipe); @@ -296,4 +302,26 @@ std::shared_ptr OsProcess::asyncExecCommand(const std::string& cmdl return t; } +pid_t OsProcess::findPid(const std::string& cmdline) +{ + try + { + std::ostringstream oss; + oss << "pgrep -f " << "\"" << cmdline << "\""; + std::string output = execCommand(oss.str()); + return std::atoi(output.c_str()); + } + catch(...) + { + return 0; + } +} + +void OsProcess::killByPid(pid_t pid) +{ + if (pid <= 0) + return; + execCommand("kill -9 " + std::to_string(pid)); +} + #endif diff --git a/src/engine/helper/HL_Process.h b/src/engine/helper/HL_Process.h index fcd7307f..2f596ae4 100644 --- a/src/engine/helper/HL_Process.h +++ b/src/engine/helper/HL_Process.h @@ -14,6 +14,10 @@ public: std::function line_callback, std::function finished_callback, bool& finish_flag); +#if defined(TARGET_OSX) || defined(TARGET_LINUX) + static pid_t findPid(const std::string& cmdline); + static void killByPid(pid_t pid); +#endif }; diff --git a/src/engine/helper/HL_String.cpp b/src/engine/helper/HL_String.cpp index b627101f..172bcaca 100644 --- a/src/engine/helper/HL_String.cpp +++ b/src/engine/helper/HL_String.cpp @@ -335,10 +335,11 @@ std::string StringHelper::replace(const std::string& s, char f, char r) std::string StringHelper::replace(const std::string& s, const std::string& tmpl, const std::string& n) { std::string result(s); - std::string::size_type p; - while ( (p = result.find(tmpl)) != std::string::npos) + std::string::size_type p = 0; + while ( (p = result.find(tmpl, p)) != std::string::npos) { result.replace(p, tmpl.size(), n); + p += n.size(); } return result; diff --git a/src/engine/helper/HL_Sync.cpp b/src/engine/helper/HL_Sync.cpp index 08c2a62c..7861befd 100644 --- a/src/engine/helper/HL_Sync.cpp +++ b/src/engine/helper/HL_Sync.cpp @@ -268,6 +268,7 @@ size_t TimerQueue::cancelAll() { void TimerQueue::run() { + ThreadHelper::setName("TimerQueue"); while (!m_finish) { auto end = calcWaitTime();