-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
81 lines (67 loc) · 1.95 KB
/
server.cpp
File metadata and controls
81 lines (67 loc) · 1.95 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
78
79
80
81
#include "server.h"
#include "./interface/abstractbusinesslogic.h"
// SERVER
Server::Server(AbstractBusinessLogic *bloc, QObject *parent) :
QTcpServer(parent),
mBloc(bloc),
mStop(false),
mCounter(0)
{
}
Server::~Server()
{
}
void Server::incomingConnection(qintptr socketId)
{
if (!mStop)
{
// create socket object
QThread *thread = new QThread;
ClientSocket *socket = new ClientSocket(socketId);
// move object to thread
socket->moveToThread(thread);
// increase counter
mCounter++;
// connection
connect(thread,SIGNAL(destroyed()),
this, SLOT(decreaseCounter()));
connect(thread,SIGNAL(finished()),
thread,SLOT(deleteLater()));
connect(socket,SIGNAL(destroyed()),
thread,SLOT(quit()));
connect(socket,SIGNAL(done()),
socket,SLOT(deleteLater()));
connect(this, SIGNAL(close()),
socket,SLOT(close()));
connect(socket,SIGNAL(stop()),
this, SLOT(stop()));
connect(thread,SIGNAL(started()),
socket,SLOT(start()));
// async communication with BLoC
connect(socket, SIGNAL(dataReceived(Buffer*, const qintptr)),
mBloc, SLOT(processData(Buffer*, const qintptr)));
connect(mBloc, SIGNAL(sendData(const QByteArray &, const qintptr)),
socket, SLOT(sendData(const QByteArray &, const qintptr)));
connect(mBloc, SIGNAL(kill()),
this, SLOT(stop()));
// start thread
thread->start();
}
}
void Server::decreaseCounter()
{
mCounter--;
checkStop();
}
void Server::stop()
{
mStop = true;
emit close();
checkStop();
}
void Server::checkStop()
{
if (mCounter == 0 && mStop) {
QCoreApplication::quit();
}
}