72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
#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;
|
|
int timeout = 30000; // 超时时间(ms)
|
|
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; }
|
|
|
|
// 控制
|
|
void setAutoReconnect(bool enable);
|
|
bool isAutoReconnect() const { return m_autoReconnect; }
|
|
|
|
signals:
|
|
void connected();
|
|
void disconnected();
|
|
void dataReceived(const QByteArray& data);
|
|
void errorOccurred(const QString& error);
|
|
|
|
protected:
|
|
// 公共方法
|
|
void startReconnectTimer();
|
|
void stopReconnectTimer();
|
|
void reconnect();
|
|
|
|
// 工具方法
|
|
QByteArray generateMessageId() const;
|
|
qint64 currentTimestamp() const;
|
|
|
|
// 成员变量
|
|
ChannelConfig m_config;
|
|
bool m_autoReconnect = true;
|
|
|
|
private slots:
|
|
void onReconnectTimeout();
|
|
|
|
private:
|
|
QTimer* m_reconnectTimer = nullptr;
|
|
int m_reconnectCount = 0;
|
|
QMutex m_mutex;
|
|
};
|