dispatch.dart 4.1 KB

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