153 lines
4.4 KiB
C
Raw Normal View History

2026-03-28 10:49:55 +08:00
#ifndef TCPSERVERPROTOCOL_H
#define TCPSERVERPROTOCOL_H
#include <functional>
#include <vector>
#include <string>
#include <memory>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include "IYTCPServer.h"
#include "ProtocolCommon.h"
/**
* @brief TCP服务器协议封装类
* 根据README.md文档实现工件孔定位TCP/IP通信协议
*
* 协议特点:
* - 服务端模式,等待客户端连接
* - JSON格式数据交换
* - 支持start_detection指令
* - 返回检测结果的3D坐标点数组
*/
class TCPServerProtocol
{
public:
/**
* @brief 检测结果结构体
*/
struct DetectionResultData {
int code = 0; // 响应码:0-成功,其他-错误
bool success = true; // 是否成功
QString message = "检测成功"; // 响应消息
qint64 timestamp = 0; // 时间戳(毫秒)
std::vector<std::vector<QJsonObject>> result; // 检测结果:孔位的3D坐标数组
};
/**
* @brief 连接状态枚举(使用公共状态定义)
*/
using TCPStatus = ConnectionStatus;
/**
* @brief 检测结果回调函数类型
* @param success 是否成功触发检测
* @param cameraIndex 相机索引(从开始检测指令中解析,-1表示所有相机)
* @param timestamp 请求时间戳
*/
using DetectionTriggerCallback = std::function<bool(bool success, int cameraIndex, qint64 timestamp)>;
public:
TCPServerProtocol();
~TCPServerProtocol();
/**
* @brief 初始化TCP服务器
* @param port TCP端口号,默认5020
* @return 0-成功,其他-错误码
*/
int Initialize(uint16_t port = 5020);
/**
* @brief 反初始化,停止服务
*/
void Deinitialize();
/**
* @brief 发送检测结果给客户端
* @param result 检测结果数据
* @param pClient 目标客户端,如果为nullptr则发送给所有客户端
* @return 0-成功,其他-错误码
*/
int SendDetectionResult(const QJsonObject& result, const TCPClient* pClient = nullptr);
/**
* @brief 获取当前连接状态
* @return 当前状态
*/
TCPStatus GetConnectionStatus() const;
/**
* @brief 设置连接状态回调
* @param callback 回调函数
*/
void SetConnectionCallback(const ConnectionCallback& callback);
/**
* @brief 设置检测触发回调
* @param callback 回调函数
*/
void SetDetectionTriggerCallback(const DetectionTriggerCallback& callback);
/**
* @brief 获取服务运行状态
* @return true-运行中,false-已停止
*/
bool IsRunning() const;
private:
/**
* @brief TCP客户端连接事件处理
* @param pClient 客户端对象
* @param eventType 事件类型
*/
void OnTCPEvent(const TCPClient* pClient, TCPServerEventType eventType);
/**
* @brief TCP数据接收处理
* @param pClient 客户端对象
* @param pData 数据指针
* @param nLen 数据长度
*/
void OnTCPDataReceived(const TCPClient* pClient, const char* pData, unsigned int nLen);
/**
* @brief 解析JSON命令
* @param pClient 客户端对象
* @param jsonData JSON数据
*/
void ParseJSONCommand(const TCPClient* pClient, const QJsonObject& jsonData);
/**
* @brief 处理开始检测命令
* @param pClient 客户端对象
* @param timestamp 时间戳
*/
void HandleStartDetectionCommand(const TCPClient* pClient, qint64 timestamp);
/**
* @brief 发送错误响应
* @param pClient 客户端对象
* @param code 错误码
* @param message 错误消息
* @param timestamp 时间戳
*/
void SendErrorResponse(const TCPClient* pClient, int code, const QString& message, qint64 timestamp);
private:
// TCP服务器相关
IYTCPServer* m_pTCPServer; // TCP服务器实例
bool m_bServerRunning; // 服务器运行状态
uint16_t m_nPort; // TCP端口
// 连接状态
TCPStatus m_connectionStatus; // TCP连接状态
// 回调函数
ConnectionCallback m_connectionCallback; // 连接状态回调
DetectionTriggerCallback m_detectionTriggerCallback; // 检测触发回调
};
#endif // TCPSERVERPROTOCOL_H