65 lines
1.7 KiB
C++
65 lines
1.7 KiB
C++
#include "ConnectionService.h"
|
|
|
|
#include "BS_Log.h"
|
|
#include "Config.h"
|
|
#include "PortService/ByteTool.h"
|
|
#include "PortService/DataPacket.h"
|
|
#include "PortService/Routing.h"
|
|
#include "Task/TaskInfo.h"
|
|
|
|
CTL::WebSocketClient ConnectionService::m_socket;
|
|
CTL::WebSocketInfo* ConnectionService::m_info = nullptr;
|
|
CTL::ThreadPool ConnectionService::m_threadPool;
|
|
|
|
CTL::WebSocketClient * ConnectionService::getSocket() {
|
|
return &m_socket;
|
|
}
|
|
|
|
CTL::WebSocketInfo * ConnectionService::getInfo() {
|
|
return m_info;
|
|
}
|
|
|
|
void ConnectionService::init() {
|
|
m_socket.OnBind(OnOpen, OnClose,OnMessage,OnError);
|
|
m_threadPool.InitStart(80,1024,1000);
|
|
}
|
|
|
|
void ConnectionService::OnOpen(CTL::WebSocketInfo &info) {
|
|
m_info = &info;
|
|
CTL::Thread::addTask(Register);
|
|
}
|
|
|
|
void ConnectionService::Close(CTL::WebSocketInfo &info) {
|
|
m_info = nullptr;
|
|
BS_Log::Log("ConnectService Close");
|
|
TaskInfo::StopAll();
|
|
}
|
|
|
|
void ConnectionService::Register() {
|
|
const auto Setting = Config::getConfig();
|
|
CTL::Thread::SleepMS(1000);
|
|
const auto IDS = APPTool::GetBytes(Setting->ID);
|
|
CTL::ByteArray data = {0x66,0xAB,0x02,0x00,IDS[0],IDS[1],0x01,0x00};
|
|
CTL::JSONObject Json = {};
|
|
Json.put("IP", Setting->IP);
|
|
Json.put("OrderUDP",Setting->OrderPort);
|
|
data.append(Json.to_String());
|
|
m_socket.SendBinary(data);
|
|
}
|
|
|
|
void ConnectionService::OnMessage(CTL::WebSocketInfo &info) {
|
|
const auto data = info.GetBuffer();
|
|
const DataPacket packet(data,m_info, true);
|
|
m_threadPool.AddTask(Routing::RoutingFunction,packet);
|
|
}
|
|
|
|
void ConnectionService::OnClose(CTL::WebSocketInfo &info) {
|
|
Close(info);
|
|
}
|
|
|
|
void ConnectionService::OnError(CTL::WebSocketInfo &info) {
|
|
Close(info);
|
|
}
|
|
|
|
|