dispatch.dart 3.1 KB

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