xx 2 лет назад
Родитель
Сommit
ae075c552e
4 измененных файлов с 48 добавлено и 3 удалено
  1. 1 0
      include/net/udp_node.h
  2. 2 1
      include/util/process.h
  3. 9 1
      src/net/udp_node.cpp
  4. 36 1
      src/util/process.cpp

+ 1 - 0
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<void(ylib::network::udp::node* node, const ylib::AddressPort& remote_ap, const char* data, uint32 len)> callback) { m_callback_recv = callback; }
+				ushort listen_port();
 				const ylib::AddressPort& local() { return m_local_ap; }
 				friend class node_lst;
 			private:

+ 2 - 1
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<std::string>& 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<std::string>& args = {}, bool wait_close = false, bool show_window = true, int* return_code = nullptr, size_t* pid = nullptr);
 		// 销毁进程 
 		bool destory(uint32 process_id);
 		// 系统进程列表
 		std::list<ylib::process::proc_info> list();
 		// 是否存在
 		size_t exist(const std::string& filepath);
+		bool exist(size_t pid);
 #ifdef _WIN32
 		// 检测多开
 		bool already_running(const std::string& name);

+ 9 - 1
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

+ 36 - 1
src/util/process.cpp

@@ -44,7 +44,7 @@
 #include <sstream>
 #include <sys/types.h>
 #include <signal.h>
-bool ylib::process::create(const std::string& filepath, const std::string& working_directory, const std::vector<std::string>& 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<std::string>& 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)