diff --git a/example/socket.dart b/example/socket.dart new file mode 100644 index 00000000..79d957a4 --- /dev/null +++ b/example/socket.dart @@ -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]); +} diff --git a/lib/angel_wings.dart b/lib/angel_wings.dart index 718a257b..9bc403ed 100644 --- a/lib/angel_wings.dart +++ b/lib/angel_wings.dart @@ -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'; \ No newline at end of file +export 'src/wings_response.dart'; +export 'src/wings_socket.dart'; \ No newline at end of file diff --git a/lib/src/wings_driver.dart b/lib/src/wings_driver.dart index 50413909..dc87e2ad 100644 --- a/lib/src/wings_driver.dart +++ b/lib/src/wings_driver.dart @@ -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 { factory AngelWings(Angel app) { - return AngelWings._(app, null); + return AngelWings._(app, WingsSocket.bind); } AngelWings._( diff --git a/lib/src/native_socket.dart b/lib/src/wings_socket.dart similarity index 54% rename from lib/src/native_socket.dart rename to lib/src/wings_socket.dart index ef1b74eb..f3365dac 100644 --- a/lib/src/native_socket.dart +++ b/lib/src/wings_socket.dart @@ -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 { }; } - static WingsSocket bind(InternetAddress address, int port) { + static Future 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); diff --git a/lib/src/wings_socket.h b/lib/src/wings_socket.h new file mode 100644 index 00000000..e69de29b