qt/LedOK/gutil/qnetwork.h

131 lines
3.9 KiB
C
Raw Normal View History

2023-04-18 14:14:46 +08:00
#ifndef QNETWORK_H
#define QNETWORK_H
#include <QNetworkReply>
#include <QJsonDocument>
#include <QEventLoop>
#include <QTimerEvent>
extern const char *const FormBoundary;
2023-07-21 17:40:49 +08:00
QNetworkAccessManager *netAccess();
2023-04-18 14:14:46 +08:00
class NetReq : public QNetworkRequest {
public:
2023-07-21 17:40:49 +08:00
#if(QT_VERSION_MAJOR > 5)
using QNetworkRequest::QNetworkRequest;
explicit NetReq(const QString &url) : QNetworkRequest{url} {};
explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {};
#else
2023-04-23 17:01:35 +08:00
NetReq() {init();};
2023-07-24 11:25:14 +08:00
explicit NetReq(const QString &url) : QNetworkRequest{url} {init();};
2023-04-23 17:01:35 +08:00
explicit NetReq(const QUrl &url) : QNetworkRequest{url} {init();};
2023-07-24 11:25:14 +08:00
explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {init();};
2023-04-23 17:01:35 +08:00
inline void init() {
setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
2023-07-21 17:40:49 +08:00
}
2023-04-23 17:01:35 +08:00
#endif
2023-07-21 17:40:49 +08:00
using QNetworkRequest::url;
inline NetReq &url(const QUrl &url) {
setUrl(url);
return *this;
}
inline NetReq &access(QNetworkAccessManager *access) {
mAccess = access;
return *this;
}
inline NetReq &header(const QByteArray &headerName, const QByteArray &value) {
setRawHeader(headerName, value);
return *this;
}
inline NetReq &header(KnownHeaders header, const QVariant &value) {
setHeader(header, value);
return *this;
2023-04-23 17:01:35 +08:00
}
2023-04-18 14:14:46 +08:00
inline NetReq &type(const QByteArray &value) {
setRawHeader("Content-Type", value);
return *this;
}
inline NetReq &typeJson() {
setRawHeader("Content-Type", "application/json");
return *this;
}
inline NetReq &timeout(int timeout) {
setTransferTimeout(timeout);
return *this;
}
inline QNetworkReply *get() {
2023-07-21 17:40:49 +08:00
if(mAccess==0) mAccess = netAccess();
return mAccess->get(*this);
2023-04-18 14:14:46 +08:00
}
inline QNetworkReply *post(const QByteArray &data) {
2023-07-21 17:40:49 +08:00
if(mAccess==0) mAccess = netAccess();
return mAccess->post(*this, data);
2023-04-18 14:14:46 +08:00
}
2023-04-23 17:01:35 +08:00
inline QNetworkReply *post(const QJsonDocument &json) {
2023-04-18 14:14:46 +08:00
setRawHeader("Content-Type", "application/json");
2023-07-21 17:40:49 +08:00
return post(json.toJson(QJsonDocument::Compact));
2023-04-23 17:01:35 +08:00
}
inline QNetworkReply *post(const QJsonObject &json) {
return post(QJsonDocument{json});
2023-04-18 14:14:46 +08:00
}
inline QNetworkReply *post(const QJsonArray &json) {
2023-04-23 17:01:35 +08:00
return post(QJsonDocument{json});
2023-04-18 14:14:46 +08:00
}
inline QNetworkReply *post(QHttpMultiPart *multiPart) {
2023-07-21 17:40:49 +08:00
if(mAccess==0) mAccess = netAccess();
return mAccess->post(*this, multiPart);
2023-04-18 14:14:46 +08:00
}
2023-07-21 17:40:49 +08:00
QNetworkAccessManager *mAccess{0};
2023-04-18 14:14:46 +08:00
};
QString errStr(QNetworkReply *);
QString errStrWithData(QNetworkReply *, QJsonDocument * = 0);
2023-07-21 17:40:49 +08:00
inline int waitFinished(QNetworkReply *reply, bool excludeUser = false) {
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
return excludeUser ? loop.exec(QEventLoop::ExcludeUserInputEvents) : loop.exec();
}
2023-04-23 17:01:35 +08:00
inline void abortSilence(QNetworkReply *reply) {
reply->blockSignals(true);
reply->abort();
reply->blockSignals(false);
reply->deleteLater();
}
2023-04-18 14:14:46 +08:00
const char* socketErrKey(int value);
class TcpSocket : public QTcpSocket {
Q_OBJECT
public:
explicit TcpSocket(QObject *parent = nullptr) : QTcpSocket{parent} {};
~TcpSocket() {
if(timerId!=0) killTimer(timerId);
};
bool waitForConnected(int msecs = 30000) override;
bool waitForDisconnected(int msecs = 30000) override;
bool waitForBytesWritten(int msecs = 30000) override;
bool waitForReadyRead(int msecs = 30000) override;
protected:
void timerEvent(QTimerEvent *e) override {
if(e->timerId()!=timerId) return;
killTimer(timerId);
timerId = 0;
emit timeout(5);
};
bool connAndExec(int msecs, QEventLoop *loop);
inline void timerStop() {
if(timerId==0) return;
killTimer(timerId);
timerId = 0;
}
int timerId = 0;
signals:
void timeout(int);
};
#endif // QNETWORK_H