@@ -12,6 +12,15 @@ import 'package:ffi/ffi.dart';
1212
1313import 'dylib_utils.dart' ;
1414
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+
1524// Runs a simple HTTP GET request using a native HTTP library that runs
1625// the request on a background thread.
1726Future <String > httpGet (String uri) async {
@@ -26,16 +35,8 @@ Future<String> httpGet(String uri) async {
2635 print ('httpGet receiver get error $e $st ' );
2736 },
2837 );
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));
3940
4041 // Invoke the native HTTP API. Our example HTTP library runs our GET
4142 // request on a background thread, and calls the callback on that same
@@ -56,20 +57,22 @@ Future<String> httpGet(String uri) async {
5657@pragma ('vm:shared' )
5758late int counter;
5859
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) {
6662 counter++ ;
6763 final typedList = requestPointer.cast <Uint8 >().asTypedList (
6864 requestPointer.length,
6965 );
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));
7376 rp.listen (
7477 (s) {
7578 print ('httpServe counter: $counter ' );
0 commit comments