diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2018-06-22 10:04:11 +0200 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2018-07-10 04:55:48 +0000 |
commit | ced0bd80683a37a4afd5ab4460e1e08812685379 (patch) | |
tree | 83955f5808937fb0587145b605e44977da0e6339 /examples/network | |
parent | d27dd47ffa7bef94ffd18977b1bcbc970e1a72e4 (diff) |
Diffstat (limited to 'examples/network')
-rw-r--r-- | examples/network/secureudpclient/association.cpp | 38 | ||||
-rw-r--r-- | examples/network/secureudpclient/association.h | 1 |
2 files changed, 26 insertions, 13 deletions
diff --git a/examples/network/secureudpclient/association.cpp b/examples/network/secureudpclient/association.cpp index 11f52ba96c..c8397b9f9f 100644 --- a/examples/network/secureudpclient/association.cpp +++ b/examples/network/secureudpclient/association.cpp @@ -65,6 +65,7 @@ DtlsAssociation::DtlsAssociation(const QHostAddress &address, quint16 port, connect(&crypto, &QDtls::handshakeTimeout, this, &DtlsAssociation::handshakeTimeout); connect(&crypto, &QDtls::pskRequired, this, &DtlsAssociation::pskRequired); + socket.connectToHost(address.toString(), port); connect(&socket, &QUdpSocket::readyRead, this, &DtlsAssociation::readyRead); pingTimer.setInterval(5000); @@ -79,10 +80,22 @@ DtlsAssociation::~DtlsAssociation() void DtlsAssociation::startHandshake() { + if (socket.state() != QAbstractSocket::ConnectedState) { + emit infoMessage(tr("%1: connecting UDP socket first ...").arg(name)); + connect(&socket, &QAbstractSocket::connected, this, &DtlsAssociation::udpSocketConnected); + return; + } + if (!crypto.doHandshake(&socket, {})) - emit errorMessage(name + tr(": failed to start a handshake - ") + crypto.dtlsErrorString()); + emit errorMessage(tr("%1: failed to start a handshake - %2").arg(name, crypto.dtlsErrorString())); else - emit infoMessage(name + tr(": starting a handshake")); + emit infoMessage(tr("%1: starting a handshake").arg(name)); +} + +void DtlsAssociation::udpSocketConnected() +{ + emit infoMessage(tr("%1: UDP socket is now in ConnectedState, continue with handshake ...").arg(name)); + startHandshake(); } void DtlsAssociation::readyRead() @@ -90,7 +103,7 @@ void DtlsAssociation::readyRead() QByteArray dgram(socket.pendingDatagramSize(), '\0'); const qint64 bytesRead = socket.readDatagram(dgram.data(), dgram.size()); if (bytesRead <= 0) { - emit warningMessage(name + tr(": spurious read notification?")); + emit warningMessage(tr("%1: spurious read notification?").arg(name)); return; } @@ -99,45 +112,44 @@ void DtlsAssociation::readyRead() const QByteArray plainText = crypto.decryptDatagram(&socket, dgram); if (plainText.size()) { emit serverResponse(name, dgram, plainText); - pingTimer.start(); return; } if (crypto.dtlsError() == QDtlsError::RemoteClosedConnectionError) { - emit errorMessage(name + tr(": shutdown alert received")); + emit errorMessage(tr("%1: shutdown alert received").arg(name)); socket.close(); pingTimer.stop(); return; } - emit warningMessage(name + tr(": zero-length datagram received?")); + emit warningMessage(tr("%1: zero-length datagram received?").arg(name)); } else { if (!crypto.doHandshake(&socket, dgram)) { - emit errorMessage(name + tr(": handshake error - ") + crypto.dtlsErrorString()); + emit errorMessage(tr("%1: handshake error - %2").arg(name, crypto.dtlsErrorString())); return; } if (crypto.connectionEncrypted()) { - emit infoMessage(name + tr(": encrypted connection established!")); + emit infoMessage(tr("%1: encrypted connection established!").arg(name)); pingTimer.start(); pingTimeout(); } else { - emit infoMessage(name + tr(": continuing with handshake ...")); + emit infoMessage(tr("%1: continuing with handshake ...").arg(name)); } } } void DtlsAssociation::handshakeTimeout() { - emit warningMessage(name + tr(": handshake timeout, trying to re-transmit")); + emit warningMessage(tr("%1: handshake timeout, trying to re-transmit").arg(name)); if (!crypto.handleTimeout(&socket)) - emit errorMessage(name + tr(": failed to re-transmit - ") + crypto.dtlsErrorString()); + emit errorMessage(tr("%1: failed to re-transmit - %2").arg(name, crypto.dtlsErrorString())); } void DtlsAssociation::pskRequired(QSslPreSharedKeyAuthenticator *auth) { Q_ASSERT(auth); - emit infoMessage(name + tr(": providing pre-shared key ...")); + emit infoMessage(tr("%1: providing pre-shared key ...").arg(name)); auth->setIdentity(name.toLatin1()); auth->setPreSharedKey(QByteArrayLiteral("\x1a\x2b\x3c\x4d\x5e\x6f")); } @@ -147,7 +159,7 @@ void DtlsAssociation::pingTimeout() static const QString message = QStringLiteral("I am %1, please, accept our ping %2"); const qint64 written = crypto.writeDatagramEncrypted(&socket, message.arg(name).arg(ping).toLatin1()); if (written <= 0) { - emit errorMessage(name + tr(": failed to send a ping - ") + crypto.dtlsErrorString()); + emit errorMessage(tr("%1: failed to send a ping - %2").arg(name, crypto.dtlsErrorString())); pingTimer.stop(); return; } diff --git a/examples/network/secureudpclient/association.h b/examples/network/secureudpclient/association.h index 08fc28e3ed..157882f23d 100644 --- a/examples/network/secureudpclient/association.h +++ b/examples/network/secureudpclient/association.h @@ -76,6 +76,7 @@ signals: private slots: + void udpSocketConnected(); void readyRead(); void handshakeTimeout(); void pskRequired(QSslPreSharedKeyAuthenticator *auth); |