put method
Publishes a string value on the given keyExpr.
Optionally set the encoding (MIME type) of the message. An optional
attachment can be included; it is consumed by this call and must not
be reused.
Throws ZenohException if the key expression is invalid, the encoding is malformed, or the put fails. Throws StateError if the session has been closed, or the attachment has been disposed or already consumed.
Implementation
void put(
String keyExpr,
String value, {
Encoding? encoding,
ZBytes? attachment,
}) {
final attachmentPtr = attachment != null ? attachment.nativePtr : nullptr;
final encodingStr = encoding != null
? encoding.mimeType.toNativeUtf8()
: nullptr;
_withKeyExpr(keyExpr, (loanedSession, loanedKe) {
final payload = ZBytes.fromString(value);
try {
final rc = bindings.zd_put(
loanedSession.cast(),
loanedKe.cast(),
payload.nativePtr.cast(),
encodingStr.cast(),
attachmentPtr.cast(),
);
// markConsumed is unconditional: z_bytes_move gravestones the owned
// bytes regardless of the return code.
payload.markConsumed();
if (attachment != null) attachment.markConsumed();
if (rc != 0) {
throw ZenohException('Put failed', rc);
}
} finally {
if (encodingStr != nullptr) malloc.free(encodingStr);
}
});
}