-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.cpp
126 lines (109 loc) · 3.94 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "client.h"
#include "mainwindow.h"
typedef QPair<quint8,QVariant> CustomPackage;
Q_DECLARE_METATYPE(CustomPackage)
static int id1 = qRegisterMetaType<CustomPackage>();
static int id2 = qRegisterMetaTypeStreamOperators<CustomPackage>();
Client::Client(QObject *parent) :
QObject(parent), connection(0), isMaster(false), status(CSunconnected)
{
connect(&server, SIGNAL(newConnection(Connection*)), this, SLOT(incomingConnection(Connection*)));
}
Client::~Client()
{
removeConnection();
}
int Client::getServerPort()
{
return server.serverPort();
}
Connection *Client::setupConnection(Connection *connection)
{
if(!connection)
connection = new Connection(this);
connect(connection, SIGNAL(readyRead()), connection, SLOT(receiveData()));
connect(connection, SIGNAL(receivedData(Connection::PackageType,QVariant)), this, SLOT(handleData(Connection::PackageType,QVariant)));
connect(connection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(removeConnection()));
connect(connection, SIGNAL(disconnected()), this, SLOT(removeConnection()));
connect(connection, SIGNAL(logMessage(QString,Qt::GlobalColor)), this, SIGNAL(logMessage(QString,Qt::GlobalColor)));
return connection;
}
void Client::incomingConnection(Connection *incomingConnection)
{
if(!connection) {
connection = setupConnection(incomingConnection);
emit logMessage(QString("Waiting for Handshake from %1:%2").arg(connection->peerAddress().toString()).arg(connection->peerPort()), Qt::yellow);
status = CSwait4Handshake;
} else {
emit logMessage(QString("dismiss incoming connection from %1:%2").
arg(incomingConnection->peerAddress().toString()).
arg(incomingConnection->peerPort()));
incomingConnection->deleteLater();
}
}
void Client::handleData(Connection::PackageType type, QVariant data)
{
switch(status) {
case CSunconnected:
emit logMessage("Received unexpected incoming data", Qt::red);
break;
case CSinitiatedConnection: // answer of initiated handshake
{
QString text = QString("client confirmed role to be %1.").arg(((data.toBool()) ? "master" : "slave"));
logMessage(text, Qt::yellow);
status = CSready;
emit establishedConnection(isMaster);
}
break;
case CSwait4Handshake: // reveiving now the awaited handshake
{
isMaster = !data.toBool();
QString text = QString("receiving role to be %1.").arg(((isMaster) ? "master" : "slave"));
logMessage(text, Qt::yellow);
connection->sendData(Connection::PTroleDefinition, isMaster);
status = CSready;
emit establishedConnection(isMaster);
}
break;
default:
{
if(type == Connection::PTcustomData) {
CustomPackage package(data.value<CustomPackage>());
emit receiveData(package.first, package.second);
}
}
}
}
void Client::initiateConnection(QString host, int port, bool isMaster)
{
if(!connection) {
this->isMaster = isMaster;
connection = setupConnection();
connection->connectToHost(host, port);
status = CSinitiatedConnection;
connect(connection, SIGNAL(connected()), this, SLOT(startHandShake()));
}
}
void Client::startHandShake()
{
connection->sendData(Connection::PTroleDefinition, isMaster);
}
void Client::removeConnection()
{
if(connection) {
emit logMessage(QString("Removing Connection... (%1)").arg(connection->errorString()), Qt::yellow);
connection->deleteLater();
}
connection = 0;
status = CSunconnected;
emit closedConnection();
}
void Client::sendData(quint8 type, QVariant data)
{
if(connection && status == CSready) {
CustomPackage package(type, data);
connection->sendData(Connection::PTcustomData, QVariant::fromValue(package));
} else {
emit logMessage("Not ready for sending data.");
}
}