-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathnetworkrunnable.cpp
More file actions
104 lines (91 loc) · 2.81 KB
/
Copy pathnetworkrunnable.cpp
File metadata and controls
104 lines (91 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "networkrunnable.h"
#include <QDebug>
#include <QEventLoop>
#include <QCoreApplication>
#include "Log4cplusWrapper.h"
#include "classmemorytracer.h"
#include "networkrequest.h"
#include "networkmanager.h"
NetworkRunnable::NetworkRunnable(const RequestTask &task, QObject *parent)
: QObject(parent)
, m_task(task)
{
TRACE_CLASS_CONSTRUCTOR(NetworkRunnable);
setAutoDelete(false);
}
NetworkRunnable::~NetworkRunnable()
{
TRACE_CLASS_DESTRUCTOR(NetworkRunnable);
}
void NetworkRunnable::run()
{
RequestTask task = m_task;
std::unique_ptr<NetworkRequest> pRequest = nullptr;
bool bQuit = false;
QEventLoop loop;
try
{
connect(this, &NetworkRunnable::exitEventLoop, &loop, [&bQuit, &loop, &pRequest]() {
loop.quit();
bQuit = true;
if (pRequest.get())
{
pRequest->disconnect();
}
}, Qt::QueuedConnection);
if (!bQuit)
{
pRequest = std::move(NetworkRequestFactory::create(task.eType));
if (pRequest.get())
{
connect(pRequest.get(), &NetworkRequest::requestFinished,
[this, &task](bool bSuccess, const QByteArray& bytesContent, const QString& strError) {
task.bSuccess = bSuccess;
task.bytesContent = bytesContent;
task.strError = strError;
emit requestFinished(task);
});
pRequest->setRequestTask(task);
pRequest->start();
}
else
{
LOG_ERROR("Unsupported type(" << task.eType << ") ---- " << task.url.url().toStdWString());
qWarning() << QString("Unsupported type(%1) ----").arg(task.eType) << task.url.url();
task.bSuccess = false;
task.strError = QString("Unsupported type(%1)").arg(task.eType);
emit requestFinished(task);
}
loop.exec();
}
}
catch (std::exception* e)
{
LOG_ERROR("NetworkRunnable::run() exception: " << e->what());
qCritical() << "NetworkRunnable::run() exception:" << QString::fromUtf8(e->what());
}
catch (...)
{
LOG_ERROR("NetworkRunnable::run() unknown exception");
qCritical() << "NetworkRunnable::run() unknown exception";
}
if (pRequest.get())
{
pRequest->abort();
pRequest.reset();
}
}
quint64 NetworkRunnable::requsetId() const
{
return m_task.uiId;
}
quint64 NetworkRunnable::batchId() const
{
return m_task.uiBatchId;
}
void NetworkRunnable::quit()
{
disconnect(this, SIGNAL(requestFinished(const RequestTask &)),
NetworkManager::globalInstance(), SLOT(onRequestFinished(const RequestTask &)));
emit exitEventLoop();
}