| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #pragma once
- #include <QObject>
- #include <QTimer> // 添加定时器头文件
- #include "httplib.h"
- #include <QMutex>
- #include <QWaitCondition> // 添加等待条件
- #include "platform.h"
- #include "NetMessageDefine.h"
- class QThreadHttpClient : public QObject
- {
- Q_OBJECT
- public:
- // 获取单例实例
- static QThreadHttpClient& instance();
- // 清空所有数据
- static void destroyInstance();
- void initHttpClient();
- bool HttpPost(const std::string& strUrl, const std::string& strRequest, std::string& strResponse);
- bool reconnect(); // 添加重连方法
- bool isConnected();
- bool reconnectNoLock();
- // 获取连接状态
- bool getReconnectStatus() const { return m_bReconnectSuccess; }
- //初始化
- void InitConnect(QObject* pCMD);
- // 等待连接(用于主线程阻塞等待)
- bool waitForConnection(int timeoutMs = 30000);
- signals:
- void signal_SignalData(std::string strUrl, std::string strRequest);
- //void signal_ConnectionStatusChanged(bool connected); // 连接状态变化信号
- //void signal_ReconnectAttempt(int attemptCount, bool success); // 重连尝试信号
- public slots:
- void slot_CMDPop(int nCmdID, OCCCOMMAND pCmdData);
- void slot_SendPost(std::string strUrl, std::string strRequest);
- void slot_ReconnectTimer(); // 定时重连槽函数
- private:
- QThreadHttpClient(QObject* parent);
- ~QThreadHttpClient();
- void AddLogJson(std::string strRequest, std::string strResponse, std::string nAPIID, bool bStatus);
- void startReconnectTimer(); // 启动重连定时器
- void stopReconnectTimer(); // 停止重连定时器
- // 禁止拷贝和赋值
- QThreadHttpClient(const QThreadHttpClient&) = delete;
- QThreadHttpClient& operator=(const QThreadHttpClient&) = delete;
- static QThreadHttpClient* g_HttpSingle;
- static QMutex m_clientMutex; // 改为 QMutex
- private:
- httplib::Client* pClient;
- QTimer* m_reconnectTimer; // 重连定时器
- bool m_bReconnectSuccess; // 重连成功标志
- int m_reconnectAttemptCount; // 重连尝试次数
- int m_maxReconnectAttempts; // 最大重连尝试次数
- QWaitCondition m_connectionCondition; // 连接等待条件
- QMutex m_connectionMutex; // 连接状态互斥锁
- };
|