From ae075c552e91bd3ccd9eda6db31f2eb2cf2f3faa Mon Sep 17 00:00:00 2001 From: xx Date: Wed, 12 Jun 2024 02:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BE=E5=80=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/net/udp_node.h | 1 + include/util/process.h | 3 ++- src/net/udp_node.cpp | 10 +++++++++- src/util/process.cpp | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/include/net/udp_node.h b/include/net/udp_node.h index cf95afb..2d178bd 100644 --- a/include/net/udp_node.h +++ b/include/net/udp_node.h @@ -21,6 +21,7 @@ namespace ylib bool close(); bool send(const std::string& remote_ipaddress, ushort remote_port, const char* pData, uint32 len); void on_recv(std::function callback) { m_callback_recv = callback; } + ushort listen_port(); const ylib::AddressPort& local() { return m_local_ap; } friend class node_lst; private: diff --git a/include/util/process.h b/include/util/process.h index f86b7bd..bb8d83e 100644 --- a/include/util/process.h +++ b/include/util/process.h @@ -25,13 +25,14 @@ namespace ylib uint32 pid = 0; }; // 创建进程 - bool create(const std::string& filepath, const std::string& working_directory = "", const std::vector& args = {}, bool wait_close = false, bool show_window = true, size_t* pid = nullptr); + bool create(const std::string& filepath, const std::string& working_directory = "", const std::vector& args = {}, bool wait_close = false, bool show_window = true, int* return_code = nullptr, size_t* pid = nullptr); // 销毁进程 bool destory(uint32 process_id); // 系统进程列表 std::list list(); // 是否存在 size_t exist(const std::string& filepath); + bool exist(size_t pid); #ifdef _WIN32 // 检测多开 bool already_running(const std::string& name); diff --git a/src/net/udp_node.cpp b/src/net/udp_node.cpp index 2ba2fb5..829a1a7 100644 --- a/src/net/udp_node.cpp +++ b/src/net/udp_node.cpp @@ -49,7 +49,7 @@ bool ylib::network::udp::node::start(const ylib::AddressPort& bind_ap) m_local_ap = bind_ap; //m_local_ap.port = bind_ap.port; - if (m_node->Start(bind_ap.address.c_str(), bind_ap.port) == false) + if (m_node->Start(bind_ap.address.empty()==true?nullptr:bind_ap.address.c_str(), bind_ap.port) == false) { this->m_lastErrorDesc = "node start failed, error code:" + std::string(m_node->GetLastErrorDesc()); close(); @@ -71,4 +71,12 @@ bool ylib::network::udp::node::send(const std::string& remote_ipaddress, ushort { return m_node->Send(remote_ipaddress.c_str(),remote_port,(const BYTE*)pData,len); } +ushort ylib::network::udp::node::listen_port() +{ + TCHAR address[256]; + int address_len = 256; + ushort port = 0; + m_node->GetLocalAddress(address, address_len, port); + return port; +} #endif \ No newline at end of file diff --git a/src/util/process.cpp b/src/util/process.cpp index 95faafb..8467fd9 100644 --- a/src/util/process.cpp +++ b/src/util/process.cpp @@ -44,7 +44,7 @@ #include #include #include -bool ylib::process::create(const std::string& filepath, const std::string& working_directory, const std::vector& args, bool wait_close, bool show_window, size_t* pid) +bool ylib::process::create(const std::string& filepath, const std::string& working_directory, const std::vector& args, bool wait_close, bool show_window, int* return_code, size_t* pid) { #ifdef _WIN32 STARTUPINFOA si; @@ -86,6 +86,18 @@ bool ylib::process::create(const std::string& filepath, const std::string& worki // 如果需要,等待进程关闭 if (wait_close) { WaitForSingleObject(pi.hProcess, INFINITE); + + // 获取子进程的返回值 + if (return_code != nullptr) + { + DWORD exitCode; + if (GetExitCodeProcess(pi.hProcess, &exitCode)) { + *return_code = exitCode; + } + } + + + } if(pid!=nullptr) *pid = pi.dwProcessId; @@ -249,6 +261,29 @@ size_t ylib::process::exist(const std::string& filepath) } return 0; } +bool ylib::process::exist(size_t pid) +{ +#ifdef _WIN32 + HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); + if (processHandle == NULL) { + return false; + } + DWORD exitCode; + if (!GetExitCodeProcess(processHandle, &exitCode)) { + CloseHandle(processHandle); + return false; + } + CloseHandle(processHandle); + return (exitCode == STILL_ACTIVE); +#else + if (kill(pid, 0) == 0) { + return true; + } + else { + return (errno == EPERM); + } +#endif +} #ifdef _WIN32 // 检测多开 bool ylib::process::already_running(const std::string& name)