| 1 | // Copyright (C) 2017 Witekio. |
| 2 | // Copyright (C) 2018 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QCOAPPROTOCOL_P_H |
| 6 | #define QCOAPPROTOCOL_P_H |
| 7 | |
| 8 | #include <QtCoap/qcoapglobal.h> |
| 9 | #include <QtCoap/qcoapreply.h> |
| 10 | #include <QtCoap/qcoapresource.h> |
| 11 | #include <QtCore/qlist.h> |
| 12 | #include <QtCore/qqueue.h> |
| 13 | #include <QtCore/qpointer.h> |
| 14 | #include <QtCore/qobject.h> |
| 15 | #include <private/qobject_p.h> |
| 16 | |
| 17 | // |
| 18 | // W A R N I N G |
| 19 | // ------------- |
| 20 | // |
| 21 | // This file is not part of the Qt API. It exists purely as an |
| 22 | // implementation detail. This header file may change from version to |
| 23 | // version without notice, or even be removed. |
| 24 | // |
| 25 | // We mean it. |
| 26 | // |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | class QCoapInternalRequest; |
| 31 | class QCoapInternalReply; |
| 32 | class QCoapProtocolPrivate; |
| 33 | class QCoapConnection; |
| 34 | class Q_AUTOTEST_EXPORT QCoapProtocol : public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | public: |
| 38 | explicit QCoapProtocol(QObject *parent = nullptr); |
| 39 | ~QCoapProtocol(); |
| 40 | |
| 41 | uint ackTimeout() const; |
| 42 | double ackRandomFactor() const; |
| 43 | uint maximumRetransmitCount() const; |
| 44 | quint16 blockSize() const; |
| 45 | uint maximumTransmitSpan() const; |
| 46 | uint maximumTransmitWait() const; |
| 47 | uint maximumLatency() const; |
| 48 | |
| 49 | uint minimumTimeout() const; |
| 50 | uint maximumTimeout() const; |
| 51 | |
| 52 | uint nonConfirmLifetime() const; |
| 53 | uint maximumServerResponseDelay() const; |
| 54 | |
| 55 | Q_SIGNALS: |
| 56 | void finished(QCoapReply *reply); |
| 57 | void responseToMulticastReceived(QCoapReply *reply, const QCoapMessage &message, |
| 58 | const QHostAddress &sender); |
| 59 | void error(QCoapReply *reply, QtCoap::Error error); |
| 60 | |
| 61 | public: |
| 62 | Q_INVOKABLE void setAckTimeout(uint ackTimeout); |
| 63 | Q_INVOKABLE void setAckRandomFactor(double ackRandomFactor); |
| 64 | Q_INVOKABLE void setMaximumRetransmitCount(uint maximumRetransmitCount); |
| 65 | Q_INVOKABLE void setBlockSize(quint16 blockSize); |
| 66 | Q_INVOKABLE void setMaximumServerResponseDelay(uint responseDelay); |
| 67 | Q_INVOKABLE void setMinimumTokenSize(int tokenSize); |
| 68 | |
| 69 | private: |
| 70 | Q_INVOKABLE void sendRequest(QPointer<QCoapReply> reply, QCoapConnection *connection); |
| 71 | Q_INVOKABLE void cancelObserve(QPointer<QCoapReply> reply) const; |
| 72 | Q_INVOKABLE void cancelObserve(const QUrl &url) const; |
| 73 | |
| 74 | private: |
| 75 | Q_DECLARE_PRIVATE(QCoapProtocol) |
| 76 | |
| 77 | friend class QCoapClient; |
| 78 | friend class QCoapClientPrivate; |
| 79 | }; |
| 80 | |
| 81 | struct CoapExchangeData { |
| 82 | QPointer<QCoapReply> userReply; |
| 83 | QSharedPointer<QCoapInternalRequest> request; |
| 84 | QList<QSharedPointer<QCoapInternalReply> > replies; |
| 85 | }; |
| 86 | |
| 87 | typedef QMap<QByteArray, CoapExchangeData> CoapExchangeMap; |
| 88 | |
| 89 | class Q_AUTOTEST_EXPORT QCoapProtocolPrivate : public QObjectPrivate |
| 90 | { |
| 91 | public: |
| 92 | QCoapProtocolPrivate() = default; |
| 93 | |
| 94 | quint16 generateUniqueMessageId() const; |
| 95 | QCoapToken generateUniqueToken() const; |
| 96 | |
| 97 | QCoapInternalReply *decode(const QByteArray &data, const QHostAddress &sender); |
| 98 | |
| 99 | void sendAcknowledgment(QCoapInternalRequest *request) const; |
| 100 | void sendReset(QCoapInternalRequest *request) const; |
| 101 | void sendRequest(QCoapInternalRequest *request, const QString& host = QString()) const; |
| 102 | |
| 103 | void onLastMessageReceived(QCoapInternalRequest *request, const QHostAddress &sender); |
| 104 | void onRequestError(QCoapInternalRequest *request, QCoapInternalReply *reply); |
| 105 | void onRequestError(QCoapInternalRequest *request, QtCoap::Error error, |
| 106 | QCoapInternalReply *reply = nullptr); |
| 107 | |
| 108 | void onRequestTimeout(QCoapInternalRequest *request); |
| 109 | void onRequestMaxTransmissionSpanReached(QCoapInternalRequest *request); |
| 110 | void onMulticastRequestExpired(QCoapInternalRequest *request); |
| 111 | void onFrameReceived(const QByteArray &data, const QHostAddress &sender); |
| 112 | void onConnectionError(QAbstractSocket::SocketError error); |
| 113 | void onRequestAborted(const QCoapToken &token); |
| 114 | |
| 115 | bool isMessageIdRegistered(quint16 id) const; |
| 116 | bool isTokenRegistered(const QCoapToken &token) const; |
| 117 | bool isRequestRegistered(const QCoapInternalRequest *request) const; |
| 118 | |
| 119 | QCoapInternalRequest *requestForToken(const QCoapToken &token) const; |
| 120 | QPointer<QCoapReply> userReplyForToken(const QCoapToken &token) const; |
| 121 | QList<QSharedPointer<QCoapInternalReply>> repliesForToken(const QCoapToken &token) const; |
| 122 | QCoapInternalReply *lastReplyForToken(const QCoapToken &token) const; |
| 123 | QCoapInternalRequest *findRequestByMessageId(quint16 messageId) const; |
| 124 | QCoapInternalRequest *findRequestByUserReply(const QCoapReply *reply) const; |
| 125 | |
| 126 | void registerExchange(const QCoapToken &token, QCoapReply *reply, |
| 127 | QSharedPointer<QCoapInternalRequest> request); |
| 128 | bool addReply(const QCoapToken &token, QSharedPointer<QCoapInternalReply> reply); |
| 129 | bool forgetExchange(const QCoapToken &token); |
| 130 | bool forgetExchange(const QCoapInternalRequest *request); |
| 131 | bool forgetExchangeReplies(const QCoapToken &token); |
| 132 | |
| 133 | CoapExchangeMap exchangeMap; |
| 134 | quint16 blockSize = 0; |
| 135 | |
| 136 | uint maximumRetransmitCount = 4; |
| 137 | uint ackTimeout = 2000; |
| 138 | uint maximumServerResponseDelay = 250 * 1000; |
| 139 | int minimumTokenSize = 4; |
| 140 | double ackRandomFactor = 1.5; |
| 141 | |
| 142 | Q_DECLARE_PUBLIC(QCoapProtocol) |
| 143 | }; |
| 144 | |
| 145 | QT_END_NAMESPACE |
| 146 | |
| 147 | #endif // QCOAPPROTOCOL_P_H |
| 148 | |