Service constructor

Service(
  1. String libname
)

Creates a Service by opening the shared library at libname, binding the five mandatory C functions, and registering the notification callback.

libname is the path passed to DynamicLibrary.open — typically just the file name (e.g. "libaudio.so") when the library is bundled next to the executable.

Implementation

Service(this.libname) {
  lib = DynamicLibrary.open(libname);

  startService = lib
      .lookup<NativeFunction<Void Function()>>('start_service')
      .asFunction<void Function()>();

  stopService = lib
      .lookup<NativeFunction<Void Function()>>('stop_service')
      .asFunction<void Function()>();

  getNextMessage = lib
      .lookup<NativeFunction<Pointer<BackendMsg> Function()>>(
        'get_next_message',
      )
      .asFunction<Pointer<BackendMsg> Function()>();

  freeMessage = lib
      .lookup<NativeFunction<Void Function(Pointer<BackendMsg>)>>(
        'free_message',
      )
      .asFunction<void Function(Pointer<BackendMsg>)>();

  final setMessageCallback = lib
      .lookup<
              NativeFunction<
                  Void Function(Pointer<NativeFunction<_NotifyNative>>)>>(
          'set_message_callback')
      .asFunction<void Function(Pointer<NativeFunction<_NotifyNative>>)>();

  // NativeCallable.listener is safe to call from any thread: the C++ worker
  // posts the notification and Dart schedules _onNotify on the event loop.
  _callable = NativeCallable<_NotifyNative>.listener(_onNotify);
  setMessageCallback(_callable.nativeFunction);
}