Skip to content

Commit

Permalink
[gui] call c code using dart ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-toterman committed Aug 2, 2023
1 parent 642accf commit 083e979
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src/client/desktop_gui/lib/ffi.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'dart:convert';
import 'dart:ffi' as ffi;
import 'dart:io';

import 'package:ffi/ffi.dart';

String _libraryName(String baseName) {
if (Platform.isLinux) return 'lib$baseName.so';
if (Platform.isWindows) return '$baseName.dll';
if (Platform.isMacOS) return 'lib$baseName.dylib';
throw const OSError('OS not supported');
}

extension on ffi.Pointer<Utf8> {
String get string {
if (this == ffi.nullptr) {
throw Exception("Couldn't retrieve data through FFI");
}
final string = toDartString();
malloc.free(this);
return string;
}
}

final _lib = ffi.DynamicLibrary.open(_libraryName('dart_ffi'));

final _multipassVersion = _lib.lookupFunction<ffi.Pointer<Utf8> Function(),
ffi.Pointer<Utf8> Function()>('multipass_version');

final _generatePetname = _lib.lookupFunction<ffi.Pointer<Utf8> Function(),
ffi.Pointer<Utf8> Function()>('generate_petname');

final _getServerAddress = _lib.lookupFunction<ffi.Pointer<Utf8> Function(),
ffi.Pointer<Utf8> Function()>('get_server_address');

final class _NativeKeyCertificatePair extends ffi.Struct {
// ignore: non_constant_identifier_names
external ffi.Pointer<Utf8> pem_cert;

// ignore: non_constant_identifier_names
external ffi.Pointer<Utf8> pem_cert_key;
}

class KeyCertificatePair {
final List<int> cert;
final List<int> key;

KeyCertificatePair(this.cert, this.key);
}

final _getCertPair = _lib.lookupFunction<_NativeKeyCertificatePair Function(),
_NativeKeyCertificatePair Function()>('get_cert_pair');

String get multipassVersion => _multipassVersion().toDartString();

String generatePetname() {
return _generatePetname().string;
}

Uri getServerAddress() {
final address = _getServerAddress().string;
final unixRegex = RegExp('unix:(.+)');
final unixSocketPath = unixRegex.firstMatch(address)?.group(1);
if (unixSocketPath != null) {
return Uri(scheme: InternetAddressType.unix.name, path: unixSocketPath);
}
final tcpRegex = RegExp(r'^(.+):(\d+)$');
final tcpMatch = tcpRegex.firstMatch(address);
if (tcpMatch != null) {
return Uri(host: tcpMatch.group(1), port: int.parse(tcpMatch.group(2)!));
}

throw Exception("Couldn't retrieve data through FFI");
}

KeyCertificatePair getCertPair() {
final pair = _getCertPair();
final cert = utf8.encode(pair.pem_cert.string);
final key = utf8.encode(pair.pem_cert_key.string);
return KeyCertificatePair(cert, key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
/* Begin PBXFileReference section */
333000ED22D3DE5D00554162 /* Warnings.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Warnings.xcconfig; sourceTree = "<group>"; };
335BBD1A22A9A15E00E9071D /* GeneratedPluginRegistrant.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneratedPluginRegistrant.swift; sourceTree = "<group>"; };
33CC10ED2044A3C60003C045 /* desktop_gui.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "desktop_gui.app"; sourceTree = BUILT_PRODUCTS_DIR; };
33CC10ED2044A3C60003C045 /* desktop_gui.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = desktop_gui.app; sourceTree = BUILT_PRODUCTS_DIR; };
33CC10F02044A3C60003C045 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
33CC10F22044A3C60003C045 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = Runner/Assets.xcassets; sourceTree = "<group>"; };
33CC10F52044A3C60003C045 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -366,6 +366,7 @@
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@executable_path/../../../../lib",
);
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -492,6 +493,7 @@
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@executable_path/../../../../lib",
);
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -512,6 +514,7 @@
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
"@executable_path/../../../../lib",
);
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 5.0;
Expand Down
8 changes: 8 additions & 0 deletions src/client/desktop_gui/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.17.1"
ffi:
dependency: "direct main"
description:
name: ffi
sha256: ed5337a5660c506388a9f012be0288fb38b49020ce2b45fe1f8b8323fe429f99
url: "https://pub.dev"
source: hosted
version: "2.0.2"
flutter:
dependency: "direct main"
description: flutter
Expand Down
1 change: 1 addition & 0 deletions src/client/desktop_gui/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:
sdk: '>=3.0.3 <4.0.0'

dependencies:
ffi: ^2.0.2
flutter:
sdk: flutter

Expand Down

0 comments on commit 083e979

Please sign in to comment.