From f81a65ec1ef80217fe24d2fdf0052f8f82f7d9ef Mon Sep 17 00:00:00 2001 From: leauny <42369176+leauny@users.noreply.github.com> Date: Wed, 11 Oct 2023 16:20:14 +0000 Subject: [PATCH 1/2] fix: ctrl+c problem in cli mode. --- src/observer/common/rc.h | 1 + src/observer/net/cli_communicator.cpp | 20 ++++++-------------- src/observer/net/server.cpp | 3 +++ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/observer/common/rc.h b/src/observer/common/rc.h index 9acc27651..76583838c 100644 --- a/src/observer/common/rc.h +++ b/src/observer/common/rc.h @@ -28,6 +28,7 @@ See the Mulan PSL v2 for more details. */ DEFINE_RC(NOMEM) \ DEFINE_RC(NOTFOUND) \ DEFINE_RC(EMPTY) \ + DEFINE_RC(EMPTY_CMD) \ DEFINE_RC(BUFFERPOOL_OPEN) \ DEFINE_RC(BUFFERPOOL_NOBUF) \ DEFINE_RC(BUFFERPOOL_INVALID_PAGE_NUM) \ diff --git a/src/observer/net/cli_communicator.cpp b/src/observer/net/cli_communicator.cpp index 7e34e9f9a..846c3f7d4 100644 --- a/src/observer/net/cli_communicator.cpp +++ b/src/observer/net/cli_communicator.cpp @@ -86,19 +86,6 @@ bool is_exit_command(const char *cmd) { 0 == strncasecmp("\\q", cmd, 2) ; } -char *read_command() -{ - const char *prompt_str = "miniob > "; - char *input_command = nullptr; - for (input_command = my_readline(prompt_str); - common::is_blank(input_command); - input_command = my_readline(prompt_str)) { - free(input_command); - input_command = nullptr; - } - return input_command; -} - RC CliCommunicator::init(int fd, Session *session, const std::string &addr) { RC rc = PlainCommunicator::init(fd, session, addr); @@ -126,7 +113,12 @@ RC CliCommunicator::init(int fd, Session *session, const std::string &addr) RC CliCommunicator::read_event(SessionEvent *&event) { event = nullptr; - char *command = read_command(); + char *command = my_readline("miniob > "); + if (common::is_blank(command)) { + free(command); + event = nullptr; + return RC::EMPTY_CMD; + } if (is_exit_command(command)) { free(command); diff --git a/src/observer/net/server.cpp b/src/observer/net/server.cpp index e6692aa6e..66fdfc85b 100644 --- a/src/observer/net/server.cpp +++ b/src/observer/net/server.cpp @@ -323,6 +323,9 @@ int Server::start_stdin_server() SessionEvent *event = nullptr; rc = communicator->read_event(event); if (OB_FAIL(rc)) { + if (rc == RC::EMPTY_CMD) { + continue; + } LOG_WARN("failed to read event. rc=%s", strrc(rc)); return -1; } From 4f018771370d16d7588a9d917b1c3b32baa04646 Mon Sep 17 00:00:00 2001 From: leauny <42369176+leauny@users.noreply.github.com> Date: Thu, 12 Oct 2023 11:40:57 +0800 Subject: [PATCH 2/2] fixed an issue where the server would execute the last time when stopped by signal in cli mode --- src/observer/net/server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/observer/net/server.cpp b/src/observer/net/server.cpp index 66fdfc85b..63160c39f 100644 --- a/src/observer/net/server.cpp +++ b/src/observer/net/server.cpp @@ -330,7 +330,7 @@ int Server::start_stdin_server() return -1; } - if (event == nullptr) { + if (event == nullptr || !started_) { break; }