Distribution_Service/Server/ThreadMain/MainThread.h

86 lines
2.7 KiB
C
Raw Normal View History

2025-11-11 18:09:51 +08:00
#ifndef DISTRIBUTION_SERVICE_MAIN_THREAD_H
#define DISTRIBUTION_SERVICE_MAIN_THREAD_H
#include "CCThreadPool.h"
#include "../Configuration/Config.h"
2025-12-03 18:08:23 +08:00
#include "Configuration/BS_Log.h"
#include "PortService/Routing.h"
#include "ConnectionService.h"
2025-11-11 18:09:51 +08:00
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() {
2025-12-03 18:08:23 +08:00
BS_Log::Flush();
BS_Log::Stop();
2025-11-11 18:09:51 +08:00
}
2025-12-03 18:08:23 +08:00
void orderRun() {
const auto Setting = Config::getConfig();
CTL::InetAddress address(Setting->IP);
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);
}
}
BS_Log::Warning("orderRun End");
}
static void ConnectService() {
const auto Setting = Config::getConfig();
const auto sock = ConnectionService::getSocket();
bool isConnect = true;
while (Setting->Flag) {
if (sock) {
const auto wsUrl = CTL::String::format("ws://{}:{}/Order",
Setting->ServerIP.c_str(),Setting->ServerPort);
bool F = sock->Connect(wsUrl);
if (F) {
BS_Log::Log("ConnectService OK");
sock->Running();
isConnect = true;
}
else {
if (isConnect) {
BS_Log::Log("ConnectService Failure");
isConnect = false;
}
}
}
CTL::Thread::SleepMS(3 * 1000);
}
2025-11-11 18:09:51 +08:00
}
public:
static ThreadMain* getInstance() {
static ThreadMain instance;
return &instance;
}
void init() {
2025-12-03 18:08:23 +08:00
const auto Setting = Config::getConfig();
Setting->Flag = true;
BS_Log::Init();
Config::Init();
ConnectionService::init();
m_threadPool.AddTask(&ThreadMain::orderRun,this);
m_threadPool.AddTask(ThreadMain::ConnectService);
2025-11-11 18:09:51 +08:00
}
};
#endif