Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchao412 committed Sep 5, 2023
1 parent 3855094 commit 91bf487
Show file tree
Hide file tree
Showing 22 changed files with 153 additions and 75 deletions.
2 changes: 0 additions & 2 deletions Common/ThreadQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class CMessageQueue
while (_queue.empty())
{
_condition.wait(lock);

}
//注意这一段必须放在if语句中,因为lock的生命域仅仅在if大括号内
msg = std::move(_queue.front());
Expand All @@ -46,7 +45,6 @@ class CMessageQueue
if (_queue.empty())
return false;


msg = std::move(_queue.front());
_queue.pop();
return true;
Expand Down
4 changes: 2 additions & 2 deletions Common/VersionConfig.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//VersionConfig.h.in
#define V_BUILD_TIME "2023-09-04_10:34:07"
#define V_GIT_INFO "master_v3.0.0-186-g1bed1b52"
#define V_BUILD_TIME "2023-08-01_17:54:37"
#define V_GIT_INFO "master_v3.0.0-168-gda97086a"
2 changes: 1 addition & 1 deletion Common/consoleoutput.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef PSS_CONSOLE_OUTPUT_H
#ifndef PSS_CONSOLE_OUTPUT_H
#define PSS_CONSOLE_OUTPUT_H

//屏幕输出管控
Expand Down
6 changes: 3 additions & 3 deletions Common/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ enum class EM_SESSION_STATE
class CConfigNetIO
{
public:
std::string ip_;
std::string ip_ = "";
io_port_type port_ = 0;
std::string protocol_type_ = "TCP";
unsigned int packet_parse_id_ = 0;
Expand Down Expand Up @@ -269,7 +269,7 @@ inline vector<std::string> string_split(const string& srcStr, const string& deli
vector<string> vec;
string strtmp = srcStr;
nPos = strtmp.find(delim.c_str());
while(-1 != nPos)
while(string::npos != nPos)
{
string temp = strtmp.substr(0, nPos);
vec.push_back(temp);
Expand Down Expand Up @@ -298,7 +298,7 @@ inline void bind_thread_to_cpu(std::thread* logic_thread)
int rc =pthread_setaffinity_np(logic_thread->native_handle(), sizeof(cpu_set_t), &cpuset);
if (rc != 0)
{
PSS_LOGGER_ERROR("[bind_thread_to_cpu]Error calling pthread_setaffinity_np:{}",rc);
PSS_LOGGER_ERROR("[bind_thread_to_cpu]Error calling pthread_setaffinity_np:{}",rc);
}
#else
auto mask = SetThreadAffinityMask(logic_thread->native_handle(), (cpuidx++) % cpunum);
Expand Down
2 changes: 1 addition & 1 deletion PSS_ASIO/Common/IoContextPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void CIoContextPool::run()
threads.push_back(thread);

//如果配置了CPU绑定关系
if (App_ServerConfig::instance()->get_config_workthread().logic_thread_bind_cpu != 0)
if (App_ServerConfig::instance()->get_config_workthread().logic_thread_bind_cpu_ != 0)
{
bind_thread_to_cpu(thread.get());
}
Expand Down
1 change: 0 additions & 1 deletion PSS_ASIO/Common/NetSvrManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ CNetSvrManager::CNetSvrManager()

CNetSvrManager::~CNetSvrManager()
{
this->close_all_service();
}

void CNetSvrManager::start_default_service()
Expand Down
2 changes: 1 addition & 1 deletion PSS_ASIO/Common/serverconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool CServerConfig::read_server_config_file(const std::string& file_name)
config_work_thread_.client_connect_timeout_ = config_work_thread["client connect timeout"];
config_work_thread_.linux_daemonize_ = config_work_thread["linux daemonize"];
config_work_thread_.io_send_time_check_ = config_work_thread["IO send data check"];
config_work_thread_.logic_thread_bind_cpu = config_work_thread["logic thread bind cpu"];
config_work_thread_.logic_thread_bind_cpu_ = config_work_thread["logic thread bind cpu"];

for (auto packet_parse : json_config["packet parse library"])
{
Expand Down
2 changes: 1 addition & 1 deletion PSS_ASIO/Common/serverconfigtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CConfigWorkThread
int s2s_timeout_seconds_ = 60;
int client_connect_timeout_ = 0;
int io_send_time_check_ = 0;
int logic_thread_bind_cpu = 0; //0为不绑定CPU,1为绑定
int logic_thread_bind_cpu_ = 0; //0为不绑定CPU,1为绑定
};

class CConfigPacketParseInfo
Expand Down
50 changes: 25 additions & 25 deletions PSS_ASIO/Message/Iobridge.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#pragma once

//ÌṩIOÇŽӷþÎñ×¢²á
//add by freeeyes

#include "IotoIo.h"
#include "ModuleLogic.h"

class CIoBridge : public IIoBridge
{
public:
virtual ~CIoBridge() = default;

bool add_session_io_mapping(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE from_io_type, const _ClientIPInfo& to_io, EM_CONNECT_IO_TYPE to_io_type, ENUM_IO_BRIDGE_TYPE bridge_type = ENUM_IO_BRIDGE_TYPE::IO_BRIDGE_BATH) final;
bool delete_session_io_mapping(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE from_io_type) final;

bool regedit_bridge_session_id(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE io_type, uint32 session_id);
void unregedit_bridge_session_id(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE io_type);
uint32 get_to_session_id(uint32 session_id, const _ClientIPInfo& from_io);

private:
CIotoIo iotoio_;
};

using App_IoBridge = PSS_singleton<CIoBridge>;
#pragma once

//提供IO桥接服务注册
//add by freeeyes

#include "IotoIo.h"
#include "ModuleLogic.h"

class CIoBridge : public IIoBridge
{
public:
virtual ~CIoBridge() = default;

bool add_session_io_mapping(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE from_io_type, const _ClientIPInfo& to_io, EM_CONNECT_IO_TYPE to_io_type, ENUM_IO_BRIDGE_TYPE bridge_type = ENUM_IO_BRIDGE_TYPE::IO_BRIDGE_BATH) final;
bool delete_session_io_mapping(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE from_io_type) final;

bool regedit_bridge_session_id(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE io_type, uint32 session_id);
void unregedit_bridge_session_id(const _ClientIPInfo& from_io, EM_CONNECT_IO_TYPE io_type);
uint32 get_to_session_id(uint32 session_id, const _ClientIPInfo& from_io);

private:
CIotoIo iotoio_;
};

using App_IoBridge = PSS_singleton<CIoBridge>;
6 changes: 3 additions & 3 deletions PSS_ASIO/Message/LoadModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class CLoadModule
void delete_module_name_list(const string& module_name);

using hashmapModuleList = unordered_map<string, shared_ptr<_ModuleInfo>>;
hashmapModuleList module_list_;
vector<string> module_name_list_; //当前插件名称列表
hashmapModuleList module_list_;
vector<string> module_name_list_; //当前插件名称列表

command_to_module_function command_to_module_function_;
plugin_name_to_module_run plugin_name_to_module_run_;
ISessionService* session_service_;
ISessionService* session_service_ = nullptr;
};
4 changes: 2 additions & 2 deletions PSS_ASIO/Message/SessionInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void CSessionInterface::check_session_io_timeout(uint32 connect_timeout, vector<
std::chrono::duration<double, std::ratio<1, 1>> elapsed = check_connect_time_ - session_io.second.session_->get_recv_time();
if (elapsed.count() >= connect_timeout)
{
PSS_LOGGER_INFO("[CSessionInterface::check_session_io_timeout]elapsed={0}.", elapsed.count());
PSS_LOGGER_INFO("[CSessionInterface::check_session_io_timeout]connectid={},elapsed={}.",session_io.first, elapsed.count());

CSessionIO_Cancel session_cancel;
session_cancel.session_id_ = session_io.first;
Expand All @@ -88,7 +88,7 @@ void CSessionInterface::check_session_io_timeout(uint32 connect_timeout, vector<
std::chrono::duration<double, std::ratio<1, 1>> elapsed = check_connect_time_ - session_io.second.session_->get_recv_time(session_io.first);
if (elapsed.count() >= connect_timeout)
{
PSS_LOGGER_INFO("[CSessionInterface::check_session_io_timeout]elapsed={0}.", elapsed.count());
PSS_LOGGER_INFO("[CSessionInterface::check_session_io_timeout]connectid={},elapsed={}.",session_io.first, elapsed.count());

CSessionIO_Cancel session_cancel;
session_cancel.session_id_ = session_io.first;
Expand Down
2 changes: 1 addition & 1 deletion PSS_ASIO/Message/SessionService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void CSessionService::regedit_bridge_session_id(uint32 connect_id)
{
if(connect_id == 0)
{
PSS_LOGGER_INFO("[CSessionService::regedit_bridge_session_id]server id must over 0, regedit_bridge_session_id fail.");
PSS_LOGGER_WARN("[CSessionService::regedit_bridge_session_id]server id must over 0, regedit_bridge_session_id fail.");
return;
}
else
Expand Down
11 changes: 7 additions & 4 deletions PSS_ASIO/TcpSession/TcpClientSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ bool CTcpClientSession::start(const CConnect_IO_Info& io_info)

//异步链接
tcp::resolver::results_type::iterator endpoint_iter;
socket_.async_connect(end_point, std::bind(&CTcpClientSession::handle_connect,
this, std::placeholders::_1, endpoint_iter));
socket_.async_connect(end_point, std::bind(&CTcpClientSession::handle_connect, this, std::placeholders::_1, endpoint_iter));
return true;
}

Expand Down Expand Up @@ -337,8 +336,12 @@ void CTcpClientSession::handle_connect(const asio::error_code& ec, tcp::resolver
local_ip_.m_strClientIP = socket_.local_endpoint().address().to_string();
local_ip_.m_u2Port = socket_.local_endpoint().port();

PSS_LOGGER_DEBUG("[CTcpClientSession::start]remote({0}:{1})", remote_ip_.m_strClientIP, remote_ip_.m_u2Port);
PSS_LOGGER_DEBUG("[CTcpClientSession::start]local({0}:{1})", local_ip_.m_strClientIP, local_ip_.m_u2Port);
PSS_LOGGER_INFO("[CTcpClientSession::handle_connect]connect_id:{} remote[{}:{}] local[{}:{}]",
connect_id_,
remote_ip_.m_strClientIP,
remote_ip_.m_u2Port,
local_ip_.m_strClientIP,
local_ip_.m_u2Port);

packet_parse_interface_->packet_connect_ptr_(connect_id_, remote_ip_, local_ip_, io_type_, App_IoBridge::instance());

Expand Down
7 changes: 5 additions & 2 deletions PSS_ASIO/TcpSession/TcpSSLClientSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ bool CTcpSSLClientSession::start(const CConnect_IO_Info& io_info)
}

ssl_socket_.set_verify_mode(asio::ssl::verify_peer);
ssl_socket_.set_verify_callback(
std::bind(&CTcpSSLClientSession::verify_certificate, this, _1, _2));
ssl_socket_.set_verify_callback(std::bind(&CTcpSSLClientSession::verify_certificate, this, _1, _2));

//建立连接(异步)
tcp::resolver resolver(*io_context_);
Expand Down Expand Up @@ -67,6 +66,10 @@ bool CTcpSSLClientSession::start(const CConnect_IO_Info& io_info)

void CTcpSSLClientSession::close(uint32 connect_id)
{
if(!socket_.is_open())
{
return;
}
auto self(shared_from_this());

auto recv_data_size = recv_data_size_;
Expand Down
4 changes: 4 additions & 0 deletions PSS_ASIO/TcpSession/TcpSSLSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ _ClientIPInfo CTcpSSLSession::get_remote_ip(uint32 connect_id)

void CTcpSSLSession::close(uint32 connect_id)
{
if(!socket_.is_open())
{
return;
}
auto self(shared_from_this());

auto recv_data_size = recv_data_size_;
Expand Down
32 changes: 24 additions & 8 deletions PSS_ASIO/TcpSession/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CTcpServer::CTcpServer(const CreateIoContextCallbackFunc callback, const std::st
{
try
{
accept_run_state_ = true;
callback_ = callback;
asio::io_context* iocontext = callback_();
acceptor_ = std::make_shared<tcp::acceptor>(*iocontext, tcp::endpoint(asio::ip::address_v4::from_string(server_ip), port));
Expand All @@ -18,20 +19,22 @@ CTcpServer::CTcpServer(const CreateIoContextCallbackFunc callback, const std::st
}
catch (std::system_error const& ex)
{
PSS_LOGGER_INFO("[CTcpServer::do_accept]({0}:{1}) accept error {2}.", server_ip, port, ex.what());
PSS_LOGGER_WARN("[CTcpServer::do_accept]({0}:{1}) accept error {2}.", server_ip, port, ex.what());
}
}

void CTcpServer::close() const
void CTcpServer::close()
{
if (nullptr != acceptor_)
{
acceptor_->close();
}
PSS_LOGGER_INFO("[CTcpServer::close]stop tcp server[{0}:{1}]", server_ip_, server_port_);
accept_run_state_ = false;
}

void CTcpServer::do_accept()
{
if (!accept_run_state_)
{
return;
}
acceptor_->async_accept(
[this](std::error_code ec, tcp::socket socket)
{
Expand All @@ -53,10 +56,23 @@ void CTcpServer::do_accept()
PSS_LOGGER_WARN("[CTcpServer::do_accept]close tcp server[{}:{}], error={}",server_ip_,server_port_, ex.what());
}
});

if (!accept_run_state_)
{
if (nullptr != acceptor_)
{
acceptor_->close();
}
return;
}
}

void CTcpServer::send_accept_listen_fail(std::error_code ec) const
void CTcpServer::send_accept_listen_fail(std::error_code ec)
{
if (!accept_run_state_)
{
return;
}
//发送监听失败消息
App_WorkThreadLogic::instance()->add_frame_events(LOGIC_LISTEN_SERVER_ERROR,
0,
Expand All @@ -65,7 +81,7 @@ void CTcpServer::send_accept_listen_fail(std::error_code ec) const
EM_CONNECT_IO_TYPE::CONNECT_IO_TCP);

//监听失败,查看错误信息
PSS_LOGGER_INFO("[CTcpServer::do_accept]({0}{1})accept error:{2}",
PSS_LOGGER_INFO("[CTcpServer::do_accept]({0}:{1})accept error:{2}",
acceptor_->local_endpoint().address().to_string(),
acceptor_->local_endpoint().port(),
ec.message());
Expand Down
7 changes: 4 additions & 3 deletions PSS_ASIO/TcpSession/TcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ class CTcpServer
public:
CTcpServer(const CreateIoContextCallbackFunc callback, const std::string& server_ip, io_port_type port, uint32 packet_parse_id, uint32 max_recv_size);

void close() const;
void close();

private:
void do_accept();

void send_accept_listen_fail(std::error_code ec) const;
void send_accept_listen_fail(std::error_code ec);

std::shared_ptr<tcp::acceptor> acceptor_;
uint32 packet_parse_id_ = 0;
uint32 max_recv_size_ = 0;
CreateIoContextCallbackFunc callback_;

bool accept_run_state_;

string server_ip_;
io_port_type server_port_;
};
Expand Down
5 changes: 4 additions & 1 deletion PSS_ASIO/TcpSession/TcpSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ _ClientIPInfo CTcpSession::get_remote_ip(uint32 connect_id)

void CTcpSession::close(uint32 connect_id)
{
if(!socket_.is_open())
{
return;
}
auto self(shared_from_this());

auto recv_data_size = recv_data_size_;
Expand Down Expand Up @@ -99,7 +103,6 @@ void CTcpSession::close(uint32 connect_id)

App_WorkThreadLogic::instance()->delete_thread_session(connect_id, self);
});

}

void CTcpSession::do_read()
Expand Down
4 changes: 4 additions & 0 deletions PSS_ASIO/UdpSession/KcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ void CKcpServer::do_receive_from(std::error_code ec, std::size_t length)

void CKcpServer::close(uint32 connect_id)
{
if(!socket_.is_open())
{
return;
}
auto self(shared_from_this());

io_context_->dispatch([self, connect_id]()
Expand Down
Loading

0 comments on commit 91bf487

Please sign in to comment.