putBytes method
Publishes a ZBytes payload on the given keyExpr.
The payload is consumed by this call and must not be reused.
Optionally set the encoding (MIME type) of the message. An optional
attachment can be included; it is also consumed by this call.
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 payload or attachment has been disposed or already consumed.
Implementation
void putBytes(
String keyExpr,
ZBytes payload, {
Encoding? encoding,
ZBytes? attachment,
}) {
_ensureOpen();
// Validate payload state before allocating KeyExpr
final payloadPtr = payload.nativePtr;
final attachmentPtr = attachment != null ? attachment.nativePtr : nullptr;
final encodingStr = encoding != null
? encoding.mimeType.toNativeUtf8()
: nullptr;
_withKeyExpr(keyExpr, (loanedSession, loanedKe) {
try {
final rc = bindings.zd_put(
loanedSession.cast(),
loanedKe.cast(),
payloadPtr.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);
}
});
}