dispatch.dart 4.1 KB

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