dispatch.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import 'dart:ffi';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:appflowy_backend/log.dart';
  4. // ignore: unnecessary_import
  5. import 'package:appflowy_backend/protobuf/dart-ffi/ffi_response.pb.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-net/network_state.pb.dart';
  8. import 'package:isolates/isolates.dart';
  9. import 'package:isolates/ports.dart';
  10. import 'package:ffi/ffi.dart';
  11. import 'package:flutter/services.dart';
  12. import 'dart:async';
  13. import 'dart:typed_data';
  14. import 'package:appflowy_backend/ffi.dart' as ffi;
  15. import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
  16. import 'package:appflowy_backend/protobuf/dart-ffi/protobuf.dart';
  17. import 'package:appflowy_backend/protobuf/flowy-folder2/protobuf.dart';
  18. import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart';
  19. import 'package:appflowy_backend/protobuf/flowy-document2/protobuf.dart';
  20. // ignore: unused_import
  21. import 'package:protobuf/protobuf.dart';
  22. import 'dart:convert' show utf8;
  23. import '../protobuf/flowy-config/entities.pb.dart';
  24. import '../protobuf/flowy-config/event_map.pb.dart';
  25. import '../protobuf/flowy-net/event_map.pb.dart';
  26. import 'error.dart';
  27. part 'dart_event/flowy-folder2/dart_event.dart';
  28. part 'dart_event/flowy-net/dart_event.dart';
  29. part 'dart_event/flowy-user/dart_event.dart';
  30. part 'dart_event/flowy-database2/dart_event.dart';
  31. part 'dart_event/flowy-document2/dart_event.dart';
  32. part 'dart_event/flowy-config/dart_event.dart';
  33. enum FFIException {
  34. RequestIsEmpty,
  35. }
  36. class DispatchException implements Exception {
  37. FFIException type;
  38. DispatchException(this.type);
  39. }
  40. class Dispatch {
  41. static Future<Either<Uint8List, Uint8List>> asyncRequest(FFIRequest request) {
  42. // FFIRequest => Rust SDK
  43. final bytesFuture = _sendToRust(request);
  44. // Rust SDK => FFIResponse
  45. final responseFuture = _extractResponse(bytesFuture);
  46. // FFIResponse's payload is the bytes of the Response object
  47. final payloadFuture = _extractPayload(responseFuture);
  48. return payloadFuture;
  49. }
  50. }
  51. Future<Either<Uint8List, Uint8List>> _extractPayload(
  52. Future<Either<FFIResponse, FlowyInternalError>> responseFuture) {
  53. return responseFuture.then((result) {
  54. return result.fold(
  55. (response) {
  56. switch (response.code) {
  57. case FFIStatusCode.Ok:
  58. return left(Uint8List.fromList(response.payload));
  59. case FFIStatusCode.Err:
  60. return right(Uint8List.fromList(response.payload));
  61. case FFIStatusCode.Internal:
  62. final error = utf8.decode(response.payload);
  63. Log.error("Dispatch internal error: $error");
  64. return right(emptyBytes());
  65. default:
  66. Log.error("Impossible to here");
  67. return right(emptyBytes());
  68. }
  69. },
  70. (error) {
  71. Log.error("Response should not be empty $error");
  72. return right(emptyBytes());
  73. },
  74. );
  75. });
  76. }
  77. Future<Either<FFIResponse, FlowyInternalError>> _extractResponse(
  78. Completer<Uint8List> bytesFuture) {
  79. return bytesFuture.future.then((bytes) {
  80. try {
  81. final response = FFIResponse.fromBuffer(bytes);
  82. return left(response);
  83. } catch (e, s) {
  84. final error = StackTraceError(e, s);
  85. Log.error('Deserialize response failed. ${error.toString()}');
  86. return right(error.asFlowyError());
  87. }
  88. });
  89. }
  90. Completer<Uint8List> _sendToRust(FFIRequest request) {
  91. Uint8List bytes = request.writeToBuffer();
  92. assert(bytes.isEmpty == false);
  93. if (bytes.isEmpty) {
  94. throw DispatchException(FFIException.RequestIsEmpty);
  95. }
  96. final Pointer<Uint8> input = calloc.allocate<Uint8>(bytes.length);
  97. final list = input.asTypedList(bytes.length);
  98. list.setAll(0, bytes);
  99. final completer = Completer<Uint8List>();
  100. final port = singleCompletePort(completer);
  101. ffi.async_event(port.nativePort, input, bytes.length);
  102. calloc.free(input);
  103. return completer;
  104. }
  105. Uint8List requestToBytes<T extends GeneratedMessage>(T? message) {
  106. try {
  107. if (message != null) {
  108. return message.writeToBuffer();
  109. } else {
  110. return emptyBytes();
  111. }
  112. } catch (e, s) {
  113. final error = StackTraceError(e, s);
  114. Log.error('Serial request failed. ${error.toString()}');
  115. return emptyBytes();
  116. }
  117. }
  118. Uint8List emptyBytes() {
  119. return Uint8List.fromList([]);
  120. }