-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconnection.cpp
61 lines (50 loc) · 1.65 KB
/
connection.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
#include "connection.h"
#include <QtNetwork>
const QDataStream::Version Connection::streamVersion = QDataStream::Qt_4_5;
Connection::Connection(QObject *parent) :
QTcpSocket(parent), blockSize(0),
authenticator("/home/rriemann/Documents/Backup/Studium/Masterarbeit/sampledata-600MB.bin")
{
}
void Connection::receiveData()
{
QDataStream in(this);
in.setVersion(streamVersion);
if (blockSize == 0) {
if (this->bytesAvailable() < (int)sizeof(qint64))
return;
in >> blockSize;
}
if (this->bytesAvailable() < blockSize)
return;
QByteArray block = this->read(blockSize);
if(authenticator.authenticate(block)) {
emit logMessage("authentication successfull", Qt::darkYellow);
QDataStream bufferStream(&block, QIODevice::ReadOnly);
bufferStream.setVersion(streamVersion);
quint16 type;
bufferStream >> type;
QVariant data;
bufferStream >> data;
emit receivedData((PackageType)type, data);
} else {
emit logMessage("authentication failed", Qt::darkRed);
}
blockSize = 0;
}
void Connection::sendData(const PackageType type, const QVariant &data)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(streamVersion);
out << (qint64)0;
out << (quint16)type;
out << data;
out.device()->seek(0);
out << (qint64)(block.size() + authenticator.getSecurityLevel() - sizeof(qint64));
block.append(authenticator.token(block.right(block.size()-sizeof(qint64))));
qint64 wsize = this->write(block);
Q_UNUSED(wsize);
Q_ASSERT(wsize == block.size());
this->flush();
}