!1541 拆分hdc kill和hdc start的处理逻辑

Merge pull request !1541 from wenlong_12/20240909
This commit is contained in:
openharmony_ci 2024-09-18 03:19:15 +00:00 committed by Gitee
commit e19d350bb3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 68 additions and 41 deletions

View File

@ -180,6 +180,10 @@ const string CMDSTR_FLASHD_FLASH = "flash";
const string CMDSTR_FLASHD_ERASE = "erase";
const string CMDSTR_FLASHD_FORMAT = "format";
// ################################ Error Message ###################################
const string TERMINAL_HDC_PROCESS_FAILED = "[E002101]:Terminal hdc process failed, "\
"please terminal the hdc process in the task manager first.";
// ################################ Macro define ###################################
#ifdef IS_RELEASE_VERSION
#define WRITE_LOG(level, fmt, ...) Base::PrintLogEx(__FUNCTION__, __LINE__, level, fmt, ##__VA_ARGS__)

View File

@ -70,47 +70,69 @@ uint32_t HdcClient::GetLastPID()
return pid;
}
bool HdcClient::StartKillServer(const char *cmd, bool startOrKill)
bool HdcClient::StartServer(const string &cmd)
{
bool isNowRunning = Base::ProgramMutex(SERVER_NAME.c_str(), true) != 0;
const int signNum = 9;
const string errMsg= "[E002101]Terminal hdc process failed, "
"please terminal the hdc process in the task manager first.";
uint32_t pid = GetLastPID();
if (startOrKill) {
if (isNowRunning) {
// already running
if (!strstr(cmd, " -r")) {
return true;
}
if (pid == 0) {
Base::PrintMessage(errMsg.c_str());
return false;
} else {
int rc = uv_kill(pid, signNum);
WRITE_LOG(LOG_DEBUG, "uv_kill rc:%d", rc);
}
}
int serverStatus = Base::ProgramMutex(SERVER_NAME.c_str(), true);
if (serverStatus < 0) {
WRITE_LOG(LOG_DEBUG, "get server status failed, serverStatus:%d", serverStatus);
return false;
}
// server is not running
if (serverStatus == 0) {
HdcServer::PullupServer(channelHostPort.c_str());
return true;
}
// server is running
if (cmd.find(" -r") == std::string::npos) {
return true;
}
// restart server
uint32_t pid = GetLastPID();
if (pid == 0) {
Base::PrintMessage(TERMINAL_HDC_PROCESS_FAILED.c_str());
return false;
}
int rc = uv_kill(pid, SIGKILL);
WRITE_LOG(LOG_DEBUG, "uv_kill rc:%d", rc);
HdcServer::PullupServer(channelHostPort.c_str());
return true;
}
bool HdcClient::KillServer(const string &cmd)
{
int serverStatus = Base::ProgramMutex(SERVER_NAME.c_str(), true);
if (serverStatus < 0) {
WRITE_LOG(LOG_DEBUG, "get server status failed, serverStatus:%d", serverStatus);
return false;
}
// server is not running
if (serverStatus == 0) {
if (cmd.find(" -r") != std::string::npos) {
HdcServer::PullupServer(channelHostPort.c_str());
}
return true;
}
// server is running
uint32_t pid = GetLastPID();
if (pid == 0) {
Base::PrintMessage(TERMINAL_HDC_PROCESS_FAILED.c_str());
return false;
}
int rc = uv_kill(pid, SIGKILL);
if (rc == 0) {
Base::PrintMessage("Kill server finish");
} else {
if (isNowRunning && pid) {
int rc = uv_kill(pid, signNum);
if (rc == 0) {
Base::PrintMessage("Kill server finish");
} else {
constexpr int size = 1024;
char buf[size] = { 0 };
uv_strerror_r(rc, buf, size);
Base::PrintMessage("Kill server failed %s", buf);
}
}
if (isNowRunning && (pid == 0)) {
Base::PrintMessage(errMsg.c_str());
return false;
}
if (!strstr(cmd, " -r")) {
return true;
}
constexpr int size = 1024;
char buf[size] = { 0 };
uv_strerror_r(rc, buf, size);
Base::PrintMessage("Kill server failed %s", buf);
}
if (cmd.find(" -r") != std::string::npos) {
HdcServer::PullupServer(channelHostPort.c_str());
}
return true;
@ -121,9 +143,9 @@ void HdcClient::DoCtrlServiceWork(uv_check_t *handle)
HdcClient *thisClass = (HdcClient *)handle->data;
string &strCmd = thisClass->command;
if (!strncmp(thisClass->command.c_str(), CMDSTR_SERVICE_START.c_str(), CMDSTR_SERVICE_START.size())) {
thisClass->StartKillServer(thisClass->command.c_str(), true);
thisClass->StartServer(strCmd);
} else if (!strncmp(thisClass->command.c_str(), CMDSTR_SERVICE_KILL.c_str(), CMDSTR_SERVICE_KILL.size())) {
thisClass->StartKillServer(thisClass->command.c_str(), false);
thisClass->KillServer(strCmd);
// clang-format off
} else if (!strncmp(thisClass->command.c_str(), CMDSTR_GENERATE_KEY.c_str(), CMDSTR_GENERATE_KEY.size()) &&
strCmd.find(" ") != std::string::npos) {

View File

@ -44,7 +44,8 @@ private:
int PreHandshake(HChannel hChannel, const uint8_t *buf);
string AutoConnectKey(string &doCommand, const string &preConnectKey) const;
uint32_t GetLastPID();
bool StartKillServer(const char *cmd, bool startOrKill);
bool StartServer(const string &cmd);
bool KillServer(const string &cmd);
void BindLocalStd();
void BindLocalStd(HChannel hChannel);
void ModifyTty(bool setOrRestore, uv_tty_t *tty);