dispatch.dart 3.6 KB

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