| 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(s: "Client ready" )); | 
| 64 |     serverProgressBar = new QProgressBar; | 
| 65 |     serverStatusLabel = new QLabel(tr(s: "Server ready" )); | 
| 66 |  | 
| 67 |     startButton = new QPushButton(tr(s: "&Start" )); | 
| 68 |     quitButton = new QPushButton(tr(s: "&Quit" )); | 
| 69 |  | 
| 70 |     buttonBox = new QDialogButtonBox; | 
| 71 |     buttonBox->addButton(button: startButton, role: QDialogButtonBox::ActionRole); | 
| 72 |     buttonBox->addButton(button: quitButton, role: QDialogButtonBox::RejectRole); | 
| 73 |  | 
| 74 |     connect(sender: startButton, signal: &QAbstractButton::clicked, receiver: this, slot: &Dialog::start); | 
| 75 |     connect(sender: quitButton, signal: &QAbstractButton::clicked, receiver: this, slot: &QWidget::close); | 
| 76 |     connect(sender: &tcpServer, signal: &QTcpServer::newConnection, | 
| 77 |             receiver: this, slot: &Dialog::acceptConnection); | 
| 78 |     connect(sender: &tcpClient, signal: &QAbstractSocket::connected, receiver: this, slot: &Dialog::startTransfer); | 
| 79 |     connect(sender: &tcpClient, signal: &QIODevice::bytesWritten, | 
| 80 |             receiver: this, slot: &Dialog::updateClientProgress); | 
| 81 |     connect(sender: &tcpClient, signal: &QAbstractSocket::errorOccurred, | 
| 82 |             receiver: this, slot: &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(stretch: 1); | 
| 90 |     mainLayout->addSpacing(size: 10); | 
| 91 |     mainLayout->addWidget(buttonBox); | 
| 92 |     setLayout(mainLayout); | 
| 93 |  | 
| 94 |     setWindowTitle(tr(s: "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(parent: this, | 
| 110 |                                         title: tr(s: "Loopback" ), | 
| 111 |                                         text: tr(s: "Unable to start the test: %1." ) | 
| 112 |                                         .arg(a: tcpServer.errorString()), | 
| 113 |                                         buttons: QMessageBox::Retry | 
| 114 |                                         | QMessageBox::Cancel); | 
| 115 |         if (ret == QMessageBox::Cancel) | 
| 116 |             return; | 
| 117 |     } | 
| 118 |  | 
| 119 |     serverStatusLabel->setText(tr(s: "Listening" )); | 
| 120 |     clientStatusLabel->setText(tr(s: "Connecting" )); | 
| 121 |     tcpClient.connectToHost(address: QHostAddress::LocalHost, port: tcpServer.serverPort()); | 
| 122 | } | 
| 123 |  | 
| 124 | void Dialog::acceptConnection() | 
| 125 | { | 
| 126 |     tcpServerConnection = tcpServer.nextPendingConnection(); | 
| 127 |     if (!tcpServerConnection) { | 
| 128 |         serverStatusLabel->setText(tr(s: "Error: got invalid pending connection!" )); | 
| 129 |         return; | 
| 130 |     } | 
| 131 |  | 
| 132 |     connect(sender: tcpServerConnection, signal: &QIODevice::readyRead, | 
| 133 |             receiver: this, slot: &Dialog::updateServerProgress); | 
| 134 |     connect(sender: tcpServerConnection, signal: &QAbstractSocket::errorOccurred, | 
| 135 |             receiver: this, slot: &Dialog::displayError); | 
| 136 |     connect(sender: tcpServerConnection, signal: &QTcpSocket::disconnected, | 
| 137 |             receiver: tcpServerConnection, slot: &QTcpSocket::deleteLater); | 
| 138 |  | 
| 139 |     serverStatusLabel->setText(tr(s: "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(data: QByteArray(PayloadSize, '@'))); | 
| 147 |     clientStatusLabel->setText(tr(s: "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(s: "Received %1MB" ) | 
| 158 |                                .arg(a: 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(data: QByteArray(qMin(a: bytesToWrite, b: PayloadSize), '@')); | 
| 177 |  | 
| 178 |     clientProgressBar->setMaximum(TotalBytes); | 
| 179 |     clientProgressBar->setValue(bytesWritten); | 
| 180 |     clientStatusLabel->setText(tr(s: "Sent %1MB" ).arg(a: bytesWritten / (1024 * 1024))); | 
| 181 | } | 
| 182 |  | 
| 183 | void Dialog::displayError(QAbstractSocket::SocketError socketError) | 
| 184 | { | 
| 185 |     if (socketError == QTcpSocket::RemoteHostClosedError) | 
| 186 |         return; | 
| 187 |  | 
| 188 |     QMessageBox::information(parent: this, title: tr(s: "Network error" ), | 
| 189 |                              text: tr(s: "The following error occurred: %1." ) | 
| 190 |                              .arg(a: tcpClient.errorString())); | 
| 191 |  | 
| 192 |     tcpClient.close(); | 
| 193 |     tcpServer.close(); | 
| 194 |     clientProgressBar->reset(); | 
| 195 |     serverProgressBar->reset(); | 
| 196 |     clientStatusLabel->setText(tr(s: "Client ready" )); | 
| 197 |     serverStatusLabel->setText(tr(s: "Server ready" )); | 
| 198 |     startButton->setEnabled(true); | 
| 199 | #ifndef QT_NO_CURSOR | 
| 200 |     QGuiApplication::restoreOverrideCursor(); | 
| 201 | #endif | 
| 202 | } | 
| 203 |  |