diff --git a/client/client.cpp b/client/client.cpp index 63391b2..4f5e0f2 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -16,7 +16,6 @@ namespace fs = std::filesystem; CClient::CClient() { client_ = std::make_shared(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 files) +bool CClient::down_update_file(const std::map& files) { std::shared_ptr buf = std::make_shared(); 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 CFileOpr::get_file_list(const std::string& input) { diff --git a/client/client.h b/client/client.h index 9ccea83..f51926d 100644 --- a/client/client.h +++ b/client/client.h @@ -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& files); - bool down_update_file(std::map files); + static bool check_update_list(const std::string& content, std::map& files); + bool down_update_file(const std::map& files); private: bool send_frame(CFrameBuffer* buf); @@ -73,7 +73,6 @@ private: bool downloading_{false}; asio::io_context io_context_; std::shared_ptr client_; - std::vector supported_; std::map> task_list_; std::shared_ptr down_; std::vector ths_; diff --git a/client/config.cpp b/client/config.cpp index a845dab..7a68a51 100644 --- a/client/config.cpp +++ b/client/config.cpp @@ -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() { diff --git a/client/config.h b/client/config.h index e724ab1..f91aae1 100644 --- a/client/config.h +++ b/client/config.h @@ -32,7 +32,7 @@ public: bool write_ini(const std::vector& set); bool append_ini(const std::string& ip, long port); bool remove_ini(long num); - bool get_ini(const std::vector& set, long num, TransmSet& use); + static bool get_ini(const std::vector& set, long num, TransmSet& use); private: void gen_default_ini(const std::string& path); diff --git a/net/net_base.cpp b/net/net_base.cpp index 36c5cf8..f990e30 100644 --- a/net/net_base.cpp +++ b/net/net_base.cpp @@ -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_; } diff --git a/net/net_base.h b/net/net_base.h index 5dbb5ce..2079f51 100644 --- a/net/net_base.h +++ b/net/net_base.h @@ -9,7 +9,7 @@ using namespace ofen; class CTcpClient : public std::enable_shared_from_this { 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 tmp_buf_; + std::array tmp_buf_{}; ExFun_t fun_; std::string remote_key_; bool is_normal_{true}; diff --git a/server/server.cpp b/server/server.cpp index fdedc3a..5c0046a 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -3,7 +3,6 @@ #include 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 socket, const std::string& client_key) +void CTcpServer::th_client(const std::shared_ptr& socket, + const std::string& client_key) { std::shared_ptr deleter(new int(0), [&](int* p) { std::unique_lock lock(cli_mut_); @@ -253,7 +253,7 @@ void CTcpServer::th_client(std::shared_ptr 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 socket, const throw asio::system_error(error); } - cache->buffer_.push(cache->tmp_buf_.data(), length); + cache->buffer_.push(cache->tmp_buf_.data(), static_cast(length)); while (true) { auto* frame = CTransProtocal::parse(cache->buffer_); if (frame) { @@ -304,7 +304,7 @@ void CTcpServer::th_client(std::shared_ptr socket, const } } -bool CTcpServer::send_frame(std::shared_ptr socket, CFrameBuffer* buf) +bool CTcpServer::send_frame(const std::shared_ptr& socket, CFrameBuffer* buf) { char* out_buf{}; int out_len{}; diff --git a/server/server.h b/server/server.h index 21c0b18..0b8e04e 100644 --- a/server/server.h +++ b/server/server.h @@ -49,8 +49,8 @@ private: private: void accept_client(); - void th_client(std::shared_ptr socket, const std::string& client_key); - bool send_frame(std::shared_ptr socket, CFrameBuffer* buf); + void th_client(const std::shared_ptr& socket, const std::string& client_key); + bool send_frame(const std::shared_ptr& socket, CFrameBuffer* buf); void monitor_idle(); private: diff --git a/util/util.cpp b/util/util.cpp index f4ac658..360e25d 100644 --- a/util/util.cpp +++ b/util/util.cpp @@ -4,12 +4,10 @@ #include 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(buffer.get_len()); + auto cur_len = static_cast(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(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() {