-
Notifications
You must be signed in to change notification settings - Fork 11
/
thread.cpp
47 lines (42 loc) · 1.26 KB
/
thread.cpp
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
#include "thread.h"
#include "tcpclient.h"
#include <QHostAddress>
#include <QAtomicInt>
static QAtomicInt count = 0;
Thread::Thread(qintptr socketDescriptor, QObject *parent)
: QThread(parent)
, socketfd(socketDescriptor)
{
}
Thread::~Thread()
{
if(isRunning()){
quit();
wait();
}
count.fetchAndSubOrdered(1);
QString str = tr("The client is offline. The current number is: ") +
QString::number(count);
emit message(str);
emit clientCount(count);
qDebug() << str;
qDebug() << "~Thread";
}
void Thread::run()
{
QScopedPointer<TcpClient> tcpSocket(new TcpClient);
if(!tcpSocket->setSocketDescriptor(socketfd)){
qWarning() << tr("connection failed fd: ") << socketfd
<< tcpSocket->errorString();
return;
}
qDebug() << "New Client: " << QThread::currentThreadId();
count.fetchAndAddOrdered(1);
connect(tcpSocket.data(), &TcpClient::readyRead, tcpSocket.data(), &TcpClient::onReadyRead);
connect(tcpSocket.data(), &TcpClient::disconnected, this, &Thread::deleteLater);
QString str = QString::number(count) + tr(" Client online: ") + tcpSocket->getInfo();
emit message(str);
emit maxCount(count);
emit clientCount(count);
exec();
}