dispatch.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (response.code == FFIStatusCode.Ok) {
  47. return left(Uint8List.fromList(response.payload));
  48. } else {
  49. // final error = utf8.decode(response.payload);
  50. // Log.error("Dispatch error: $error");
  51. return right(Uint8List.fromList(response.payload));
  52. }
  53. },
  54. (error) {
  55. Log.error("Response should not be empty $error");
  56. return right(emptyBytes());
  57. },
  58. );
  59. });
  60. }
  61. Future<Either<FFIResponse, FlowyError>> _extractResponse(
  62. Completer<Uint8List> bytesFuture) {
  63. return bytesFuture.future.then((bytes) {
  64. try {
  65. final response = FFIResponse.fromBuffer(bytes);
  66. return left(response);
  67. } catch (e, s) {
  68. final error = StackTraceError(e, s);
  69. Log.error('Deserialize response failed. ${error.toString()}');
  70. return right(error.asFlowyError());
  71. }
  72. });
  73. }
  74. Completer<Uint8List> _sendToRust(FFIRequest request) {
  75. Uint8List bytes = request.writeToBuffer();
  76. assert(bytes.isEmpty == false);
  77. if (bytes.isEmpty) {
  78. throw DispatchException(FFIException.RequestIsEmpty);
  79. }
  80. final Pointer<Uint8> input = calloc.allocate<Uint8>(bytes.length);
  81. final list = input.asTypedList(bytes.length);
  82. list.setAll(0, bytes);
  83. final completer = Completer<Uint8List>();
  84. final port = singleCompletePort(completer);
  85. ffi.async_command(port.nativePort, input, bytes.length);
  86. calloc.free(input);
  87. return completer;
  88. }
  89. Uint8List requestToBytes<T extends GeneratedMessage>(T? message) {
  90. try {
  91. if (message != null) {
  92. return message.writeToBuffer();
  93. } else {
  94. return emptyBytes();
  95. }
  96. } catch (e, s) {
  97. final error = StackTraceError(e, s);
  98. Log.error('Serial request failed. ${error.toString()}');
  99. return emptyBytes();
  100. }
  101. }
  102. Uint8List emptyBytes() {
  103. return Uint8List.fromList([]);
  104. }