From 648f421192a5746d8ef43a5d402bd1c5f1dac4c4 Mon Sep 17 00:00:00 2001 From: wenlong_12 Date: Mon, 9 Sep 2024 15:20:16 +0800 Subject: [PATCH] Split the function logic of hdc kill and hdc start Signed-off-by: wenlong_12 Change-Id: I83a0fa603cace90ba640ba5f833425bc854516a0 --- src/common/define.h | 4 ++ src/host/client.cpp | 102 +++++++++++++++++++++++++++----------------- src/host/client.h | 3 +- 3 files changed, 68 insertions(+), 41 deletions(-) diff --git a/src/common/define.h b/src/common/define.h index ec6c2fe3..b7c46748 100644 --- a/src/common/define.h +++ b/src/common/define.h @@ -179,6 +179,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__) diff --git a/src/host/client.cpp b/src/host/client.cpp index 85343672..65929721 100755 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -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) { diff --git a/src/host/client.h b/src/host/client.h index ee272161..b681268c 100755 --- a/src/host/client.h +++ b/src/host/client.h @@ -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);