dispatch.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // ignore: unused_import
  17. import 'package:flowy_sdk/protobuf/flowy-infra/protobuf.dart';
  18. import 'package:protobuf/protobuf.dart';
  19. import 'error.dart';
  20. part 'code_gen.dart';
  21. enum FFIException {
  22. RequestIsEmpty,
  23. }
  24. class DispatchException implements Exception {
  25. FFIException type;
  26. DispatchException(this.type);
  27. }
  28. class Dispatch {
  29. static Future<Either<Uint8List, Uint8List>> asyncRequest(FFIRequest request) {
  30. // FFIRequest => Rust SDK
  31. final bytesFuture = _sendToRust(request);
  32. // Rust SDK => FFIResponse
  33. final responseFuture = _extractResponse(bytesFuture);
  34. // FFIResponse's payload is the bytes of the Response object
  35. final payloadFuture = _extractPayload(responseFuture);
  36. return payloadFuture;
  37. }
  38. }
  39. Future<Either<Uint8List, Uint8List>> _extractPayload(
  40. Future<Either<FFIResponse, FlowyError>> responseFuture) {
  41. return responseFuture.then((result) {
  42. return result.fold(
  43. (response) {
  44. if (response.code == FFIStatusCode.Ok) {
  45. return left(Uint8List.fromList(response.payload));
  46. } else {
  47. return right(Uint8List.fromList(response.payload));
  48. }
  49. },
  50. (error) => right(emptyBytes()),
  51. );
  52. });
  53. }
  54. Future<Either<FFIResponse, FlowyError>> _extractResponse(
  55. Completer<Uint8List> bytesFuture) {
  56. return bytesFuture.future.then((bytes) {
  57. try {
  58. final response = FFIResponse.fromBuffer(bytes);
  59. return left(response);
  60. } catch (e, s) {
  61. final error = StackTraceError(e, s);
  62. Log.error('Deserialize response failed. ${error.toString()}');
  63. return right(error.asFlowyError());
  64. }
  65. });
  66. }
  67. Completer<Uint8List> _sendToRust(FFIRequest request) {
  68. Uint8List bytes = request.writeToBuffer();
  69. assert(bytes.isEmpty == false);
  70. if (bytes.isEmpty) {
  71. throw DispatchException(FFIException.RequestIsEmpty);
  72. }
  73. final Pointer<Uint8> input = calloc.allocate<Uint8>(bytes.length);
  74. final list = input.asTypedList(bytes.length);
  75. list.setAll(0, bytes);
  76. final completer = Completer<Uint8List>();
  77. final port = singleCompletePort(completer);
  78. ffi.async_command(port.nativePort, input, bytes.length);
  79. calloc.free(input);
  80. return completer;
  81. }
  82. Uint8List requestToBytes<T extends GeneratedMessage>(T? message) {
  83. try {
  84. if (message != null) {
  85. return message.writeToBuffer();
  86. } else {
  87. return emptyBytes();
  88. }
  89. } catch (e, s) {
  90. final error = StackTraceError(e, s);
  91. Log.error('Serial request failed. ${error.toString()}');
  92. return emptyBytes();
  93. }
  94. }
  95. Uint8List emptyBytes() {
  96. return Uint8List.fromList([]);
  97. }