This commit is contained in:
Tobe O 2019-04-26 19:11:58 -04:00
parent 841cd8b552
commit 589f38bb11
4 changed files with 60 additions and 11 deletions

View file

@ -1,3 +1,4 @@
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false

View file

@ -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<int> {
void closeWingsSocket(int pointer) native 'Dart_WingsSocket_close';
class WingsSocket extends Stream<int> {
final StreamController<int> _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<int> listen(void Function(int event) onData,
@ -24,4 +63,13 @@ class NativeSocket extends Stream<int> {
return _ctrl.stream
.listen(onData, onError: onError, cancelOnError: cancelOnError);
}
Future<void> close() async {
if (_open) {
_open = false;
closeWingsSocket(_pointer);
_recv.close();
await _ctrl.close();
}
}
}

View file

@ -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<int, int, NativeSocket, WingsRequestContext,
class AngelWings extends Driver<int, int, WingsSocket, WingsRequestContext,
WingsResponseContext> {
factory AngelWings(Angel app) {
return AngelWings._(app, null);
}
AngelWings._(
Angel app, Future<NativeSocket> Function(dynamic, int) serverGenerator)
Angel app, Future<WingsSocket> Function(dynamic, int) serverGenerator)
: super(app, serverGenerator);
@override
@ -27,7 +26,7 @@ class AngelWings extends Driver<int, int, NativeSocket, WingsRequestContext,
@override
Future closeResponse(int response) {
closeNativeSocket(response);
closeNativeSocketDescriptor(response);
return Future.value();
}

View file

@ -7,4 +7,5 @@ dependencies:
mock_request: ^1.0.0
dev_dependencies:
angel_static: ^2.0.0
build_runner: ^1.0.0
build_runner: ^1.0.0
pedantic: ^1.0.0