-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathworkerthread.cc
More file actions
66 lines (54 loc) · 1.53 KB
/
workerthread.cc
File metadata and controls
66 lines (54 loc) · 1.53 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
#include "workerthread.hpp"
#include "subreactor.hpp"
#include <QDebug>
class WorkerThread::WorkerThreadPrivate
{
public:
explicit WorkerThreadPrivate(WorkerThread *q)
: q_ptr(q)
{}
WorkerThread *q_ptr;
QPointer<SubReactor> subReactorPtr;
ConnectionCallbacks callbacks;
};
WorkerThread::WorkerThread(const ConnectionCallbacks &callbacks, QObject *parent)
: QThread(parent)
, d_ptr(new WorkerThreadPrivate(this))
{
d_ptr->callbacks = callbacks;
}
WorkerThread::~WorkerThread()
{
if (isRunning()) {
quit();
wait();
}
}
void WorkerThread::run()
{
QScopedPointer<SubReactor> subReactorPtr(new SubReactor(d_ptr->callbacks));
d_ptr->subReactorPtr = subReactorPtr.data();
connect(subReactorPtr.data(), &SubReactor::message, this, &WorkerThread::message);
connect(subReactorPtr.data(),
&SubReactor::clientConnected,
this,
&WorkerThread::clientConnected);
connect(subReactorPtr.data(),
&SubReactor::clientDisconnected,
this,
&WorkerThread::clientDisconnected);
exec();
}
void WorkerThread::handleConnection(qintptr socketDescriptor)
{
if (d_ptr->subReactorPtr) {
QMetaObject::invokeMethod(
d_ptr->subReactorPtr.data(),
[this, socketDescriptor]() { d_ptr->subReactorPtr->addConnection(socketDescriptor); },
Qt::QueuedConnection);
}
}
int WorkerThread::clientCount() const
{
return d_ptr->subReactorPtr ? d_ptr->subReactorPtr->clientCount() : 0;
}