86 lines
2.7 KiB
C++
86 lines
2.7 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"
|
|
|
|
|
|
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();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
public:
|
|
static ThreadMain* getInstance() {
|
|
static ThreadMain instance;
|
|
return &instance;
|
|
}
|
|
void init() {
|
|
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);
|
|
}
|
|
};
|
|
|
|
#endif |