diff --git a/analysis_options.yaml b/analysis_options.yaml index eae1e42a..c230cee7 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,3 +1,4 @@ +include: package:pedantic/analysis_options.yaml analyzer: strong-mode: implicit-casts: false \ No newline at end of file diff --git a/lib/src/native_socket.dart b/lib/src/native_socket.dart index c2bbe8e7..ef1b74eb 100644 --- a/lib/src/native_socket.dart +++ b/lib/src/native_socket.dart @@ -1,22 +1,61 @@ import 'dart:async'; +import 'dart:io'; import 'dart:isolate'; import 'dart:typed_data'; import 'dart-ext:angel_wings'; -int bindNativeServerSocket(String addr, int port, SendPort sendPort) - native 'Dart_NativeSocket_bind'; +int bindWingsIPv4ServerSocket(Uint8List address, int port, SendPort sendPort) + native 'Dart_WingsSocket_bind'; + +int bindWingsIPv6ServerSocket(Uint8List address, int port, SendPort sendPort) + native 'Dart_WingsSocket_bind'; + +int getWingsServerSocketPort(int pointer) native 'Dart_WingsSocket_getPort'; void writeToNativeSocket(int fd, Uint8List data) - native 'Dart_NativeSocket_write'; + native 'Dart_WingsSocket_write'; -void closeNativeSocket(int fd) native 'Dart_NativeSocket_close'; +void closeNativeSocketDescriptor(int fd) + native 'Dart_WingsSocket_closeDescriptor'; -class NativeSocket extends Stream { +void closeWingsSocket(int pointer) native 'Dart_WingsSocket_close'; + +class WingsSocket extends Stream { final StreamController _ctrl = StreamController(); final int _pointer; + final RawReceivePort _recv; bool _open = true; + int _port; - NativeSocket._(this._pointer); + WingsSocket._(this._pointer, this._recv) { + _recv.handler = (h) { + if (!_ctrl.isClosed) { + _ctrl.add(h as int); + } + }; + } + + static WingsSocket bind(InternetAddress address, int port) { + var recv = RawReceivePort(); + int ptr; + + try { + if (address.type == InternetAddressType.IPv6) { + ptr = bindWingsIPv6ServerSocket( + Uint8List.fromList(address.rawAddress), port, recv.sendPort); + } else { + ptr = bindWingsIPv4ServerSocket( + Uint8List.fromList(address.rawAddress), port, recv.sendPort); + } + + return WingsSocket._(ptr, recv); + } catch (e) { + recv.close(); + rethrow; + } + } + + int get port => _port ??= getWingsServerSocketPort(_pointer); @override StreamSubscription listen(void Function(int event) onData, @@ -24,4 +63,13 @@ class NativeSocket extends Stream { return _ctrl.stream .listen(onData, onError: onError, cancelOnError: cancelOnError); } + + Future close() async { + if (_open) { + _open = false; + closeWingsSocket(_pointer); + _recv.close(); + await _ctrl.close(); + } + } } diff --git a/lib/src/wings_driver.dart b/lib/src/wings_driver.dart index 41bba0af..50413909 100644 --- a/lib/src/wings_driver.dart +++ b/lib/src/wings_driver.dart @@ -3,19 +3,18 @@ import 'dart:convert'; import 'dart:io' show Cookie; import 'dart:typed_data'; import 'package:angel_framework/angel_framework.dart'; -import 'package:angel_framework/http.dart'; import 'native_socket.dart'; import 'wings_request.dart'; import 'wings_response.dart'; -class AngelWings extends Driver { factory AngelWings(Angel app) { return AngelWings._(app, null); } AngelWings._( - Angel app, Future Function(dynamic, int) serverGenerator) + Angel app, Future Function(dynamic, int) serverGenerator) : super(app, serverGenerator); @override @@ -27,7 +26,7 @@ class AngelWings extends Driver