Skip to content

Commit

Permalink
Support operating network service interfaces for logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchao412 committed Jul 22, 2023
1 parent 9636c93 commit 0115c72
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 170 deletions.
2 changes: 2 additions & 0 deletions Common/ISessionService.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ class ISessionService
virtual uint32 get_connect_id(uint32 server_id) = 0;

virtual void regedit_bridge_session_id(uint32 connect_id = 0) = 0;
virtual void start_single_service(const CConfigNetIO& netio) = 0;
virtual void close_single_service(const CConfigNetIO& netio) = 0;
};
1 change: 1 addition & 0 deletions Common/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class CConfigNetIO
public:
std::string ip_;
io_port_type port_ = 0;
std::string protocol_type_ = "TCP";
unsigned int packet_parse_id_ = 0;
unsigned int recv_buff_size_ = 1024;
unsigned int send_buff_size_ = 1024;
Expand Down
198 changes: 198 additions & 0 deletions PSS_ASIO/Common/NetSvrManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
#include "NetSvrManager.h"
#include <cxxabi.h>
#include <execinfo.h>

CNetSvrManager::CNetSvrManager()
{
}

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

void CNetSvrManager::start_default_service()
{
string stripport;

//加载Tcp监听
for(auto tcp_server : App_ServerConfig::instance()->get_config_tcp_list())
{
if (tcp_server.ssl_server_password_ != ""
&& tcp_server.ssl_server_pem_file_ != ""
&& tcp_server.ssl_dh_pem_file_ != "")
{
#ifdef SSL_SUPPORT
auto tcp_ssl_service = make_shared<CTcpSSLServer>(CreateIoContextFunctor,
tcp_server.ip_,
tcp_server.port_,
tcp_server.packet_parse_id_,
tcp_server.recv_buff_size_,
tcp_server.ssl_server_password_,
tcp_server.ssl_server_pem_file_,
tcp_server.ssl_dh_pem_file_);
stripport = tcp_server.ip_ + "_" + std::to_string(tcp_server.port_);
tcp_ssl_service_map_[stripport] = tcp_ssl_service;
#else
PSS_LOGGER_DEBUG("[CNetSvrManager::start_default_service]you must set SSL_SUPPORT macro on compilation options.");
#endif
}
else
{
//正常的tcp链接
auto tcp_service = make_shared<CTcpServer>(CreateIoContextFunctor,
tcp_server.ip_,
tcp_server.port_,
tcp_server.packet_parse_id_,
tcp_server.recv_buff_size_);
stripport = tcp_server.ip_ + "_" + std::to_string(tcp_server.port_);
tcp_service_map_[stripport] = tcp_service;
}
}

//加载UDP监听
for (auto udp_server : App_ServerConfig::instance()->get_config_udp_list())
{
auto udp_service = make_shared<CUdpServer>(CreateIoContextFunctor(),
udp_server.ip_,
udp_server.port_,
udp_server.packet_parse_id_,
udp_server.recv_buff_size_,
udp_server.send_buff_size_,
udp_server.em_net_type_);
udp_service->start();
stripport = udp_server.ip_ + "_" + std::to_string(udp_server.port_);
udp_service_map_[stripport] = udp_service;
}

//加载KCP监听
for (auto kcp_server : App_ServerConfig::instance()->get_config_kcp_list())
{
auto kcp_service = make_shared<CKcpServer>(CreateIoContextFunctor(),
kcp_server.ip_,
kcp_server.port_,
kcp_server.packet_parse_id_,
kcp_server.recv_buff_size_,
kcp_server.send_buff_size_);
kcp_service->start();
stripport = kcp_server.ip_ + "_" + std::to_string(kcp_server.port_);
kcp_service_map_[stripport] = kcp_service;
}

//加载tty监听
for (auto tty_server : App_ServerConfig::instance()->get_config_tty_list())
{
auto tty_service = make_shared<CTTyServer>(
tty_server.packet_parse_id_,
tty_server.recv_buff_size_,
tty_server.send_buff_size_);
tty_service->start(CreateIoContextFunctor(),
tty_server.tty_name_,
(uint16)tty_server.tty_port_,
(uint8)tty_server.char_size_,
0);
tty_service_map_[tty_server.tty_name_] = tty_service;
}

return;
}

void CNetSvrManager::close_all_service()
{
PSS_LOGGER_DEBUG("[CNetSvrManager::close_all_service]begin.");

//停止所有的TCP监听(TCP)
for (const auto& tcp_service : tcp_service_map_)
{
tcp_service.second->close();
}

#ifdef SSL_SUPPORT
//停止所有的SSL监听
for (const auto& tcp_ssl_service : tcp_ssl_service_map_)
{
tcp_ssl_service.second->close();
}
#endif

//清理所有kcp资源
for (const auto& kcp_service : kcp_service_map_)
{
kcp_service.second->close_all();
}

//清理所有udp资源
for (const auto& udp_service : udp_service_map_)
{
udp_service.second->close_all();
}

tcp_service_map_.clear();

kcp_service_map_.clear();

udp_service_map_.clear();

#ifdef SSL_SUPPORT
tcp_ssl_service_map_.clear();
#endif
PSS_LOGGER_DEBUG("[CNetSvrManager::close_all_service]end.");
}

void CNetSvrManager::start_single_service(const CConfigNetIO& netio)
{
std::lock_guard <std::mutex> lock(mutex_);
string stripport;
if("TCP" == netio.protocol_type_)
{
PSS_LOGGER_INFO("[CNetSvrManager::start_single_service]create tcp service[{}:{}]",netio.ip_,netio.port_);
auto tcp_service = make_shared<CTcpServer>(CreateIoContextFunctor,
netio.ip_,
netio.port_,
netio.packet_parse_id_,
netio.recv_buff_size_);
stripport = netio.ip_ + "_" + std::to_string(netio.port_);
tcp_service_map_[stripport] = tcp_service;
}
else
{
PSS_LOGGER_INFO("[CNetSvrManager::start_single_service]create udp service[{}:{}]",netio.ip_,netio.port_);
auto udp_service = make_shared<CUdpServer>(CreateIoContextFunctor(),
netio.ip_,
netio.port_,
netio.packet_parse_id_,
netio.recv_buff_size_,
netio.send_buff_size_,
netio.em_net_type_);
udp_service->start();
stripport = netio.ip_ + "_" + std::to_string(netio.port_);
udp_service_map_[stripport] = udp_service;
}
}

void CNetSvrManager::close_single_service(const CConfigNetIO& netio)
{
std::lock_guard <std::mutex> lock(mutex_);
string stripport = netio.ip_ + "_" + std::to_string(netio.port_);
if("TCP" == netio.protocol_type_)
{
PSS_LOGGER_INFO("[CNetSvrManager::close_single_service]close tcp service[{}:{}]",netio.ip_,netio.port_);
auto tcp = tcp_service_map_.find(stripport);
if (tcp != tcp_service_map_.end())
{
tcp->second->close();
tcp_service_map_.erase(stripport);
}
}
else
{
PSS_LOGGER_INFO("[CNetSvrManager::close_single_service]close udp service[{}:{}]",netio.ip_,netio.port_);
auto udp = udp_service_map_.find(stripport);
if (udp != udp_service_map_.end())
{
udp->second->close_all();
udp_service_map_.erase(stripport);
}
}
}

51 changes: 51 additions & 0 deletions PSS_ASIO/Common/NetSvrManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "TcpSSLServer.h"
#include "TcpServer.h"
#include "UdpServer.h"
#include "KcpServer.h"
#include "TtyServer.h"
#include "QueueService.h"
#include "serverconfig.h"
#include "CommunicationService.h"
#include "SessionService.h"
#include "IoContextPool.h"

#if PSS_PLATFORM == PLATFORM_WIN
#include <tchar.h>
#endif

class CNetSvrManager : private asio::noncopyable
{
public:
CNetSvrManager();
~CNetSvrManager();
void start_default_service();
void close_all_service();

void start_single_service(const CConfigNetIO& netio);
void close_single_service(const CConfigNetIO& netio);

private:
using hashipport2tcpserver = unordered_map<string,shared_ptr<CTcpServer>>;
hashipport2tcpserver tcp_service_map_;

using hashipport2udpserver = unordered_map<string,shared_ptr<CUdpServer>>;
hashipport2udpserver udp_service_map_;

using hashipport2kcpserver = unordered_map<string,shared_ptr<CKcpServer>>;
hashipport2kcpserver kcp_service_map_;

using hashttyname2tcpserver = unordered_map<string,shared_ptr<CTTyServer>>;
hashttyname2tcpserver tty_service_map_;

#ifdef SSL_SUPPORT
using hashipport2tcpsslserver = unordered_map<string,shared_ptr<CTcpSSLServer>>;
hashipport2tcpsslserver tcp_ssl_service_map_;
#endif

private:
std::mutex mutex_;
};

using App_NetSvrManager = PSS_singleton<CNetSvrManager>;
Loading

0 comments on commit 0115c72

Please sign in to comment.