open static method

Session open({
  1. Config? config,
})

Opens a Zenoh session.

If config is provided, it is consumed by the session and must not be reused or disposed by the caller. If config is null, a default configuration is created internally.

Throws ZenohException if the session cannot be opened.

Implementation

static Session open({Config? config}) {
  final size = bindings.zd_session_sizeof();
  final Pointer<Void> ptr = calloc.allocate(size);

  Config effectiveConfig;
  bool ownsConfig;
  if (config != null) {
    effectiveConfig = config;
    ownsConfig = false;
  } else {
    effectiveConfig = Config();
    ownsConfig = true;
  }

  final rc = bindings.zd_open_session(
    ptr.cast(),
    effectiveConfig.nativePtr.cast(),
  );

  // Mark user-provided config as consumed regardless of success/failure,
  // because z_config_move already consumed the native pointer.
  if (config != null) {
    config.markConsumed();
  }

  if (rc != 0) {
    calloc.free(ptr);
    if (ownsConfig) effectiveConfig.dispose();
    throw ZenohException('Failed to open session', rc);
  }

  return Session._(ptr);
}