veilid/veilid-flutter/lib/veilid_ffi.dart

251 lines
8.4 KiB
Dart
Raw Normal View History

2022-02-07 02:18:42 +00:00
import 'dart:async';
2022-02-09 14:47:36 +00:00
import 'dart:ffi';
2022-02-07 02:18:42 +00:00
import 'dart:io';
2022-02-09 14:47:36 +00:00
import 'dart:isolate';
import 'dart:convert';
2022-02-07 02:18:42 +00:00
import 'package:ffi/ffi.dart';
2022-02-09 14:47:36 +00:00
import 'veilid.dart';
2022-02-07 02:18:42 +00:00
//////////////////////////////////////////////////////////
// Load the veilid_flutter library once
const _base = 'veilid_flutter';
final _path = Platform.isWindows
? '$_base.dll'
: Platform.isMacOS
? 'lib$_base.dylib'
: 'lib$_base.so';
2022-02-09 14:47:36 +00:00
late final _dylib =
Platform.isIOS ? DynamicLibrary.process() : DynamicLibrary.open(_path);
2022-02-07 02:18:42 +00:00
// Linkage for initialization
2022-02-09 14:47:36 +00:00
typedef _dart_postCObject
= NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>;
2022-02-07 02:18:42 +00:00
// fn free_string(s: *mut std::os::raw::c_char)
typedef _free_string_C = Void Function(Pointer<Utf8>);
typedef _free_string_Dart = void Function(Pointer<Utf8>);
// fn initialize_veilid_flutter(dart_post_c_object_ptr: ffi::DartPostCObjectFnType)
typedef _initializeVeilidFlutter_C = Void Function(Pointer<_dart_postCObject>);
2022-02-09 14:47:36 +00:00
typedef _initializeVeilidFlutter_Dart = void Function(
Pointer<_dart_postCObject>);
2022-02-07 02:18:42 +00:00
// fn startup_veilid_core(port: i64, config: FfiStr)
typedef _startup_veilid_core_C = Void Function(Int64, Pointer<Utf8>);
typedef _startup_veilid_core_Dart = void Function(int, Pointer<Utf8>);
// fn get_veilid_state(port: i64)
typedef _get_veilid_state_C = Void Function(Int64);
typedef _get_veilid_state_Dart = void Function(int);
// fn change_api_log_level(port: i64, log_level: FfiStr)
typedef _change_api_log_level_C = Void Function(Int64, Pointer<Utf8>);
typedef _change_api_log_level_Dart = void Function(int, Pointer<Utf8>);
// fn shutdown_veilid_core(port: i64)
typedef _shutdown_veilid_core_C = Void Function(Int64);
typedef _shutdown_veilid_core_Dart = void Function(int);
// fn veilid_version_string() -> *mut c_char
typedef _veilid_version_string_C = Pointer<Utf8> Function();
typedef _veilid_version_string_Dart = Pointer<Utf8> Function();
2022-02-09 14:47:36 +00:00
2022-02-07 02:18:42 +00:00
// fn veilid_version() -> VeilidVersion
2022-02-09 14:47:36 +00:00
class VeilidVersionFFI extends Struct {
2022-02-07 02:18:42 +00:00
@Uint32()
external int major;
@Uint32()
external int minor;
@Uint32()
external int patch;
}
2022-02-09 14:47:36 +00:00
typedef _veilid_version_C = VeilidVersionFFI Function();
typedef _veilid_version_Dart = VeilidVersionFFI Function();
// Async message types
const int MESSAGE_OK = 0;
const int MESSAGE_ERR = 1;
const int MESSAGE_OK_JSON = 2;
const int MESSAGE_ERR_JSON = 3;
const int MESSAGE_STREAM_ITEM = 4;
const int MESSAGE_STREAM_ITEM_JSON = 5;
const int MESSAGE_STREAM_ABORT = 6;
const int MESSAGE_STREAM_ABORT_JSON = 7;
const int MESSAGE_STREAM_CLOSE = 8;
2022-02-07 02:18:42 +00:00
// Interface factory for high level Veilid API
Veilid getVeilid() => VeilidFFI(_dylib);
2022-02-09 14:47:36 +00:00
// Parse handle async returns
Future<T> processSingleAsyncReturn<T>(Future<dynamic> future) async {
return future.then((value) {
final list = value as List<dynamic>;
switch (list[0] as int) {
case MESSAGE_OK:
{
if (list[1] != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK value '${list[1]}' where null expected");
}
return list[1] as T;
}
case MESSAGE_ERR:
{
throw VeilidAPIExceptionInternal("Internal API Error: ${value[1]}");
}
case MESSAGE_OK_JSON:
{
var ret = jsonDecode(list[1] as String);
if (ret != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK_JSON value '$ret' where null expected");
}
return ret as T;
}
case MESSAGE_ERR_JSON:
{
throw VeilidAPIException.fromJson(value[1] as String);
}
default:
{
throw VeilidAPIExceptionInternal(
"Unexpected async return message type: ${value[0]}");
}
}
}).catchError((e) {
// Wrap all other errors in VeilidAPIExceptionInternal
throw VeilidAPIExceptionInternal(e.toString());
}, test: (e) {
// Pass errors that are already VeilidAPIException through without wrapping
return e is! VeilidAPIException;
});
}
Future<void> processSingleAsyncVoid(Future<dynamic> future) async {
return future.then((value) {
final list = value as List<dynamic>;
switch (list[0] as int) {
case MESSAGE_OK:
{
if (list[1] != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK value '${list[1]}' where null expected");
}
return;
}
case MESSAGE_ERR:
{
throw VeilidAPIExceptionInternal("Internal API Error: ${value[1]}");
}
case MESSAGE_OK_JSON:
{
var ret = jsonDecode(list[1] as String);
if (ret != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK_JSON value '$ret' where null expected");
}
return;
}
case MESSAGE_ERR_JSON:
{
throw VeilidAPIException.fromJson(value[1] as String);
}
default:
{
throw VeilidAPIExceptionInternal(
"Unexpected async return message type: ${value[0]}");
}
}
}).catchError((e) {
// Wrap all other errors in VeilidAPIExceptionInternal
throw VeilidAPIExceptionInternal(e.toString());
}, test: (e) {
// Pass errors that are already VeilidAPIException through without wrapping
return e is! VeilidAPIException;
});
}
2022-02-07 02:18:42 +00:00
// FFI implementation of high level Veilid API
2022-02-09 14:47:36 +00:00
class VeilidFFI implements Veilid {
2022-02-07 02:18:42 +00:00
// veilid_core shared library
final DynamicLibrary _dylib;
// Shared library functions
final _free_string_Dart _freeString;
final _startup_veilid_core_Dart _startupVeilidCore;
final _get_veilid_state_Dart _getVeilidState;
final _change_api_log_level_Dart _changeApiLogLevel;
final _shutdown_veilid_core_Dart _shutdownVeilidCore;
final _veilid_version_string_Dart _veilidVersionString;
2022-02-09 14:47:36 +00:00
final _veilid_version_Dart _veilidVersion;
2022-02-07 02:18:42 +00:00
2022-02-09 14:47:36 +00:00
VeilidFFI(DynamicLibrary dylib)
: _dylib = dylib,
_freeString = dylib
.lookupFunction<_free_string_C, _free_string_Dart>('free_string'),
_startupVeilidCore = dylib.lookupFunction<_startup_veilid_core_C,
_startup_veilid_core_Dart>('startup_veilid_core'),
_getVeilidState =
dylib.lookupFunction<_get_veilid_state_C, _get_veilid_state_Dart>(
'get_veilid_state'),
_changeApiLogLevel = dylib.lookupFunction<_change_api_log_level_C,
_change_api_log_level_Dart>('change_api_log_level'),
_shutdownVeilidCore = dylib.lookupFunction<_shutdown_veilid_core_C,
_shutdown_veilid_core_Dart>('shutdown_veilid_core'),
_veilidVersionString = dylib.lookupFunction<_veilid_version_string_C,
_veilid_version_string_Dart>('veilid_version_string'),
_veilidVersion =
dylib.lookupFunction<_veilid_version_C, _veilid_version_Dart>(
'veilid_version') {
// Get veilid_flutter initializer
var initializeVeilidFlutter = _dylib.lookupFunction<
_initializeVeilidFlutter_C,
_initializeVeilidFlutter_Dart>('initialize_veilid_flutter');
2022-02-07 02:18:42 +00:00
initializeVeilidFlutter(NativeApi.postCObject);
2022-02-09 14:47:36 +00:00
}
@override
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config) async* {}
2022-02-07 02:18:42 +00:00
2022-02-09 14:47:36 +00:00
@override
Future<VeilidState> getVeilidState() async {
final recv_port = ReceivePort("shutdown_veilid_core");
final send_port = recv_port.sendPort;
_shutdownVeilidCore(send_port.nativePort);
processSingleAsyncReturn(recv_port.single);
2022-02-07 02:18:42 +00:00
}
2022-02-09 14:47:36 +00:00
@override
Future<void> changeApiLogLevel(VeilidLogLevel logLevel) async {
var nativeLogLevel = jsonEncode(logLevel).toNativeUtf8();
final recv_port = ReceivePort("change_api_log_level");
final send_port = recv_port.sendPort;
_changeApiLogLevel(send_port.nativePort, nativeLogLevel);
malloc.free(nativeLogLevel);
processSingleAsyncVoid(recv_port.single);
2022-02-07 02:18:42 +00:00
}
2022-02-09 14:47:36 +00:00
@override
Future<void> shutdownVeilidCore() async {
final recv_port = ReceivePort("shutdown_veilid_core");
final send_port = recv_port.sendPort;
_shutdownVeilidCore(send_port.nativePort);
processSingleAsyncVoid(recv_port.single);
}
@override
2022-02-07 02:18:42 +00:00
String veilidVersionString() {
2022-02-09 14:47:36 +00:00
final versionString = _veilidVersionString();
String ret = versionString.toDartString();
_freeString(versionString);
return ret;
2022-02-07 02:18:42 +00:00
}
2022-02-09 14:47:36 +00:00
@override
2022-02-07 02:18:42 +00:00
VeilidVersion veilidVersion() {
2022-02-09 14:47:36 +00:00
final version = _veilidVersion();
return VeilidVersion(
version.major,
version.minor,
version.patch,
);
2022-02-07 02:18:42 +00:00
}
}