dispatch.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'dart:ffi';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:flowy_logger/flowy_logger.dart';
  4. import 'package:flowy_sdk/dispatch/flowy_error.dart';
  5. import 'package:flowy_sdk/protobuf/ffi_response.pb.dart';
  6. import 'package:isolates/isolates.dart';
  7. import 'package:isolates/ports.dart';
  8. import 'package:ffi/ffi.dart';
  9. // ignore: unused_import
  10. import 'package:flutter/services.dart';
  11. import 'dart:async';
  12. import 'dart:typed_data';
  13. import 'package:flowy_sdk/ffi/ffi.dart' as ffi;
  14. import 'package:flowy_sdk/protobuf.dart';
  15. import 'package:protobuf/protobuf.dart';
  16. part 'code_gen.dart';
  17. enum FFIException {
  18. RequestIsEmpty,
  19. }
  20. class DispatchException implements Exception {
  21. FFIException type;
  22. DispatchException(this.type);
  23. }
  24. class Dispatch {
  25. static Future<FFIResponse> asyncRequest(FFIRequest request) {
  26. try {
  27. return _asyncRequest(request).future.then((value) {
  28. try {
  29. final response = FFIResponse.fromBuffer(value);
  30. return Future.microtask(() => response);
  31. } catch (e, s) {
  32. Log.error('FlowyFFI asyncRequest error: ${e.runtimeType}\n');
  33. Log.error('Stack trace \n $s');
  34. final response = error_response(request, "${e.runtimeType}");
  35. return Future.microtask(() => response);
  36. }
  37. });
  38. } catch (e, s) {
  39. Log.error('FlowyFFI asyncRequest error: ${e.runtimeType}\n');
  40. Log.error('Stack trace \n $s');
  41. final response = error_response(request, "${e.runtimeType}");
  42. return Future.microtask(() => response);
  43. }
  44. }
  45. }
  46. Completer<Uint8List> _asyncRequest(FFIRequest request) {
  47. Uint8List bytes = request.writeToBuffer();
  48. assert(bytes.isEmpty == false);
  49. if (bytes.isEmpty) {
  50. throw DispatchException(FFIException.RequestIsEmpty);
  51. }
  52. final Pointer<Uint8> input = calloc.allocate<Uint8>(bytes.length);
  53. final list = input.asTypedList(bytes.length);
  54. list.setAll(0, bytes);
  55. final completer = Completer<Uint8List>();
  56. final port = singleCompletePort(completer);
  57. ffi.async_command(port.nativePort, input, bytes.length);
  58. calloc.free(input);
  59. return completer;
  60. }
  61. FFIResponse error_response(FFIRequest request, String message) {
  62. var response = FFIResponse();
  63. response.code = FFIStatusCode.Err;
  64. response.error = "${request.event}: ${message}";
  65. return response;
  66. }
  67. Either<Uint8List, String> protobufToBytes<T extends GeneratedMessage>(
  68. T? message) {
  69. try {
  70. if (message != null) {
  71. return left(message.writeToBuffer());
  72. } else {
  73. return left(Uint8List.fromList([]));
  74. }
  75. } catch (e, s) {
  76. return right('FlowyFFI error: ${e.runtimeType}. Stack trace: $s');
  77. }
  78. }