put method

void put(
  1. String value, {
  2. Encoding? encoding,
  3. ZBytes? attachment,
})

Publishes a string value through this publisher.

Optionally override the encoding for this specific put. An optional attachment can be included (consumed by this call).

Implementation

void put(String value, {Encoding? encoding, ZBytes? attachment}) {
  _ensureOpen();
  final loaned = bindings.zd_publisher_loan(_ptr.cast());
  final payload = ZBytes.fromString(value);

  final encodingStr = encoding != null
      ? encoding.mimeType.toNativeUtf8()
      : nullptr;
  final attachmentPtr = attachment != null ? attachment.nativePtr : nullptr;

  try {
    final rc = bindings.zd_publisher_put(
      loaned,
      payload.nativePtr.cast(),
      encodingStr.cast(),
      attachmentPtr.cast(),
    );

    payload.markConsumed();
    if (attachment != null) attachment.markConsumed();

    if (rc != 0) {
      throw ZenohException('Publisher put failed', rc);
    }
  } finally {
    if (encodingStr != nullptr) malloc.free(encodingStr);
  }
}