| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 patch class RawServerSocket { | 5 patch class RawServerSocket { |
| 6 /* patch */ static Future<RawServerSocket> bind(address, | 6 /* patch */ static Future<RawServerSocket> bind(address, |
| 7 int port, | 7 int port, |
| 8 {int backlog: 0, | 8 {int backlog: 0, |
| 9 bool v6Only: false}) { | 9 bool v6Only: false}) { |
| 10 return _RawServerSocket.bind(address, port, backlog, v6Only); | 10 return _RawServerSocket.bind(address, port, backlog, v6Only); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 213 |
| 214 // The type flags for this socket. | 214 // The type flags for this socket. |
| 215 final int typeFlags; | 215 final int typeFlags; |
| 216 | 216 |
| 217 // Holds the port of the socket, null if not known. | 217 // Holds the port of the socket, null if not known. |
| 218 int localPort; | 218 int localPort; |
| 219 | 219 |
| 220 // Holds the address used to connect or bind the socket. | 220 // Holds the address used to connect or bind the socket. |
| 221 InternetAddress address; | 221 InternetAddress address; |
| 222 | 222 |
| 223 // Native port for socket services. | |
| 224 static SendPort socketService; | |
| 225 | |
| 226 static Future<List<InternetAddress>> lookup( | 223 static Future<List<InternetAddress>> lookup( |
| 227 String host, {InternetAddressType type: InternetAddressType.ANY}) { | 224 String host, {InternetAddressType type: InternetAddressType.ANY}) { |
| 228 ensureSocketService(); | 225 return IOService.dispatch(SOCKET_LOOKUP, [host, type._value]) |
| 229 return socketService.call([HOST_NAME_LOOKUP, host, type._value]) | |
| 230 .then((response) { | 226 .then((response) { |
| 231 if (isErrorResponse(response)) { | 227 if (isErrorResponse(response)) { |
| 232 throw createError(response, "Failed host lookup: '$host'"); | 228 throw createError(response, "Failed host lookup: '$host'"); |
| 233 } else { | 229 } else { |
| 234 return response.skip(1).map((result) { | 230 return response.skip(1).map((result) { |
| 235 var type = new InternetAddressType._from(result[0]); | 231 var type = new InternetAddressType._from(result[0]); |
| 236 return new _InternetAddress(type, result[1], host, result[2]); | 232 return new _InternetAddress(type, result[1], host, result[2]); |
| 237 }).toList(); | 233 }).toList(); |
| 238 } | 234 } |
| 239 }); | 235 }); |
| 240 } | 236 } |
| 241 | 237 |
| 242 static Future<InternetAddress> reverseLookup(InternetAddress addr) { | 238 static Future<InternetAddress> reverseLookup(InternetAddress addr) { |
| 243 ensureSocketService(); | 239 return IOService.dispatch(SOCKET_REVERSE_LOOKUP, [addr._sockaddr_storage]) |
| 244 return socketService.call([REVERSE_LOOKUP, addr._sockaddr_storage]) | |
| 245 .then((response) { | 240 .then((response) { |
| 246 if (isErrorResponse(response)) { | 241 if (isErrorResponse(response)) { |
| 247 throw createError(response, "Failed reverse host lookup", addr); | 242 throw createError(response, "Failed reverse host lookup", addr); |
| 248 } else { | 243 } else { |
| 249 return addr._cloneWithNewHost(response); | 244 return addr._cloneWithNewHost(response); |
| 250 } | 245 } |
| 251 }); | 246 }); |
| 252 } | 247 } |
| 253 | 248 |
| 254 static Future<List<NetworkInterface>> listInterfaces({ | 249 static Future<List<NetworkInterface>> listInterfaces({ |
| 255 bool includeLoopback: false, | 250 bool includeLoopback: false, |
| 256 bool includeLinkLocal: false, | 251 bool includeLinkLocal: false, |
| 257 InternetAddressType type: InternetAddressType.ANY}) { | 252 InternetAddressType type: InternetAddressType.ANY}) { |
| 258 ensureSocketService(); | 253 return IOService.dispatch(SOCKET_LIST_INTERFACES, [type._value]) |
| 259 return socketService.call([LIST_INTERFACES, type._value]) | |
| 260 .then((response) { | 254 .then((response) { |
| 261 if (isErrorResponse(response)) { | 255 if (isErrorResponse(response)) { |
| 262 throw createError(response, "Failed listing interfaces"); | 256 throw createError(response, "Failed listing interfaces"); |
| 263 } else { | 257 } else { |
| 264 var list = new List<NetworkInterface>(); | 258 var list = new List<NetworkInterface>(); |
| 265 var map = response.skip(1) | 259 var map = response.skip(1) |
| 266 .fold(new Map<String, List<InternetAddress>>(), (map, result) { | 260 .fold(new Map<String, List<InternetAddress>>(), (map, result) { |
| 267 var type = new InternetAddressType._from(result[0]); | 261 var type = new InternetAddressType._from(result[0]); |
| 268 var name = result[3]; | 262 var name = result[3]; |
| 269 var address = new _InternetAddress( | 263 var address = new _InternetAddress( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 288 if (host is _InternetAddress) return host; | 282 if (host is _InternetAddress) return host; |
| 289 return lookup(host) | 283 return lookup(host) |
| 290 .then((list) { | 284 .then((list) { |
| 291 if (list.length == 0) { | 285 if (list.length == 0) { |
| 292 throw createError(response, "Failed host lookup: '$host'"); | 286 throw createError(response, "Failed host lookup: '$host'"); |
| 293 } | 287 } |
| 294 return list[0]; | 288 return list[0]; |
| 295 }); | 289 }); |
| 296 }) | 290 }) |
| 297 .then((address) { | 291 .then((address) { |
| 298 ensureSocketService(); | |
| 299 var socket = new _NativeSocket.normal(); | 292 var socket = new _NativeSocket.normal(); |
| 300 socket.address = address; | 293 socket.address = address; |
| 301 var result = socket.nativeCreateConnect( | 294 var result = socket.nativeCreateConnect( |
| 302 address._sockaddr_storage, port); | 295 address._sockaddr_storage, port); |
| 303 if (result is OSError) { | 296 if (result is OSError) { |
| 304 throw createError(result, "Connection failed", address, port); | 297 throw createError(result, "Connection failed", address, port); |
| 305 } else { | 298 } else { |
| 306 socket.port; // Query the local port, for error messages. | 299 socket.port; // Query the local port, for error messages. |
| 307 var completer = new Completer(); | 300 var completer = new Completer(); |
| 308 // Setup handlers for receiving the first write event which | 301 // Setup handlers for receiving the first write event which |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 } | 591 } |
| 599 } | 592 } |
| 600 | 593 |
| 601 void disconnectFromEventHandler() { | 594 void disconnectFromEventHandler() { |
| 602 if (eventPort != null) { | 595 if (eventPort != null) { |
| 603 eventPort.close(); | 596 eventPort.close(); |
| 604 eventPort = null; | 597 eventPort = null; |
| 605 } | 598 } |
| 606 } | 599 } |
| 607 | 600 |
| 608 static void ensureSocketService() { | |
| 609 if (socketService == null) { | |
| 610 socketService = _NativeSocket.newServicePort(); | |
| 611 } | |
| 612 } | |
| 613 | |
| 614 // Check whether this is an error response from a native port call. | 601 // Check whether this is an error response from a native port call. |
| 615 static bool isErrorResponse(response) { | 602 static bool isErrorResponse(response) { |
| 616 return response is List && response[0] != _SUCCESS_RESPONSE; | 603 return response is List && response[0] != _SUCCESS_RESPONSE; |
| 617 } | 604 } |
| 618 | 605 |
| 619 // Create the appropriate error/exception from different returned | 606 // Create the appropriate error/exception from different returned |
| 620 // error objects. | 607 // error objects. |
| 621 static createError(error, | 608 static createError(error, |
| 622 String message, | 609 String message, |
| 623 [InternetAddress address, | 610 [InternetAddress address, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 native "Socket_WriteList"; | 653 native "Socket_WriteList"; |
| 667 nativeCreateConnect(List<int> addr, | 654 nativeCreateConnect(List<int> addr, |
| 668 int port) native "Socket_CreateConnect"; | 655 int port) native "Socket_CreateConnect"; |
| 669 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only) | 656 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only) |
| 670 native "ServerSocket_CreateBindListen"; | 657 native "ServerSocket_CreateBindListen"; |
| 671 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; | 658 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; |
| 672 int nativeGetPort() native "Socket_GetPort"; | 659 int nativeGetPort() native "Socket_GetPort"; |
| 673 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; | 660 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; |
| 674 OSError nativeGetError() native "Socket_GetError"; | 661 OSError nativeGetError() native "Socket_GetError"; |
| 675 bool nativeSetOption(int option, bool enabled) native "Socket_SetOption"; | 662 bool nativeSetOption(int option, bool enabled) native "Socket_SetOption"; |
| 676 | |
| 677 static SendPort newServicePort() native "Socket_NewServicePort"; | |
| 678 } | 663 } |
| 679 | 664 |
| 680 | 665 |
| 681 class _RawServerSocket extends Stream<RawSocket> | 666 class _RawServerSocket extends Stream<RawSocket> |
| 682 implements RawServerSocket { | 667 implements RawServerSocket { |
| 683 final _NativeSocket _socket; | 668 final _NativeSocket _socket; |
| 684 StreamController<RawSocket> _controller; | 669 StreamController<RawSocket> _controller; |
| 685 | 670 |
| 686 static Future<_RawServerSocket> bind(address, | 671 static Future<_RawServerSocket> bind(address, |
| 687 int port, | 672 int port, |
| (...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1224 if (_detachReady != null) { | 1209 if (_detachReady != null) { |
| 1225 _detachReady.complete(null); | 1210 _detachReady.complete(null); |
| 1226 } else { | 1211 } else { |
| 1227 if (_raw != null) { | 1212 if (_raw != null) { |
| 1228 _raw.shutdown(SocketDirection.SEND); | 1213 _raw.shutdown(SocketDirection.SEND); |
| 1229 _disableWriteEvent(); | 1214 _disableWriteEvent(); |
| 1230 } | 1215 } |
| 1231 } | 1216 } |
| 1232 } | 1217 } |
| 1233 } | 1218 } |
| OLD | NEW |