@@ -12,6 +12,15 @@ import 'package:ffi/ffi.dart';
12
12
13
13
import 'dylib_utils.dart' ;
14
14
15
+ Function (Pointer <Utf8 >) createGetSender (SendPort sendPort) {
16
+ return (Pointer <Utf8 > responsePointer) {
17
+ final typedList = responsePointer.cast <Uint8 >().asTypedList (
18
+ responsePointer.length,
19
+ );
20
+ sendPort.send (utf8.decode (typedList));
21
+ };
22
+ }
23
+
15
24
// Runs a simple HTTP GET request using a native HTTP library that runs
16
25
// the request on a background thread.
17
26
Future <String > httpGet (String uri) async {
@@ -26,16 +35,8 @@ Future<String> httpGet(String uri) async {
26
35
print ('httpGet receiver get error $e $st ' );
27
36
},
28
37
);
29
- final sendPort = rp.sendPort;
30
- final callback = NativeCallable <HttpCallback >.isolateGroupBound ((
31
- Pointer <Utf8 > responsePointer,
32
- ) {
33
- final typedList = responsePointer.cast <Uint8 >().asTypedList (
34
- responsePointer.length,
35
- );
36
- final s = utf8.decode (typedList);
37
- sendPort.send (s);
38
- });
38
+ final callback = NativeCallable <HttpCallback >.isolateGroupBound (
39
+ createGetSender (rp.sendPort));
39
40
40
41
// Invoke the native HTTP API. Our example HTTP library runs our GET
41
42
// request on a background thread, and calls the callback on that same
@@ -56,20 +57,22 @@ Future<String> httpGet(String uri) async {
56
57
@pragma ('vm:shared' )
57
58
late int counter;
58
59
59
- // Start a HTTP server on a background thread.
60
- ReceivePort httpServe (void Function (String ) onRequest) {
61
- counter = 0 ;
62
- final rp = ReceivePort ();
63
- final callback = NativeCallable <HttpCallback >.isolateGroupBound ((
64
- Pointer <Utf8 > requestPointer,
65
- ) {
60
+ Function (Pointer <Utf8 >) createServeSender (SendPort sendPort) {
61
+ return (Pointer <Utf8 > requestPointer) {
66
62
counter++ ;
67
63
final typedList = requestPointer.cast <Uint8 >().asTypedList (
68
64
requestPointer.length,
69
65
);
70
- final s = utf8.decode (typedList);
71
- rp.sendPort.send (s);
72
- });
66
+ sendPort.send (utf8.decode (typedList));
67
+ };
68
+ }
69
+
70
+ // Start a HTTP server on a background thread.
71
+ ReceivePort httpServe (void Function (String ) onRequest) {
72
+ counter = 0 ;
73
+ final rp = ReceivePort ();
74
+ final callback = NativeCallable <HttpCallback >.isolateGroupBound (
75
+ createServeSender (rp.sendPort));
73
76
rp.listen (
74
77
(s) {
75
78
print ('httpServe counter: $counter ' );
0 commit comments