diff --git a/client/client.cpp b/client/client.cpp index f9e9c93..35c7e86 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -17,7 +17,7 @@ CClient::CClient(const std::shared_ptr& logger) : logger_(logger { client_ = std::make_shared(io_context_, logger_); supported_.push_back("Get"); - sleep_.set_timeout(2000); + sleep_.set_timeout(5000); } CClient::~CClient() diff --git a/server/server.cpp b/server/server.cpp index faf8b5c..d0069ad 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -4,8 +4,8 @@ using namespace ofen; constexpr int g_MaxCacheLen = 1024 * 1024 * 50; -constexpr int check_idle_percycle = 1000 * 60; // 毫秒 -constexpr int remove_after_time = 60 * 10; // 秒 +constexpr int check_idle_percycle = 1000 * 30; // 毫秒 +constexpr int remove_after_time = 60; // 秒 CTcpServer::CTcpServer(asio::io_context& io_context, const std::shared_ptr& logger) : io_context_(io_context), acceptor_(io_context), logger_(logger) { @@ -60,7 +60,7 @@ bool CTcpServer::start(unsigned short port) void CTcpServer::stop() { acceptor_.close(); - std::lock_guard lock(cli_mut_); + std::unique_lock lock(cli_mut_); for (auto& [key, thread] : client_threads_) { if (thread.joinable()) { thread.join(); @@ -72,7 +72,7 @@ void CTcpServer::stop() std::vector CTcpServer::get_clients() { std::vector result; - std::lock_guard lock(cli_mut_); + std::shared_lock lock(cli_mut_); for (const auto& item : client_map_) { TaskList t; t.id_ = item.first; @@ -110,7 +110,7 @@ void CTcpServer::trans_data(CFrameBuffer* buf) std::shared_ptr tcli = nullptr; { - std::lock_guard lock(cli_mut_); + std::shared_lock lock(cli_mut_); if (client_map_.count(buf->fid_)) { fcli = client_map_[buf->fid_]; } @@ -178,7 +178,7 @@ void CTcpServer::trans_data(CFrameBuffer* buf) bool CTcpServer::check_double(CFrameBuffer* buf, std::shared_ptr& fcli, std::shared_ptr& tcli) { - std::lock_guard lock(cli_mut_); + std::shared_lock lock(cli_mut_); if (client_map_.count(buf->fid_)) { fcli = client_map_[buf->fid_]; } @@ -215,7 +215,7 @@ void CTcpServer::accept_client() bool can = false; { - std::lock_guard lock(cli_mut_); + std::unique_lock lock(cli_mut_); if (client_map_.size() >= 100) { logger_->info("Max client connections reached. Closing connection from {}", client_key); socket->close(); @@ -242,7 +242,7 @@ void CTcpServer::accept_client() void CTcpServer::th_client(std::shared_ptr socket, const std::string& client_key) { std::shared_ptr deleter(new int(0), [&](int* p) { - std::lock_guard lock(cli_mut_); + std::unique_lock lock(cli_mut_); delete p; client_map_.erase(client_key); if (client_threads_.find(client_key) != client_threads_.end()) { @@ -256,7 +256,7 @@ void CTcpServer::th_client(std::shared_ptr socket, const std::shared_ptr cache = nullptr; { - std::lock_guard lock(cli_mut_); + std::shared_lock lock(cli_mut_); if (!client_map_.count(client_key)) { logger_->error("Not Find Client{} in cache.", client_key); return; @@ -278,7 +278,7 @@ void CTcpServer::th_client(std::shared_ptr socket, const auto* frame = CTransProtocal::parse(cache->buffer_); if (frame) { if (frame->type_ == TYPE_HEARTS) { - std::lock_guard lock(cli_mut_); + std::unique_lock lock(cli_mut_); if (client_map_.count(client_key)) { auto& cli = client_map_[client_key]; cli->last_active_time_ = std::chrono::high_resolution_clock::now(); @@ -336,7 +336,7 @@ void CTcpServer::monitor_idle() break; } std::vector remove_vec; - std::lock_guard lock(cli_mut_); + std::unique_lock lock(cli_mut_); for (auto& item : client_map_) { auto now = std::chrono::high_resolution_clock::now(); auto duration = diff --git a/server/server.h b/server/server.h index 7084384..1d0e012 100644 --- a/server/server.h +++ b/server/server.h @@ -1,11 +1,12 @@ #pragma once +#include #include #include #include +#include #include #include #include -#include using namespace ofen; using high_c = std::chrono::time_point; @@ -58,8 +59,7 @@ private: std::shared_ptr logger_; std::map> client_map_; std::map client_threads_; - std::mutex cli_mut_; - std::mutex buf_mut_; + std::shared_mutex cli_mut_; std::thread th_monitor_idle_; std::string server_ip_; CThreadSleep sleep_;