qt/LedOK/gutil/qnetwork.h

147 lines
4.4 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>
2023-08-07 09:04:53 +08:00
#include <QCoreApplication>
2023-04-18 14:14:46 +08:00
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:
2023-08-07 09:04:53 +08:00
using QTcpSocket::QTcpSocket;
2023-04-18 14:14:46 +08:00
~TcpSocket() {
2023-08-07 09:04:53 +08:00
if(timerId) killTimer(timerId);
2023-04-18 14:14:46 +08:00
};
2023-08-07 09:04:53 +08:00
void abort() {
stopTimer();
QTcpSocket::abort();
}
void close() override {
stopTimer();
QTcpSocket::close();
}
2023-04-18 14:14:46 +08:00
bool waitForConnected(int msecs = 30000) override;
bool waitForDisconnected(int msecs = 30000) override;
bool waitForBytesWritten(int msecs = 30000) override;
bool waitForReadyRead(int msecs = 30000) override;
2023-08-07 09:04:53 +08:00
void startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer) {
if(timerId) killTimer(timerId);
timerId = QTcpSocket::startTimer(interval, timerType);
}
void stopTimer() {
2023-04-18 14:14:46 +08:00
if(timerId==0) return;
killTimer(timerId);
timerId = 0;
}
2023-08-07 09:04:53 +08:00
int timerId{0};
protected:
void timerEvent(QTimerEvent *e) override {
if(e->timerId()!=timerId) QTcpSocket::timerEvent(e);
else {
abort();
setSocketError(SocketTimeoutError);
setErrorString(QCoreApplication::translate("QAbstractSocket", "Socket operation timed out"));
emit errorOccurred(QAbstractSocket::SocketTimeoutError);
}
};
bool connAndExec(int msecs, QEventLoop *loop);
2023-04-18 14:14:46 +08:00
};
#endif // QNETWORK_H