diff --git a/client/config.cpp b/client/config.cpp index d05ea1b..02b8011 100644 --- a/client/config.cpp +++ b/client/config.cpp @@ -136,6 +136,27 @@ bool CServerConfig::get_ini(const std::vector& set, long num, TransmS return find; } +bool CServerConfig::save_last_use(const std::string& ip, long port) +{ + assert(init_ == true); + ini_handle_.SetValue("Base", "LastUseIP", ip.c_str()); + ini_handle_.SetLongValue("Base", "LastUsePort", port); + ini_handle_.SaveFile(config_path_.c_str()); + return true; +} + +bool CServerConfig::get_last_use(std::string& ip, long& port) +{ + assert(init_ == true); + if (!ini_handle_.KeyExists("Base", "LastUseIP") || !ini_handle_.KeyExists("Base", "LastUsePort")) { + TLOGE("Not Found Last Use Record."); + return false; + } + ip = ini_handle_.GetValue("Base", "LastUseIP"); + port = ini_handle_.GetLongValue("Base", "LastUsePort"); + return true; +} + void CServerConfig::gen_default_ini(const std::string& path) { TLOGW("Gen Default Setting Ini in [{}].", path); diff --git a/client/config.h b/client/config.h index b95a08b..c258ff0 100644 --- a/client/config.h +++ b/client/config.h @@ -20,6 +20,7 @@ struct CmdParam { long use_config{-1}; bool parsed{false}; bool direct_use{false}; + bool last_use{false}; }; class CServerConfig @@ -37,6 +38,10 @@ public: bool remove_ini(long num); static bool get_ini(const std::vector& set, long num, TransmSet& use); +public: + bool save_last_use(const std::string& ip, long port); + bool get_last_use(std::string&ip, long& port); + private: void gen_default_ini(const std::string& path); diff --git a/client/main.cpp b/client/main.cpp index acb9d4f..c645e14 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -23,11 +23,12 @@ int parse_cmd(int argc, char** argv, CmdParam& param) fmt::format("tsc cmd introduce, version: {}\nopensource: {}", VERSION_NUM, VERSION_URL)); CLI::App app(intro); - app.add_option("-n, --number", param.use_config, "使用服务器地址组(值为使用--show中显示的序号)"); + app.add_option("-u, --use", param.use_config, "使用服务器地址组(值为使用--show中显示的序号)"); app.add_option("-a, --append", param.appendValue, "添加服务器地址组(地址格式:127.0.0.1:9898:注释)"); app.add_flag("-s, --show", param.showValue, "查看服务器地址组"); app.add_option("-r, --remove", param.removeValue, "移除服务器地址组(值为使用--show中显示的序号)"); app.add_flag("-d, --direct", param.direct_use, "添加服务器时直接使用此服务器。"); + app.add_flag("-l, --last", param.last_use, "直接使用之前最后一次使用的服务器。"); if (argc == 1) { std::cout << app.help() << std::endl; @@ -92,6 +93,9 @@ bool exec_cmd(CmdParam& param, bool& run) TLOGI("remove config num=[{}] success!", param.removeValue); return true; } + if (param.last_use) { + return true; + } TLOGW("not matched!", param.removeValue); return false; } @@ -126,23 +130,34 @@ int main(int argc, char* argv[]) TLOGW("exec_cmd failed!"); return -1; } - if (!run && !param.direct_use) { + if (!run && !param.direct_use && !param.last_use) { return 0; } - std::vector set; - if (!g_Config->read_ini(set)) { - return -1; - } - TransmSet use; - if (!g_Config->get_ini(set, param.use_config, use)) { - TLOGW("Not found config by num:[{}]", param.use_config); - return -1; + std::string ip; + long port{}; + if (param.last_use) { + if (!g_Config->get_last_use(ip, port)) { + return -1; + } + } else { + std::vector set; + if (!g_Config->read_ini(set)) { + return -1; + } + TransmSet use; + if (!g_Config->get_ini(set, param.use_config, use)) { + TLOGW("Not found config by num:[{}]", param.use_config); + return -1; + } + ip = use.ip; + port = use.port; } + g_Config->save_last_use(ip, port); TLOGI("Build At {} {} under {} on {}", __DATE__, __TIME__, VERSION_GIT_COMMIT, VERSION_GIT_BRANCH); - TLOGI("use ip => [{}], port => [{}]", use.ip, use.port); + TLOGI("use ip => [{}], port => [{}]", ip, port); CClient client; - client.run(use.ip, std::to_string(use.port)); + client.run(ip, std::to_string(port)); TLOGI("exit =========="); return 0; } \ No newline at end of file