Examples: Revamp Loopback
* order and sort includes * use functor connect * use nullptr * use member init * added sanity check for nextPendingConnection() * small cleanup here and there Change-Id: I72c6758b5fedea0937a1f2cb9031cb7203f5d955 Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
parent
7cb5b324f0
commit
503920ac9e
@ -48,11 +48,11 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
#include <QtNetwork>
|
|
||||||
|
|
||||||
#include "dialog.h"
|
#include "dialog.h"
|
||||||
|
|
||||||
|
#include <QtNetwork>
|
||||||
|
#include <QtWidgets>
|
||||||
|
|
||||||
static const int TotalBytes = 50 * 1024 * 1024;
|
static const int TotalBytes = 50 * 1024 * 1024;
|
||||||
static const int PayloadSize = 64 * 1024; // 64 KB
|
static const int PayloadSize = 64 * 1024; // 64 KB
|
||||||
|
|
||||||
@ -71,15 +71,15 @@ Dialog::Dialog(QWidget *parent)
|
|||||||
buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
|
buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
|
||||||
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
||||||
|
|
||||||
connect(startButton, SIGNAL(clicked()), this, SLOT(start()));
|
connect(startButton, &QAbstractButton::clicked, this, &Dialog::start);
|
||||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
||||||
connect(&tcpServer, SIGNAL(newConnection()),
|
connect(&tcpServer, &QTcpServer::newConnection,
|
||||||
this, SLOT(acceptConnection()));
|
this, &Dialog::acceptConnection);
|
||||||
connect(&tcpClient, SIGNAL(connected()), this, SLOT(startTransfer()));
|
connect(&tcpClient, &QAbstractSocket::connected, this, &Dialog::startTransfer);
|
||||||
connect(&tcpClient, SIGNAL(bytesWritten(qint64)),
|
connect(&tcpClient, &QIODevice::bytesWritten,
|
||||||
this, SLOT(updateClientProgress(qint64)));
|
this, &Dialog::updateClientProgress);
|
||||||
connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),
|
connect(&tcpClient, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
|
||||||
this, SLOT(displayError(QAbstractSocket::SocketError)));
|
this, &Dialog::displayError);
|
||||||
|
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||||
mainLayout->addWidget(clientProgressBar);
|
mainLayout->addWidget(clientProgressBar);
|
||||||
@ -124,10 +124,18 @@ void Dialog::start()
|
|||||||
void Dialog::acceptConnection()
|
void Dialog::acceptConnection()
|
||||||
{
|
{
|
||||||
tcpServerConnection = tcpServer.nextPendingConnection();
|
tcpServerConnection = tcpServer.nextPendingConnection();
|
||||||
connect(tcpServerConnection, SIGNAL(readyRead()),
|
if (!tcpServerConnection) {
|
||||||
this, SLOT(updateServerProgress()));
|
serverStatusLabel->setText(tr("Error: got invalid pending connection!"));
|
||||||
connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
|
return;
|
||||||
this, SLOT(displayError(QAbstractSocket::SocketError)));
|
}
|
||||||
|
|
||||||
|
connect(tcpServerConnection, &QIODevice::readyRead,
|
||||||
|
this, &Dialog::updateServerProgress);
|
||||||
|
connect(tcpServerConnection,
|
||||||
|
QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
|
||||||
|
this, &Dialog::displayError);
|
||||||
|
connect(tcpServerConnection, &QTcpSocket::disconnected,
|
||||||
|
tcpServerConnection, &QTcpSocket::deleteLater);
|
||||||
|
|
||||||
serverStatusLabel->setText(tr("Accepted connection"));
|
serverStatusLabel->setText(tr("Accepted connection"));
|
||||||
tcpServer.close();
|
tcpServer.close();
|
||||||
@ -136,13 +144,13 @@ void Dialog::acceptConnection()
|
|||||||
void Dialog::startTransfer()
|
void Dialog::startTransfer()
|
||||||
{
|
{
|
||||||
// called when the TCP client connected to the loopback server
|
// called when the TCP client connected to the loopback server
|
||||||
bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, '@'));
|
bytesToWrite = TotalBytes - int(tcpClient.write(QByteArray(PayloadSize, '@')));
|
||||||
clientStatusLabel->setText(tr("Connected"));
|
clientStatusLabel->setText(tr("Connected"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::updateServerProgress()
|
void Dialog::updateServerProgress()
|
||||||
{
|
{
|
||||||
bytesReceived += (int)tcpServerConnection->bytesAvailable();
|
bytesReceived += int(tcpServerConnection->bytesAvailable());
|
||||||
tcpServerConnection->readAll();
|
tcpServerConnection->readAll();
|
||||||
|
|
||||||
serverProgressBar->setMaximum(TotalBytes);
|
serverProgressBar->setMaximum(TotalBytes);
|
||||||
@ -161,17 +169,16 @@ void Dialog::updateServerProgress()
|
|||||||
|
|
||||||
void Dialog::updateClientProgress(qint64 numBytes)
|
void Dialog::updateClientProgress(qint64 numBytes)
|
||||||
{
|
{
|
||||||
// callen when the TCP client has written some bytes
|
// called when the TCP client has written some bytes
|
||||||
bytesWritten += (int)numBytes;
|
bytesWritten += int(numBytes);
|
||||||
|
|
||||||
// only write more if not finished and when the Qt write buffer is below a certain size.
|
// only write more if not finished and when the Qt write buffer is below a certain size.
|
||||||
if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4*PayloadSize)
|
if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4 * PayloadSize)
|
||||||
bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));
|
bytesToWrite -= tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@'));
|
||||||
|
|
||||||
clientProgressBar->setMaximum(TotalBytes);
|
clientProgressBar->setMaximum(TotalBytes);
|
||||||
clientProgressBar->setValue(bytesWritten);
|
clientProgressBar->setValue(bytesWritten);
|
||||||
clientStatusLabel->setText(tr("Sent %1MB")
|
clientStatusLabel->setText(tr("Sent %1MB").arg(bytesWritten / (1024 * 1024)));
|
||||||
.arg(bytesWritten / (1024 * 1024)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::displayError(QAbstractSocket::SocketError socketError)
|
void Dialog::displayError(QAbstractSocket::SocketError socketError)
|
||||||
|
@ -60,9 +60,6 @@ class QDialogButtonBox;
|
|||||||
class QLabel;
|
class QLabel;
|
||||||
class QProgressBar;
|
class QProgressBar;
|
||||||
class QPushButton;
|
class QPushButton;
|
||||||
class QTcpServer;
|
|
||||||
class QTcpSocket;
|
|
||||||
class QAction;
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
class Dialog : public QDialog
|
class Dialog : public QDialog
|
||||||
@ -70,7 +67,7 @@ class Dialog : public QDialog
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dialog(QWidget *parent = 0);
|
Dialog(QWidget *parent = nullptr);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void start();
|
void start();
|
||||||
@ -81,21 +78,21 @@ public slots:
|
|||||||
void displayError(QAbstractSocket::SocketError socketError);
|
void displayError(QAbstractSocket::SocketError socketError);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QProgressBar *clientProgressBar;
|
QProgressBar *clientProgressBar = nullptr;
|
||||||
QProgressBar *serverProgressBar;
|
QProgressBar *serverProgressBar = nullptr;
|
||||||
QLabel *clientStatusLabel;
|
QLabel *clientStatusLabel = nullptr;
|
||||||
QLabel *serverStatusLabel;
|
QLabel *serverStatusLabel = nullptr;
|
||||||
|
|
||||||
QPushButton *startButton;
|
QPushButton *startButton = nullptr;
|
||||||
QPushButton *quitButton;
|
QPushButton *quitButton = nullptr;
|
||||||
QDialogButtonBox *buttonBox;
|
QDialogButtonBox *buttonBox = nullptr;
|
||||||
|
|
||||||
QTcpServer tcpServer;
|
QTcpServer tcpServer;
|
||||||
QTcpSocket tcpClient;
|
QTcpSocket tcpClient;
|
||||||
QTcpSocket *tcpServerConnection;
|
QTcpSocket *tcpServerConnection = nullptr;
|
||||||
int bytesToWrite;
|
int bytesToWrite = 0;
|
||||||
int bytesWritten;
|
int bytesWritten = 0;
|
||||||
int bytesReceived;
|
int bytesReceived = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -48,10 +48,10 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QApplication>
|
|
||||||
|
|
||||||
#include "dialog.h"
|
#include "dialog.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user