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() CClient::CClient()
{ {
client_ = std::make_shared<CTcpClient>(io_context_); client_ = std::make_shared<CTcpClient>(io_context_);
supported_.push_back("Get");
sleep_.set_timeout(5000); sleep_.set_timeout(5000);
} }
CClient::~CClient() CClient::~CClient()
@ -60,7 +59,7 @@ void CClient::run(const std::string& ip, const std::string& port)
hearts_ = std::thread([&]() { hearts(); }); hearts_ = std::thread([&]() { hearts(); });
std::thread thread([&]() { io_context_.run(); }); std::thread thread([&]() { io_context_.run(); });
CFrameBuffer* bf = new CFrameBuffer(); auto* bf = new CFrameBuffer();
bf->type_ = TYPE_GET_ID; bf->type_ = TYPE_GET_ID;
send_frame(bf); send_frame(bf);
delete bf; delete bf;
@ -457,7 +456,7 @@ bool CClient::check_update_list(const std::string& content, std::map<std::string
return valid; 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>(); std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->tid_ = list_serve_id_; buf->tid_ = list_serve_id_;
@ -519,7 +518,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
if (real.empty()) { if (real.empty()) {
continue; continue;
} }
if (real.find("[") == std::string::npos) { if (real.find('[') == std::string::npos) {
#ifdef _WIN32 #ifdef _WIN32
mpinfo("FILE ==> {}", CCodec::u8_to_ansi(real)); mpinfo("FILE ==> {}", CCodec::u8_to_ansi(real));
#else #else
@ -527,15 +526,15 @@ void CClient::handle_frame(CFrameBuffer* buf)
#endif #endif
task_list_[index]->files.push_back(real); task_list_[index]->files.push_back(real);
} else { } else {
auto a = real.find_first_of("[") + 1; auto a = real.find_first_of('[') + 1;
auto b = real.find_first_of("]"); auto b = real.find_first_of(']');
std::string str_index = real.substr(a, b - a); std::string str_index = real.substr(a, b - a);
index = std::stoi(str_index); index = std::stoi(str_index);
std::string backup = real; std::string backup = real;
backup.erase(0, b + 1); backup.erase(0, b + 1);
auto aa = backup.find_first_of("[") + 1; auto aa = backup.find_first_of('[') + 1;
auto bb = backup.find_first_of("]"); auto bb = backup.find_first_of(']');
std::string id = backup.substr(aa, bb - aa); std::string id = backup.substr(aa, bb - aa);
if (!task_list_.count(index)) { if (!task_list_.count(index)) {
@ -590,7 +589,7 @@ void CClient::handle_frame(CFrameBuffer* buf)
keys = buf->fid_; keys = buf->fid_;
} }
if (!keys.empty()) { 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; 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) 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 = ""); 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 = ""); void report_trans_ret(TransState state, const std::string& key = "");
bool request_update_list(const std::string& param); bool request_update_list(const std::string& param);
bool check_update_list(const std::string& content, 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(std::map<std::string, std::string> files); bool down_update_file(const std::map<std::string, std::string>& files);
private: private:
bool send_frame(CFrameBuffer* buf); bool send_frame(CFrameBuffer* buf);
@ -73,7 +73,6 @@ private:
bool downloading_{false}; bool downloading_{false};
asio::io_context io_context_; asio::io_context io_context_;
std::shared_ptr<CTcpClient> client_; std::shared_ptr<CTcpClient> client_;
std::vector<std::string> supported_;
std::map<int, std::shared_ptr<DownClientInfo>> task_list_; std::map<int, std::shared_ptr<DownClientInfo>> task_list_;
std::shared_ptr<TransInfomation> down_; std::shared_ptr<TransInfomation> down_;
std::vector<std::thread> ths_; std::vector<std::thread> ths_;

View File

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

View File

@ -32,7 +32,7 @@ public:
bool write_ini(const std::vector<TransmSet>& set); bool write_ini(const std::vector<TransmSet>& set);
bool append_ini(const std::string& ip, long port); bool append_ini(const std::string& ip, long port);
bool remove_ini(long num); 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: private:
void gen_default_ini(const std::string& path); 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) 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_; return is_normal_;
} }

View File

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

View File

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

View File

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

View File

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