add:添加支持直接使用最后一次使用的服务器选项。

This commit is contained in:
taynpg 2025-03-28 12:01:35 +08:00
parent e2d66a64e7
commit 23d9ecee8a
3 changed files with 53 additions and 12 deletions

View File

@ -136,6 +136,27 @@ bool CServerConfig::get_ini(const std::vector<TransmSet>& 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);

View File

@ -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<TransmSet>& 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);

View File

@ -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,10 +130,17 @@ 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::string ip;
long port{};
if (param.last_use) {
if (!g_Config->get_last_use(ip, port)) {
return -1;
}
} else {
std::vector<TransmSet> set;
if (!g_Config->read_ini(set)) {
return -1;
@ -139,10 +150,14 @@ int main(int argc, char* argv[])
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;
}