client:客户端添加多选服务端IP,避免重复手动修改配置文件。

This commit is contained in:
taynpg 2025-01-06 16:25:39 +08:00
parent d82a65dea6
commit cc9aadd68a
10 changed files with 11311 additions and 95 deletions

View File

@ -18,7 +18,10 @@
"ignoreFailures": true
}
],
"visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis"
"visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis",
"args": [
"-a", "106.14.150.219:9898"
]
},
"cmake.environment": {
"PATH": "${env:PATH};"

10998
3rd/CLI11.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ if (MSVC)
add_compile_options(/source-charset:utf-8)
endif()
add_executable(transmc main.cpp client.h client.cpp file_oper.h file_oper.cpp)
add_executable(transmc main.cpp client.h client.cpp config.h config.cpp)
target_link_libraries(transmc PRIVATE trans_net trans_util)
if (UNIX)
target_link_libraries(transmc PRIVATE pthread)

View File

@ -635,3 +635,37 @@ void CClient::judget_down_active()
send_frame(buf.get());
}
}
CFileOpr::CFileOpr()
{
}
CFileOpr::~CFileOpr()
{
}
std::vector<std::string> CFileOpr::get_file_list(const std::string& input)
{
std::vector<std::string> result;
std::string backup = input;
backup.erase(0, backup.find_first_of(" "));
backup = COfStr::trim(backup);
if (backup.empty()) {
return result;
}
auto vec = COfStr::split(backup, "|");
for (const auto& item : vec) {
std::string ret(item);
#ifdef _WIN32
if (item.find("\"") != std::string::npos) {
ret = COfStr::replace(item, "\"", "");
}
#else
if (item.find(R"(')") != std::string::npos) {
ret = COfStr::replace(item, R"(')", "");
}
#endif
result.push_back(COfPath::to_full(ret));
}
return result;
}

View File

@ -1,5 +1,4 @@
#pragma once
#include "file_oper.h"
#include <memory>
#include <mutex>
#include <net_base.h>
@ -82,4 +81,13 @@ private:
std::string list_file_;
std::string list_serve_id_;
std::thread update_list_th_;
};
class CFileOpr
{
public:
CFileOpr();
~CFileOpr();
public:
static std::vector<std::string> get_file_list(const std::string& input);
};

117
client/config.cpp Normal file
View File

@ -0,0 +1,117 @@
#include "config.h"
#include <cassert>
#ifdef USE_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
CServerConfig::CServerConfig(std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
{
}
CServerConfig::~CServerConfig()
{
}
bool CServerConfig::baseInit()
{
fs::path home(COfPath::get_home());
config_path_ = home.append("transm.ini").string();
if (!fs::exists(config_path_)) {
gen_default_ini(config_path_);
}
SI_Error ret = ini_handle_.LoadFile(config_path_.c_str());
if (ret != SI_OK) {
logger_->error("Load Ini [{}] Failed.", config_path_);
return false;
}
init_ = true;
return true;
}
bool CServerConfig::read_ini(std::vector<TransmSet>& set)
{
assert(init_ == true);
long groups = ini_handle_.GetLongValue("BASE", "GROUPS");
if (groups < 1) {
logger_->error("GROUPS num < 1.");
return false;
}
set.clear();
for (long i = 0; i < groups; ++i) {
std::string key = "GROUP" + std::to_string(i);
TransmSet ts;
ts.group = key;
ts.grp_id = i;
ts.ip = ini_handle_.GetValue(key.c_str(), "IP");
ts.port = ini_handle_.GetLongValue(key.c_str(), "PORT");
set.push_back(ts);
}
return true;
}
bool CServerConfig::write_ini(const std::vector<TransmSet>& set)
{
assert(init_ == true);
for (size_t start = 0; start < set.size(); ++start) {
std::string key = "GROUP" + std::to_string(start);
ini_handle_.SetValue(key.c_str(), "IP", set[start].ip.c_str());
ini_handle_.SetLongValue(key.c_str(), "PORT", set[start].port);
}
ini_handle_.SaveFile(config_path_.c_str());
return true;
}
bool CServerConfig::append_ini(const std::string& ip, long port)
{
assert(init_ == true);
int group = ini_handle_.GetLongValue("BASE", "GROUPS");
std::string node_name = "GROUP" + std::to_string(group);
ini_handle_.SetValue(node_name.c_str(), "IP", ip.c_str());
ini_handle_.SetLongValue(node_name.c_str(), "PORT", port);
ini_handle_.SetLongValue("BASE", "GROUPS", group + 1);
ini_handle_.SaveFile(config_path_.c_str());
return true;
}
bool CServerConfig::remove_ini(long num)
{
assert(init_ == true);
std::vector<TransmSet> set;
if (!read_ini(set)) {
return false;
}
set.erase(std::remove_if(set.begin(), set.end(),
[&num](const TransmSet& item) { return item.grp_id == num; }),
set.end());
ini_handle_.Reset();
ini_handle_.SetLongValue("BASE", "GROUPS", static_cast<long>(set.size()));
return write_ini(set);
}
bool CServerConfig::get_ini(const std::vector<TransmSet>& set, long num, TransmSet& use)
{
bool find = false;
for (const auto& item : set) {
if (item.grp_id == num) {
find = true;
use = item;
break;
}
}
return find;
}
void CServerConfig::gen_default_ini(const std::string& path)
{
logger_->warn("Gen Default Setting Ini in [{}].", path);
ini_handle_.LoadFile(path.c_str());
ini_handle_.SetLongValue("BASE", "GROUPS", 1);
ini_handle_.SetValue("GROUP0", "IP", "127.0.0.1");
ini_handle_.SetValue("GROUP0", "PORT", "9898");
ini_handle_.SaveFile(path.c_str());
}

44
client/config.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <SimpleIni.h>
#include <of_path.h>
#include <util.h>
using namespace ofen;
struct TransmSet {
std::string group;
std::string ip{};
long port{};
long grp_id{};
};
struct CmdParam {
std::string removeValue;
std::string appendValue;
bool showValue{false};
long use_config{-1};
};
class CServerConfig
{
public:
CServerConfig(std::shared_ptr<spdlog::logger>& logger);
~CServerConfig();
public:
bool baseInit();
bool read_ini(std::vector<TransmSet>& set);
bool write_ini(const std::vector<TransmSet>& set);
bool append_ini(const std::string& ip, long port);
bool remove_ini(long num);
bool get_ini(const std::vector<TransmSet>& set, long num, TransmSet& use);
private:
void gen_default_ini(const std::string& path);
private:
bool init_{false};
CSimpleIniA ini_handle_{};
std::string config_path_{};
std::shared_ptr<spdlog::logger> logger_;
};

View File

@ -1,35 +0,0 @@
#include "file_oper.h"
CFileOpr::CFileOpr()
{
}
CFileOpr::~CFileOpr()
{
}
std::vector<std::string> CFileOpr::get_file_list(const std::string& input)
{
std::vector<std::string> result;
std::string backup = input;
backup.erase(0, backup.find_first_of(" "));
backup = COfStr::trim(backup);
if (backup.empty()) {
return result;
}
auto vec = COfStr::split(backup, "|");
for (const auto& item : vec) {
std::string ret(item);
#ifdef _WIN32
if (item.find("\"") != std::string::npos) {
ret = COfStr::replace(item, "\"", "");
}
#else
if (item.find(R"(')") != std::string::npos) {
ret = COfStr::replace(item, R"(')", "");
}
#endif
result.push_back(COfPath::to_full(ret));
}
return result;
}

View File

@ -1,14 +0,0 @@
#include <string>
#include <of_path.h>
#include <of_str.h>
#include <vector>
using namespace ofen;
class CFileOpr
{
public:
CFileOpr();
~CFileOpr();
public:
static std::vector<std::string> get_file_list(const std::string& input);
};

View File

@ -1,66 +1,127 @@
#include "client.h"
#include "config.h"
#include "version.h"
#include <SimpleIni.h>
#include <CLI11.hpp>
#include <iostream>
#include <of_path.h>
using namespace ofen;
#ifdef USE_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
struct TransmSet {
std::string ip{};
std::string port{};
};
#include <regex>
std::shared_ptr<spdlog::logger> g_Logger = nullptr;
bool gen_default_ini(const std::string& path)
std::shared_ptr<CServerConfig> g_Config = nullptr;
bool parse_cmd(int argc, char** argv, CmdParam& param)
{
g_Logger->warn("Gen Default Setting Ini in [{}].", path);
CSimpleIniA ini_handle{};
ini_handle.LoadFile(path.c_str());
ini_handle.SetValue("Setting", "IP", "127.0.0.1");
ini_handle.SetValue("Setting", "PORT", "9898");
ini_handle.SaveFile(path.c_str());
return true;
std::string intro("transmc cmd introduce.");
CLI::App app(intro);
app.add_option("-n, --number", 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中显示的序号)");
try {
CLI11_PARSE(app, argc, argv);
return true;
} catch (const CLI::ParseError& e) {
std::cerr << "Error parsing command line: " << e.what() << std::endl;
return false;
}
return false;
}
bool read_ini(TransmSet& set)
bool exec_cmd(const CmdParam& param, bool& run)
{
fs::path home(COfPath::get_home());
std::string config_path = home.append("transm.ini").string();
if (!fs::exists(config_path) && !gen_default_ini(config_path)) {
run = false;
// 如果是展示
if (param.showValue) {
std::vector<TransmSet> set;
if (!g_Config->read_ini(set)) {
return false;
}
for (const auto& item : set) {
g_Logger->info("{} => {}:{}", item.group, item.ip, item.port);
}
return true;
}
if (param.use_config != -1) {
run = true;
return true;
}
if (!param.appendValue.empty() && !param.removeValue.empty()) {
g_Logger->error("append and remove can't simultaneous operate!");
return false;
}
CSimpleIniA ini_handle{};
SI_Error ret = ini_handle.LoadFile(config_path.c_str());
if (ret != SI_OK) {
g_Logger->error("Load Ini [{}] Failed.", config_path);
return false;
// 如果是移除
if (!param.appendValue.empty()) {
std::regex rg(R"(([^:]+):(\d+))");
std::smatch match;
if (!std::regex_search(param.appendValue, match, rg)) {
g_Logger->error("append invalid format!");
return false;
}
std::string ip = match[1].str();
std::string port = match[2].str();
if (!g_Config->append_ini(ip, std::stol(port))) {
g_Logger->error("add {}:{} failed.", ip, port);
return false;
}
g_Logger->info("add {}:{} success.", ip, port);
return true;
}
set.ip = ini_handle.GetValue("Setting", "IP");
set.port = ini_handle.GetValue("Setting", "PORT");
return true;
if (!param.removeValue.empty()) {
if (!g_Config->remove_ini(std::stol(param.removeValue))) {
g_Logger->error("remove {} failed, please check!", param.removeValue);
return false;
}
g_Logger->info("remove {} success!", param.removeValue);
return true;
}
g_Logger->error("not matched!", param.removeValue);
return false;
}
int main(int argc, char* argv[])
{
auto log_path = ofen::COfPath::to_full("client.log");
g_Logger = get_logger("client", log_path);
g_Logger->info("Configure At {} under {} on {}", VERSION_BUILD_DATE, VERSION_GIT_HASH,
VERSION_GIT_BRANCH);
TransmSet set;
if (!read_ini(set)) {
g_Config = std::make_shared<CServerConfig>(g_Logger);
if (!g_Config->baseInit()) {
return -1;
}
bool run = false;
CmdParam param;
if (!parse_cmd(argc, argv, param)) {
g_Logger->error("parse cmd failed!");
return -1;
}
if (param.appendValue.empty() && param.removeValue.empty() && param.showValue == false &&
param.use_config == -1) {
g_Logger->warn("Use --help To Get How To Use.");
return 0;
}
if (!exec_cmd(param, run)) {
g_Logger->error("exec_cmd failed!");
return -1;
}
if (!run) {
return 0;
}
g_Logger->info("Configure At {} under {} on {}", VERSION_BUILD_DATE, VERSION_GIT_HASH,
VERSION_GIT_BRANCH);
std::vector<TransmSet> set;
if (!g_Config->read_ini(set)) {
return -1;
}
TransmSet use;
if (!g_Config->get_ini(set, param.use_config, use)) {
return -1;
}
g_Logger->info("use ip:[{}], port:[{}]", use.ip, use.port);
CClient client(g_Logger);
client.run(set.ip, set.port);
g_Logger->info("exit &&&&&&&&&&&&&");
client.run(use.ip, std::to_string(use.port));
g_Logger->info("exit ==========");
return 0;
}