-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientthread.cpp
113 lines (101 loc) · 3.72 KB
/
clientthread.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
#include "clientthread.h"
#include <QApplication>
#include <QThread>
#include <QTimer>
#include <QDateTime>
void ClientThread::run() {
QTcpSocket socket;
socket.connectToHost(host,25000);
if (!socket.waitForConnected()) {
emit setStatus("Could not connect");
return;
} else {
emit setStatus("Connected");
}
socket.setReadBufferSize(bytesPerFrame * 16);
sendBuffer.resize(bytesPerFrame);
for (int i = 0; i < sendBuffer.size(); i++) {
sendBuffer[i] = static_cast<char>(i);
}
connect(&socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
this, &ClientThread::error);
connect(&socket, &QTcpSocket::disconnected, this, &ClientThread::setStop);
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &ClientThread::updateStatus);
timer->start(1000);
// socket.setSocketOption(QAbstractSocket::SendBufferSizeSocketOption, QVariant(1024*1024));
socket.setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, QVariant(1024*1024));
socket.socketOption(QAbstractSocket::SendBufferSizeSocketOption);
timestamp = QDateTime::currentDateTime();
bool isBusy;
while(true) {
isBusy = false;
if (socket.bytesAvailable() >= 0 * bytesPerFrame && !pause) {
receiveBuffer = socket.read(bytesPerFrame);
bytesRead += receiveBuffer.size();
int offset = receiveBuffer[0];
for (int i = 0; i < receiveBuffer.size(); i++) {
if (receiveBuffer[i] != static_cast<char>(i+offset)) {
errors++;
}
}
isBusy = true;
}
QApplication::processEvents();
if (socket.bytesToWrite() < 10 * bytesPerFrame && !pause) {
socket.write(sendBuffer);
bytesWritten += sendBuffer.size();
isBusy = true;
}
if (pause) {
QThread::msleep(1);
}
if (stop) {
break;
}
}
socket.disconnectFromHost();
deleteLater();
}
void ClientThread::setStop() {
stop = true;
}
void ClientThread::setPause() {
pause = true;
}
void ClientThread::setResume() {
pause = false;
}
void ClientThread::updateStatus() {
if (stop) {
return;
}
long long write = (bytesWritten - bytesWrittenLast) / 1000000;
long long read = (bytesRead - bytesReadLast) / 1000000;
QString elapsed = QTime(0,0,0).addSecs(timestamp.secsTo(QDateTime::currentDateTime())).toString("hh:mm:ss");
emit setStatus("Start: " + timestamp.toString("hh:mm:ss dd.MM.yyyy")
+ "\nElapsed: " + elapsed
+ "\nR: " + QString::number(bytesRead/1000000) + " MB (" + QString::number(read) + " MB/s)"
+ "\nS: " + QString::number(bytesWritten/1000000) + " MB (" + QString::number(write) + " MB/s)"
+ "\nErrors: " + QString::number(errors));
bytesWrittenLast = bytesWritten;
bytesReadLast = bytesRead;
}
void ClientThread::error(QAbstractSocket::SocketError socketError)
{
QString prefix = QDateTime::currentDateTime().toString("hh:mm:ss dd.MM.yyyy") + ": ";
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
emit setError(prefix + "Connection closed");
break;
case QAbstractSocket::HostNotFoundError:
emit setError(prefix + "Host not found");
break;
case QAbstractSocket::ConnectionRefusedError:
emit setError(prefix + "Connection refused");
break;
default:
emit setError(prefix + "Connection lost " + QString::number(socketError));
}
stop = true;
}