add:添加后选服务器。

This commit is contained in:
taynpg 2025-03-28 12:20:58 +08:00
parent 23d9ecee8a
commit b41a54ca89
3 changed files with 79 additions and 17 deletions

View File

@ -515,10 +515,10 @@ bool CClient::down_update_file(const std::map<std::string, std::string>& files)
}
if (suc) {
buf->type_ = TYPE_DONE_UPDATE_LIST;
TLOGI("Do Task From Remote {} Done!", buf->tid_);
TLOGW("################## Do Task From Remote {} Done!", buf->tid_);
} else {
buf->type_ = TYPE_FAILED_UPDATE_LIST;
TLOGI("Do Task From Remote {} Failed!", buf->tid_);
TLOGE("################## Do Task From Remote {} Failed!", buf->tid_);
}
send_frame(buf.get());
return suc;

View File

@ -21,6 +21,7 @@ struct CmdParam {
bool parsed{false};
bool direct_use{false};
bool last_use{false};
bool null_use{false};
};
class CServerConfig

View File

@ -29,6 +29,7 @@ int parse_cmd(int argc, char** argv, CmdParam& param)
app.add_option("-r, --remove", param.removeValue, "移除服务器地址组(值为使用--show中显示的序号)");
app.add_flag("-d, --direct", param.direct_use, "添加服务器时直接使用此服务器。");
app.add_flag("-l, --last", param.last_use, "直接使用之前最后一次使用的服务器。");
app.add_flag("-n, --null", param.null_use, "先运行在选择服务器。");
if (argc == 1) {
std::cout << app.help() << std::endl;
@ -45,6 +46,58 @@ int parse_cmd(int argc, char** argv, CmdParam& param)
return 0;
}
bool select_server(const std::vector<TransmSet>& sets, std::string& ip, long& port)
{
TLOGI("Please Select a Server:");
if (sets.empty()) {
TLOGE("No servers available to select.");
return false;
}
// 打印所有服务器选项(带编号)
for (size_t i = 0; i < sets.size(); ++i) {
const auto& server = sets[i];
if (server.comment.empty()) {
TLOGI("[{}] {}:{}", i + 1, server.ip, server.port);
} else {
TLOGI("[{}] {}:{} ({})", i + 1, server.ip, server.port, server.comment);
}
}
while (true) {
TLOGW("Enter server number (or 'exit' to cancel): ");
std::string input;
std::getline(std::cin, input);
// 检查是否输入了退出命令
if (input == "exit" || input == "quit" || input == "q") {
TLOGD("User canceled server selection.");
return false;
}
// 检查输入是否为空或非数字
if (input.empty() || !std::all_of(input.begin(), input.end(), ::isdigit)) {
TLOGE("Invalid input '{}'. Please enter a valid number or 'exit'.", input);
continue;
}
// 转换为数字
int choice = std::stoi(input);
// 检查数字是否在有效范围内
if (choice < 1 || choice > static_cast<int>(sets.size())) {
TLOGE("Invalid choice '{}'. Please select a number between 1 and {}.", choice, sets.size());
continue;
}
// 选择成功,返回对应的 IP 和端口
const auto& selected = sets[choice - 1];
ip = selected.ip;
port = selected.port;
TLOGI("Selected server: {}:{} ({})", ip, port, selected.comment);
return true;
}
}
bool exec_cmd(CmdParam& param, bool& run)
{
run = false;
@ -93,7 +146,7 @@ bool exec_cmd(CmdParam& param, bool& run)
TLOGI("remove config num=[{}] success!", param.removeValue);
return true;
}
if (param.last_use) {
if (param.last_use || param.null_use) {
return true;
}
TLOGW("not matched!", param.removeValue);
@ -130,29 +183,37 @@ int main(int argc, char* argv[])
TLOGW("exec_cmd failed!");
return -1;
}
if (!run && !param.direct_use && !param.last_use) {
if (!run && !param.direct_use && !param.last_use && !param.null_use) {
return 0;
}
std::string ip;
long port{};
std::vector<TransmSet> sets;
if (!g_Config->read_ini(sets)) {
return -1;
}
if (param.null_use) {
if (!select_server(sets, ip, port)) {
return -1;
}
} else {
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;
}
TransmSet use;
if (!g_Config->get_ini(set, param.use_config, use)) {
if (!g_Config->get_ini(sets, 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 => [{}]", ip, port);