-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconnectthread.cpp
64 lines (54 loc) · 1.28 KB
/
connectthread.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "connectthread.h"
ConnectThread::ConnectThread(int socketDescriptor, QObject *parent):
QThread(parent),
socketDescriptor(socketDescriptor),
runThread(true),
userID(0),
tcpMsg(new TcpSocketMsg(this))
{
connect (tcpMsg, &TcpSocketMsg::disconnected, this, &ConnectThread::disconnect);
connect (tcpMsg, &TcpSocketMsg::readyRead, this, &ConnectThread::threadReadyRead);
if (!tcpMsg->setSocketDescriptor (socketDescriptor)) {
emit error (tcpMsg->error ());
return;
}
}
void ConnectThread::run()
{
while (runThread) {
}
if (tcpMsg) {
if (tcpMsg->state () != TcpSocketMsg::ConnectedState) {
tcpMsg->disconnectFromHost ();
tcpMsg->waitForDisconnected();
}
tcpMsg->deleteLater ();
}
}
void ConnectThread::stop()
{
runThread = false;
}
void ConnectThread::setUserID(quint32 id)
{
userID = id;
}
quint32 ConnectThread::getUserID()
{
return userID;
}
void ConnectThread::threadReadyRead()
{
Message *msg = tcpMsg->read ();
if (msg == nullptr) return;
emit newMsg (this, msg);
}
void ConnectThread::disconnect()
{
emit loseConnect (this);
}
void ConnectThread::sendMsg(ConnectThread *thread, Message *msg)
{
if (thread != this) return;
tcpMsg->send (msg);
}