mem:处理内存泄漏。

This commit is contained in:
taynpg 2024-12-14 23:59:13 +08:00
parent a104868c47
commit b02f091f79
6 changed files with 12 additions and 18 deletions

View File

@ -314,10 +314,10 @@ void CClient::send_file_data_th()
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->type_ = TYPE_TRANS_FILE;
buf->tid_ = str_key;
buf->data_ = new char[1024]{};
buf->data_ = new char[g_BuffSize]{};
buf->mark_ = 1;
while (!feof(t->file_)) {
buf->len_ = fread(buf->data_, 1, 1024, t->file_);
buf->len_ = fread(buf->data_, 1, g_BuffSize, t->file_);
if (!send_frame(buf.get())) {
logger_->error("send_file_data_th send failed.");
return;

View File

@ -25,7 +25,7 @@ private:
asio::ip::tcp::socket socket_;
std::mutex mutex_;
CMutBuffer buffer_;
std::array<char, 1024> tmp_buf_;
std::array<char, g_BuffSize> tmp_buf_;
ExFun_t fun_;
std::string remote_key_;
};

View File

@ -85,13 +85,6 @@ void CTcpServer::get_client_list(CFrameBuffer** buf)
tbuf->len_ = std::snprintf(tbuf->data_, msg.size() + 1, "%s", msg.data());
}
bool CTcpServer::push_frame(CFrameBuffer* buf)
{
std::lock_guard<std::mutex> lock(buf_mut_);
cache_.push(buf);
return true;
}
void CTcpServer::handle_frame()
{
CFrameBuffer* buf = nullptr;
@ -100,11 +93,11 @@ void CTcpServer::handle_frame()
std::lock_guard<std::mutex> lock(buf_mut_);
if (!cache_.empty()) {
buf = cache_.front();
cache_.pop();
cache_.pop_front();
}
}
if (!buf) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
@ -241,7 +234,8 @@ void CTcpServer::th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const
auto* frame = CTransProtocal::parse(cache->buffer_);
if (frame) {
frame->fid_ = client_key;
push_frame(frame);
std::lock_guard<std::mutex> lock(buf_mut_);
cache_.push_back(frame);
}
}
} catch (std::exception& e) {

View File

@ -1,7 +1,7 @@
#pragma once
#include <net_base.h>
#include <of_util.h>
#include <queue>
#include <list>
#include <string>
#include <util.h>
#include <vector>
@ -10,7 +10,7 @@ using namespace ofen;
struct ClientCache {
std::shared_ptr<asio::ip::tcp::socket> socket_;
CMutBuffer buffer_{};
std::array<char, 1024> tmp_buf_{};
std::array<char, g_BuffSize> tmp_buf_{};
std::string task_{};
std::string time_{};
FrameType cur_type_{TYPE_DEFAULT};
@ -36,7 +36,6 @@ private:
void get_client_list(CFrameBuffer** buf);
private:
bool push_frame(CFrameBuffer* buf);
void handle_frame();
private:
@ -57,7 +56,7 @@ private:
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
std::map<std::string, std::thread> client_threads_;
std::mutex cli_mut_;
std::queue<CFrameBuffer*> cache_;
std::list<CFrameBuffer*> cache_;
std::mutex buf_mut_;
std::shared_ptr<CThreadPool> handle_pool_;
std::string server_ip_;

View File

@ -4,7 +4,7 @@
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file)
{
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file, 1024 * 50, 3);
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file, g_BuffSize * 50, 3);
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e][%l]: %v");
console_sink->set_color(spdlog::level::level_enum::info, 0xFFFF);

View File

@ -6,6 +6,7 @@
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
constexpr int g_BuffSize = 102400;
enum FrameType : int16_t {
TYPE_DEFAULT = 0,
TYPE_GET_LIST,