dispatch.dart 4.0 KB

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