qt/LedOK/gutil/qnetwork.cpp

101 lines
3.5 KiB
C++
Raw Permalink Normal View History

2023-04-18 14:14:46 +08:00
#include "qnetwork.h"
#include <QCoreApplication>
#include <QMetaEnum>
const char *const FormBoundary = "----GangphonQtBoundary_.oOo._";
2023-07-21 17:40:49 +08:00
QNetworkAccessManager *netAccess() {
2023-04-18 14:14:46 +08:00
static QNetworkAccessManager access;
2023-07-21 17:40:49 +08:00
return &access;
2023-04-18 14:14:46 +08:00
};
QString errStr(QNetworkReply *reply) {
reply->deleteLater();
auto error = reply->error();
if(error != QNetworkReply::NoError) {
2023-07-21 17:40:49 +08:00
if(error==QNetworkReply::OperationCanceledError) return "TimeoutError ("+QString::number(QNetworkReply::TimeoutError)+") "+QCoreApplication::translate("Net","Connection Timeout");
2023-04-18 14:14:46 +08:00
auto errStr = reply->errorString();
if(error!=QNetworkReply::InternalServerError || ! errStr.endsWith("replied: Unknown")) return QString(QMetaEnum::fromType<QNetworkReply::NetworkError>().valueToKey(error))+" ("+QString::number(error)+") "+errStr;
}
auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(status != 200) return QString::number(status)+" "+reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
return "";
}
2023-10-23 11:44:22 +08:00
QString errStrWithData(QNetworkReply *reply, JValue *outJson) {
auto err = errStr(reply);
if(! err.isEmpty()) {
auto data = reply->readAll();
if(! data.isEmpty()) err += "\n"+data;
return err;
}
if(outJson) {
auto data = reply->readAll();
QString error;
*outJson = JFrom(data, &error);
if(! error.isEmpty()) return "JSON "+QCoreApplication::translate("Net","Error")+": "+error+"\n"+data;
}
return "";
}
2023-04-18 14:14:46 +08:00
QString errStrWithData(QNetworkReply *reply, QJsonDocument *outJson) {
auto err = errStr(reply);
if(! err.isEmpty()) {
auto data = reply->readAll();
if(! data.isEmpty()) err += "\n"+data;
return err;
}
if(outJson) {
auto data = reply->readAll();
QJsonParseError jsonErr;
*outJson = QJsonDocument::fromJson(data, &jsonErr);
2023-05-18 18:24:40 +08:00
if(jsonErr.error != QJsonParseError::NoError) return "JSON "+QCoreApplication::translate("Net","Error")+": "+jsonErr.errorString()+"\n"+data;
2023-04-18 14:14:46 +08:00
}
return "";
}
const char* socketErrKey(int value) {
static auto metaEnum = QMetaEnum::fromType<QAbstractSocket::SocketError>();
return metaEnum.valueToKey(value);
}
bool TcpSocket::waitForConnected(int msecs) {
if(state() == ConnectedState) return true;
QEventLoop loop;
connect(this, &QTcpSocket::connected, &loop, &QEventLoop::quit);
return connAndExec(msecs, &loop);
}
bool TcpSocket::waitForDisconnected(int msecs) {
if(state() == UnconnectedState) return true;
QEventLoop loop;
connect(this, &QTcpSocket::disconnected, &loop, &QEventLoop::quit);
return connAndExec(msecs, &loop);
}
bool TcpSocket::waitForBytesWritten(int msecs) {
2023-09-19 11:49:20 +08:00
if(bytesToWrite() == 0) return true;
2023-04-18 14:14:46 +08:00
QEventLoop loop;
connect(this, &QTcpSocket::bytesWritten, &loop, &QEventLoop::quit);
return connAndExec(msecs, &loop);
}
bool TcpSocket::waitForReadyRead(int msecs) {
2023-09-19 11:49:20 +08:00
if(bytesAvailable()) return true;
2023-04-18 14:14:46 +08:00
QEventLoop loop;
connect(this, &QTcpSocket::readyRead, &loop, &QEventLoop::quit);
return connAndExec(msecs, &loop);
}
bool TcpSocket::connAndExec(int msecs, QEventLoop *loop) {
connect(this, &QTcpSocket::errorOccurred, loop, [loop] {
loop->exit(1);
});
2023-08-07 09:04:53 +08:00
if(timerId) {
killTimer(timerId);
timerId = 0;
2023-04-18 14:14:46 +08:00
}
2023-08-07 09:04:53 +08:00
if(msecs > 0) startTimer(msecs);
2023-04-18 14:14:46 +08:00
auto res = loop->exec();
2023-08-07 09:04:53 +08:00
stopTimer();
2023-04-18 14:14:46 +08:00
return res==0;
}