-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
63 lines (48 loc) · 1.55 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
52
53
54
55
56
57
58
59
60
61
62
63
#include "client.h"
#include <QHostAddress>
#include <QStringList>
#include <QDebug>
Client::Client(UVoipData* voipData, QObject *parent)
: QObject(parent)
, m_client()
, m_voipData(voipData)
{
connect(&m_client, SIGNAL(connected()), this, SLOT(connected()), Qt::DirectConnection);
connect(&m_client, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
connect(m_voipData, SIGNAL(requestConnectChanged()), this, SLOT(attemptConnection()), Qt::DirectConnection);
connect(m_voipData, SIGNAL(requestDisconnectChanged()), this, SLOT(attemptDisconnection()), Qt::DirectConnection);
}
QTcpSocket *Client::socket()
{
return &m_client;
}
void Client::connected()
{
// qDebug() << "The client has just send some data!";
m_client.setSocketOption(QTcpSocket::LowDelayOption, 1);
m_voipData->setClientConnected(true);
emit connectedSocket(&m_client);
}
void Client::attemptConnection()
{
QStringList list = m_voipData->socketUrl().split(":", QString::SkipEmptyParts);
QString host = list.first();
int port = list.last().toInt();
qDebug() << "Client::attemptConnection" << m_voipData->socketUrl();
qDebug() << "--> Host:" << host;
qDebug() << "--> Port:" << port;
m_client.connectToHost(host, port);
// We wait for 3 seconds max!
if(!m_client.waitForConnected(3000))
{
m_voipData->emitClientConnectionFailed();
}
}
void Client::attemptDisconnection()
{
m_client.disconnectFromHost();
}
void Client::disconnected()
{
m_voipData->setClientConnected(false);
}