-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathaccepterthread.cc
More file actions
59 lines (48 loc) · 1.43 KB
/
accepterthread.cc
File metadata and controls
59 lines (48 loc) · 1.43 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
#include "accepterthread.hpp"
#include "tcpserver.hpp"
#include <QDebug>
class AccepterThread::AccepterThreadPrivate
{
public:
explicit AccepterThreadPrivate(AccepterThread *q)
: q_ptr(q)
{}
AccepterThread *q_ptr;
quint16 port = 0;
int threadCount = 0;
ConnectionCallbacks callbacks;
};
AccepterThread::AccepterThread(quint16 port, int threadCount, QObject *parent)
: QThread(parent)
, d_ptr(new AccepterThreadPrivate(this))
{
d_ptr->port = port;
d_ptr->threadCount = threadCount;
}
AccepterThread::~AccepterThread()
{
if (isRunning()) {
quit();
wait();
}
}
void AccepterThread::setCallbacks(const ConnectionCallbacks &callbacks)
{
d_ptr->callbacks = callbacks;
}
void AccepterThread::run()
{
QScopedPointer<TcpServer> tcpServerPtr(new TcpServer(d_ptr->threadCount, d_ptr->callbacks));
connect(tcpServerPtr.data(), &TcpServer::message, this, &AccepterThread::message);
connect(tcpServerPtr.data(),
&TcpServer::clientCountChanged,
this,
&AccepterThread::clientCountChanged);
connect(tcpServerPtr.data(), &TcpServer::maxClientCount, this, &AccepterThread::maxClientCount);
if (!tcpServerPtr->listen(QHostAddress::Any, d_ptr->port)) {
emit message("Failed to start server: " + tcpServerPtr->errorString());
return;
}
emit message(QString("Server started on port %1").arg(d_ptr->port));
exec();
}