-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
51 lines (44 loc) · 1.12 KB
/
client.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
#include "client.h"
Client::Client(QObject *parent)
: QObject{parent}
{
socket = new QTcpSocket(this);
}
Client::~Client()
{
if (socket->state() == QAbstractSocket::ConnectedState) {
socket->disconnectFromHost();
}
delete socket;
}
void Client::setPort(int port)
{
this->port = port;
}
void Client::initialize()
{
socket->connectToHost(QHostAddress::LocalHost, port);
if (!socket->waitForConnected())
{
qWarning() << "[Client] Failed to connect to the server on port" << port;
}
qDebug() << "[Clinet] Connected to the server on port" << port;
}
void Client::receiveNotify(QString notify)
{
if (!notify.endsWith("\r\n")) {
notify.append("\r\n");
}
if (socket->state() == QAbstractSocket::ConnectedState) {
socket->write(notify.toUtf8());
socket->flush();
}
else {
qWarning() << "[Clinet] The connection has been blocked somehow, trying to reconnect...";
initialize();
receiveNotify(notify);
// QTimer::singleShot(50, this, [this, notify]() {
// this->receiveNotify(notify);
// });
}
}