| 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 | |
| 8 /** | 7 /** |
| 9 * [InternetAddressType] is the type an [InternetAddress]. Currently, | 8 * [InternetAddressType] is the type an [InternetAddress]. Currently, |
| 10 * IP version 4 (IPv4) and IP version 6 (IPv6) are supported. | 9 * IP version 4 (IPv4) and IP version 6 (IPv6) are supported. |
| 11 */ | 10 */ |
| 12 class InternetAddressType { | 11 class InternetAddressType { |
| 13 static const InternetAddressType IP_V4 = const InternetAddressType._(0); | 12 static const InternetAddressType IP_V4 = const InternetAddressType._(0); |
| 14 static const InternetAddressType IP_V6 = const InternetAddressType._(1); | 13 static const InternetAddressType IP_V6 = const InternetAddressType._(1); |
| 15 static const InternetAddressType ANY = const InternetAddressType._(-1); | 14 static const InternetAddressType ANY = const InternetAddressType._(-1); |
| 16 | 15 |
| 17 final int _value; | 16 final int _value; |
| 18 | 17 |
| 19 const InternetAddressType._(this._value); | 18 const InternetAddressType._(this._value); |
| 20 | 19 |
| 21 factory InternetAddressType._from(int value) { | 20 factory InternetAddressType._from(int value) { |
| 22 if (value == 0) return IP_V4; | 21 if (value == 0) return IP_V4; |
| 23 if (value == 1) return IP_V6; | 22 if (value == 1) return IP_V6; |
| 24 throw new ArgumentError("Invalid type: $value"); | 23 throw new ArgumentError("Invalid type: $value"); |
| 25 } | 24 } |
| 26 | 25 |
| 27 /** | 26 /** |
| 28 * Get the name of the type, e.g. "IP_V4" or "IP_V6". | 27 * Get the name of the type, e.g. "IP_V4" or "IP_V6". |
| 29 */ | 28 */ |
| 30 String get name { | 29 String get name { |
| 31 switch (_value) { | 30 switch (_value) { |
| 32 case -1: return "ANY"; | 31 case -1: |
| 33 case 0: return "IP_V4"; | 32 return "ANY"; |
| 34 case 1: return "IP_V6"; | 33 case 0: |
| 35 default: throw new ArgumentError("Invalid InternetAddress"); | 34 return "IP_V4"; |
| 35 case 1: |
| 36 return "IP_V6"; |
| 37 default: |
| 38 throw new ArgumentError("Invalid InternetAddress"); |
| 36 } | 39 } |
| 37 } | 40 } |
| 38 | 41 |
| 39 String toString() => "InternetAddressType: $name"; | 42 String toString() => "InternetAddressType: $name"; |
| 40 } | 43 } |
| 41 | 44 |
| 42 | |
| 43 /** | 45 /** |
| 44 * An internet address. | 46 * An internet address. |
| 45 * | 47 * |
| 46 * This object holds an internet address. If this internet address | 48 * This object holds an internet address. If this internet address |
| 47 * is the result of a DNS lookup, the address also holds the hostname | 49 * is the result of a DNS lookup, the address also holds the hostname |
| 48 * used to make the lookup. | 50 * used to make the lookup. |
| 49 * An Internet address combined with a port number represents an | 51 * An Internet address combined with a port number represents an |
| 50 * endpoint to which a socket can connect or a listening socket can | 52 * endpoint to which a socket can connect or a listening socket can |
| 51 * bind. | 53 * bind. |
| 52 */ | 54 */ |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 134 |
| 133 /** | 135 /** |
| 134 * Lookup a host, returning a Future of a list of | 136 * Lookup a host, returning a Future of a list of |
| 135 * [InternetAddress]s. If [type] is [InternetAddressType.ANY], it | 137 * [InternetAddress]s. If [type] is [InternetAddressType.ANY], it |
| 136 * will lookup both IP version 4 (IPv4) and IP version 6 (IPv6) | 138 * will lookup both IP version 4 (IPv4) and IP version 6 (IPv6) |
| 137 * addresses. If [type] is either [InternetAddressType.IP_V4] or | 139 * addresses. If [type] is either [InternetAddressType.IP_V4] or |
| 138 * [InternetAddressType.IP_V6] it will only lookup addresses of the | 140 * [InternetAddressType.IP_V6] it will only lookup addresses of the |
| 139 * specified type. The order of the list can, and most likely will, | 141 * specified type. The order of the list can, and most likely will, |
| 140 * change over time. | 142 * change over time. |
| 141 */ | 143 */ |
| 142 external static Future<List<InternetAddress>> lookup( | 144 external static Future<List<InternetAddress>> lookup(String host, |
| 143 String host, {InternetAddressType type: InternetAddressType.ANY}); | 145 {InternetAddressType type: InternetAddressType.ANY}); |
| 144 | 146 |
| 145 /** | 147 /** |
| 146 * Clones the given [address] with the new [host]. | 148 * Clones the given [address] with the new [host]. |
| 147 * | 149 * |
| 148 * The [address] must be an [InternetAddress] that was created with one | 150 * The [address] must be an [InternetAddress] that was created with one |
| 149 * of the static methods of this class. | 151 * of the static methods of this class. |
| 150 */ | 152 */ |
| 151 external static InternetAddress _cloneWithNewHost( | 153 external static InternetAddress _cloneWithNewHost( |
| 152 InternetAddress address, String host); | 154 InternetAddress address, String host); |
| 153 } | 155 } |
| 154 | 156 |
| 155 | |
| 156 /** | 157 /** |
| 157 * A [NetworkInterface] represents an active network interface on the current | 158 * A [NetworkInterface] represents an active network interface on the current |
| 158 * system. It contains a list of [InternetAddress]es that are bound to the | 159 * system. It contains a list of [InternetAddress]es that are bound to the |
| 159 * interface. | 160 * interface. |
| 160 */ | 161 */ |
| 161 abstract class NetworkInterface { | 162 abstract class NetworkInterface { |
| 162 /** | 163 /** |
| 163 * Get the name of the [NetworkInterface]. | 164 * Get the name of the [NetworkInterface]. |
| 164 */ | 165 */ |
| 165 String get name; | 166 String get name; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 188 * If [includeLoopback] is `true`, the returned list will include the | 189 * If [includeLoopback] is `true`, the returned list will include the |
| 189 * loopback device. Default is `false`. | 190 * loopback device. Default is `false`. |
| 190 * | 191 * |
| 191 * If [includeLinkLocal] is `true`, the list of addresses of the returned | 192 * If [includeLinkLocal] is `true`, the list of addresses of the returned |
| 192 * [NetworkInterface]s, may include link local addresses. Default is `false`. | 193 * [NetworkInterface]s, may include link local addresses. Default is `false`. |
| 193 * | 194 * |
| 194 * If [type] is either [InternetAddressType.IP_V4] or | 195 * If [type] is either [InternetAddressType.IP_V4] or |
| 195 * [InternetAddressType.IP_V6] it will only lookup addresses of the | 196 * [InternetAddressType.IP_V6] it will only lookup addresses of the |
| 196 * specified type. Default is [InternetAddressType.ANY]. | 197 * specified type. Default is [InternetAddressType.ANY]. |
| 197 */ | 198 */ |
| 198 external static Future<List<NetworkInterface>> list({ | 199 external static Future<List<NetworkInterface>> list( |
| 199 bool includeLoopback: false, | 200 {bool includeLoopback: false, |
| 200 bool includeLinkLocal: false, | 201 bool includeLinkLocal: false, |
| 201 InternetAddressType type: InternetAddressType.ANY}); | 202 InternetAddressType type: InternetAddressType.ANY}); |
| 202 } | 203 } |
| 203 | 204 |
| 204 | |
| 205 /** | 205 /** |
| 206 * A [RawServerSocket] represents a listening socket, and provides a | 206 * A [RawServerSocket] represents a listening socket, and provides a |
| 207 * stream of low-level [RawSocket] objects, one for each connection | 207 * stream of low-level [RawSocket] objects, one for each connection |
| 208 * made to the listening socket. | 208 * made to the listening socket. |
| 209 * | 209 * |
| 210 * See [RawSocket] for more info. | 210 * See [RawSocket] for more info. |
| 211 */ | 211 */ |
| 212 abstract class RawServerSocket implements Stream<RawSocket> { | 212 abstract class RawServerSocket implements Stream<RawSocket> { |
| 213 /** | 213 /** |
| 214 * Returns a future for a [:RawServerSocket:]. When the future | 214 * Returns a future for a [:RawServerSocket:]. When the future |
| (...skipping 25 matching lines...) Expand all Loading... |
| 240 * value of [:0:] (the default) a reasonable value will be chosen by | 240 * value of [:0:] (the default) a reasonable value will be chosen by |
| 241 * the system. | 241 * the system. |
| 242 * | 242 * |
| 243 * The optional argument [shared] specifies whether additional RawServerSocket | 243 * The optional argument [shared] specifies whether additional RawServerSocket |
| 244 * objects can bind to the same combination of `address`, `port` and `v6Only`. | 244 * objects can bind to the same combination of `address`, `port` and `v6Only`. |
| 245 * If `shared` is `true` and more `RawServerSocket`s from this isolate or | 245 * If `shared` is `true` and more `RawServerSocket`s from this isolate or |
| 246 * other isolates are bound to the port, then the incoming connections will be | 246 * other isolates are bound to the port, then the incoming connections will be |
| 247 * distributed among all the bound `RawServerSocket`s. Connections can be | 247 * distributed among all the bound `RawServerSocket`s. Connections can be |
| 248 * distributed over multiple isolates this way. | 248 * distributed over multiple isolates this way. |
| 249 */ | 249 */ |
| 250 external static Future<RawServerSocket> bind(address, | 250 external static Future<RawServerSocket> bind(address, int port, |
| 251 int port, | 251 {int backlog: 0, bool v6Only: false, bool shared: false}); |
| 252 {int backlog: 0, | |
| 253 bool v6Only: false, | |
| 254 bool shared: false}); | |
| 255 | 252 |
| 256 /** | 253 /** |
| 257 * Returns the port used by this socket. | 254 * Returns the port used by this socket. |
| 258 */ | 255 */ |
| 259 int get port; | 256 int get port; |
| 260 | 257 |
| 261 /** | 258 /** |
| 262 * Returns the address used by this socket. | 259 * Returns the address used by this socket. |
| 263 */ | 260 */ |
| 264 InternetAddress get address; | 261 InternetAddress get address; |
| 265 | 262 |
| 266 /** | 263 /** |
| 267 * Closes the socket. The returned future completes when the socket | 264 * Closes the socket. The returned future completes when the socket |
| 268 * is fully closed and is no longer bound. | 265 * is fully closed and is no longer bound. |
| 269 */ | 266 */ |
| 270 Future<RawServerSocket> close(); | 267 Future<RawServerSocket> close(); |
| 271 } | 268 } |
| 272 | 269 |
| 273 | |
| 274 /** | 270 /** |
| 275 * A [ServerSocket] represents a listening socket, and provides a | 271 * A [ServerSocket] represents a listening socket, and provides a |
| 276 * stream of [Socket] objects, one for each connection made to the | 272 * stream of [Socket] objects, one for each connection made to the |
| 277 * listening socket. | 273 * listening socket. |
| 278 * | 274 * |
| 279 * See [Socket] for more info. | 275 * See [Socket] for more info. |
| 280 */ | 276 */ |
| 281 abstract class ServerSocket implements Stream<Socket> { | 277 abstract class ServerSocket implements Stream<Socket> { |
| 282 /** | 278 /** |
| 283 * Returns a future for a [:ServerSocket:]. When the future | 279 * Returns a future for a [:ServerSocket:]. When the future |
| (...skipping 25 matching lines...) Expand all Loading... |
| 309 * value of [:0:] (the default) a reasonable value will be chosen by | 305 * value of [:0:] (the default) a reasonable value will be chosen by |
| 310 * the system. | 306 * the system. |
| 311 * | 307 * |
| 312 * The optional argument [shared] specifies whether additional ServerSocket | 308 * The optional argument [shared] specifies whether additional ServerSocket |
| 313 * objects can bind to the same combination of `address`, `port` and `v6Only`. | 309 * objects can bind to the same combination of `address`, `port` and `v6Only`. |
| 314 * If `shared` is `true` and more `ServerSocket`s from this isolate or other | 310 * If `shared` is `true` and more `ServerSocket`s from this isolate or other |
| 315 * isolates are bound to the port, then the incoming connections will be | 311 * isolates are bound to the port, then the incoming connections will be |
| 316 * distributed among all the bound `ServerSocket`s. Connections can be | 312 * distributed among all the bound `ServerSocket`s. Connections can be |
| 317 * distributed over multiple isolates this way. | 313 * distributed over multiple isolates this way. |
| 318 */ | 314 */ |
| 319 external static Future<ServerSocket> bind(address, | 315 external static Future<ServerSocket> bind(address, int port, |
| 320 int port, | 316 {int backlog: 0, bool v6Only: false, bool shared: false}); |
| 321 {int backlog: 0, | |
| 322 bool v6Only: false, | |
| 323 bool shared: false}); | |
| 324 | 317 |
| 325 /** | 318 /** |
| 326 * Returns the port used by this socket. | 319 * Returns the port used by this socket. |
| 327 */ | 320 */ |
| 328 int get port; | 321 int get port; |
| 329 | 322 |
| 330 /** | 323 /** |
| 331 * Returns the address used by this socket. | 324 * Returns the address used by this socket. |
| 332 */ | 325 */ |
| 333 InternetAddress get address; | 326 InternetAddress get address; |
| 334 | 327 |
| 335 /** | 328 /** |
| 336 * Closes the socket. The returned future completes when the socket | 329 * Closes the socket. The returned future completes when the socket |
| 337 * is fully closed and is no longer bound. | 330 * is fully closed and is no longer bound. |
| 338 */ | 331 */ |
| 339 Future<ServerSocket> close(); | 332 Future<ServerSocket> close(); |
| 340 } | 333 } |
| 341 | 334 |
| 342 | |
| 343 /** | 335 /** |
| 344 * The [SocketDirection] is used as a parameter to [Socket.close] and | 336 * The [SocketDirection] is used as a parameter to [Socket.close] and |
| 345 * [RawSocket.close] to close a socket in the specified direction(s). | 337 * [RawSocket.close] to close a socket in the specified direction(s). |
| 346 */ | 338 */ |
| 347 class SocketDirection { | 339 class SocketDirection { |
| 348 static const SocketDirection RECEIVE = const SocketDirection._(0); | 340 static const SocketDirection RECEIVE = const SocketDirection._(0); |
| 349 static const SocketDirection SEND = const SocketDirection._(1); | 341 static const SocketDirection SEND = const SocketDirection._(1); |
| 350 static const SocketDirection BOTH = const SocketDirection._(2); | 342 static const SocketDirection BOTH = const SocketDirection._(2); |
| 351 final _value; | 343 final _value; |
| 352 | 344 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 382 */ | 374 */ |
| 383 class RawSocketEvent { | 375 class RawSocketEvent { |
| 384 static const RawSocketEvent READ = const RawSocketEvent._(0); | 376 static const RawSocketEvent READ = const RawSocketEvent._(0); |
| 385 static const RawSocketEvent WRITE = const RawSocketEvent._(1); | 377 static const RawSocketEvent WRITE = const RawSocketEvent._(1); |
| 386 static const RawSocketEvent READ_CLOSED = const RawSocketEvent._(2); | 378 static const RawSocketEvent READ_CLOSED = const RawSocketEvent._(2); |
| 387 static const RawSocketEvent CLOSED = const RawSocketEvent._(3); | 379 static const RawSocketEvent CLOSED = const RawSocketEvent._(3); |
| 388 final int _value; | 380 final int _value; |
| 389 | 381 |
| 390 const RawSocketEvent._(this._value); | 382 const RawSocketEvent._(this._value); |
| 391 String toString() { | 383 String toString() { |
| 392 return const ['RawSocketEvent:READ', | 384 return const [ |
| 393 'RawSocketEvent:WRITE', | 385 'RawSocketEvent:READ', |
| 394 'RawSocketEvent:READ_CLOSED', | 386 'RawSocketEvent:WRITE', |
| 395 'RawSocketEvent:CLOSED'][_value]; | 387 'RawSocketEvent:READ_CLOSED', |
| 388 'RawSocketEvent:CLOSED' |
| 389 ][_value]; |
| 396 } | 390 } |
| 397 } | 391 } |
| 398 | 392 |
| 399 /** | 393 /** |
| 400 * The [RawSocket] is a low-level interface to a socket, exposing the raw | 394 * The [RawSocket] is a low-level interface to a socket, exposing the raw |
| 401 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s. | 395 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s. |
| 402 */ | 396 */ |
| 403 abstract class RawSocket implements Stream<RawSocketEvent> { | 397 abstract class RawSocket implements Stream<RawSocketEvent> { |
| 404 /** | 398 /** |
| 405 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.READ] | 399 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.READ] |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 /** | 561 /** |
| 568 * Returns the remote [InternetAddress] connected to by this socket. | 562 * Returns the remote [InternetAddress] connected to by this socket. |
| 569 */ | 563 */ |
| 570 InternetAddress get remoteAddress; | 564 InternetAddress get remoteAddress; |
| 571 | 565 |
| 572 Future<Socket> close(); | 566 Future<Socket> close(); |
| 573 | 567 |
| 574 Future<Socket> get done; | 568 Future<Socket> get done; |
| 575 } | 569 } |
| 576 | 570 |
| 577 | |
| 578 /** | 571 /** |
| 579 * Datagram package. Data send to and received from datagram sockets | 572 * Datagram package. Data send to and received from datagram sockets |
| 580 * contains the internet address and port of the destination or source | 573 * contains the internet address and port of the destination or source |
| 581 * togeter with the data. | 574 * togeter with the data. |
| 582 */ | 575 */ |
| 583 class Datagram { | 576 class Datagram { |
| 584 List<int> data; | 577 List<int> data; |
| 585 InternetAddress address; | 578 InternetAddress address; |
| 586 int port; | 579 int port; |
| 587 | 580 |
| 588 Datagram(this.data, this.address, this.port); | 581 Datagram(this.data, this.address, this.port); |
| 589 } | 582 } |
| 590 | 583 |
| 591 | |
| 592 /** | 584 /** |
| 593 * The [RawDatagramSocket] is a low-level interface to an UDP socket, | 585 * The [RawDatagramSocket] is a low-level interface to an UDP socket, |
| 594 * exposing the raw events signaled by the system. It's a [Stream] of | 586 * exposing the raw events signaled by the system. It's a [Stream] of |
| 595 * [RawSocketEvent]s. | 587 * [RawSocketEvent]s. |
| 596 * | 588 * |
| 597 * Note that the event [RawSocketEvent.READ_CLOSED] will never be | 589 * Note that the event [RawSocketEvent.READ_CLOSED] will never be |
| 598 * received as an UDP socket cannot be closed by a remote peer. | 590 * received as an UDP socket cannot be closed by a remote peer. |
| 599 */ | 591 */ |
| 600 abstract class RawDatagramSocket extends Stream<RawSocketEvent> { | 592 abstract class RawDatagramSocket extends Stream<RawSocketEvent> { |
| 601 /** | 593 /** |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 648 * | 640 * |
| 649 * For IPv6 there is no general broadcast mechanism. Use multicast | 641 * For IPv6 there is no general broadcast mechanism. Use multicast |
| 650 * instead. | 642 * instead. |
| 651 */ | 643 */ |
| 652 bool broadcastEnabled; | 644 bool broadcastEnabled; |
| 653 | 645 |
| 654 /** | 646 /** |
| 655 * Creates a new raw datagram socket binding it to an address and | 647 * Creates a new raw datagram socket binding it to an address and |
| 656 * port. | 648 * port. |
| 657 */ | 649 */ |
| 658 external static Future<RawDatagramSocket> bind( | 650 external static Future<RawDatagramSocket> bind(host, int port, |
| 659 host, int port, {bool reuseAddress: true}); | 651 {bool reuseAddress: true}); |
| 660 | 652 |
| 661 /** | 653 /** |
| 662 * Returns the port used by this socket. | 654 * Returns the port used by this socket. |
| 663 */ | 655 */ |
| 664 int get port; | 656 int get port; |
| 665 | 657 |
| 666 /** | 658 /** |
| 667 * Returns the address used by this socket. | 659 * Returns the address used by this socket. |
| 668 */ | 660 */ |
| 669 InternetAddress get address; | 661 InternetAddress get address; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 699 | 691 |
| 700 /** | 692 /** |
| 701 * Leave a multicast group. | 693 * Leave a multicast group. |
| 702 * | 694 * |
| 703 * If an error occur when trying to join the multicase group an | 695 * If an error occur when trying to join the multicase group an |
| 704 * exception is thrown. | 696 * exception is thrown. |
| 705 */ | 697 */ |
| 706 void leaveMulticast(InternetAddress group, {NetworkInterface interface}); | 698 void leaveMulticast(InternetAddress group, {NetworkInterface interface}); |
| 707 } | 699 } |
| 708 | 700 |
| 709 | |
| 710 class SocketException implements IOException { | 701 class SocketException implements IOException { |
| 711 final String message; | 702 final String message; |
| 712 final OSError osError; | 703 final OSError osError; |
| 713 final InternetAddress address; | 704 final InternetAddress address; |
| 714 final int port; | 705 final int port; |
| 715 | 706 |
| 716 const SocketException(this.message, {this.osError, this.address, this.port}); | 707 const SocketException(this.message, {this.osError, this.address, this.port}); |
| 717 const SocketException.closed() | 708 const SocketException.closed() |
| 718 : message = 'Socket has been closed', | 709 : message = 'Socket has been closed', |
| 719 osError = null, | 710 osError = null, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 733 } | 724 } |
| 734 if (address != null) { | 725 if (address != null) { |
| 735 sb.write(", address = ${address.host}"); | 726 sb.write(", address = ${address.host}"); |
| 736 } | 727 } |
| 737 if (port != null) { | 728 if (port != null) { |
| 738 sb.write(", port = $port"); | 729 sb.write(", port = $port"); |
| 739 } | 730 } |
| 740 return sb.toString(); | 731 return sb.toString(); |
| 741 } | 732 } |
| 742 } | 733 } |
| OLD | NEW |