This commit is contained in:
Tobe O 2019-04-26 19:22:30 -04:00
parent 589f38bb11
commit 12d6e07133
5 changed files with 36 additions and 14 deletions

6
example/socket.dart Normal file
View file

@ -0,0 +1,6 @@
import 'package:angel_wings/angel_wings.dart';
main() async {
var socket = await WingsSocket.bind('127.0.0.1', 3000);
print([socket.port]);
}

View file

@ -1,5 +1,5 @@
export 'src/angel_wings.dart';
export 'src/native_socket.dart';
export 'src/wings_driver.dart';
export 'src/wings_request.dart';
export 'src/wings_response.dart';
export 'src/wings_socket.dart';

View file

@ -3,14 +3,14 @@ import 'dart:convert';
import 'dart:io' show Cookie;
import 'dart:typed_data';
import 'package:angel_framework/angel_framework.dart';
import 'native_socket.dart';
import 'wings_request.dart';
import 'wings_response.dart';
import 'wings_socket.dart';
class AngelWings extends Driver<int, int, WingsSocket, WingsRequestContext,
WingsResponseContext> {
factory AngelWings(Angel app) {
return AngelWings._(app, null);
return AngelWings._(app, WingsSocket.bind);
}
AngelWings._(

View file

@ -4,11 +4,11 @@ import 'dart:isolate';
import 'dart:typed_data';
import 'dart-ext:angel_wings';
int bindWingsIPv4ServerSocket(Uint8List address, int port, SendPort sendPort)
native 'Dart_WingsSocket_bind';
int bindWingsIPv4ServerSocket(Uint8List address, int port, bool shared,
int backlog, bool v6Only, SendPort sendPort) native 'Dart_WingsSocket_bind';
int bindWingsIPv6ServerSocket(Uint8List address, int port, SendPort sendPort)
native 'Dart_WingsSocket_bind';
int bindWingsIPv6ServerSocket(Uint8List address, int port, bool shared,
int backlog, bool v6Only, SendPort sendPort) native 'Dart_WingsSocket_bind';
int getWingsServerSocketPort(int pointer) native 'Dart_WingsSocket_getPort';
@ -35,17 +35,33 @@ class WingsSocket extends Stream<int> {
};
}
static WingsSocket bind(InternetAddress address, int port) {
static Future<WingsSocket> bind(address, int port,
{bool shared = false, int backlog = 0, bool v6Only = false}) async {
var recv = RawReceivePort();
int ptr;
InternetAddress addr;
if (address is InternetAddress) {
addr = address;
} else if (address is String) {
var addrs = await InternetAddress.lookup(address);
if (addrs.isNotEmpty) {
addr = addrs[0];
} else {
throw StateError('Internet address lookup failed: $address');
}
} else {
throw ArgumentError.value(
address, 'address', 'must be an InternetAddress or String');
}
try {
if (address.type == InternetAddressType.IPv6) {
ptr = bindWingsIPv6ServerSocket(
Uint8List.fromList(address.rawAddress), port, recv.sendPort);
if (addr.type == InternetAddressType.IPv6) {
ptr = bindWingsIPv6ServerSocket(Uint8List.fromList(addr.rawAddress),
port, shared, backlog, v6Only, recv.sendPort);
} else {
ptr = bindWingsIPv4ServerSocket(
Uint8List.fromList(address.rawAddress), port, recv.sendPort);
ptr = bindWingsIPv4ServerSocket(Uint8List.fromList(addr.rawAddress),
port, shared, backlog, v6Only, recv.sendPort);
}
return WingsSocket._(ptr, recv);

0
lib/src/wings_socket.h Normal file
View file