build method

Pointer<OBX_mesh_options> build()

Builds the native mesh options object from this configuration.

The caller takes ownership of the returned pointer; it must either be passed to sync_opt_mesh (which frees it) or freed via mesh_opt_free. If building fails, the options are freed and the error is rethrown.

Implementation

Pointer<OBX_mesh_options> build() {
  final opt = checkObxPtr(
    withNativeString(meshId, C.mesh_opt),
    'failed to create mesh options (mesh ID: "$meshId")',
  );
  try {
    if (maxConnectionCount != null) {
      checkObx(C.mesh_opt_max_connection_count(opt, maxConnectionCount!));
    }
    if (backoffMillis != null) {
      checkObx(C.mesh_opt_backoff_millis(opt, backoffMillis!));
    }
    if (evictionBackoffMillis != null) {
      checkObx(
        C.mesh_opt_eviction_backoff_millis(opt, evictionBackoffMillis!),
      );
    }
    if (randomSeed != null) {
      checkObx(C.mesh_opt_random_seed(opt, randomSeed!));
    }
    if (requestTimeoutMillis != null) {
      checkObx(C.mesh_opt_request_timeout_millis(opt, requestTimeoutMillis!));
    }
    if (advertisingDelayMillis != null) {
      checkObx(
        C.mesh_opt_advertising_delay_millis(opt, advertisingDelayMillis!),
      );
    }
    if (advertisingRetryMillis != null) {
      checkObx(
        C.mesh_opt_advertising_retry_millis(opt, advertisingRetryMillis!),
      );
    }
    if (advertisingRetryMaxMillis != null) {
      checkObx(
        C.mesh_opt_advertising_retry_max_millis(
          opt,
          advertisingRetryMaxMillis!,
        ),
      );
    }
    if (connectDelayMillis != null) {
      checkObx(C.mesh_opt_connect_delay_millis(opt, connectDelayMillis!));
    }
    if (initialDiscoveryDurationSeconds != null) {
      checkObx(
        C.mesh_opt_initial_discovery_duration_seconds(
          opt,
          initialDiscoveryDurationSeconds!,
        ),
      );
    }
    if (discoveryDurationSeconds != null) {
      checkObx(
        C.mesh_opt_discovery_duration_seconds(opt, discoveryDurationSeconds!),
      );
    }
    if (discoveryPauseSeconds != null) {
      checkObx(
        C.mesh_opt_discovery_pause_seconds(opt, discoveryPauseSeconds!),
      );
    }
    if (discoveryPauseJitterSeconds != null) {
      checkObx(
        C.mesh_opt_discovery_pause_jitter_seconds(
          opt,
          discoveryPauseJitterSeconds!,
        ),
      );
    }
    if (txLogBatchSizeKb != null) {
      checkObx(C.mesh_opt_tx_log_batch_size_kb(opt, txLogBatchSizeKb!));
    }
    if (txLogBatchMaxCount != null) {
      checkObx(C.mesh_opt_tx_log_batch_max_count(opt, txLogBatchMaxCount!));
    }
    if (txLogMaxAgeSeconds != null) {
      checkObx(C.mesh_opt_tx_log_max_age_seconds(opt, txLogMaxAgeSeconds!));
    }
    for (final handle in _networkInternalHandles) {
      checkObx(
        C.mesh_opt_network_internal(opt, Pointer<Void>.fromAddress(handle)),
      );
    }
    for (final handle in _networkHandles) {
      checkObx(
        C.mesh_opt_network(
          opt,
          Pointer<OBX_mesh_network>.fromAddress(handle),
        ),
      );
    }
  } catch (e) {
    // Free the options if any option method call failed (like due to invalid
    // arguments).
    C.mesh_opt_free(opt);
    rethrow;
  }
  return opt;
}