forked from loafle/openapi-generator-original
Simpler timeout with QTimer::singleShot (#4430)
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <QFileInfo>
|
||||
#include <QBuffer>
|
||||
#include <QtGlobal>
|
||||
#include <QTimer>
|
||||
|
||||
#include "{{prefix}}HttpRequest.h"
|
||||
|
||||
@@ -47,23 +48,15 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
|
||||
|
||||
|
||||
{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent)
|
||||
: QObject(parent), manager(nullptr)
|
||||
: QObject(parent), manager(nullptr), _timeOut(0)
|
||||
{
|
||||
qsrand(QDateTime::currentDateTime().toTime_t());
|
||||
timeout = 0;
|
||||
timer = new QTimer();
|
||||
manager = new QNetworkAccessManager(this);
|
||||
workingDirectory = QDir::currentPath();
|
||||
connect(manager, &QNetworkAccessManager::finished, this, &{{prefix}}HttpRequestWorker::on_manager_finished);
|
||||
}
|
||||
|
||||
{{prefix}}HttpRequestWorker::~{{prefix}}HttpRequestWorker() {
|
||||
if(timer != nullptr){
|
||||
if(timer->isActive()){
|
||||
timer->stop();
|
||||
}
|
||||
timer->deleteLater();
|
||||
}
|
||||
for (const auto & item: multiPartFields) {
|
||||
if(item != nullptr) {
|
||||
delete item;
|
||||
@@ -97,8 +90,8 @@ QByteArray *{{prefix}}HttpRequestWorker::getMultiPartField(const QString &fieldn
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void {{prefix}}HttpRequestWorker::setTimeOut(int tout){
|
||||
timeout = tout;
|
||||
void {{prefix}}HttpRequestWorker::setTimeOut(int timeOut){
|
||||
_timeOut = _timeOut;
|
||||
}
|
||||
|
||||
void {{prefix}}HttpRequestWorker::setWorkingDirectory(const QString &path){
|
||||
@@ -358,11 +351,8 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
|
||||
buffer->setParent(reply);
|
||||
#endif
|
||||
}
|
||||
if(timeout > 0){
|
||||
timer->setSingleShot(true);
|
||||
timer->setInterval(timeout);
|
||||
connect(timer, &QTimer::timeout, this, [=](){ on_manager_timeout(reply); });
|
||||
timer->start();
|
||||
if(_timeOut > 0){
|
||||
QTimer::singleShot(_timeOut, [=](){ on_manager_timeout(reply); });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -375,6 +365,7 @@ void {{prefix}}HttpRequestWorker::on_manager_finished(QNetworkReply *reply) {
|
||||
headers.insert(item.first, item.second);
|
||||
}
|
||||
}
|
||||
disconnect(this, nullptr, nullptr, nullptr);
|
||||
reply->deleteLater();
|
||||
process_form_response();
|
||||
emit on_execution_finished(this);
|
||||
@@ -384,7 +375,6 @@ void {{prefix}}HttpRequestWorker::on_manager_timeout(QNetworkReply *reply) {
|
||||
error_type = QNetworkReply::TimeoutError;
|
||||
response = "";
|
||||
error_str = "Timed out waiting for response";
|
||||
disconnect(manager, nullptr, nullptr, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
emit on_execution_finished(this);
|
||||
|
||||
@@ -10,12 +10,10 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
#include <QMap>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
|
||||
|
||||
#include "{{prefix}}HttpFileElement.h"
|
||||
|
||||
{{#cppNamespaceDeclarations}}
|
||||
@@ -52,7 +50,6 @@ public:
|
||||
QByteArray response;
|
||||
QNetworkReply::NetworkError error_type;
|
||||
QString error_str;
|
||||
QTimer *timer;
|
||||
explicit {{prefix}}HttpRequestWorker(QObject *parent = nullptr);
|
||||
virtual ~{{prefix}}HttpRequestWorker();
|
||||
|
||||
@@ -73,7 +70,7 @@ private:
|
||||
QMap<QString, {{prefix}}HttpFileElement> files;
|
||||
QMap<QString, QByteArray*> multiPartFields;
|
||||
QString workingDirectory;
|
||||
int timeout;
|
||||
int _timeOut;
|
||||
void on_manager_timeout(QNetworkReply *reply);
|
||||
void process_form_response();
|
||||
private slots:
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <QFileInfo>
|
||||
#include <QBuffer>
|
||||
#include <QtGlobal>
|
||||
#include <QTimer>
|
||||
|
||||
#include "PFXHttpRequest.h"
|
||||
|
||||
@@ -56,23 +57,15 @@ void PFXHttpRequestInput::add_file(QString variable_name, QString local_filename
|
||||
|
||||
|
||||
PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent)
|
||||
: QObject(parent), manager(nullptr)
|
||||
: QObject(parent), manager(nullptr), _timeOut(0)
|
||||
{
|
||||
qsrand(QDateTime::currentDateTime().toTime_t());
|
||||
timeout = 0;
|
||||
timer = new QTimer();
|
||||
manager = new QNetworkAccessManager(this);
|
||||
workingDirectory = QDir::currentPath();
|
||||
connect(manager, &QNetworkAccessManager::finished, this, &PFXHttpRequestWorker::on_manager_finished);
|
||||
}
|
||||
|
||||
PFXHttpRequestWorker::~PFXHttpRequestWorker() {
|
||||
if(timer != nullptr){
|
||||
if(timer->isActive()){
|
||||
timer->stop();
|
||||
}
|
||||
timer->deleteLater();
|
||||
}
|
||||
for (const auto & item: multiPartFields) {
|
||||
if(item != nullptr) {
|
||||
delete item;
|
||||
@@ -106,8 +99,8 @@ QByteArray *PFXHttpRequestWorker::getMultiPartField(const QString &fieldname){
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void PFXHttpRequestWorker::setTimeOut(int tout){
|
||||
timeout = tout;
|
||||
void PFXHttpRequestWorker::setTimeOut(int timeOut){
|
||||
_timeOut = _timeOut;
|
||||
}
|
||||
|
||||
void PFXHttpRequestWorker::setWorkingDirectory(const QString &path){
|
||||
@@ -367,11 +360,8 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
|
||||
buffer->setParent(reply);
|
||||
#endif
|
||||
}
|
||||
if(timeout > 0){
|
||||
timer->setSingleShot(true);
|
||||
timer->setInterval(timeout);
|
||||
connect(timer, &QTimer::timeout, this, [=](){ on_manager_timeout(reply); });
|
||||
timer->start();
|
||||
if(_timeOut > 0){
|
||||
QTimer::singleShot(_timeOut, [=](){ on_manager_timeout(reply); });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,10 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QTimer>
|
||||
#include <QMap>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
|
||||
|
||||
#include "PFXHttpFileElement.h"
|
||||
|
||||
namespace test_namespace {
|
||||
@@ -61,7 +59,6 @@ public:
|
||||
QByteArray response;
|
||||
QNetworkReply::NetworkError error_type;
|
||||
QString error_str;
|
||||
QTimer *timer;
|
||||
explicit PFXHttpRequestWorker(QObject *parent = nullptr);
|
||||
virtual ~PFXHttpRequestWorker();
|
||||
|
||||
@@ -82,7 +79,7 @@ private:
|
||||
QMap<QString, PFXHttpFileElement> files;
|
||||
QMap<QString, QByteArray*> multiPartFields;
|
||||
QString workingDirectory;
|
||||
int timeout;
|
||||
int _timeOut;
|
||||
void on_manager_timeout(QNetworkReply *reply);
|
||||
void process_form_response();
|
||||
private slots:
|
||||
|
||||
Reference in New Issue
Block a user