| 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 | 7 |
| 8 /** | 8 /** |
| 9 * [InternetAddressType] is the type an [InternetAddress]. Currently, | 9 * [InternetAddressType] is the type an [InternetAddress]. Currently, |
| 10 * IP version 4 (IPv4) and IP version 6 (IPv6) are supported. | 10 * IP version 4 (IPv4) and IP version 6 (IPv6) are supported. |
| 11 */ | 11 */ |
| 12 class InternetAddressType { | 12 class InternetAddressType { |
| 13 static const InternetAddressType IP_V4 = const InternetAddressType._(0); | 13 static const InternetAddressType IP_V4 = const InternetAddressType._(0); |
| 14 static const InternetAddressType IP_V6 = const InternetAddressType._(1); | 14 static const InternetAddressType IP_V6 = const InternetAddressType._(1); |
| 15 static const InternetAddressType ANY = const InternetAddressType._(-1); | 15 static const InternetAddressType ANY = const InternetAddressType._(-1); |
| 16 | 16 |
| 17 final int _value; | 17 final int _value; |
| 18 | 18 |
| 19 const InternetAddressType._(int this._value); | 19 const InternetAddressType._(this._value); |
| 20 | 20 |
| 21 factory InternetAddressType._from(int value) { | 21 factory InternetAddressType._from(int value) { |
| 22 if (value == 0) return IP_V4; | 22 if (value == 0) return IP_V4; |
| 23 if (value == 1) return IP_V6; | 23 if (value == 1) return IP_V6; |
| 24 throw new ArgumentError("Invalid type: $value"); | 24 throw new ArgumentError("Invalid type: $value"); |
| 25 } | 25 } |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Get the name of the type, e.g. "IP_V4" or "IP_V6". | 28 * Get the name of the type, e.g. "IP_V4" or "IP_V6". |
| 29 */ | 29 */ |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 297 } |
| 298 | 298 |
| 299 /** | 299 /** |
| 300 * The [SocketDirection] is used as a parameter to [Socket.close] and | 300 * The [SocketDirection] is used as a parameter to [Socket.close] and |
| 301 * [RawSocket.close] to close a socket in the specified direction(s). | 301 * [RawSocket.close] to close a socket in the specified direction(s). |
| 302 */ | 302 */ |
| 303 class SocketDirection { | 303 class SocketDirection { |
| 304 static const SocketDirection RECEIVE = const SocketDirection._(0); | 304 static const SocketDirection RECEIVE = const SocketDirection._(0); |
| 305 static const SocketDirection SEND = const SocketDirection._(1); | 305 static const SocketDirection SEND = const SocketDirection._(1); |
| 306 static const SocketDirection BOTH = const SocketDirection._(2); | 306 static const SocketDirection BOTH = const SocketDirection._(2); |
| 307 final _value; |
| 308 |
| 307 const SocketDirection._(this._value); | 309 const SocketDirection._(this._value); |
| 308 final _value; | |
| 309 } | 310 } |
| 310 | 311 |
| 311 /** | 312 /** |
| 312 * The [SocketOption] is used as a parameter to [Socket.setOption] and | 313 * The [SocketOption] is used as a parameter to [Socket.setOption] and |
| 313 * [RawSocket.setOption] to set customize the behaviour of the underlying | 314 * [RawSocket.setOption] to set customize the behaviour of the underlying |
| 314 * socket. | 315 * socket. |
| 315 */ | 316 */ |
| 316 class SocketOption { | 317 class SocketOption { |
| 317 /** | 318 /** |
| 318 * Enable or disable no-delay on the socket. If TCP_NODELAY is enabled, the | 319 * Enable or disable no-delay on the socket. If TCP_NODELAY is enabled, the |
| 319 * socket will not buffer data internally, but instead write each data chunk | 320 * socket will not buffer data internally, but instead write each data chunk |
| 320 * as an invidual TCP packet. | 321 * as an invidual TCP packet. |
| 321 * | 322 * |
| 322 * TCP_NODELAY is disabled by default. | 323 * TCP_NODELAY is disabled by default. |
| 323 */ | 324 */ |
| 324 static const SocketOption TCP_NODELAY = const SocketOption._(0); | 325 static const SocketOption TCP_NODELAY = const SocketOption._(0); |
| 325 | 326 |
| 326 static const SocketOption _IP_MULTICAST_LOOP = const SocketOption._(1); | 327 static const SocketOption _IP_MULTICAST_LOOP = const SocketOption._(1); |
| 327 static const SocketOption _IP_MULTICAST_HOPS = const SocketOption._(2); | 328 static const SocketOption _IP_MULTICAST_HOPS = const SocketOption._(2); |
| 328 static const SocketOption _IP_MULTICAST_IF = const SocketOption._(3); | 329 static const SocketOption _IP_MULTICAST_IF = const SocketOption._(3); |
| 329 static const SocketOption _IP_BROADCAST = const SocketOption._(4); | 330 static const SocketOption _IP_BROADCAST = const SocketOption._(4); |
| 331 final _value; |
| 330 | 332 |
| 331 const SocketOption._(this._value); | 333 const SocketOption._(this._value); |
| 332 final _value; | |
| 333 } | 334 } |
| 334 | 335 |
| 335 /** | 336 /** |
| 336 * Events for the [RawSocket]. | 337 * Events for the [RawSocket]. |
| 337 */ | 338 */ |
| 338 class RawSocketEvent { | 339 class RawSocketEvent { |
| 339 static const RawSocketEvent READ = const RawSocketEvent._(0); | 340 static const RawSocketEvent READ = const RawSocketEvent._(0); |
| 340 static const RawSocketEvent WRITE = const RawSocketEvent._(1); | 341 static const RawSocketEvent WRITE = const RawSocketEvent._(1); |
| 341 static const RawSocketEvent READ_CLOSED = const RawSocketEvent._(2); | 342 static const RawSocketEvent READ_CLOSED = const RawSocketEvent._(2); |
| 342 static const RawSocketEvent CLOSED = const RawSocketEvent._(3); | 343 static const RawSocketEvent CLOSED = const RawSocketEvent._(3); |
| 344 final int _value; |
| 345 |
| 343 const RawSocketEvent._(this._value); | 346 const RawSocketEvent._(this._value); |
| 344 final int _value; | |
| 345 String toString() { | 347 String toString() { |
| 346 return ['RawSocketEvent:READ', | 348 return ['RawSocketEvent:READ', |
| 347 'RawSocketEvent:WRITE', | 349 'RawSocketEvent:WRITE', |
| 348 'RawSocketEvent:READ_CLOSED', | 350 'RawSocketEvent:READ_CLOSED', |
| 349 'RawSocketEvent:CLOSED'][_value]; | 351 'RawSocketEvent:CLOSED'][_value]; |
| 350 } | 352 } |
| 351 } | 353 } |
| 352 | 354 |
| 353 /** | 355 /** |
| 354 * The [RawSocket] is a low-level interface to a socket, exposing the raw | 356 * The [RawSocket] is a low-level interface to a socket, exposing the raw |
| 355 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s. | 357 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s. |
| 356 */ | 358 */ |
| 357 abstract class RawSocket implements Stream<RawSocketEvent> { | 359 abstract class RawSocket implements Stream<RawSocketEvent> { |
| 358 /** | 360 /** |
| 361 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.READ] |
| 362 * events. Default is [true]. |
| 363 */ |
| 364 bool readEventsEnabled; |
| 365 |
| 366 /** |
| 367 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.WRITE] |
| 368 * events. Default is [true]. |
| 369 * This is a one-shot listener, and writeEventsEnabled must be set |
| 370 * to true again to receive another write event. |
| 371 */ |
| 372 bool writeEventsEnabled; |
| 373 |
| 374 /** |
| 359 * Creates a new socket connection to the host and port and returns a [Future] | 375 * Creates a new socket connection to the host and port and returns a [Future] |
| 360 * that will complete with either a [RawSocket] once connected or an error | 376 * that will complete with either a [RawSocket] once connected or an error |
| 361 * if the host-lookup or connection failed. | 377 * if the host-lookup or connection failed. |
| 362 * | 378 * |
| 363 * [host] can either be a [String] or an [InternetAddress]. If [host] is a | 379 * [host] can either be a [String] or an [InternetAddress]. If [host] is a |
| 364 * [String], [connect] will perform a [InternetAddress.lookup] and use | 380 * [String], [connect] will perform a [InternetAddress.lookup] and use |
| 365 * the first value in the list. | 381 * the first value in the list. |
| 366 */ | 382 */ |
| 367 external static Future<RawSocket> connect(host, int port); | 383 external static Future<RawSocket> connect(host, int port); |
| 368 | 384 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 440 |
| 425 /** | 441 /** |
| 426 * Shutdown the socket in the [direction]. Calling [shutdown] will never | 442 * Shutdown the socket in the [direction]. Calling [shutdown] will never |
| 427 * throw an exception and calling it several times is supported. Calling | 443 * throw an exception and calling it several times is supported. Calling |
| 428 * shutdown with either [SocketDirection.BOTH] or [SocketDirection.RECEIVE] | 444 * shutdown with either [SocketDirection.BOTH] or [SocketDirection.RECEIVE] |
| 429 * can result in a [RawSocketEvent.READ_CLOSED] event. | 445 * can result in a [RawSocketEvent.READ_CLOSED] event. |
| 430 */ | 446 */ |
| 431 void shutdown(SocketDirection direction); | 447 void shutdown(SocketDirection direction); |
| 432 | 448 |
| 433 /** | 449 /** |
| 434 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.READ] | |
| 435 * events. Default is [true]. | |
| 436 */ | |
| 437 bool readEventsEnabled; | |
| 438 | |
| 439 /** | |
| 440 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.WRITE] | |
| 441 * events. Default is [true]. | |
| 442 * This is a one-shot listener, and writeEventsEnabled must be set | |
| 443 * to true again to receive another write event. | |
| 444 */ | |
| 445 bool writeEventsEnabled; | |
| 446 | |
| 447 /** | |
| 448 * Use [setOption] to customize the [RawSocket]. See [SocketOption] for | 450 * Use [setOption] to customize the [RawSocket]. See [SocketOption] for |
| 449 * available options. | 451 * available options. |
| 450 * | 452 * |
| 451 * Returns [true] if the option was set successfully, false otherwise. | 453 * Returns [true] if the option was set successfully, false otherwise. |
| 452 */ | 454 */ |
| 453 bool setOption(SocketOption option, bool enabled); | 455 bool setOption(SocketOption option, bool enabled); |
| 454 } | 456 } |
| 455 | 457 |
| 456 /** | 458 /** |
| 457 * A high-level class for communicating over a TCP socket. | 459 * A high-level class for communicating over a TCP socket. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 /** | 530 /** |
| 529 * The [RawDatagramSocket] is a low-level interface to an UDP socket, | 531 * The [RawDatagramSocket] is a low-level interface to an UDP socket, |
| 530 * exposing the raw events signaled by the system. It's a [Stream] of | 532 * exposing the raw events signaled by the system. It's a [Stream] of |
| 531 * [RawSocketEvent]s. | 533 * [RawSocketEvent]s. |
| 532 * | 534 * |
| 533 * Note that the event [RawSocketEvent.READ_CLOSED] will never be | 535 * Note that the event [RawSocketEvent.READ_CLOSED] will never be |
| 534 * received as an UDP socket cannot be closed by a remote peer. | 536 * received as an UDP socket cannot be closed by a remote peer. |
| 535 */ | 537 */ |
| 536 abstract class RawDatagramSocket extends Stream<RawSocketEvent> { | 538 abstract class RawDatagramSocket extends Stream<RawSocketEvent> { |
| 537 /** | 539 /** |
| 538 * Creates a new raw datagram socket binding it to an address and | |
| 539 * port. | |
| 540 */ | |
| 541 external static Future<RawDatagramSocket> bind( | |
| 542 host, int port, {bool reuseAddress: true}); | |
| 543 | |
| 544 /** | |
| 545 * Returns the port used by this socket. | |
| 546 */ | |
| 547 int get port; | |
| 548 | |
| 549 /** | |
| 550 * Returns the address used by this socket. | |
| 551 */ | |
| 552 InternetAddress get address; | |
| 553 | |
| 554 /** | |
| 555 * Close the datagram socket. | |
| 556 */ | |
| 557 void close(); | |
| 558 | |
| 559 /** | |
| 560 * Send a datagram. | |
| 561 * | |
| 562 * Returns the number of bytes written. This will always be either | |
| 563 * the size of [buffer] or `0`. | |
| 564 */ | |
| 565 int send(List<int> buffer, InternetAddress address, int port); | |
| 566 | |
| 567 /** | |
| 568 * Receive a datagram. If there are no datagrams available `null` is | |
| 569 * returned. | |
| 570 */ | |
| 571 Datagram receive(); | |
| 572 | |
| 573 /** | |
| 574 * Join a multicast group. | |
| 575 * | |
| 576 * If an error occur when trying to join the multicast group an | |
| 577 * exception is thrown. | |
| 578 */ | |
| 579 void joinMulticast(InternetAddress group, {NetworkInterface interface}); | |
| 580 | |
| 581 /** | |
| 582 * Leave a multicast group. | |
| 583 * | |
| 584 * If an error occur when trying to join the multicase group an | |
| 585 * exception is thrown. | |
| 586 */ | |
| 587 void leaveMulticast(InternetAddress group, {NetworkInterface interface}); | |
| 588 | |
| 589 /** | |
| 590 * Set or get, if the [RawDatagramSocket] should listen for | 540 * Set or get, if the [RawDatagramSocket] should listen for |
| 591 * [RawSocketEvent.READ] events. Default is [true]. | 541 * [RawSocketEvent.READ] events. Default is [true]. |
| 592 */ | 542 */ |
| 593 bool readEventsEnabled; | 543 bool readEventsEnabled; |
| 594 | 544 |
| 595 /** | 545 /** |
| 596 * Set or get, if the [RawDatagramSocket] should listen for | 546 * Set or get, if the [RawDatagramSocket] should listen for |
| 597 * [RawSocketEvent.WRITE] events. Default is [true]. This is a | 547 * [RawSocketEvent.WRITE] events. Default is [true]. This is a |
| 598 * one-shot listener, and writeEventsEnabled must be set to true | 548 * one-shot listener, and writeEventsEnabled must be set to true |
| 599 * again to receive another write event. | 549 * again to receive another write event. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 /** | 581 /** |
| 632 * Set or get, whether IPv4 broadcast is enabled. | 582 * Set or get, whether IPv4 broadcast is enabled. |
| 633 * | 583 * |
| 634 * IPv4 broadcast needs to be enabled by the sender for sending IPv4 | 584 * IPv4 broadcast needs to be enabled by the sender for sending IPv4 |
| 635 * broadcast packages. By default IPv4 broadcast is disabled. | 585 * broadcast packages. By default IPv4 broadcast is disabled. |
| 636 * | 586 * |
| 637 * For IPv6 there is no general broadcast mechanism. Use multicast | 587 * For IPv6 there is no general broadcast mechanism. Use multicast |
| 638 * instead. | 588 * instead. |
| 639 */ | 589 */ |
| 640 bool broadcastEnabled; | 590 bool broadcastEnabled; |
| 591 |
| 592 /** |
| 593 * Creates a new raw datagram socket binding it to an address and |
| 594 * port. |
| 595 */ |
| 596 external static Future<RawDatagramSocket> bind( |
| 597 host, int port, {bool reuseAddress: true}); |
| 598 |
| 599 /** |
| 600 * Returns the port used by this socket. |
| 601 */ |
| 602 int get port; |
| 603 |
| 604 /** |
| 605 * Returns the address used by this socket. |
| 606 */ |
| 607 InternetAddress get address; |
| 608 |
| 609 /** |
| 610 * Close the datagram socket. |
| 611 */ |
| 612 void close(); |
| 613 |
| 614 /** |
| 615 * Send a datagram. |
| 616 * |
| 617 * Returns the number of bytes written. This will always be either |
| 618 * the size of [buffer] or `0`. |
| 619 */ |
| 620 int send(List<int> buffer, InternetAddress address, int port); |
| 621 |
| 622 /** |
| 623 * Receive a datagram. If there are no datagrams available `null` is |
| 624 * returned. |
| 625 */ |
| 626 Datagram receive(); |
| 627 |
| 628 /** |
| 629 * Join a multicast group. |
| 630 * |
| 631 * If an error occur when trying to join the multicast group an |
| 632 * exception is thrown. |
| 633 */ |
| 634 void joinMulticast(InternetAddress group, {NetworkInterface interface}); |
| 635 |
| 636 /** |
| 637 * Leave a multicast group. |
| 638 * |
| 639 * If an error occur when trying to join the multicase group an |
| 640 * exception is thrown. |
| 641 */ |
| 642 void leaveMulticast(InternetAddress group, {NetworkInterface interface}); |
| 641 } | 643 } |
| 642 | 644 |
| 643 | 645 |
| 644 class SocketException implements IOException { | 646 class SocketException implements IOException { |
| 645 final String message; | 647 final String message; |
| 646 final OSError osError; | 648 final OSError osError; |
| 647 final InternetAddress address; | 649 final InternetAddress address; |
| 648 final int port; | 650 final int port; |
| 649 | 651 |
| 650 const SocketException(String this.message, | 652 const SocketException(this.message, {this.osError, this.address, this.port}); |
| 651 {OSError this.osError, | |
| 652 InternetAddress this.address, | |
| 653 int this.port}); | |
| 654 | 653 |
| 655 String toString() { | 654 String toString() { |
| 656 StringBuffer sb = new StringBuffer(); | 655 StringBuffer sb = new StringBuffer(); |
| 657 sb.write("SocketException"); | 656 sb.write("SocketException"); |
| 658 if (!message.isEmpty) { | 657 if (!message.isEmpty) { |
| 659 sb.write(": $message"); | 658 sb.write(": $message"); |
| 660 if (osError != null) { | 659 if (osError != null) { |
| 661 sb.write(" ($osError)"); | 660 sb.write(" ($osError)"); |
| 662 } | 661 } |
| 663 } else if (osError != null) { | 662 } else if (osError != null) { |
| 664 sb.write(": $osError"); | 663 sb.write(": $osError"); |
| 665 } | 664 } |
| 666 if (address != null) { | 665 if (address != null) { |
| 667 sb.write(", address = ${address.host}"); | 666 sb.write(", address = ${address.host}"); |
| 668 } | 667 } |
| 669 if (port != null) { | 668 if (port != null) { |
| 670 sb.write(", port = $port"); | 669 sb.write(", port = $port"); |
| 671 } | 670 } |
| 672 return sb.toString(); | 671 return sb.toString(); |
| 673 } | 672 } |
| 674 } | 673 } |
| OLD | NEW |