-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmyclient.cpp
More file actions
77 lines (58 loc) · 1.91 KB
/
myclient.cpp
File metadata and controls
77 lines (58 loc) · 1.91 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
67
68
69
70
71
72
73
74
75
76
77
// myclient.cpp
#include "myclient.h"
MyClient::MyClient(QObject *parent, QThreadPool* _pool) :
QObject(parent)
{
pool = _pool;
//QThreadPool::globalInstance()->setMaxThreadCount(5);
}
void MyClient::setSocket(qintptr descriptor)
{
// make a new socket
socket = new QTcpSocket(this);
qDebug() << "A new socket created!";
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
socket->setSocketDescriptor(descriptor);
qDebug() << " Client connected at " << descriptor;
}
// asynchronous - runs separately from the thread we created
void MyClient::connected()
{
qDebug() << "Client connected event";
}
// asynchronous
void MyClient::disconnected()
{
qDebug() << "Client disconnected";
}
// Our main thread of execution
// This happening via main thread
// Our code running in our current thread not in another QThread
// That's why we're getting the thread from the pool
void MyClient::readyRead()
{
qDebug() << "MyClient::readyRead()";
qDebug() << socket->readAll();
// Time consumer
MyTask *mytask = new MyTask();
// mytask->setAutoDelete(true);
// using queued connection
connect(mytask, SIGNAL(Result(int)), this, SLOT(TaskResult(int)), Qt::QueuedConnection);
qDebug() << "Starting a new task using a thread from the QThreadPool";
// QThreadPool::globalInstance() returns global QThreadPool instance
// QThreadPool::globalInstance()->start(mytask);
pool->start(mytask);
}
// After a task performed a time consuming task,
// we grab the result here, and send it to client
void MyClient::TaskResult(int Number)
{
QByteArray Buffer;
Buffer.append("\r\nTask result = ");
Buffer.append(QString::number(Number));
socket->write(Buffer);
socket->flush();
socket->deleteLater();
}