QCoapMessage Class
QCoapMessage 类存储有关 CoAP 消息的信息,该消息可以是请求,也可以是响应。更多内容...
| 头文件: | #include <QCoapMessage> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Coap) target_link_libraries(mytarget PRIVATE Qt6::Coap) |
| qmake: | QT += coap |
| 由以下接口继承: |
注:本类中的所有函数均为可重入函数。
公共类型
| enum class | Type { Confirmable, NonConfirmable, Acknowledgment, Reset } |
公共函数
| QCoapMessage() | |
| QCoapMessage(const QCoapMessage &other) | |
| ~QCoapMessage() | |
| void | addOption(const QCoapOption &option) |
| void | addOption(QCoapOption::OptionName name, const QByteArray &value = QByteArray()) |
| void | clearOptions() |
| bool | hasOption(QCoapOption::OptionName name) const |
| quint16 | messageId() const |
| QCoapOption | option(QCoapOption::OptionName name) const |
| QCoapOption | optionAt(int index) const |
| int | optionCount() const |
| const QList<QCoapOption> & | options() const |
| QList<QCoapOption> | options(QCoapOption::OptionName name) const |
| QByteArray | payload() const |
| void | removeOption(QCoapOption::OptionName name) |
| void | removeOption(const QCoapOption &option) |
| void | setMessageId(quint16 id) |
| void | setOptions(const QList<QCoapOption> &options) |
| void | setPayload(const QByteArray &payload) |
| void | setToken(const QByteArray &token) |
| void | setType(const QCoapMessage::Type &type) |
| void | setVersion(quint8 version) |
| void | swap(QCoapMessage &other) |
| QByteArray | token() const |
| quint8 | tokenLength() const |
| QCoapMessage::Type | type() const |
| quint8 | version() const |
| QCoapMessage & | operator=(QCoapMessage &&other) |
| QCoapMessage & | operator=(const QCoapMessage &other) |
另请参阅 QCoapOption 、QCoapReply 和QCoapRequest 。
成员类型文档
enum class QCoapMessage::Type
表示消息的类型。
| 常量 | 值 | 描述 |
|---|---|---|
QCoapMessage::Type::Confirmable | 0 | 可确认消息。目标端点需要对该消息进行确认。 |
QCoapMessage::Type::NonConfirmable | 1 | 不可确认的消息。目标端点无需对该消息进行确认。 |
QCoapMessage::Type::Acknowledgment | 2 | 确认消息。作为对可确认消息的响应而发送或接收的消息。 |
QCoapMessage::Type::Reset | 3 | 重置消息。此消息类型用于处理错误或停止正在进行的传输。(例如,用于取消一项观测)。 |
成员函数文档
QCoapMessage::QCoapMessage()
创建一个新的 QCoapMessage 对象。
QCoapMessage::QCoapMessage(const QCoapMessage &other)
创建other 的浅拷贝。
[noexcept] QCoapMessage::~QCoapMessage()
销毁QCoapMessage 。
void QCoapMessage::addOption(const QCoapOption &option)
添加指定的 CoAPoption 。
void QCoapMessage::addOption(QCoapOption::OptionName name, const QByteArray &value = QByteArray())
添加 CoAP 选项,并指定name 和value 。
这是一个重载函数。
void QCoapMessage::clearOptions()
清除所有选项。
bool QCoapMessage::hasOption(QCoapOption::OptionName name) const
如果消息中至少包含一个带有name 的选项,则返回true 。
quint16 QCoapMessage::messageId() const
返回消息 ID。
另请参阅 setMessageId()。
QCoapOption QCoapMessage::option(QCoapOption::OptionName name) const
查找并返回第一个具有指定name 的选项。如果不存在此类选项,则返回一个值为空的无效QCoapOption 。
QCoapOption QCoapMessage::optionAt(int index) const
返回位于index 位置的选项。
int QCoapMessage::optionCount() const
返回选项的数量。
const QList<QCoapOption> &QCoapMessage::options() const
返回选项列表。
另请参阅 setOptions()。
QList<QCoapOption> QCoapMessage::options(QCoapOption::OptionName name) const
查找并返回具有给定name 的选项列表。
QByteArray QCoapMessage::payload() const
返回有效载荷。
另请参阅 setPayload()。
void QCoapMessage::removeOption(QCoapOption::OptionName name)
删除所有包含指定name 的选项。CoAP协议允许同一选项重复出现。
void QCoapMessage::removeOption(const QCoapOption &option)
删除指定的option 。
void QCoapMessage::setMessageId(quint16 id)
将消息 ID 设置为id 。
另请参阅 messageId()。
void QCoapMessage::setOptions(const QList<QCoapOption> &options)
将消息选项设置为options 。
另请参阅 options()。
void QCoapMessage::setPayload(const QByteArray &payload)
将消息负载设置为payload 。该负载可以采用CoAP内容格式注册表中定义的任一内容格式表示。
注意:CoAP 支持 XML、JSON 等常见内容格式,但这些格式基于文本,因此无论在有效载荷还是处理方面都较为耗资源。推荐与 CoAP 配合使用的内容格式之一是 CBOR,该格式正是为这类场景而设计的。
另请参阅 payload()、QCborStreamWriter 以及QCborStreamReader 。
void QCoapMessage::setToken(const QByteArray &token)
将消息令牌设置为token 。
另请参阅 token()。
void QCoapMessage::setType(const QCoapMessage::Type &type)
将消息类型设置为type 。
另请参阅 type()。
void QCoapMessage::setVersion(quint8 version)
将 CoAP 版本设置为version 。
另请参阅 version()。
[noexcept] void QCoapMessage::swap(QCoapMessage &other)
将此消息与other 进行交换。此操作速度极快,且绝不会失败。
QByteArray QCoapMessage::token() const
返回消息令牌。
另请参阅 setToken()。
quint8 QCoapMessage::tokenLength() const
返回令牌的长度。
QCoapMessage::Type QCoapMessage::type() const
返回消息类型。
另请参阅 setType()。
quint8 QCoapMessage::version() const
返回 CoAP 版本。
另请参阅 setVersion()。
[noexcept] QCoapMessage &QCoapMessage::operator=(QCoapMessage &&other)
将 `other ` 移入此消息中,并返回对此 `QCoapMessage` 的引用。
QCoapMessage &QCoapMessage::operator=(const QCoapMessage &other)
将other 中的内容复制到此消息中。返回对此QCoapMessage 的引用。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.