QCoapClient Class

The QCoapClient class allows the application to send CoAP requests and receive replies. 更多...

头: #include <QCoapClient>
CMake: find_package(Qt6 REQUIRED COMPONENTS Coap)
target_link_libraries(mytarget PRIVATE Qt6::Coap)
qmake: QT += coap
继承: QObject

注意: 此类的所有函数 可重入 .

公共函数

QCoapClient (QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSecurity, QObject * parent = nullptr)
virtual ~QCoapClient ()
void cancelObserve (QCoapReply * notifiedReply )
void cancelObserve (const QUrl & url )
QCoapReply * deleteResource (const QCoapRequest & request )
QCoapReply * deleteResource (const QUrl & url )
void disconnect ()
QCoapResourceDiscoveryReply * discover (const QUrl & url , const QString & discoveryPath = QLatin1String("/.well-known/core"))
QCoapResourceDiscoveryReply * discover (QtCoap::MulticastGroup group = QtCoap::MulticastGroup::AllCoapNodesIPv4, int port = QtCoap::DefaultPort, const QString & discoveryPath = QLatin1String("/.well-known/core"))
QCoapReply * get (const QCoapRequest & request )
QCoapReply * get (const QUrl & url )
QCoapReply * observe (const QCoapRequest & request )
QCoapReply * observe (const QUrl & url )
QCoapReply * post (const QCoapRequest & request , const QByteArray & data = QByteArray())
QCoapReply * post (const QCoapRequest & request , QIODevice * device )
QCoapReply * post (const QUrl & url , const QByteArray & data = QByteArray())
QCoapReply * put (const QCoapRequest & request , const QByteArray & data = QByteArray())
QCoapReply * put (const QCoapRequest & request , QIODevice * device )
QCoapReply * put (const QUrl & url , const QByteArray & data = QByteArray())
void setAckRandomFactor (double ackRandomFactor )
void setAckTimeout (uint ackTimeout )
void setBlockSize (quint16 blockSize )
void setMaximumRetransmitCount (uint maximumRetransmitCount )
void setMaximumServerResponseDelay (uint responseDelay )
void setMinimumTokenSize (int tokenSize )
void setSecurityConfiguration (const QCoapSecurityConfiguration & configuration )
void setSocketOption (QAbstractSocket::SocketOption option , const QVariant & value )

信号

void error (QCoapReply * reply , QtCoap::Error error )
void finished (QCoapReply * reply )
void responseToMulticastReceived (QCoapReply * reply , const QCoapMessage & message , const QHostAddress & sender )

详细描述

The QCoapClient class contains signals that get triggered when the reply of a sent request has arrived.

The application can use a QCoapClient to send requests over a CoAP network. It provides functions for standard requests: each returns a QCoapReply object, to which the response data shall be delivered; this can be read when the finished () signal arrives.

A simple request can be sent with:

QCoapClient *client = new QCoapClient(this);
connect(client, &QCoapClient::finished, this, &TestClass::slotFinished);
client->get(QCoapRequest(Qurl("coap://coap.me/test")));
					

注意: After processing of the request has finished, it is the responsibility of the user to delete the QCoapReply 对象,在适当时。不要直接在槽内删除它,因为槽已连接到 finished ()。可以使用 deleteLater () 函数。

You can also use an observe request. This can be used as above, or more conveniently with the QCoapReply::notified () signal:

QCoapRequest request = QCoapRequest(Qurl("coap://coap.me/obs"));
QCoapReply *reply = client->observe(request);
connect(reply, &QCoapReply::notified, this, &TestClass::slotNotified);
					

And the observation can be cancelled with:

client->cancelObserve(reply);
					

When a reply arrives, the QCoapClient emits a finished () 信号。

注意: For a discovery request, the returned object is a QCoapResourceDiscoveryReply . It can be used the same way as a QCoapReply but contains also a list of resources.

另请参阅 QCoapRequest , QCoapReply ,和 QCoapResourceDiscoveryReply .

成员函数文档编制

[explicit] QCoapClient:: QCoapClient ( QtCoap::SecurityMode securityMode = QtCoap::SecurityMode::NoSecurity, QObject * parent = nullptr)

Constructs a QCoapClient object for the given securityMode 并设置 parent 作为父级对象。

默认为 securityMode is QtCoap::NoSecurity , which disables security.

This connects using a QCoapQUdpConnection; to use a custom transport, sub-class QCoapConnection and pass an instance to one of the other constructors.

[virtual noexcept] QCoapClient:: ~QCoapClient ()

销毁 QCoapClient 对象并释放任何资源。注意 QCoapReply objects that are returned from this class have the QCoapClient set as their parents, which means that they will be deleted along with it.

void QCoapClient:: cancelObserve ( QCoapReply * notifiedReply )

这是重载函数。

Cancels the observation of a resource using the reply notifiedReply returned by the observe () 方法。

另请参阅 observe ().

void QCoapClient:: cancelObserve (const QUrl & url )

这是重载函数。

Cancels the observation of a resource identified by the url .

另请参阅 observe ().

QCoapReply *QCoapClient:: deleteResource (const QCoapRequest & request )

发送 request using the DELETE method and returns a new QCoapReply 对象。

另请参阅 get (), put (), post (), observe (),和 discover ().

QCoapReply *QCoapClient:: deleteResource (const QUrl & url )

这是重载函数。

Sends a DELETE request to the target url .

另请参阅 get (), put (), post (), observe (),和 discover ().

void QCoapClient:: disconnect ()

Closes the open sockets and connections to free the transport.

注意: In the secure mode this needs to be called before changing the security configuration or connecting to another server.

另请参阅 setSecurityConfiguration ().

QCoapResourceDiscoveryReply *QCoapClient:: discover (const QUrl & url , const QString & discoveryPath = QLatin1String("/.well-known/core"))

Discovers the resources available at the given url 并返回新的 QCoapResourceDiscoveryReply object which emits the QCoapResourceDiscoveryReply::discovered () signal whenever the response arrives.

Discovery path defaults to "/.well-known/core", but can be changed by passing a different path to discoveryPath . Discovery is described in RFC 6690 .

另请参阅 get (), post (), put (), deleteResource (),和 observe ().

QCoapResourceDiscoveryReply *QCoapClient:: discover ( QtCoap::MulticastGroup group = QtCoap::MulticastGroup::AllCoapNodesIPv4, int port = QtCoap::DefaultPort, const QString & discoveryPath = QLatin1String("/.well-known/core"))

这是重载函数。

Discovers the resources available at the endpoints which have joined the group 在给定 port . Returns a new QCoapResourceDiscoveryReply object which emits the QCoapResourceDiscoveryReply::discovered () signal whenever a response arrives. The group is one of the CoAP multicast group addresses and defaults to QtCoap::AllCoapNodesIPv4 .

Discovery path defaults to "/.well-known/core", but can be changed by passing a different path to discoveryPath . Discovery is described in RFC 6690 .

另请参阅 get (), post (), put (), deleteResource (),和 observe ().

[signal] void QCoapClient:: error ( QCoapReply * reply , QtCoap::Error error )

This signal is emitted whenever an error occurs. The reply parameter can be nullptr if the error is not related to a specific QCoapReply error parameter contains the error code.

另请参阅 finished (), QCoapReply::error (),和 QCoapReply::finished ().

[signal] void QCoapClient:: finished ( QCoapReply * reply )

This signal is emitted along with the QCoapReply::finished () signal whenever a CoAP reply is received, after either a success or an error. The reply parameter will contain a pointer to the reply that has just been received.

另请参阅 error (), QCoapReply::finished (),和 QCoapReply::error ().

QCoapReply *QCoapClient:: get (const QCoapRequest & request )

发送 request using the GET method and returns a new QCoapReply 对象。

另请参阅 post (), put (), deleteResource (), observe (),和 discover ().

QCoapReply *QCoapClient:: get (const QUrl & url )

这是重载函数。

Sends a GET request to url 并返回新的 QCoapReply 对象。

另请参阅 post (), put (), deleteResource (), observe (),和 discover ().

QCoapReply *QCoapClient:: observe (const QCoapRequest & request )

Sends a request to observe the target request 并返回新的 QCoapReply object which emits the QCoapReply::notified () signal whenever a new notification arrives.

另请参阅 cancelObserve (), get (), post (), put (), deleteResource (),和 discover ().

QCoapReply *QCoapClient:: observe (const QUrl & url )

这是重载函数。

Sends a request to observe the target url 并返回新的 QCoapReply object which emits the QCoapReply::notified () signal whenever a new notification arrives.

另请参阅 cancelObserve (), get (), post (), put (), deleteResource (),和 discover ().

QCoapReply *QCoapClient:: post (const QCoapRequest & request , const QByteArray & data = QByteArray())

发送 request using the POST method and returns a new QCoapReply object. Uses data as the payload for this request. If data is empty, the payload of the request 会被使用。

另请参阅 get (), put (), deleteResource (), observe (),和 discover ().

QCoapReply *QCoapClient:: post (const QCoapRequest & request , QIODevice * device )

这是重载函数。

发送 request using the POST method and returns a new QCoapReply object. Uses device content as the payload for this request. A null device is treated as empty content.

注意: The device has to be open and readable before calling this function.

另请参阅 get (), put (), deleteResource (), observe (),和 discover ().

QCoapReply *QCoapClient:: post (const QUrl & url , const QByteArray & data = QByteArray())

这是重载函数。

Sends a POST request to url 并返回新的 QCoapReply object. Uses data as the payload for this request.

另请参阅 get (), put (), deleteResource (), observe (),和 discover ().

QCoapReply *QCoapClient:: put (const QCoapRequest & request , const QByteArray & data = QByteArray())

发送 request using the PUT method and returns a new QCoapReply object. Uses data as the payload for this request. If data is empty, the payload of the request 会被使用。

另请参阅 get (), post (), deleteResource (), observe (),和 discover ().

QCoapReply *QCoapClient:: put (const QCoapRequest & request , QIODevice * device )

这是重载函数。

发送 request using the PUT method and returns a new QCoapReply object. Uses device content as the payload for this request. A null device is treated as empty content.

注意: The device has to be open and readable before calling this function.

另请参阅 get (), post (), deleteResource (), observe (),和 discover ().

QCoapReply *QCoapClient:: put (const QUrl & url , const QByteArray & data = QByteArray())

这是重载函数。

Sends a PUT request to url 并返回新的 QCoapReply object. Uses data as the payload for this request.

另请参阅 get (), post (), deleteResource (), observe (),和 discover ().

[signal] void QCoapClient:: responseToMulticastReceived ( QCoapReply * reply , const QCoapMessage & message , const QHostAddress & sender )

This signal is emitted when a unicast response to a multicast request arrives. The reply parameter contains a pointer to the reply that has just been received, message contains the payload and the message details, and sender contains the sender address.

另请参阅 error (), QCoapReply::finished (),和 QCoapReply::error ().

void QCoapClient:: setAckRandomFactor ( double ackRandomFactor )

设置 ACK_RANDOM_FACTOR value defined in RFC 7252 - Section 4.2 ,到 ackRandomFactor . This value should be greater than or equal to 1. The default is 1.5.

另请参阅 setAckTimeout ().

void QCoapClient:: setAckTimeout ( uint ackTimeout )

设置 ACK_TIMEOUT value defined in RFC 7252 - Section 4.2 to ackTimeout in milliseconds. The default is 2000 ms.

This timeout only applies to confirmable messages. The actual timeout for reliable transmissions is a random value between ACK_TIMEOUT and ACK_TIMEOUT * ACK_RANDOM_FACTOR .

另请参阅 setAckRandomFactor ().

void QCoapClient:: setBlockSize ( quint16 blockSize )

Sets the maximum block size used by the protocol to blockSize when sending requests and receiving replies. The block size must be a power of two.

void QCoapClient:: setMaximumRetransmitCount ( uint maximumRetransmitCount )

设置 MAX_RETRANSMIT value defined in RFC 7252 - Section 4.2 to maximumRetransmitCount . This value should be less than or equal to 25. The default is 4.

void QCoapClient:: setMaximumServerResponseDelay ( uint responseDelay )

设置 MAX_SERVER_RESPONSE_DELAY value to responseDelay in milliseconds. The default is 250 seconds.

As defined in RFC 7390 - Section 2.5 , MAX_SERVER_RESPONSE_DELAY is the expected maximum response delay over all servers that the client can send a multicast request to.

void QCoapClient:: setMinimumTokenSize ( int tokenSize )

Sets the minimum token size to tokenSize in bytes. For security reasons it is recommended to use tokens with a length of at least 4 bytes. The default value for this parameter is 4 bytes.

void QCoapClient:: setSecurityConfiguration (const QCoapSecurityConfiguration & configuration )

Sets the security configuration parameters from configuration . Configuration will be ignored if the QtCoap::NoSecurity mode is used.

注意: This method must be called before the handshake starts. If you need to change the security configuration after establishing a secure connection with the server, the client needs to be disconnected first.

另请参阅 disconnect ().

void QCoapClient:: setSocketOption ( QAbstractSocket::SocketOption option , const QVariant & value )

设置 QUdpSocket socket option to value .