Service class
Base class for a C++ shared-library service accessed through dart:ffi.
A service is a shared library (.so / .dll / .dylib) that exports
exactly these five C functions:
void start_service();
void stop_service();
T* get_next_message(); // T is your message struct; nullptr = empty
void free_message(T*);
void set_message_callback(void (*cb)()); // store cb; call it when a message is ready
Instead of a periodic polling timer, each service registers a
NativeCallable with set_message_callback. The C++ worker thread calls
the callback whenever a new message is pushed; Dart then drains the entire
queue in one shot on the event loop — zero CPU consumption at idle.
Subclassing
Extend Service to bind any additional functions your library exposes:
class ColorService extends Service {
ColorService(super.libname) {
getColor = lib
.lookup<NativeFunction<Uint32 Function(Pointer<BackendMsg>)>>('get_color')
.asFunction<int Function(Pointer<BackendMsg>)>();
}
late final int Function(Pointer<BackendMsg>) getColor;
}
Receiving messages
Use assignJob to register a callback that is invoked for every message:
colorService.assignJob((msg) {
myNotifier.value = colorService.getColor(msg);
});
freeMessage is called automatically after the job returns.
Lifecycle
Add the service to a ServicePool (which calls startService for you), or subclass StandaloneService to start the service immediately. Call dispose when done.
- Implementers
Constructors
Properties
-
freeMessage
↔ void Function(Pointer<
BackendMsg> ) -
Bound to the C
free_message()function.getter/setter pair -
getNextMessage
↔ Pointer<
BackendMsg> Function() -
Bound to the C
get_next_message()function.getter/setter pair - hashCode → int
-
The hash code for this object.
no setterinherited
- lib ↔ DynamicLibrary
-
The opened DynamicLibrary. Available to subclasses for binding
additional native functions.
getter/setter pair
- libname → String
-
Path to the shared library, as passed to DynamicLibrary.open.
final
-
messageController
→ StreamController<
Pointer< BackendMsg> > -
Broadcast stream of raw message pointers drained from the C++ queue.
final
-
messageStream
→ Stream<
Pointer< BackendMsg> > -
The broadcast stream fed by messageController.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- startService ↔ void Function()
-
Bound to the C
start_service()function.getter/setter pair - stopService ↔ void Function()
-
Bound to the C
stop_service()function.getter/setter pair
Methods
-
assignJob(
void job(Pointer< BackendMsg> )) → void -
Registers
jobas the handler invoked for every message emitted by this service. -
dispose(
) → void - Stops the service and releases the native callback.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited