mem:处理内存泄漏。
This commit is contained in:
parent
a104868c47
commit
b02f091f79
@ -314,10 +314,10 @@ void CClient::send_file_data_th()
|
|||||||
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
||||||
buf->type_ = TYPE_TRANS_FILE;
|
buf->type_ = TYPE_TRANS_FILE;
|
||||||
buf->tid_ = str_key;
|
buf->tid_ = str_key;
|
||||||
buf->data_ = new char[1024]{};
|
buf->data_ = new char[g_BuffSize]{};
|
||||||
buf->mark_ = 1;
|
buf->mark_ = 1;
|
||||||
while (!feof(t->file_)) {
|
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())) {
|
if (!send_frame(buf.get())) {
|
||||||
logger_->error("send_file_data_th send failed.");
|
logger_->error("send_file_data_th send failed.");
|
||||||
return;
|
return;
|
||||||
|
@ -25,7 +25,7 @@ private:
|
|||||||
asio::ip::tcp::socket socket_;
|
asio::ip::tcp::socket socket_;
|
||||||
std::mutex mutex_;
|
std::mutex mutex_;
|
||||||
CMutBuffer buffer_;
|
CMutBuffer buffer_;
|
||||||
std::array<char, 1024> tmp_buf_;
|
std::array<char, g_BuffSize> tmp_buf_;
|
||||||
ExFun_t fun_;
|
ExFun_t fun_;
|
||||||
std::string remote_key_;
|
std::string remote_key_;
|
||||||
};
|
};
|
@ -85,13 +85,6 @@ void CTcpServer::get_client_list(CFrameBuffer** buf)
|
|||||||
tbuf->len_ = std::snprintf(tbuf->data_, msg.size() + 1, "%s", msg.data());
|
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()
|
void CTcpServer::handle_frame()
|
||||||
{
|
{
|
||||||
CFrameBuffer* buf = nullptr;
|
CFrameBuffer* buf = nullptr;
|
||||||
@ -100,11 +93,11 @@ void CTcpServer::handle_frame()
|
|||||||
std::lock_guard<std::mutex> lock(buf_mut_);
|
std::lock_guard<std::mutex> lock(buf_mut_);
|
||||||
if (!cache_.empty()) {
|
if (!cache_.empty()) {
|
||||||
buf = cache_.front();
|
buf = cache_.front();
|
||||||
cache_.pop();
|
cache_.pop_front();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,7 +234,8 @@ void CTcpServer::th_client(std::shared_ptr<asio::ip::tcp::socket> socket, const
|
|||||||
auto* frame = CTransProtocal::parse(cache->buffer_);
|
auto* frame = CTransProtocal::parse(cache->buffer_);
|
||||||
if (frame) {
|
if (frame) {
|
||||||
frame->fid_ = client_key;
|
frame->fid_ = client_key;
|
||||||
push_frame(frame);
|
std::lock_guard<std::mutex> lock(buf_mut_);
|
||||||
|
cache_.push_back(frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (std::exception& e) {
|
} catch (std::exception& e) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <net_base.h>
|
#include <net_base.h>
|
||||||
#include <of_util.h>
|
#include <of_util.h>
|
||||||
#include <queue>
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <util.h>
|
#include <util.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -10,7 +10,7 @@ using namespace ofen;
|
|||||||
struct ClientCache {
|
struct ClientCache {
|
||||||
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
std::shared_ptr<asio::ip::tcp::socket> socket_;
|
||||||
CMutBuffer buffer_{};
|
CMutBuffer buffer_{};
|
||||||
std::array<char, 1024> tmp_buf_{};
|
std::array<char, g_BuffSize> tmp_buf_{};
|
||||||
std::string task_{};
|
std::string task_{};
|
||||||
std::string time_{};
|
std::string time_{};
|
||||||
FrameType cur_type_{TYPE_DEFAULT};
|
FrameType cur_type_{TYPE_DEFAULT};
|
||||||
@ -36,7 +36,6 @@ private:
|
|||||||
void get_client_list(CFrameBuffer** buf);
|
void get_client_list(CFrameBuffer** buf);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool push_frame(CFrameBuffer* buf);
|
|
||||||
void handle_frame();
|
void handle_frame();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -57,7 +56,7 @@ private:
|
|||||||
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
|
std::map<std::string, std::shared_ptr<ClientCache>> client_map_;
|
||||||
std::map<std::string, std::thread> client_threads_;
|
std::map<std::string, std::thread> client_threads_;
|
||||||
std::mutex cli_mut_;
|
std::mutex cli_mut_;
|
||||||
std::queue<CFrameBuffer*> cache_;
|
std::list<CFrameBuffer*> cache_;
|
||||||
std::mutex buf_mut_;
|
std::mutex buf_mut_;
|
||||||
std::shared_ptr<CThreadPool> handle_pool_;
|
std::shared_ptr<CThreadPool> handle_pool_;
|
||||||
std::string server_ip_;
|
std::string server_ip_;
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
std::shared_ptr<spdlog::logger> get_logger(const std::string& mark, const std::string& log_file)
|
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>();
|
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");
|
file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e][%l]: %v");
|
||||||
console_sink->set_color(spdlog::level::level_enum::info, 0xFFFF);
|
console_sink->set_color(spdlog::level::level_enum::info, 0xFFFF);
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
constexpr int g_BuffSize = 102400;
|
||||||
enum FrameType : int16_t {
|
enum FrameType : int16_t {
|
||||||
TYPE_DEFAULT = 0,
|
TYPE_DEFAULT = 0,
|
||||||
TYPE_GET_LIST,
|
TYPE_GET_LIST,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user