DiagramDesigner/diagramCommunication/include/baseChannel.h

74 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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