Distribution_Service/Server/ThreadMain/MainThread.h
2026-03-24 18:14:23 +08:00

151 lines
5.1 KiB
C++

#ifndef DISTRIBUTION_SERVICE_MAIN_THREAD_H
#define DISTRIBUTION_SERVICE_MAIN_THREAD_H
#include "CCThreadPool.h"
#include "../Configuration/Config.h"
#include "Configuration/BS_Log.h"
#include "PortService/Routing.h"
#include "ConnectionService.h"
#include "Task/TaskInfo.h"
#include "TaskModel/TaskExecutor.h"
class ThreadMain {
CTL::ThreadPool m_threadPool;
public:
int MaxPool = 1024;
int CorePool = 60;
int KeepAliveTime = 1000 * 10;
private:
ThreadMain() {
m_threadPool.InitStart(CorePool, MaxPool, KeepAliveTime);
}
~ThreadMain() {
BS_Log::Flush();
BS_Log::Stop();
TaskExecutor::ReleasePool();
TaskInfo::thread_pool.Stop();
}
void orderRun() {
const auto Setting = Config::getConfig();
CTL::InetAddress address(Setting->IP);
{
std::unique_lock<std::shared_mutex> lock(Config::m_mutex_udp);
Setting->m_socket_udp = new CTL::DatagramSocket(Setting->OrderPort,address);
}
BS_Log::Log("OrderRun Start");
while (!Setting->m_socket_udp->isClosed()) {
if (Setting->m_socket_udp->available()) {
CTL::DatagramPacket packet;
Setting->m_socket_udp->receive(packet);
CCHostInfo hostInfo{};
hostInfo.IPAddress = packet.getAddress().getHostAddress();
hostInfo.Port = packet.getPort();
DataPacket packet_t(packet.getData(), packet.getLength(),&hostInfo);
m_threadPool.AddTask(Routing::RoutingFunction, packet_t);
}
else {
CTL::Thread::SleepMS(1);
}
}
{
std::unique_lock<std::shared_mutex> lock(Config::m_mutex_udp);
delete Setting->m_socket_udp;
Setting->m_socket_udp = nullptr;
}
BS_Log::Warning("orderRun End");
}
void ConfigTool() {
const auto Setting = Config::getConfig();
#ifdef _WIN32
const CTL::InetAddress address(Setting->ServerIP);
#else
const CTL::InetAddress address = CTL::InetAddress::getAnyAddress();
#endif
{
std::unique_lock<std::shared_mutex> lock(Config::m_mutex_udp_tool);
Setting->m_socket_udp_tool = new CTL::DatagramSocket(Setting->ConfigPort,address);
}
BS_Log::Log("ConfigTool Start");
bool F = Setting->m_socket_udp_tool->setSockOpt(BROADCAST);
while (!Setting->m_socket_udp_tool->isClosed() && F) {
if (Setting->m_socket_udp_tool->available()) {
CTL::DatagramPacket packet;
Setting->m_socket_udp_tool->receive(packet);
CCHostInfo hostInfo{};
hostInfo.IPAddress = packet.getAddress().getHostAddress();
hostInfo.Port = packet.getPort();
DataPacket packet_t(packet.getData(), packet.getLength(),&hostInfo);
m_threadPool.AddTask(Routing::RoutingFunction, packet_t);
}
else {
CTL::Thread::SleepMS(1);
}
}
{
std::unique_lock<std::shared_mutex> lock(Config::m_mutex_udp_tool);
delete Setting->m_socket_udp_tool;
Setting->m_socket_udp_tool = nullptr;
}
BS_Log::Warning("ConfigTool End");
}
static void ConnectService() {
CTL::Thread::SleepMS(3000);
const auto Setting = Config::getConfig();
const auto sock = ConnectionService::getSocket();
bool IsConn = true;
while (Setting->Flag) {
if (sock) {
const auto wsUrl = CTL::String::format("ws://{}:{}/Order",
Setting->ServerIP.c_str(),Setting->ServerPort);
Setting->isConnect = sock->Connect(wsUrl);
if (Setting->isConnect) {
BS_Log::Log("ConnectService OK");
sock->Running();
IsConn = true;
}
else {
if (IsConn) {
BS_Log::Log("ConnectService Failure");
IsConn = false;
}
}
}
CTL::Thread::SleepMS(3 * 1000);
}
}
static bool CloseFun() {
CTL::Application::StopDaemon();
CTL::Application::ReleaseLock();
std::terminate();
return true;
}
public:
static ThreadMain* getInstance() {
static ThreadMain instance;
return &instance;
}
void init() {
const auto Setting = Config::getConfig();
Setting->Flag = true;
BS_Log::Init();
Config::Init();
TaskExecutor::InitPool();
TaskInfo::thread_pool.InitStart(40, 255, 1000);
try
{
ConnectionService::init();
CC::SetCloseFun(CloseFun);
m_threadPool.AddTask(&ThreadMain::orderRun,this);
m_threadPool.AddTask(&ThreadMain::ConfigTool,this);
m_threadPool.AddTask(ThreadMain::ConnectService);
m_threadPool.AddTask(TaskInfo::InitSocket);
}
catch (CCException& e)
{
BS_Log::Error("ThreadMain init: {}",e.what());
}
}
};
#endif