Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/lib/io/socket.dart

Issue 2698353003: unfork DDC's copy of most SDK libraries (Closed)
Patch Set: revert core_patch Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart.io;
6
7
8 /**
9 * [InternetAddressType] is the type an [InternetAddress]. Currently,
10 * IP version 4 (IPv4) and IP version 6 (IPv6) are supported.
11 */
12 class InternetAddressType {
13 static const InternetAddressType IP_V4 = const InternetAddressType._(0);
14 static const InternetAddressType IP_V6 = const InternetAddressType._(1);
15 static const InternetAddressType ANY = const InternetAddressType._(-1);
16
17 final int _value;
18
19 const InternetAddressType._(this._value);
20
21 factory InternetAddressType._from(int value) {
22 if (value == 0) return IP_V4;
23 if (value == 1) return IP_V6;
24 throw new ArgumentError("Invalid type: $value");
25 }
26
27 /**
28 * Get the name of the type, e.g. "IP_V4" or "IP_V6".
29 */
30 String get name {
31 switch (_value) {
32 case -1: return "ANY";
33 case 0: return "IP_V4";
34 case 1: return "IP_V6";
35 default: throw new ArgumentError("Invalid InternetAddress");
36 }
37 }
38
39 String toString() => "InternetAddressType: $name";
40 }
41
42
43 /**
44 * An internet address.
45 *
46 * This object holds an internet address. If this internet address
47 * is the result of a DNS lookup, the address also holds the hostname
48 * used to make the lookup.
49 * An Internet address combined with a port number represents an
50 * endpoint to which a socket can connect or a listening socket can
51 * bind.
52 */
53 abstract class InternetAddress {
54 /**
55 * IP version 4 loopback address. Use this address when listening on
56 * or connecting to the loopback adapter using IP version 4 (IPv4).
57 */
58 external static InternetAddress get LOOPBACK_IP_V4;
59
60 /**
61 * IP version 6 loopback address. Use this address when listening on
62 * or connecting to the loopback adapter using IP version 6 (IPv6).
63 */
64 external static InternetAddress get LOOPBACK_IP_V6;
65
66 /**
67 * IP version 4 any address. Use this address when listening on
68 * all adapters IP addresses using IP version 4 (IPv4).
69 */
70 external static InternetAddress get ANY_IP_V4;
71
72 /**
73 * IP version 6 any address. Use this address when listening on
74 * all adapters IP addresses using IP version 6 (IPv6).
75 */
76 external static InternetAddress get ANY_IP_V6;
77
78 /**
79 * The [type] of the [InternetAddress] specified what IP protocol.
80 */
81 InternetAddressType type;
82
83 /**
84 * The numeric address of the host. For IPv4 addresses this is using
85 * the dotted-decimal notation. For IPv6 it is using the
86 * hexadecimal representation.
87 */
88 String get address;
89
90 /**
91 * The host used to lookup the address. If there is no host
92 * associated with the address this returns the numeric address.
93 */
94 String get host;
95
96 /**
97 * Get the raw address of this [InternetAddress]. The result is either a
98 * 4 or 16 byte long list. The returned list is a copy, making it possible
99 * to change the list without modifying the [InternetAddress].
100 */
101 List<int> get rawAddress;
102
103 /**
104 * Returns true if the [InternetAddress] is a loopback address.
105 */
106 bool get isLoopback;
107
108 /**
109 * Returns true if the [InternetAddress]s scope is a link-local.
110 */
111 bool get isLinkLocal;
112
113 /**
114 * Returns true if the [InternetAddress]s scope is multicast.
115 */
116 bool get isMulticast;
117
118 /**
119 * Creates a new [InternetAddress] from a numeric address.
120 *
121 * If the address in [address] is not a numeric IPv4
122 * (dotted-decimal notation) or IPv6 (hexadecimal representation).
123 * address [ArgumentError] is thrown.
124 */
125 external factory InternetAddress(String address);
126
127 /**
128 * Perform a reverse dns lookup on the [address], creating a new
129 * [InternetAddress] where the host field set to the result.
130 */
131 Future<InternetAddress> reverse();
132
133 /**
134 * Lookup a host, returning a Future of a list of
135 * [InternetAddress]s. If [type] is [InternetAddressType.ANY], it
136 * will lookup both IP version 4 (IPv4) and IP version 6 (IPv6)
137 * addresses. If [type] is either [InternetAddressType.IP_V4] or
138 * [InternetAddressType.IP_V6] it will only lookup addresses of the
139 * specified type. The order of the list can, and most likely will,
140 * change over time.
141 */
142 external static Future<List<InternetAddress>> lookup(
143 String host, {InternetAddressType type: InternetAddressType.ANY});
144
145 /**
146 * Clones the given [address] with the new [host].
147 *
148 * The [address] must be an [InternetAddress] that was created with one
149 * of the static methods of this class.
150 */
151 external static InternetAddress _cloneWithNewHost(
152 InternetAddress address, String host);
153 }
154
155
156 /**
157 * 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 * interface.
160 */
161 abstract class NetworkInterface {
162 /**
163 * Get the name of the [NetworkInterface].
164 */
165 String get name;
166
167 /**
168 * Get the index of the [NetworkInterface].
169 */
170 String get index;
171
172 /**
173 * Get a list of [InternetAddress]es currently bound to this
174 * [NetworkInterface].
175 */
176 List<InternetAddress> get addresses;
177
178 /**
179 * Whether [list] is supported.
180 *
181 * [list] is currently unsupported on Android.
182 */
183 external static bool get listSupported;
184
185 /**
186 * Query the system for [NetworkInterface]s.
187 *
188 * If [includeLoopback] is `true`, the returned list will include the
189 * loopback device. Default is `false`.
190 *
191 * If [includeLinkLocal] is `true`, the list of addresses of the returned
192 * [NetworkInterface]s, may include link local addresses. Default is `false`.
193 *
194 * If [type] is either [InternetAddressType.IP_V4] or
195 * [InternetAddressType.IP_V6] it will only lookup addresses of the
196 * specified type. Default is [InternetAddressType.ANY].
197 */
198 external static Future<List<NetworkInterface>> list({
199 bool includeLoopback: false,
200 bool includeLinkLocal: false,
201 InternetAddressType type: InternetAddressType.ANY});
202 }
203
204
205 /**
206 * A [RawServerSocket] represents a listening socket, and provides a
207 * stream of low-level [RawSocket] objects, one for each connection
208 * made to the listening socket.
209 *
210 * See [RawSocket] for more info.
211 */
212 abstract class RawServerSocket implements Stream<RawSocket> {
213 /**
214 * Returns a future for a [:RawServerSocket:]. When the future
215 * completes the server socket is bound to the given [address] and
216 * [port] and has started listening on it.
217 *
218 * The [address] can either be a [String] or an
219 * [InternetAddress]. If [address] is a [String], [bind] will
220 * perform a [InternetAddress.lookup] and use the first value in the
221 * list. To listen on the loopback adapter, which will allow only
222 * incoming connections from the local host, use the value
223 * [InternetAddress.LOOPBACK_IP_V4] or
224 * [InternetAddress.LOOPBACK_IP_V6]. To allow for incoming
225 * connection from the network use either one of the values
226 * [InternetAddress.ANY_IP_V4] or [InternetAddress.ANY_IP_V6] to
227 * bind to all interfaces or the IP address of a specific interface.
228 *
229 * If an IP version 6 (IPv6) address is used, both IP version 6
230 * (IPv6) and version 4 (IPv4) connections will be accepted. To
231 * restrict this to version 6 (IPv6) only, use [v6Only] to set
232 * version 6 only.
233 *
234 * If [port] has the value [:0:] an ephemeral port will
235 * be chosen by the system. The actual port used can be retrieved
236 * using the [:port:] getter.
237 *
238 * The optional argument [backlog] can be used to specify the listen
239 * backlog for the underlying OS listen setup. If [backlog] has the
240 * value of [:0:] (the default) a reasonable value will be chosen by
241 * the system.
242 *
243 * The optional argument [shared] specifies whether additional RawServerSocket
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
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
248 * distributed over multiple isolates this way.
249 */
250 external static Future<RawServerSocket> bind(address,
251 int port,
252 {int backlog: 0,
253 bool v6Only: false,
254 bool shared: false});
255
256 /**
257 * Returns the port used by this socket.
258 */
259 int get port;
260
261 /**
262 * Returns the address used by this socket.
263 */
264 InternetAddress get address;
265
266 /**
267 * Closes the socket. The returned future completes when the socket
268 * is fully closed and is no longer bound.
269 */
270 Future<RawServerSocket> close();
271 }
272
273
274 /**
275 * A [ServerSocket] represents a listening socket, and provides a
276 * stream of [Socket] objects, one for each connection made to the
277 * listening socket.
278 *
279 * See [Socket] for more info.
280 */
281 abstract class ServerSocket implements Stream<Socket> {
282 /**
283 * Returns a future for a [:ServerSocket:]. When the future
284 * completes the server socket is bound to the given [address] and
285 * [port] and has started listening on it.
286 *
287 * The [address] can either be a [String] or an
288 * [InternetAddress]. If [address] is a [String], [bind] will
289 * perform a [InternetAddress.lookup] and use the first value in the
290 * list. To listen on the loopback adapter, which will allow only
291 * incoming connections from the local host, use the value
292 * [InternetAddress.LOOPBACK_IP_V4] or
293 * [InternetAddress.LOOPBACK_IP_V6]. To allow for incoming
294 * connection from the network use either one of the values
295 * [InternetAddress.ANY_IP_V4] or [InternetAddress.ANY_IP_V6] to
296 * bind to all interfaces or the IP address of a specific interface.
297 *
298 * If an IP version 6 (IPv6) address is used, both IP version 6
299 * (IPv6) and version 4 (IPv4) connections will be accepted. To
300 * restrict this to version 6 (IPv6) only, use [v6Only] to set
301 * version 6 only.
302 *
303 * If [port] has the value [:0:] an ephemeral port will be chosen by
304 * the system. The actual port used can be retrieved using the
305 * [port] getter.
306 *
307 * The optional argument [backlog] can be used to specify the listen
308 * backlog for the underlying OS listen setup. If [backlog] has the
309 * value of [:0:] (the default) a reasonable value will be chosen by
310 * the system.
311 *
312 * The optional argument [shared] specifies whether additional ServerSocket
313 * 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
315 * isolates are bound to the port, then the incoming connections will be
316 * distributed among all the bound `ServerSocket`s. Connections can be
317 * distributed over multiple isolates this way.
318 */
319 external static Future<ServerSocket> bind(address,
320 int port,
321 {int backlog: 0,
322 bool v6Only: false,
323 bool shared: false});
324
325 /**
326 * Returns the port used by this socket.
327 */
328 int get port;
329
330 /**
331 * Returns the address used by this socket.
332 */
333 InternetAddress get address;
334
335 /**
336 * Closes the socket. The returned future completes when the socket
337 * is fully closed and is no longer bound.
338 */
339 Future<ServerSocket> close();
340 }
341
342
343 /**
344 * The [SocketDirection] is used as a parameter to [Socket.close] and
345 * [RawSocket.close] to close a socket in the specified direction(s).
346 */
347 class SocketDirection {
348 static const SocketDirection RECEIVE = const SocketDirection._(0);
349 static const SocketDirection SEND = const SocketDirection._(1);
350 static const SocketDirection BOTH = const SocketDirection._(2);
351 final _value;
352
353 const SocketDirection._(this._value);
354 }
355
356 /**
357 * The [SocketOption] is used as a parameter to [Socket.setOption] and
358 * [RawSocket.setOption] to set customize the behaviour of the underlying
359 * socket.
360 */
361 class SocketOption {
362 /**
363 * Enable or disable no-delay on the socket. If TCP_NODELAY is enabled, the
364 * socket will not buffer data internally, but instead write each data chunk
365 * as an invidual TCP packet.
366 *
367 * TCP_NODELAY is disabled by default.
368 */
369 static const SocketOption TCP_NODELAY = const SocketOption._(0);
370
371 static const SocketOption _IP_MULTICAST_LOOP = const SocketOption._(1);
372 static const SocketOption _IP_MULTICAST_HOPS = const SocketOption._(2);
373 static const SocketOption _IP_MULTICAST_IF = const SocketOption._(3);
374 static const SocketOption _IP_BROADCAST = const SocketOption._(4);
375 final _value;
376
377 const SocketOption._(this._value);
378 }
379
380 /**
381 * Events for the [RawSocket].
382 */
383 class RawSocketEvent {
384 static const RawSocketEvent READ = const RawSocketEvent._(0);
385 static const RawSocketEvent WRITE = const RawSocketEvent._(1);
386 static const RawSocketEvent READ_CLOSED = const RawSocketEvent._(2);
387 static const RawSocketEvent CLOSED = const RawSocketEvent._(3);
388 final int _value;
389
390 const RawSocketEvent._(this._value);
391 String toString() {
392 return const ['RawSocketEvent:READ',
393 'RawSocketEvent:WRITE',
394 'RawSocketEvent:READ_CLOSED',
395 'RawSocketEvent:CLOSED'][_value];
396 }
397 }
398
399 /**
400 * 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.
402 */
403 abstract class RawSocket implements Stream<RawSocketEvent> {
404 /**
405 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.READ]
406 * events. Default is [:true:].
407 */
408 bool readEventsEnabled;
409
410 /**
411 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.WRITE]
412 * events. Default is [:true:].
413 * This is a one-shot listener, and writeEventsEnabled must be set
414 * to true again to receive another write event.
415 */
416 bool writeEventsEnabled;
417
418 /**
419 * Creates a new socket connection to the host and port and returns a [Future]
420 * that will complete with either a [RawSocket] once connected or an error
421 * if the host-lookup or connection failed.
422 *
423 * [host] can either be a [String] or an [InternetAddress]. If [host] is a
424 * [String], [connect] will perform a [InternetAddress.lookup] and try
425 * all returned [InternetAddress]es, until connected. Unless a
426 * connection was established, the error from the first failing connection is
427 * returned.
428 *
429 * The argument [sourceAddress] can be used to specify the local
430 * address to bind when making the connection. `sourceAddress` can either
431 * be a `String` or an `InternetAddress`. If a `String` is passed it must
432 * hold a numeric IP address.
433 */
434 external static Future<RawSocket> connect(host, int port, {sourceAddress});
435
436 /**
437 * Returns the number of received and non-read bytes in the socket that
438 * can be read.
439 */
440 int available();
441
442 /**
443 * Read up to [len] bytes from the socket. This function is
444 * non-blocking and will only return data if data is available. The
445 * number of bytes read can be less then [len] if fewer bytes are
446 * available for immediate reading. If no data is available [:null:]
447 * is returned.
448 */
449 List<int> read([int len]);
450
451 /**
452 * Writes up to [count] bytes of the buffer from [offset] buffer offset to
453 * the socket. The number of successfully written bytes is returned. This
454 * function is non-blocking and will only write data if buffer space is
455 * available in the socket.
456 *
457 * The default value for [offset] is 0, and the default value for [count] is
458 * [:buffer.length - offset:].
459 */
460 int write(List<int> buffer, [int offset, int count]);
461
462 /**
463 * Returns the port used by this socket.
464 */
465 int get port;
466
467 /**
468 * Returns the remote port connected to by this socket.
469 */
470 int get remotePort;
471
472 /**
473 * Returns the [InternetAddress] used to connect this socket.
474 */
475 InternetAddress get address;
476
477 /**
478 * Returns the remote [InternetAddress] connected to by this socket.
479 */
480 InternetAddress get remoteAddress;
481
482 /**
483 * Closes the socket. Returns a Future that completes with [this] when the
484 * underlying connection is completely destroyed.
485 *
486 * Calling [close] will never throw an exception
487 * and calling it several times is supported. Calling [close] can result in
488 * a [RawSocketEvent.READ_CLOSED] event.
489 */
490 Future<RawSocket> close();
491
492 /**
493 * Shutdown the socket in the [direction]. Calling [shutdown] will never
494 * throw an exception and calling it several times is supported. Calling
495 * shutdown with either [SocketDirection.BOTH] or [SocketDirection.RECEIVE]
496 * can result in a [RawSocketEvent.READ_CLOSED] event.
497 */
498 void shutdown(SocketDirection direction);
499
500 /**
501 * Use [setOption] to customize the [RawSocket]. See [SocketOption] for
502 * available options.
503 *
504 * Returns [:true:] if the option was set successfully, false otherwise.
505 */
506 bool setOption(SocketOption option, bool enabled);
507 }
508
509 /**
510 * A high-level class for communicating over a TCP socket.
511 *
512 * The [Socket] exposes both a [Stream] and a [IOSink] interface, making it
513 * ideal for using together with other [Stream]s.
514 */
515 abstract class Socket implements Stream<List<int>>, IOSink {
516 /**
517 * Creates a new socket connection to the host and port and returns a [Future]
518 * that will complete with either a [Socket] once connected or an error
519 * if the host-lookup or connection failed.
520 *
521 * [host] can either be a [String] or an [InternetAddress]. If [host] is a
522 * [String], [connect] will perform a [InternetAddress.lookup] and try
523 * all returned [InternetAddress]es, until connected. Unless a
524 * connection was established, the error from the first failing connection is
525 * returned.
526 *
527 * The argument [sourceAddress] can be used to specify the local
528 * address to bind when making the connection. `sourceAddress` can either
529 * be a `String` or an `InternetAddress`. If a `String` is passed it must
530 * hold a numeric IP address.
531 */
532 external static Future<Socket> connect(host, int port, {sourceAddress});
533
534 /**
535 * Destroy the socket in both directions. Calling [destroy] will make the
536 * send a close event on the stream and will no longer react on data being
537 * piped to it.
538 *
539 * Call [close](inherited from [IOSink]) to only close the [Socket]
540 * for sending data.
541 */
542 void destroy();
543
544 /**
545 * Use [setOption] to customize the [RawSocket]. See [SocketOption] for
546 * available options.
547 *
548 * Returns [:true:] if the option was set successfully, false otherwise.
549 */
550 bool setOption(SocketOption option, bool enabled);
551
552 /**
553 * Returns the port used by this socket.
554 */
555 int get port;
556
557 /**
558 * Returns the remote port connected to by this socket.
559 */
560 int get remotePort;
561
562 /**
563 * Returns the [InternetAddress] used to connect this socket.
564 */
565 InternetAddress get address;
566
567 /**
568 * Returns the remote [InternetAddress] connected to by this socket.
569 */
570 InternetAddress get remoteAddress;
571 }
572
573
574 /**
575 * Datagram package. Data send to and received from datagram sockets
576 * contains the internet address and port of the destination or source
577 * togeter with the data.
578 */
579 class Datagram {
580 List<int> data;
581 InternetAddress address;
582 int port;
583
584 Datagram(this.data, this.address, this.port);
585 }
586
587
588 /**
589 * The [RawDatagramSocket] is a low-level interface to an UDP socket,
590 * exposing the raw events signaled by the system. It's a [Stream] of
591 * [RawSocketEvent]s.
592 *
593 * Note that the event [RawSocketEvent.READ_CLOSED] will never be
594 * received as an UDP socket cannot be closed by a remote peer.
595 */
596 abstract class RawDatagramSocket extends Stream<RawSocketEvent> {
597 /**
598 * Set or get, if the [RawDatagramSocket] should listen for
599 * [RawSocketEvent.READ] events. Default is [:true:].
600 */
601 bool readEventsEnabled;
602
603 /**
604 * Set or get, if the [RawDatagramSocket] should listen for
605 * [RawSocketEvent.WRITE] events. Default is [:true:]. This is a
606 * one-shot listener, and writeEventsEnabled must be set to true
607 * again to receive another write event.
608 */
609 bool writeEventsEnabled;
610
611 /**
612 * Set or get, whether multicast traffic is looped back to the host.
613 *
614 * By default multicast loopback is enabled.
615 */
616 bool multicastLoopback;
617
618 /**
619 * Set or get, the maximum network hops for multicast packages
620 * originating from this socket.
621 *
622 * For IPv4 this is referred to as TTL (time to live).
623 *
624 * By default this value is 1 causing multicast traffic to stay on
625 * the local network.
626 */
627 int multicastHops;
628
629 /**
630 * Set or get, the network interface used for outgoing multicast packages.
631 *
632 * A value of `null`indicate that the system chooses the network
633 * interface to use.
634 *
635 * By default this value is `null`
636 */
637 NetworkInterface multicastInterface;
638
639 /**
640 * Set or get, whether IPv4 broadcast is enabled.
641 *
642 * IPv4 broadcast needs to be enabled by the sender for sending IPv4
643 * broadcast packages. By default IPv4 broadcast is disabled.
644 *
645 * For IPv6 there is no general broadcast mechanism. Use multicast
646 * instead.
647 */
648 bool broadcastEnabled;
649
650 /**
651 * Creates a new raw datagram socket binding it to an address and
652 * port.
653 */
654 external static Future<RawDatagramSocket> bind(
655 host, int port, {bool reuseAddress: true});
656
657 /**
658 * Returns the port used by this socket.
659 */
660 int get port;
661
662 /**
663 * Returns the address used by this socket.
664 */
665 InternetAddress get address;
666
667 /**
668 * Close the datagram socket.
669 */
670 void close();
671
672 /**
673 * Send a datagram.
674 *
675 * Returns the number of bytes written. This will always be either
676 * the size of [buffer] or `0`.
677 */
678 int send(List<int> buffer, InternetAddress address, int port);
679
680 /**
681 * Receive a datagram. If there are no datagrams available `null` is
682 * returned.
683 *
684 * The maximum length of the datagram that can be received is 65503 bytes.
685 */
686 Datagram receive();
687
688 /**
689 * Join a multicast group.
690 *
691 * If an error occur when trying to join the multicast group an
692 * exception is thrown.
693 */
694 void joinMulticast(InternetAddress group, {NetworkInterface interface});
695
696 /**
697 * Leave a multicast group.
698 *
699 * If an error occur when trying to join the multicase group an
700 * exception is thrown.
701 */
702 void leaveMulticast(InternetAddress group, {NetworkInterface interface});
703 }
704
705
706 class SocketException implements IOException {
707 final String message;
708 final OSError osError;
709 final InternetAddress address;
710 final int port;
711
712 const SocketException(this.message, {this.osError, this.address, this.port});
713 const SocketException.closed()
714 : message = 'Socket has been closed',
715 osError = null,
716 address = null,
717 port = null;
718
719 String toString() {
720 StringBuffer sb = new StringBuffer();
721 sb.write("SocketException");
722 if (!message.isEmpty) {
723 sb.write(": $message");
724 if (osError != null) {
725 sb.write(" ($osError)");
726 }
727 } else if (osError != null) {
728 sb.write(": $osError");
729 }
730 if (address != null) {
731 sb.write(", address = ${address.host}");
732 }
733 if (port != null) {
734 sb.write(", port = $port");
735 }
736 return sb.toString();
737 }
738 }
OLDNEW
« no previous file with comments | « pkg/dev_compiler/tool/input_sdk/lib/io/service_object.dart ('k') | pkg/dev_compiler/tool/input_sdk/lib/io/stdio.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698