Redesign
This commit is contained in:
parent
841cd8b552
commit
589f38bb11
4 changed files with 60 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
include: package:pedantic/analysis_options.yaml
|
||||||
analyzer:
|
analyzer:
|
||||||
strong-mode:
|
strong-mode:
|
||||||
implicit-casts: false
|
implicit-casts: false
|
|
@ -1,22 +1,61 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'dart-ext:angel_wings';
|
import 'dart-ext:angel_wings';
|
||||||
|
|
||||||
int bindNativeServerSocket(String addr, int port, SendPort sendPort)
|
int bindWingsIPv4ServerSocket(Uint8List address, int port, SendPort sendPort)
|
||||||
native 'Dart_NativeSocket_bind';
|
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)
|
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 StreamController<int> _ctrl = StreamController();
|
||||||
final int _pointer;
|
final int _pointer;
|
||||||
|
final RawReceivePort _recv;
|
||||||
bool _open = true;
|
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
|
@override
|
||||||
StreamSubscription<int> listen(void Function(int event) onData,
|
StreamSubscription<int> listen(void Function(int event) onData,
|
||||||
|
@ -24,4 +63,13 @@ class NativeSocket extends Stream<int> {
|
||||||
return _ctrl.stream
|
return _ctrl.stream
|
||||||
.listen(onData, onError: onError, cancelOnError: cancelOnError);
|
.listen(onData, onError: onError, cancelOnError: cancelOnError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> close() async {
|
||||||
|
if (_open) {
|
||||||
|
_open = false;
|
||||||
|
closeWingsSocket(_pointer);
|
||||||
|
_recv.close();
|
||||||
|
await _ctrl.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,19 +3,18 @@ import 'dart:convert';
|
||||||
import 'dart:io' show Cookie;
|
import 'dart:io' show Cookie;
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'package:angel_framework/angel_framework.dart';
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
import 'package:angel_framework/http.dart';
|
|
||||||
import 'native_socket.dart';
|
import 'native_socket.dart';
|
||||||
import 'wings_request.dart';
|
import 'wings_request.dart';
|
||||||
import 'wings_response.dart';
|
import 'wings_response.dart';
|
||||||
|
|
||||||
class AngelWings extends Driver<int, int, NativeSocket, WingsRequestContext,
|
class AngelWings extends Driver<int, int, WingsSocket, WingsRequestContext,
|
||||||
WingsResponseContext> {
|
WingsResponseContext> {
|
||||||
factory AngelWings(Angel app) {
|
factory AngelWings(Angel app) {
|
||||||
return AngelWings._(app, null);
|
return AngelWings._(app, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
AngelWings._(
|
AngelWings._(
|
||||||
Angel app, Future<NativeSocket> Function(dynamic, int) serverGenerator)
|
Angel app, Future<WingsSocket> Function(dynamic, int) serverGenerator)
|
||||||
: super(app, serverGenerator);
|
: super(app, serverGenerator);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -27,7 +26,7 @@ class AngelWings extends Driver<int, int, NativeSocket, WingsRequestContext,
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future closeResponse(int response) {
|
Future closeResponse(int response) {
|
||||||
closeNativeSocket(response);
|
closeNativeSocketDescriptor(response);
|
||||||
return Future.value();
|
return Future.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,3 +8,4 @@ dependencies:
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
angel_static: ^2.0.0
|
angel_static: ^2.0.0
|
||||||
build_runner: ^1.0.0
|
build_runner: ^1.0.0
|
||||||
|
pedantic: ^1.0.0
|
Loading…
Reference in a new issue