QCoapMessage Class

QCoapMessage 类保存有关 CoAP 消息的信息,该消息可以是请求也可以是回复。更多

Header: #include <QCoapMessage>
CMake: find_package(Qt6 REQUIRED COMPONENTS Coap)
target_link_libraries(mytarget PRIVATE Qt6::Coap)
qmake: QT += coap
继承于

QCoapRequest

注意:该类中的所有函数都是可重入的

公共类型

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)

详细说明

该类保存信息,如消息类型、消息 ID、标记和其他辅助数据。

另请参阅 QCoapOption,QCoapReply, 和QCoapRequest

成员类型文档

enum class QCoapMessage::Type

表示信息的类型。

常量说明
QCoapMessage::Type::Confirmable0可确认报文。目的端点需要确认信息。
QCoapMessage::Type::NonConfirmable1不可确认信息。目标端点无需确认信息。
QCoapMessage::Type::Acknowledgment2确认信息。作为对可确认信息的回复而发送或接收的信息。
QCoapMessage::Type::Reset3重置报文。这种报文类型用于发生错误或停止正在进行的传输。(例如,用于取消观测)。

成员函数文档

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())

这是一个重载函数。

使用给定的namevalue 添加 CoAP 选项。

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 的引用。

© 2025 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.