曾倩功能

This commit is contained in:
xx
2024-06-12 02:19:39 +08:00
parent 4db10b83d9
commit ae075c552e
4 changed files with 48 additions and 3 deletions

View File

@@ -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:

View File

@@ -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);

View File

@@ -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

View File

@@ -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)