138 lines
4.1 KiB
C
138 lines
4.1 KiB
C
|
|
#ifndef TCPSERVERPROTOCOL_H
|
|||
|
|
#define TCPSERVERPROTOCOL_H
|
|||
|
|
|
|||
|
|
#include <atomic>
|
|||
|
|
#include <functional>
|
|||
|
|
#include <map>
|
|||
|
|
#include <mutex>
|
|||
|
|
#include <set>
|
|||
|
|
#include <vector>
|
|||
|
|
#include <string>
|
|||
|
|
#include <memory>
|
|||
|
|
#include <QByteArray>
|
|||
|
|
#include "IYTCPServer.h"
|
|||
|
|
#include "ProtocolCommon.h"
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief TCP服务器协议封装类
|
|||
|
|
* 根据协议文档实现定子工件定位 TCP/IP 通信协议
|
|||
|
|
*
|
|||
|
|
* 协议特点:
|
|||
|
|
* - 服务端模式,等待客户端连接
|
|||
|
|
* - 请求和结果均使用简单文本帧
|
|||
|
|
* - 字段使用 '_' 分隔,帧尾使用 "\r\n"
|
|||
|
|
*/
|
|||
|
|
class TCPServerProtocol
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
/**
|
|||
|
|
* @brief 连接状态枚举(使用公共状态定义)
|
|||
|
|
*/
|
|||
|
|
using TCPStatus = ConnectionStatus;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 检测结果回调函数类型
|
|||
|
|
* @param success 是否成功触发检测
|
|||
|
|
* @param cameraIndex 相机索引(从开始检测指令中解析,-1表示所有相机)
|
|||
|
|
* @param timestamp 请求时间戳
|
|||
|
|
*/
|
|||
|
|
using DetectionTriggerCallback = std::function<bool(bool success, int cameraIndex, qint64 timestamp)>;
|
|||
|
|
using BinDetectionTriggerCallback = std::function<bool(int cameraIndex, qint64 timestamp)>;
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
TCPServerProtocol();
|
|||
|
|
~TCPServerProtocol();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 初始化TCP服务器
|
|||
|
|
* @param port TCP/IP 协议端口号,默认7800
|
|||
|
|
* @return 0-成功,其他-错误码
|
|||
|
|
*/
|
|||
|
|
int Initialize(uint16_t port = 7800);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 反初始化,停止服务
|
|||
|
|
*/
|
|||
|
|
void Deinitialize();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 发送简单文本帧给客户端,函数会自动补 "\r\n"
|
|||
|
|
*/
|
|||
|
|
int SendTextFrame(const QByteArray& frame, 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);
|
|||
|
|
void SetBinDetectionTriggerCallback(const BinDetectionTriggerCallback& 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);
|
|||
|
|
|
|||
|
|
void ParseTextCommand(const TCPClient* pClient, const QByteArray& frame);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @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服务器实例
|
|||
|
|
std::atomic<bool> m_bServerRunning; // 服务器运行状态
|
|||
|
|
uint16_t m_nPort; // TCP端口
|
|||
|
|
|
|||
|
|
// 连接状态
|
|||
|
|
std::atomic<TCPStatus> m_connectionStatus; // TCP连接状态
|
|||
|
|
|
|||
|
|
// 回调函数
|
|||
|
|
ConnectionCallback m_connectionCallback; // 连接状态回调
|
|||
|
|
DetectionTriggerCallback m_detectionTriggerCallback; // 检测触发回调
|
|||
|
|
BinDetectionTriggerCallback m_binDetectionTriggerCallback;
|
|||
|
|
|
|||
|
|
// TCP receive callbacks may run concurrently for different clients.
|
|||
|
|
std::mutex m_sendMutex;
|
|||
|
|
std::mutex m_connectionEventMutex;
|
|||
|
|
std::mutex m_clientBuffersMutex;
|
|||
|
|
std::set<const TCPClient*> m_connectedClients;
|
|||
|
|
std::map<const TCPClient*, QByteArray> m_clientBuffers;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif // TCPSERVERPROTOCOL_H
|