veilid/veilid-flutter/lib/veilid_ffi.dart

288 lines
9.5 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
2022-02-10 02:40:01 +00:00
Future<T> processFuturePlain<T>(
T Function(Map<String, dynamic>)? jsonConstructor,
Future<dynamic> future) {
2022-02-09 14:47:36 +00:00
return future.then((value) {
final list = value as List<dynamic>;
switch (list[0] as int) {
case MESSAGE_OK:
{
2022-02-10 02:40:01 +00:00
if (list[1] == null) {
throw VeilidAPIExceptionInternal("Null MESSAGE_OK value");
2022-02-09 14:47:36 +00:00
}
return list[1] as T;
}
case MESSAGE_ERR:
{
2022-02-10 02:40:01 +00:00
throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}");
}
case MESSAGE_ERR_JSON:
{
throw VeilidAPIException.fromJson(jsonDecode(list[1]));
}
default:
{
throw VeilidAPIExceptionInternal(
"Unexpected async return message type: ${list[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<T> processFutureJson<T>(
T Function(Map<String, dynamic>) jsonConstructor,
Future<dynamic> future) {
return future.then((value) {
final list = value as List<dynamic>;
switch (list[0] as int) {
case MESSAGE_ERR:
{
throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}");
2022-02-09 14:47:36 +00:00
}
case MESSAGE_OK_JSON:
{
2022-02-10 02:40:01 +00:00
if (list[1] == null) {
throw VeilidAPIExceptionInternal("Null MESSAGE_OK_JSON value");
2022-02-09 14:47:36 +00:00
}
2022-02-10 02:40:01 +00:00
var ret = jsonDecode(list[1] as String);
return jsonConstructor(ret);
2022-02-09 14:47:36 +00:00
}
case MESSAGE_ERR_JSON:
{
2022-02-10 02:40:01 +00:00
throw VeilidAPIException.fromJson(jsonDecode(list[1]));
2022-02-09 14:47:36 +00:00
}
default:
{
throw VeilidAPIExceptionInternal(
2022-02-10 02:40:01 +00:00
"Unexpected async return message type: ${list[0]}");
2022-02-09 14:47:36 +00:00
}
}
}).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-10 02:40:01 +00:00
Future<void> processFutureVoid(Future<dynamic> future) {
2022-02-09 14:47:36 +00:00
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:
{
2022-02-10 02:40:01 +00:00
throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}");
2022-02-09 14:47:36 +00:00
}
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:
{
2022-02-10 02:40:01 +00:00
throw VeilidAPIException.fromJson(jsonDecode(list[1] as String));
2022-02-09 14:47:36 +00:00
}
default:
{
throw VeilidAPIExceptionInternal(
2022-02-10 02:40:01 +00:00
"Unexpected async return message type: ${list[0]}");
2022-02-09 14:47:36 +00:00
}
}
}).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
2022-02-10 02:40:01 +00:00
Stream<VeilidUpdate> startupVeilidCore(VeilidConfig config) {
var nativeConfig = jsonEncode(config.json, toEncodable: veilidApiToEncodable).toNativeUtf8();
final recvPort = ReceivePort("startup_veilid_core");
final sendPort = recvPort.sendPort;
_startupVeilidCore(sendPort.nativePort, nativeConfig);
malloc.free(nativeConfig);
xxx
return processStreamJson(VeilidUpdate.fromJson, recvPort);
}
2022-02-07 02:18:42 +00:00
2022-02-09 14:47:36 +00:00
@override
Future<VeilidState> getVeilidState() async {
2022-02-10 02:40:01 +00:00
final recvPort = ReceivePort("get_veilid_state");
final sendPort = recvPort.sendPort;
_shutdownVeilidCore(sendPort.nativePort);
return processFutureJson(VeilidState.fromJson, recvPort.single);
2022-02-07 02:18:42 +00:00
}
2022-02-09 14:47:36 +00:00
@override
2022-02-10 02:40:01 +00:00
Future<void> changeApiLogLevel(VeilidConfigLogLevel logLevel) async {
var nativeLogLevel = logLevel.json.toNativeUtf8();
final recvPort = ReceivePort("change_api_log_level");
final sendPort = recvPort.sendPort;
_changeApiLogLevel(sendPort.nativePort, nativeLogLevel);
2022-02-09 14:47:36 +00:00
malloc.free(nativeLogLevel);
2022-02-10 02:40:01 +00:00
return processFutureVoid(recvPort.single);
2022-02-07 02:18:42 +00:00
}
2022-02-09 14:47:36 +00:00
@override
Future<void> shutdownVeilidCore() async {
2022-02-10 02:40:01 +00:00
final recvPort = ReceivePort("shutdown_veilid_core");
final sendPort = recvPort.sendPort;
_shutdownVeilidCore(sendPort.nativePort);
return processFutureVoid(recvPort.single);
2022-02-09 14:47:36 +00:00
}
@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
}
}