dispatch.dart 4.1 KB

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