diff --git a/general/src/function/daemon.cpp b/general/src/function/daemon.cpp index cb2b36e..a0c6bc4 100644 --- a/general/src/function/daemon.cpp +++ b/general/src/function/daemon.cpp @@ -29,93 +29,78 @@ vector RangeProcess(); vector StripList(const char *in, const char *d) { - vector ret; - - vector pos; - + map pos; vector pos_strip_continue; - int lastpos = -1; if ((nullptr == in) || (nullptr == d)) { - return ret; } - int len = strlen(in); - int sublen = strlen(d); - if (len <= 0) - return ret; - if (sublen <= 0) - return ret; - for (int i = 0; i < (len - strlen(d)); i++) + for (int i = 0; i < (len - sublen + 1); i++) { - bool found = true; - for (int j = 0; j < strlen(d); j++) { - if (in[i + j] != d[j]) { - found = false; } } - - if (found) + if (!found) { - + pos.insert(make_pair(i,true)); i += sublen - 1; - - pos.push_back(i); - continue; } } - if (pos.size() == 0) { - ret.push_back(string(in)); - return ret; } + bool started = true; + uint16_t startpos = 0; + uint16_t endpos = 0; - for (int i = 0; i < pos.size() - 1; i++) - { + for(int i = 0;i < len - 1;i++){ - if (pos[i + 1] != (pos[i] + sublen)) - { - - pos_strip_continue.push_back(pos[i] + 1); - - pos_strip_continue.push_back(pos[i + 1] - 1); + if(pos[i] == true){ + if(started){ + startpos = i; + started = false; + } + if((pos[i + sublen] != true)){ + endpos = i; + // std::cout< 0){ + std::cout<< strip[strip.size() - 1]< 0){ + }else{ + this->AddNewProcess(path); + } + } - this->m_path_process.push_back(path); } #ifdef linux @@ -480,78 +470,59 @@ void start_process(string path) int DaemonizeMonitor::AddNewProcess(string path) { - - this->m_path_process.push_back(path); - if (path == "") { - return -1; } - + this->m_path_process.push_back(path); #ifdef linux - if (!existed(path)) { - return -1; } - start_process(path); + usleep(100000); + auto strip = StripList(path.c_str(), "/"); + + auto process = RangeProcess(); + for (auto itr = process.begin(); itr != process.end(); itr++) + { + m_pids[itr->process_name.substr(0, itr->process_name.size() - 1)] = itr->pid; + } + if (m_pids[strip[strip.size() - 1]] > 0) + this->m_running_pid[strip[strip.size() - 1]] = m_pids[strip[strip.size() - 1]]; #endif + #ifdef _WIN32 - STARTUPINFO si = {sizeof(si)}; - PROCESS_INFORMATION pi; - si.dwFlags = STARTF_USESHOWWINDOW; //指定wShowWindow成员有效 - si.wShowWindow = TRUE; //此成员设为TRUE的话则显示新建进程的主窗 BOOL bRet = CreateProcess( - NULL, //不在此指定可执行文件的文件名 - (LPSTR)path.c_str(), //命令行参 - NULL, //默认进程安全 - NULL, //默认进程安全 - FALSE, //指定当前进程内句柄不可以被子进程继承 - CREATE_NEW_CONSOLE, //为新进程创建一个新的控制台窗口 - NULL, //使用本进程的环境变量 - NULL, //使用本进程的驱动器和目录 - &si, - &pi); if (bRet) - { - //不使用的句柄最好关 - CloseHandle(pi.hThread); - CloseHandle(pi.hProcess); - printf("new process id %d\n", pi.dwProcessId); - this->m_running_pid[path] = pi.dwProcessId; - return 0; } - return -1; - #endif } @@ -661,69 +632,44 @@ int DaemonizeMonitor::StartMonitor() #endif #ifdef linux - - while (true) - - { + while (true){ auto process = RangeProcess(); - usleep(50000); - for (auto itr = m_running_pid.begin(); itr != m_running_pid.end(); itr++) { - bool found = false; - for (auto itr2 = process.begin(); itr2 != process.end(); itr2++) { - if (itr->second == itr2->pid) { - - std::cout << itr->second << itr2->pid << std::endl; - + // std::cout << itr->second << itr2->pid << std::endl; found = true; - break; } - - if (!found) - { - - // 没找到该应用实例就重启应 - - auto it = m_path_process.begin(); - - while (it != m_path_process.end()) - - { - - if (*it == itr->first) - - { - - it = m_path_process.erase(it); - } - - else - - it++; - } - - for (auto it = m_path_process.begin(); it != m_path_process.end(); it++) - { - - std::cout << *it << std::endl; - } - - std::cout << "add new process" << std::endl; - - if (0 > this->AddNewProcess(itr->first)) - - std::cout << "error not found" << std::endl; - } } + + if (!found) + { + // 没找到该应用实例就重启应 + auto it = m_path_process.begin(); + while (it != m_path_process.end()) + { + if (*it == itr->first) + { + it = m_path_process.erase(it); + } + else + it++; + } + for (auto it = m_path_process.begin(); it != m_path_process.end(); it++) + { + std::cout << *it << std::endl; + } + std::cout << "add new process" << std::endl; + if (0 > this->AddNewProcess(itr->first)) + std::cout << "error not found" << std::endl; + } } }