1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "dialog.h" |
52 | |
53 | #include <QtNetwork> |
54 | #include <QtWidgets> |
55 | |
56 | static const int TotalBytes = 50 * 1024 * 1024; |
57 | static const int PayloadSize = 64 * 1024; // 64 KB |
58 | |
59 | Dialog::Dialog(QWidget *parent) |
60 | : QDialog(parent) |
61 | { |
62 | clientProgressBar = new QProgressBar; |
63 | clientStatusLabel = new QLabel(tr("Client ready" )); |
64 | serverProgressBar = new QProgressBar; |
65 | serverStatusLabel = new QLabel(tr("Server ready" )); |
66 | |
67 | startButton = new QPushButton(tr("&Start" )); |
68 | quitButton = new QPushButton(tr("&Quit" )); |
69 | |
70 | buttonBox = new QDialogButtonBox; |
71 | buttonBox->addButton(startButton, QDialogButtonBox::ActionRole); |
72 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
73 | |
74 | connect(startButton, &QAbstractButton::clicked, this, &Dialog::start); |
75 | connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); |
76 | connect(&tcpServer, &QTcpServer::newConnection, |
77 | this, &Dialog::acceptConnection); |
78 | connect(&tcpClient, &QAbstractSocket::connected, this, &Dialog::startTransfer); |
79 | connect(&tcpClient, &QIODevice::bytesWritten, |
80 | this, &Dialog::updateClientProgress); |
81 | connect(&tcpClient, &QAbstractSocket::errorOccurred, |
82 | this, &Dialog::displayError); |
83 | |
84 | QVBoxLayout *mainLayout = new QVBoxLayout; |
85 | mainLayout->addWidget(clientProgressBar); |
86 | mainLayout->addWidget(clientStatusLabel); |
87 | mainLayout->addWidget(serverProgressBar); |
88 | mainLayout->addWidget(serverStatusLabel); |
89 | mainLayout->addStretch(1); |
90 | mainLayout->addSpacing(10); |
91 | mainLayout->addWidget(buttonBox); |
92 | setLayout(mainLayout); |
93 | |
94 | setWindowTitle(tr("Loopback" )); |
95 | } |
96 | |
97 | void Dialog::start() |
98 | { |
99 | startButton->setEnabled(false); |
100 | |
101 | #ifndef QT_NO_CURSOR |
102 | QGuiApplication::setOverrideCursor(Qt::WaitCursor); |
103 | #endif |
104 | |
105 | bytesWritten = 0; |
106 | bytesReceived = 0; |
107 | |
108 | while (!tcpServer.isListening() && !tcpServer.listen()) { |
109 | QMessageBox::StandardButton ret = QMessageBox::critical(this, |
110 | tr("Loopback" ), |
111 | tr("Unable to start the test: %1." ) |
112 | .arg(tcpServer.errorString()), |
113 | QMessageBox::Retry |
114 | | QMessageBox::Cancel); |
115 | if (ret == QMessageBox::Cancel) |
116 | return; |
117 | } |
118 | |
119 | serverStatusLabel->setText(tr("Listening" )); |
120 | clientStatusLabel->setText(tr("Connecting" )); |
121 | tcpClient.connectToHost(QHostAddress::LocalHost, tcpServer.serverPort()); |
122 | } |
123 | |
124 | void Dialog::acceptConnection() |
125 | { |
126 | tcpServerConnection = tcpServer.nextPendingConnection(); |
127 | if (!tcpServerConnection) { |
128 | serverStatusLabel->setText(tr("Error: got invalid pending connection!" )); |
129 | return; |
130 | } |
131 | |
132 | connect(tcpServerConnection, &QIODevice::readyRead, |
133 | this, &Dialog::updateServerProgress); |
134 | connect(tcpServerConnection, &QAbstractSocket::errorOccurred, |
135 | this, &Dialog::displayError); |
136 | connect(tcpServerConnection, &QTcpSocket::disconnected, |
137 | tcpServerConnection, &QTcpSocket::deleteLater); |
138 | |
139 | serverStatusLabel->setText(tr("Accepted connection" )); |
140 | tcpServer.close(); |
141 | } |
142 | |
143 | void Dialog::startTransfer() |
144 | { |
145 | // called when the TCP client connected to the loopback server |
146 | bytesToWrite = TotalBytes - int(tcpClient.write(QByteArray(PayloadSize, '@'))); |
147 | clientStatusLabel->setText(tr("Connected" )); |
148 | } |
149 | |
150 | void Dialog::updateServerProgress() |
151 | { |
152 | bytesReceived += int(tcpServerConnection->bytesAvailable()); |
153 | tcpServerConnection->readAll(); |
154 | |
155 | serverProgressBar->setMaximum(TotalBytes); |
156 | serverProgressBar->setValue(bytesReceived); |
157 | serverStatusLabel->setText(tr("Received %1MB" ) |
158 | .arg(bytesReceived / (1024 * 1024))); |
159 | |
160 | if (bytesReceived == TotalBytes) { |
161 | tcpServerConnection->close(); |
162 | startButton->setEnabled(true); |
163 | #ifndef QT_NO_CURSOR |
164 | QGuiApplication::restoreOverrideCursor(); |
165 | #endif |
166 | } |
167 | } |
168 | |
169 | void Dialog::updateClientProgress(qint64 numBytes) |
170 | { |
171 | // called when the TCP client has written some bytes |
172 | bytesWritten += int(numBytes); |
173 | |
174 | // only write more if not finished and when the Qt write buffer is below a certain size. |
175 | if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4 * PayloadSize) |
176 | bytesToWrite -= tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@')); |
177 | |
178 | clientProgressBar->setMaximum(TotalBytes); |
179 | clientProgressBar->setValue(bytesWritten); |
180 | clientStatusLabel->setText(tr("Sent %1MB" ).arg(bytesWritten / (1024 * 1024))); |
181 | } |
182 | |
183 | void Dialog::displayError(QAbstractSocket::SocketError socketError) |
184 | { |
185 | if (socketError == QTcpSocket::RemoteHostClosedError) |
186 | return; |
187 | |
188 | QMessageBox::information(this, tr("Network error" ), |
189 | tr("The following error occurred: %1." ) |
190 | .arg(tcpClient.errorString())); |
191 | |
192 | tcpClient.close(); |
193 | tcpServer.close(); |
194 | clientProgressBar->reset(); |
195 | serverProgressBar->reset(); |
196 | clientStatusLabel->setText(tr("Client ready" )); |
197 | serverStatusLabel->setText(tr("Server ready" )); |
198 | startButton->setEnabled(true); |
199 | #ifndef QT_NO_CURSOR |
200 | QGuiApplication::restoreOverrideCursor(); |
201 | #endif |
202 | } |
203 | |