-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsubreactor.cc
More file actions
73 lines (58 loc) · 1.74 KB
/
subreactor.cc
File metadata and controls
73 lines (58 loc) · 1.74 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
#include "subreactor.hpp"
#include "tcpconnection.hpp"
#include <QDebug>
class SubReactor::SubReactorPrivate
{
public:
explicit SubReactorPrivate(SubReactor *q)
: q_ptr(q)
{}
~SubReactorPrivate()
{
if (connections.isEmpty()) {
return;
}
qDeleteAll(connections);
connections.clear();
}
SubReactor *q_ptr;
TcpConnectionList connections;
int connectionCount = 0;
ConnectionCallbacks callbacks;
};
SubReactor::SubReactor(const ConnectionCallbacks &callbacks, QObject *parent)
: QObject(parent)
, d_ptr(new SubReactorPrivate(this))
{
d_ptr->callbacks = callbacks;
}
SubReactor::~SubReactor() {}
void SubReactor::addConnection(qintptr socketDescriptor)
{
auto connectionPtr = std::make_unique<TcpConnection>(socketDescriptor, d_ptr->callbacks);
if (!connectionPtr->isValid()) {
emit message(tr("Failed to create connection: %1").arg(connectionPtr->errorString()));
return;
}
auto *connection = connectionPtr.release();
connect(connection, &TcpConnection::handleDisconnected, this, &SubReactor::onConnectionClosed);
d_ptr->connections.append(connection);
d_ptr->connectionCount++;
emit message(tr("New connection: %1").arg(connection->clientInfo()));
emit clientConnected();
}
int SubReactor::clientCount() const
{
return d_ptr->connectionCount;
}
void SubReactor::onConnectionClosed()
{
auto *connection = qobject_cast<TcpConnection *>(sender());
if (connection) {
d_ptr->connections.removeAll(connection);
d_ptr->connectionCount--;
connection->deleteLater();
emit message(tr("Connection closed:%1").arg(connection->clientInfo()));
emit clientDisconnected();
}
}