pro:一些语法优化。

This commit is contained in:
taynpg 2025-02-06 09:37:24 +08:00
parent f19b1e3f4d
commit e775175c14
9 changed files with 34 additions and 49 deletions

View File

@ -16,7 +16,6 @@ namespace fs = std::filesystem;
CClient::CClient()
{
client_ = std::make_shared<CTcpClient>(io_context_);
supported_.push_back("Get");
sleep_.set_timeout(5000);
}
CClient::~CClient()
@ -60,7 +59,7 @@ void CClient::run(const std::string& ip, const std::string& port)
hearts_ = std::thread([&]() { hearts(); });
std::thread thread([&]() { io_context_.run(); });
CFrameBuffer* bf = new CFrameBuffer();
auto* bf = new CFrameBuffer();
bf->type_ = TYPE_GET_ID;
send_frame(bf);
delete bf;
@ -457,7 +456,7 @@ bool CClient::check_update_list(const std::string& content, std::map<std::string
return valid;
}
bool CClient::down_update_file(std::map<std::string, std::string> files)
bool CClient::down_update_file(const std::map<std::string, std::string>& files)
{
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->tid_ = list_serve_id_;
@ -519,7 +518,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
if (real.empty()) {
continue;
}
if (real.find("[") == std::string::npos) {
if (real.find('[') == std::string::npos) {
#ifdef _WIN32
mpinfo("FILE ==> {}", CCodec::u8_to_ansi(real));
#else
@ -527,15 +526,15 @@ void CClient::handle_frame(CFrameBuffer* buf)
#endif
task_list_[index]->files.push_back(real);
} else {
auto a = real.find_first_of("[") + 1;
auto b = real.find_first_of("]");
auto a = real.find_first_of('[') + 1;
auto b = real.find_first_of(']');
std::string str_index = real.substr(a, b - a);
index = std::stoi(str_index);
std::string backup = real;
backup.erase(0, b + 1);
auto aa = backup.find_first_of("[") + 1;
auto bb = backup.find_first_of("]");
auto aa = backup.find_first_of('[') + 1;
auto bb = backup.find_first_of(']');
std::string id = backup.substr(aa, bb - aa);
if (!task_list_.count(index)) {
@ -590,7 +589,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
keys = buf->fid_;
}
if (!keys.empty()) {
ths_.emplace_back(std::thread([this, keys]() { send_file_data_th(keys.c_str()); }));
ths_.emplace_back([this, keys]() { send_file_data_th(keys.c_str()); });
}
break;
}
@ -767,13 +766,9 @@ void CClient::judget_down_active()
}
}
CFileOpr::CFileOpr()
{
}
CFileOpr::CFileOpr() = default;
CFileOpr::~CFileOpr()
{
}
CFileOpr::~CFileOpr() = default;
std::vector<std::string> CFileOpr::get_file_list(const std::string& input)
{

View File

@ -50,8 +50,8 @@ public:
bool down_one_file(const std::string& id, const std::string& file, const std::string& local_dir = "");
void report_trans_ret(TransState state, const std::string& key = "");
bool request_update_list(const std::string& param);
bool check_update_list(const std::string& content, std::map<std::string, std::string>& files);
bool down_update_file(std::map<std::string, std::string> files);
static bool check_update_list(const std::string& content, std::map<std::string, std::string>& files);
bool down_update_file(const std::map<std::string, std::string>& files);
private:
bool send_frame(CFrameBuffer* buf);
@ -73,7 +73,6 @@ private:
bool downloading_{false};
asio::io_context io_context_;
std::shared_ptr<CTcpClient> client_;
std::vector<std::string> supported_;
std::map<int, std::shared_ptr<DownClientInfo>> task_list_;
std::shared_ptr<TransInfomation> down_;
std::vector<std::thread> ths_;

View File

@ -9,13 +9,9 @@ namespace fs = boost::filesystem;
namespace fs = std::filesystem;
#endif
CServerConfig::CServerConfig()
{
}
CServerConfig::CServerConfig() = default;
CServerConfig::~CServerConfig()
{
}
CServerConfig::~CServerConfig() = default;
bool CServerConfig::baseInit()
{

View File

@ -32,7 +32,7 @@ public:
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);
static bool get_ini(const std::vector<TransmSet>& set, long num, TransmSet& use);
private:
void gen_default_ini(const std::string& path);

View File

@ -4,9 +4,7 @@ CTcpClient::CTcpClient(asio::io_context& io_context) : io_context_(io_context),
{
}
CTcpClient::~CTcpClient()
{
}
CTcpClient::~CTcpClient() = default;
bool CTcpClient::connect(const std::string& host, const std::string& port)
{
@ -90,7 +88,7 @@ void CTcpClient::async_recv()
});
}
bool CTcpClient::is_normal()
bool CTcpClient::is_normal() const
{
return is_normal_;
}

View File

@ -9,7 +9,7 @@ using namespace ofen;
class CTcpClient : public std::enable_shared_from_this<CTcpClient>
{
public:
CTcpClient(asio::io_context& io_context);
explicit CTcpClient(asio::io_context& io_context);
~CTcpClient();
public:
@ -18,14 +18,14 @@ public:
bool send(const char* data, int len);
void register_func(ExFun_t&& f);
void async_recv();
bool is_normal();
bool is_normal() const;
private:
asio::io_context& io_context_;
asio::ip::tcp::socket socket_;
std::mutex mutex_;
CMutBuffer buffer_;
std::array<char, g_BuffSize> tmp_buf_;
std::array<char, g_BuffSize> tmp_buf_{};
ExFun_t fun_;
std::string remote_key_;
bool is_normal_{true};

View File

@ -3,7 +3,6 @@
#include <of_util.h>
using namespace ofen;
constexpr int g_MaxCacheLen = 1024 * 1024 * 50;
constexpr int check_idle_percycle = 1000 * 30; // 毫秒
constexpr int remove_after_time = 60; // 秒
CTcpServer::CTcpServer(asio::io_context& io_context) : io_context_(io_context), acceptor_(io_context)
@ -31,7 +30,7 @@ bool CTcpServer::start(unsigned short port)
mpdebug("Here are the local IP addresses you may use.");
mpdebug("===========================================");
int i = 1;
while (it != asio::ip::tcp::resolver::iterator()) {
while (!it.empty()) {
asio::ip::address addr = it->endpoint().address();
mpinfo("({}){}", i, addr.to_string());
++it;
@ -233,7 +232,7 @@ void CTcpServer::accept_client()
can = true;
}
}
if (can == false) {
if (!can) {
std::this_thread::sleep_for(std::chrono::minutes(1));
} else {
client_threads_[client_key] = std::thread(&CTcpServer::th_client, this, socket, client_key);
@ -243,7 +242,8 @@ void CTcpServer::accept_client()
});
}
void CTcpServer::th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const std::string& client_key)
void CTcpServer::th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket,
const std::string& client_key)
{
std::shared_ptr<int> deleter(new int(0), [&](int* p) {
std::unique_lock<std::shared_mutex> lock(cli_mut_);
@ -253,7 +253,7 @@ void CTcpServer::th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const
client_threads_.at(client_key).detach();
client_threads_.erase(client_key);
}
mpwarn("{} client {} exit.", __FUNCTION__, client_key);
mpwarn("th_client deleter client {} exit.", client_key);
});
try {
@ -277,7 +277,7 @@ void CTcpServer::th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const
throw asio::system_error(error);
}
cache->buffer_.push(cache->tmp_buf_.data(), length);
cache->buffer_.push(cache->tmp_buf_.data(), static_cast<int>(length));
while (true) {
auto* frame = CTransProtocal::parse(cache->buffer_);
if (frame) {
@ -304,7 +304,7 @@ void CTcpServer::th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const
}
}
bool CTcpServer::send_frame(std::shared_ptr<asio::ip::tcp::socket> socket, CFrameBuffer* buf)
bool CTcpServer::send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket, CFrameBuffer* buf)
{
char* out_buf{};
int out_len{};

View File

@ -49,8 +49,8 @@ private:
private:
void accept_client();
void th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const std::string& client_key);
bool send_frame(std::shared_ptr<asio::ip::tcp::socket> socket, CFrameBuffer* buf);
void th_client(const std::shared_ptr<asio::ip::tcp::socket>& socket, const std::string& client_key);
bool send_frame(const std::shared_ptr<asio::ip::tcp::socket>& socket, CFrameBuffer* buf);
void monitor_idle();
private:

View File

@ -4,12 +4,10 @@
#include <thread>
CTransProtocal::CTransProtocal()
{
}
= default;
CTransProtocal::~CTransProtocal()
{
}
= default;
/*
transm TCP
@ -29,7 +27,7 @@ CFrameBuffer* CTransProtocal::parse(CMutBuffer& buffer)
unsigned char tail[] = {0xFF, 0xFF};
// 如果超出 1MB的内容都无法解析成功,则认为是有无效客户端参与链接。
size_t cur_len = static_cast<size_t>(buffer.get_len());
auto cur_len = static_cast<size_t>(buffer.get_len());
if (cur_len > MAX_FRAME_SIZE) {
buffer.clear();
// 这里故意延迟。
@ -108,7 +106,7 @@ void CTransProtocal::display_progress(float percent)
return;
}
const int barWidth = 38;
int pos = barWidth * percent;
int pos = static_cast<int>(barWidth * percent);
std::cout << "[";
for (int i = 0; i < barWidth; ++i) {
@ -126,8 +124,7 @@ void CTransProtocal::display_progress(float percent)
}
CFrameBuffer::CFrameBuffer()
{
}
= default;
CFrameBuffer::~CFrameBuffer()
{