Skip to content

Commit

Permalink
add subprocess module
Browse files Browse the repository at this point in the history
  • Loading branch information
PHZ76 committed May 24, 2020
1 parent 6e035d9 commit 4ce06c8
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/net/Process.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "Process.h"
#include <tlhelp32.h>

using namespace xop;

Process::Process()
: pid_(0)
{

}

Process::~Process()
{

}

bool Process::Start(std::string app_path, std::string cmd, std::string log_path)
{
if (pid_ > 0) {
return false;
}

#if defined(WIN32) || defined(_WIN32)
PROCESS_INFORMATION pi;
STARTUPINFO si = { sizeof(STARTUPINFO) };
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL,TRUE };
si.wShowWindow = SW_HIDE;

if (!log_path.empty()) {
file_ = CreateFile(log_path.c_str(), GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
&sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_ != INVALID_HANDLE_VALUE) {
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = file_;
si.hStdError = file_;
}
}

if (CreateProcess(app_path.c_str(), (LPTSTR)cmd.c_str(), NULL, NULL, true, 0, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
pid_ = static_cast<int64_t>(pi.dwProcessId);
}
else {
return false;
}

#elif defined(__linux) || defined(__linux__)



#endif
return false;
}

void Process::Stop()
{
if (pid_ <= 0) {
return ;
}
#if defined(WIN32) || defined(_WIN32)
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, (DWORD)pid_);
if (hProcess != NULL) {
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}

if (file_ != INVALID_HANDLE_VALUE) {
CloseHandle(file_);
file_ = INVALID_HANDLE_VALUE;
}
#elif defined(__linux) || defined(__linux__)

#endif
}

bool Process::IsAlive()
{
if (pid_ <= 0) {
return false;
}
#if defined(WIN32) || defined(_WIN32)
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid_);
//hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, (DWORD)pid_);
if (hProcess != NULL) {
CloseHandle(hProcess);
return true;
}
#elif defined(__linux) || defined(__linux__)

#endif
return false;
}
33 changes: 33 additions & 0 deletions src/net/Process.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef XOP_PROCESS_H
#define XOP_PROCESS_H

#include <string>
#include <cstdio>
#if defined(WIN32) || defined(_WIN32)
#include <Windows.h>
#endif

namespace xop {

class Process
{
public:
Process();
virtual ~Process();

bool Start(std::string app_path, std::string cmd, std::string log_path ="");
void Stop();
bool IsAlive();

private:

std::string command_;
int64_t pid_;
#if defined(WIN32) || defined(_WIN32)
HANDLE file_ = INVALID_HANDLE_VALUE;
#endif
};

}

#endif
6 changes: 6 additions & 0 deletions src/net/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@
#define SOCKET_ERROR (-1)

#elif defined(WIN32) || defined(_WIN32)
#ifndef FD_SETSIZE
#define FD_SETSIZE 1024
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include <WinSock2.h>
#include <windows.h>
#include <ws2tcpip.h>
Expand Down

0 comments on commit 4ce06c8

Please sign in to comment.