@@ -7,82 +7,82 @@ import 'package:ffi/ffi.dart';
77
88import 'package:flutter/services.dart' ;
99
10+ import '../common/utils/util.dart' ;
1011import '../generated/ffi/libserver.dart' ;
1112
1213class GrpcService {
1314 static const platform = MethodChannel ("honmaple.com/maple_file" );
1415
16+ late String _addr;
17+ late grpcapi.ClientChannel _client;
18+
19+ String get addr {
20+ return _addr;
21+ }
22+
23+ grpcapi.ClientChannel get client {
24+ return _client;
25+ }
26+
1527 Future <void > init () async {
16- if (Platform .isMacOS ) {
17- await _initDesktop ();
28+ if (Util .isDesktop ) {
29+ _addr = await _initDesktop ();
1830 } else {
19- await _initMobile ();
31+ _addr = await _initMobile ();
2032 }
33+ _client = _createChannel (addr);
2134 }
2235
23- _initMobile () async {
36+ Future < String > _initMobile () async {
2437 final directory = await getApplicationDocumentsDirectory ();
2538 final Map <String , dynamic > args = {
2639 'path' : directory.path,
2740 };
2841
29- await platform.invokeMethod ("Start" , args).then ((addr) {
30- _createChannel (addr);
31- }).catchError ((err) {
42+ return await platform.invokeMethod ("Start" , args).catchError ((err) {
3243 print (err);
3344 });
3445 }
3546
3647 // https://docs.flutter.dev/platform-integration/macos/c-interop
37- _initDesktop () async {
38- if (Platform .isMacOS) {}
39-
40- var libname = "libserver.dylib" ;
48+ Future <String > _initDesktop () async {
49+ var libname = "libserver" ;
50+ if (Util .isWindows) {
51+ libname += ".dll" ;
52+ } else if (Util .isMacOS) {
53+ libname += ".dylib" ;
54+ } else if (Util .isLinux) {
55+ libname += ".so" ;
56+ }
4157
42- final lib = LibserverBind (ffi.DynamicLibrary .open (libname));
4358 final directory = await getApplicationDocumentsDirectory ();
4459 final path = directory.path;
45- print (path);
4660
4761 // ffi.Char
62+ final lib = LibserverBind (ffi.DynamicLibrary .open (libname));
4863 final result = lib.Start (path.toNativeUtf8 ().cast ());
4964 if (result.r1.address == ffi.nullptr.address) {
50- _createChannel (result.r0.cast <Utf8 >().toDartString ());
51- } else {
52- print (result.r1.cast <Utf8 >().toDartString ());
65+ return Future .value (result.r0.cast <Utf8 >().toDartString ());
5366 }
67+ return Future .error (result.r1.cast <Utf8 >().toDartString ());
5468 }
5569
56- late String _addr;
57- late grpcapi.ClientChannel _client;
58-
59- String get addr {
60- return _addr;
61- }
62-
63- grpcapi.ClientChannel get client {
64- return _client;
65- }
66-
67- _createChannel (String addr) {
70+ grpcapi.ClientChannel _createChannel (String addr) {
6871 const options = ChannelOptions (
6972 credentials: ChannelCredentials .insecure (),
7073 );
7174 if (addr.endsWith (".sock" )) {
72- _addr = addr;
73- _client = ClientChannel (
75+ return ClientChannel (
7476 InternetAddress (addr, type: InternetAddressType .unix),
7577 options: options,
7678 );
77- } else {
78- final addrs = addr.split (":" );
79-
80- _addr = addr;
81- _client = ClientChannel (
82- addrs[0 ],
83- port: int .parse (addrs[1 ]),
84- options: options,
85- );
8679 }
80+
81+ final addrs = addr.split (":" );
82+ return ClientChannel (
83+ addrs[0 ],
84+ port: int .parse (addrs[1 ]),
85+ options: options,
86+ );
8787 }
8888}
0 commit comments