1
This commit is contained in:
+2
-1
@@ -190,6 +190,8 @@ message("------------------simde 向量化计算-----------")
|
||||
add_subdirectory ("third/simde")
|
||||
message("------------------magic_enum 枚举静态反射-----------")
|
||||
add_subdirectory ("third/magic_enum")
|
||||
message("------------------cpp-subprocess 进程库-----------")
|
||||
add_subdirectory ("third/cpp-subprocess")
|
||||
|
||||
message("------------------其余库以其他形式链接----------")
|
||||
message("------------------nlohmann-json json库---------")
|
||||
@@ -204,7 +206,6 @@ message("------------------sqlite 数据库-----------")
|
||||
message("------------------tinyobjloader obj格式库-----------")
|
||||
message("------------------tinygltf gltf格式库-----------")
|
||||
message("------------------mp3 音频格式库-----------")
|
||||
message("------------------subprocess 进程库-----------")
|
||||
|
||||
message("------------------解析dl-----------------------")
|
||||
add_subdirectory ("dl")
|
||||
|
||||
+2
-2
@@ -195,8 +195,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE wcwidth)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE simde)
|
||||
# magic enmu
|
||||
include_directories(${CMAKE_SOURCE_DIR}/third/magic_enum/include)
|
||||
# subprocess
|
||||
include_directories(${CMAKE_SOURCE_DIR}/third/subprocess)
|
||||
# cpp-subprocess
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE cpp-subprocess::subprocess)
|
||||
|
||||
# curl
|
||||
if (NOT DL_DISABLE_CURL)
|
||||
|
||||
+50
-76
@@ -1,115 +1,89 @@
|
||||
#include "dl_process.h"
|
||||
|
||||
#include <subprocess.h>
|
||||
#include <cpp-subprocess/subprocess.hpp>
|
||||
using namespace subprocess;
|
||||
|
||||
#include "memory/dl_code_cvt.h"
|
||||
#include "io/dl_log.h"
|
||||
#include "base/dl_string.h"
|
||||
#include "dl_time.h"
|
||||
|
||||
namespace dl
|
||||
{
|
||||
bool Process::Exec(const std::vector<std::string>& cmd, Result* result)
|
||||
bool Process::Exec(const std::vector<std::string>& cmd, Result& result)
|
||||
{
|
||||
if (cmd.size() >= 64)
|
||||
try
|
||||
{
|
||||
log_err("命令行参数过多,最多63个!");
|
||||
return false;
|
||||
Popen p = Popen(cmd, output{ PIPE });
|
||||
// 等待结束,并读取管道
|
||||
result._str = p.communicate().first.buf.data();
|
||||
result._exitCode = p.retcode();
|
||||
return true;
|
||||
}
|
||||
const char* p_cmd[64] = {};
|
||||
size_t i = 0;
|
||||
for (; i < cmd.size(); ++i)
|
||||
catch (CalledProcessError& e)
|
||||
{
|
||||
p_cmd[i] = cmd[i].c_str();
|
||||
log_err("创建进程失败{},{}", dl::to_string(cmd), dl::to_string(e.what()));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
log_err0("未知异常!");
|
||||
}
|
||||
|
||||
|
||||
subprocess_s p;
|
||||
int rc = subprocess_create(p_cmd, 0, &p);
|
||||
if (0 != rc)
|
||||
{
|
||||
log_err("创建进程失败[{}]:{}", rc, dl::to_string(cmd));
|
||||
return false;
|
||||
}
|
||||
|
||||
int process_return;
|
||||
rc = subprocess_join(&p, &process_return);
|
||||
if (0 != rc)
|
||||
{
|
||||
log_err("执行进程失败[{}]:{}", rc, dl::to_string(cmd));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
result->_exitCode = process_return;
|
||||
|
||||
FILE* p_stdout = subprocess_stdout(&p);
|
||||
|
||||
while (true)
|
||||
{
|
||||
char buf[1024];
|
||||
char* c = fgets(buf, 1024, p_stdout);
|
||||
if (c == nullptr)
|
||||
break;
|
||||
result->_log += buf;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Process::ExecMulti(const std::vector<std::string>& cmd, Instance* instance)
|
||||
bool Process::ExecMulti(const std::vector<std::string>& cmd, Instance& instance)
|
||||
{
|
||||
if (cmd.size() >= 64)
|
||||
try
|
||||
{
|
||||
log_err("命令行参数过多,最多63个!");
|
||||
return false;
|
||||
instance._p = new Popen(cmd, output{ PIPE }, input{ PIPE });
|
||||
}
|
||||
const char* p_cmd[64] = {};
|
||||
size_t i = 0;
|
||||
for (; i < cmd.size(); ++i)
|
||||
catch (CalledProcessError& e)
|
||||
{
|
||||
p_cmd[i] = cmd[i].c_str();
|
||||
log_err("创建进程失败{},{}", dl::to_string(cmd), dl::to_string(e.what()));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
log_err0("未知异常!");
|
||||
}
|
||||
|
||||
instance->_p = new subprocess_s;
|
||||
int rc = subprocess_create(p_cmd, 0, (subprocess_s*)instance->_p);
|
||||
if (0 != rc)
|
||||
{
|
||||
log_err("创建进程失败[{}]:{}", rc, dl::to_string(cmd));
|
||||
return false;
|
||||
}
|
||||
|
||||
instance->_thread = g_thread->Create();
|
||||
instance->_thread->Start(_thread_exec, instance);
|
||||
instance._thread = g_thread->Create();
|
||||
instance._thread->Start(_thread_exec, &instance);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Process::Write(Instance* instance, std::string_view str)
|
||||
void Process::Send(Instance& instance, std::string_view str)
|
||||
{
|
||||
FILE* p_stdin = subprocess_stdin((subprocess_s*)instance->_p);
|
||||
fputs(str.data(), p_stdin);
|
||||
fflush(p_stdin);
|
||||
instance._p->send(std::string{ str } + '\n');
|
||||
fflush(instance._p->input());
|
||||
}
|
||||
|
||||
std::string Process::Read(Instance* instance)
|
||||
void Process::_thread_exec(void* p)
|
||||
{
|
||||
FILE* p_stdout = subprocess_stdout((subprocess_s*)instance->_p);
|
||||
std::string ret;
|
||||
Instance& instance = *(Instance*)p;
|
||||
|
||||
while (true)
|
||||
{
|
||||
char buf[1024];
|
||||
char* c = fgets(buf, 1024, p_stdout);
|
||||
if (c == nullptr)
|
||||
break;
|
||||
ret += buf;
|
||||
instance._mutexRet.lock();
|
||||
instance._result._str += Process::_read(instance);
|
||||
instance._mutexRet.unlock();
|
||||
g_time->Sleep();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Process::Terminate(Instance* data)
|
||||
std::string Process::_read(Instance& instance)
|
||||
{
|
||||
//TerminateProcess(data->_p, 0);//终止进程
|
||||
data->_thread->Terminate();
|
||||
char buf[1024];
|
||||
if (fgets(buf, sizeof(buf), instance._p->output()))
|
||||
return buf;
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
||||
void Process::Terminate(Instance& instance)
|
||||
{
|
||||
instance._p->kill();
|
||||
instance._thread->Terminate();
|
||||
}
|
||||
}
|
||||
|
||||
+24
-21
@@ -6,7 +6,7 @@
|
||||
* @author lveyou
|
||||
* @date 24-04-12
|
||||
*
|
||||
* @note 基于subprocess.h库
|
||||
* @note 基于cpp-subprocess库
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
@@ -14,6 +14,11 @@
|
||||
|
||||
#include "system/dl_thread.h"
|
||||
|
||||
namespace subprocess
|
||||
{
|
||||
class Popen;
|
||||
}
|
||||
|
||||
namespace dl
|
||||
{
|
||||
//! 系统进程
|
||||
@@ -31,44 +36,42 @@ public:
|
||||
struct Result
|
||||
{
|
||||
int _exitCode;
|
||||
std::string _log;
|
||||
std::string _str;
|
||||
};
|
||||
// 异步调用
|
||||
struct Instance
|
||||
class Instance
|
||||
{
|
||||
friend class Process;
|
||||
public:
|
||||
std::string GetOutput()
|
||||
{
|
||||
std::lock_guard guard{ _mutexRet };
|
||||
return _result._str;
|
||||
}
|
||||
private:
|
||||
Thread* _thread;
|
||||
void* _p;
|
||||
subprocess::Popen* _p;
|
||||
std::vector<std::string> _cmd;
|
||||
Result _result;
|
||||
bool _bEnd = false;
|
||||
std::mutex _mutexRet;
|
||||
Result _result;
|
||||
};
|
||||
|
||||
// 阻塞调用
|
||||
static bool Exec(const std::vector<std::string>& cmd, Result* result = nullptr);
|
||||
static bool Exec(const std::vector<std::string>& cmd, Result& result);
|
||||
|
||||
// 异步调用
|
||||
static bool ExecMulti(const std::vector<std::string>& cmd, Instance* instance);
|
||||
static bool ExecMulti(const std::vector<std::string>& cmd, Instance& instance);
|
||||
|
||||
|
||||
// 中止进程
|
||||
static void Terminate(Instance* data);
|
||||
static void Terminate(Instance& instance);
|
||||
|
||||
static void Write(Instance* instance, std::string_view str);
|
||||
static void Send(Instance& instance, std::string_view str);
|
||||
private:
|
||||
static void _thread_exec(void* p)
|
||||
{
|
||||
Instance* instance = (Instance*)p;
|
||||
|
||||
while (true)
|
||||
{
|
||||
instance->_mutexRet.lock();
|
||||
instance->_result._log += Process::Read(instance);
|
||||
instance->_mutexRet.unlock();
|
||||
}
|
||||
}
|
||||
static void _thread_exec(void* p);
|
||||
|
||||
|
||||
static std::string Read(Instance* instance);
|
||||
static std::string _read(Instance& instance);
|
||||
};
|
||||
}
|
||||
+1
-1
Submodule project/dl_chess updated: 836a32a642...6890f72410
Reference in New Issue
Block a user