-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cpp
72 lines (60 loc) · 1.97 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
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
//-----------------Thread : created when incomming connection of server is called-----------//
#include "thread.h"
#include <QRegularExpression>
Thread::Thread(int ID , QObject *parent) : QThread(parent)
{
//Thread constructor.
// Set the socket descriptor id
this->socketdescriptor = ID;
}
void Thread::run()
{
//Aim : We will run this to start our thread. All inside " " are the signals/slots
//Step 1: Create a socket
//Step 2: if success good else give error
//Step 3: connect socket "readyread" to this class "readyread"
//Step 4: connect "disconnected" of socket to "disconnected" of this object
//exec for keeping the thread open
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketdescriptor))
{
emit error(socket->error());
emit status(socket->errorString());
return;
}
connect(socket,SIGNAL(readyRead()),this,SLOT(readyread()),Qt::DirectConnection);
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
qDebug() << socketdescriptor << ":Client Connected";
emit status("Client connected.");
exec();
}
//On Client Disconnect - When the client disconnected emit status as : Disconnected
void Thread::disconnected()
{
socket->deleteLater();
emit status("Disconnected");
exit(0);
}
void Thread::readyread()
{
// Aim: Read data from socket and update the status bar
// Step 1: Read all
// Step 2: Send the data
// Step 3: Send status to status bar
QByteArray Data = socket->readAll();
QRegularExpression re("[0-9]\\d{0,3},[0-9]\\d{0,3},\\S+,\\S+,-[0-9]\\d{0,3}");
QRegularExpressionMatch match2 = re.match(Data);
if(match2.hasMatch())
{
emit info(Data);
emit status("Recieved data :" + Data);
}
else {
emit status("Recieved wrong data format :" + Data);
}
}
void Thread::writeconfig(QByteArray str)
{
// Aim: Write to socket the updated configuration
socket->write(str);
}