DiagramDesigner/diagramCommunication/include/baseChannel.h

74 lines
1.9 KiB
C
Raw Normal View History

2025-12-12 17:46:37 +08:00
#pragma once
#include <QObject>
#include <QUrl>
#include <QTimer>
#include <QMutex>
#include <QMap>
#include "export.hpp"
class DIAGRAM_DESIGNER_PUBLIC BaseChannel : public QObject
{
Q_OBJECT
public:
struct ChannelConfig {
QString channelId;
QUrl endpoint;
2025-12-25 09:03:35 +08:00
int timeout = 10000; // 超时时间(ms)
2025-12-12 17:46:37 +08:00
int reconnectInterval = 5000; // 重连间隔(ms)
int maxRetries = 3; // 最大重试次数
QVariantMap params; // 自定义参数
};
explicit BaseChannel(const ChannelConfig& config, QObject* parent = nullptr);
virtual ~BaseChannel();
// 连接管理
virtual bool connect() = 0;
virtual bool disconnect() = 0;
virtual bool isConnected() const = 0;
// 数据发送
virtual bool send(const QByteArray& data) = 0;
// 信息获取
QString channelId() const { return m_config.channelId; }
ChannelConfig config() const { return m_config; }
QUrl endpoint() const { return m_config.endpoint; }
2025-12-19 18:28:13 +08:00
QString sessionId() const {return m_sessionId;}
2025-12-12 17:46:37 +08:00
// 控制
void setAutoReconnect(bool enable);
bool isAutoReconnect() const { return m_autoReconnect; }
signals:
2025-12-19 18:28:13 +08:00
void connected(const QString& = "");
void disconnected(const QString& = "");
void dataReceived(const QByteArray& data,const QString& = "");
void errorOccurred(const QString& error,const QString& = "");
2025-12-12 17:46:37 +08:00
protected:
// 公共方法
void startReconnectTimer();
void stopReconnectTimer();
void reconnect();
// 工具方法
QByteArray generateMessageId() const;
qint64 currentTimestamp() const;
// 成员变量
ChannelConfig m_config;
bool m_autoReconnect = true;
2025-12-19 18:28:13 +08:00
QString m_sessionId; //会话idhttp无
2025-12-12 17:46:37 +08:00
private slots:
void onReconnectTimeout();
private:
QTimer* m_reconnectTimer = nullptr;
int m_reconnectCount = 0;
QMutex m_mutex;
};