131 lines
3.9 KiB
C++
131 lines
3.9 KiB
C++
#ifndef QNETWORK_H
|
|
#define QNETWORK_H
|
|
|
|
#include <QNetworkReply>
|
|
#include <QJsonDocument>
|
|
#include <QEventLoop>
|
|
#include <QTimerEvent>
|
|
|
|
extern const char *const FormBoundary;
|
|
|
|
QNetworkAccessManager *netAccess();
|
|
|
|
class NetReq : public QNetworkRequest {
|
|
public:
|
|
#if(QT_VERSION_MAJOR > 5)
|
|
using QNetworkRequest::QNetworkRequest;
|
|
explicit NetReq(const QString &url) : QNetworkRequest{url} {};
|
|
explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {};
|
|
#else
|
|
NetReq() {init();};
|
|
explicit NetReq(const QString &url) : QNetworkRequest{url} {init();};
|
|
explicit NetReq(const QUrl &url) : QNetworkRequest{url} {init();};
|
|
explicit NetReq(QNetworkAccessManager *access) : mAccess(access) {init();};
|
|
inline void init() {
|
|
setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
}
|
|
#endif
|
|
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;
|
|
}
|
|
|
|
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() {
|
|
if(mAccess==0) mAccess = netAccess();
|
|
return mAccess->get(*this);
|
|
}
|
|
inline QNetworkReply *post(const QByteArray &data) {
|
|
if(mAccess==0) mAccess = netAccess();
|
|
return mAccess->post(*this, data);
|
|
}
|
|
inline QNetworkReply *post(const QJsonDocument &json) {
|
|
setRawHeader("Content-Type", "application/json");
|
|
return post(json.toJson(QJsonDocument::Compact));
|
|
}
|
|
inline QNetworkReply *post(const QJsonObject &json) {
|
|
return post(QJsonDocument{json});
|
|
}
|
|
inline QNetworkReply *post(const QJsonArray &json) {
|
|
return post(QJsonDocument{json});
|
|
}
|
|
inline QNetworkReply *post(QHttpMultiPart *multiPart) {
|
|
if(mAccess==0) mAccess = netAccess();
|
|
return mAccess->post(*this, multiPart);
|
|
}
|
|
QNetworkAccessManager *mAccess{0};
|
|
};
|
|
|
|
QString errStr(QNetworkReply *);
|
|
QString errStrWithData(QNetworkReply *, QJsonDocument * = 0);
|
|
|
|
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();
|
|
}
|
|
inline void abortSilence(QNetworkReply *reply) {
|
|
reply->blockSignals(true);
|
|
reply->abort();
|
|
reply->blockSignals(false);
|
|
reply->deleteLater();
|
|
}
|
|
|
|
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
|