diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2019-05-23 15:37:21 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2019-05-23 15:39:27 +0200 |
commit | d2744ade327fe6b63bb80d6acd9c1cd7ae6e317f (patch) | |
tree | b39a74a4fa41e0ac8da9a95e3acfaf5691b318a4 | |
parent | fd94d8e5673a5d5228aa2d38b605fde7a881332b (diff) |
Remove QCoapClien's constructor for specifying a custom connection
Change-Id: I5b51c71ab11cbce5755a69c58f34bfa5e7cd5385
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r-- | src/coap/qcoapclient.cpp | 34 | ||||
-rw-r--r-- | src/coap/qcoapclient.h | 1 | ||||
-rw-r--r-- | src/coap/qcoapclient_p.h | 2 | ||||
-rw-r--r-- | src/coap/qcoapprotocol_p.h | 1 | ||||
-rw-r--r-- | tests/auto/qcoapclient/tst_qcoapclient.cpp | 16 |
5 files changed, 37 insertions, 17 deletions
diff --git a/src/coap/qcoapclient.cpp b/src/coap/qcoapclient.cpp index 5469ef2..4ff4ced 100644 --- a/src/coap/qcoapclient.cpp +++ b/src/coap/qcoapclient.cpp @@ -162,16 +162,8 @@ QCoapClientPrivate::~QCoapClientPrivate() constructors. */ QCoapClient::QCoapClient(QtCoap::SecurityMode securityMode, QObject *parent) : - QCoapClient(new QCoapQUdpConnection(securityMode), parent) -{ -} - -/*! - Constructs a QCoapClient object with the given \a connection and - sets \a parent as the parent object. -*/ -QCoapClient::QCoapClient(QCoapConnection *connection, QObject *parent) : - QObject(*new QCoapClientPrivate(new QCoapProtocol, connection), parent) + QObject(*new QCoapClientPrivate(new QCoapProtocol, new QCoapQUdpConnection(securityMode)), + parent) { Q_D(QCoapClient); @@ -210,6 +202,28 @@ QCoapClient::QCoapClient(QCoapConnection *connection, QObject *parent) : } /*! + \internal + + Sets the client's connection to \a customConnection. +*/ +void QCoapClientPrivate::setConnection(QCoapConnection *customConnection) +{ + Q_Q(QCoapClient); + + delete connection; + connection = customConnection; + + q->connect(connection, &QCoapConnection::readyRead, protocol, + [this](const QByteArray &data, const QHostAddress &sender) { + protocol->d_func()->onFrameReceived(data, sender); + }); + q->connect(connection, &QCoapConnection::error, protocol, + [this](QAbstractSocket::SocketError socketError) { + protocol->d_func()->onConnectionError(socketError); + }); +} + +/*! Destroys the QCoapClient object and frees up any resources. Note that QCoapReply objects that are returned from this class have the QCoapClient set as their parents, which means that diff --git a/src/coap/qcoapclient.h b/src/coap/qcoapclient.h index 7171b59..31bf9f2 100644 --- a/src/coap/qcoapclient.h +++ b/src/coap/qcoapclient.h @@ -55,7 +55,6 @@ class Q_COAP_EXPORT QCoapClient : public QObject public: explicit QCoapClient(QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSecurity, QObject *parent = nullptr); - explicit QCoapClient(QCoapConnection *connection, QObject *parent = nullptr); ~QCoapClient(); QCoapReply *get(const QCoapRequest &request); diff --git a/src/coap/qcoapclient_p.h b/src/coap/qcoapclient_p.h index 84d3a83..18c916e 100644 --- a/src/coap/qcoapclient_p.h +++ b/src/coap/qcoapclient_p.h @@ -63,6 +63,8 @@ public: QCoapResourceDiscoveryReply *sendDiscovery(const QCoapRequest &request); bool send(QCoapReply *reply); + void setConnection(QCoapConnection *customConnection); + Q_DECLARE_PUBLIC(QCoapClient) }; diff --git a/src/coap/qcoapprotocol_p.h b/src/coap/qcoapprotocol_p.h index 4bd61c8..b24363e 100644 --- a/src/coap/qcoapprotocol_p.h +++ b/src/coap/qcoapprotocol_p.h @@ -100,6 +100,7 @@ private: Q_DECLARE_PRIVATE(QCoapProtocol) friend class QCoapClient; + friend class QCoapClientPrivate; }; struct CoapExchangeData { diff --git a/tests/auto/qcoapclient/tst_qcoapclient.cpp b/tests/auto/qcoapclient/tst_qcoapclient.cpp index 6f66346..0af3bee 100644 --- a/tests/auto/qcoapclient/tst_qcoapclient.cpp +++ b/tests/auto/qcoapclient/tst_qcoapclient.cpp @@ -108,9 +108,11 @@ private: class QCoapClientForSocketErrorTests : public QCoapClient { public: - QCoapClientForSocketErrorTests() : - QCoapClient(new QCoapQUdpConnectionSocketTests) - {} + QCoapClientForSocketErrorTests() + { + QCoapClientPrivate *privateClient = static_cast<QCoapClientPrivate *>(d_func()); + privateClient->setConnection(new QCoapQUdpConnectionSocketTests()); + } QCoapQUdpConnection *connection() { @@ -162,9 +164,11 @@ public: class QCoapClientForMulticastTests : public QCoapClient { public: - QCoapClientForMulticastTests() : - QCoapClient(new QCoapConnectionMulticastTests) - {} + QCoapClientForMulticastTests() + { + QCoapClientPrivate *privateClient = static_cast<QCoapClientPrivate *>(d_func()); + privateClient->setConnection(new QCoapConnectionMulticastTests()); + } QCoapConnection *connection() { |