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 /** | |
9 * [InternetAddressType] is the type an [InternetAddress]. Currently, IPv4 and | |
10 * IPv6 are supported. | |
11 */ | |
12 class InternetAddressType { | |
13 static const InternetAddressType IPv4 = const InternetAddressType._(0); | |
14 static const InternetAddressType IPv6 = const InternetAddressType._(1); | |
15 static const InternetAddressType ANY = const InternetAddressType._(-1); | |
16 | |
17 final int _value; | |
18 | |
19 const InternetAddressType._(int this._value); | |
20 | |
21 factory InternetAddressType._from(int value) { | |
22 if (value == 0) return IPv4; | |
23 if (value == 1) return IPv6; | |
24 throw new ArgumentError("Invalid type: $value"); | |
25 } | |
26 | |
27 /** | |
28 * Get the name of the type, e.g. "IPv4" or "IPv6". | |
29 */ | |
30 String get name { | |
31 switch (_value) { | |
32 case -1: return "ANY"; | |
33 case 0: return "IPv4"; | |
34 case 1: return "IPv6"; | |
35 default: throw new ArgumentError("Invalid InternetAddress"); | |
36 } | |
37 } | |
38 | |
39 String toString() => "InternetAddressType($name)"; | |
40 } | |
41 | |
42 | |
43 /** | |
44 * The [InternetAddress] is an object reflecting either a remote or a local | |
45 * address, for wich a socket can be connected to or bound on. | |
46 * | |
47 */ | |
48 abstract class InternetAddress { | |
49 /** | |
50 * The [type] of the [InternetAddress] specified what IP procotol. | |
51 */ | |
52 InternetAddressType type; | |
53 | |
54 /** | |
55 * The resolved address of the host. | |
56 */ | |
57 String get address; | |
58 | |
59 /** | |
60 * The host used to lookup the address. | |
61 */ | |
62 String get host; | |
63 | |
64 /** | |
65 * Lookup a host, returning a Future of a list of [InternetAddress]s. If | |
66 * [type] is [InternetAddressType.ANY], it will lookup both IPv4 and IPv6 | |
67 * addresses. The order of the list depends on the local machine and the DNS | |
68 * lookup performed, and can as such change over time. | |
69 */ | |
70 external static Future<List<InternetAddress>> lookup( | |
71 String host, {InternetAddressType type: InternetAddressType.ANY}); | |
72 } | |
73 | |
74 /** | 7 /** |
75 * The RawServerSocket is a server socket, providing a stream of low-level | 8 * The RawServerSocket is a server socket, providing a stream of low-level |
76 * [RawSocket]s. | 9 * [RawSocket]s. |
77 * | 10 * |
78 * See [RawSocket] for more info. | 11 * See [RawSocket] for more info. |
79 */ | 12 */ |
80 abstract class RawServerSocket implements Stream<RawSocket> { | 13 abstract class RawServerSocket implements Stream<RawSocket> { |
81 /** | 14 /** |
82 * Returns a future for a [:RawServerSocket:]. When the future | 15 * Returns a future for a [:RawServerSocket:]. When the future |
83 * completes the server socket is bound to the given [address] and | 16 * completes the server socket is bound to the given [address] and |
84 * [port] and has started listening on it. | 17 * [port] and has started listening on it. |
85 * | 18 * |
86 * The default value for [address] is 127.0.0.1, which will allow | 19 * The default value for [address] is 127.0.0.1, which will allow |
87 * only incoming connections from the local host. To allow for | 20 * only incoming connections from the local host. To allow for |
88 * incoming connection from the network use either the value 0.0.0.0 | 21 * incoming connection from the network use either the value 0.0.0.0 |
89 * to bind to all interfaces or the IP address of a specific | 22 * to bind to all interfaces or the IP address of a specific |
90 * interface. | 23 * interface. |
91 * | 24 * |
92 * If [port] has the value [:0:] (the default) an ephemeral port will | 25 * If [port] has the value [:0:] (the default) an ephemeral port will |
93 * be chosen by the system. The actual port used can be retrieved | 26 * be chosen by the system. The actual port used can be retrieved |
94 * using the [:port:] getter. | 27 * using the [:port:] getter. |
95 * | 28 * |
96 * The optional argument [backlog] can be used to specify the listen | 29 * The optional argument [backlog] can be used to specify the listen |
97 * backlog for the underlying OS listen setup. If [backlog] has the | 30 * backlog for the underlying OS listen setup. If [backlog] has the |
98 * value of [:0:] (the default) a reasonable value will be chosen by | 31 * value of [:0:] (the default) a reasonable value will be chosen by |
99 * the system. | 32 * the system. |
100 */ | 33 */ |
101 external static Future<RawServerSocket> bind([String address = "127.0.0.1", | 34 external static Future<RawServerSocket> bind([String address = "127.0.0.1", |
102 int port = 0, | 35 int port = 0, |
103 int backlog = 0]); | 36 int backlog = 0]); |
104 | 37 |
105 /** | 38 /** |
106 * Returns the port used by this socket. | 39 * Returns the port used by this socket. |
107 */ | 40 */ |
108 int get port; | 41 int get port; |
109 | 42 |
110 /** | 43 /** |
111 * Closes the socket. | 44 * Closes the socket. |
112 */ | 45 */ |
113 void close(); | 46 void close(); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 | 137 |
205 /** | 138 /** |
206 * The [RawSocket] is a low-level interface to a socket, exposing the raw | 139 * The [RawSocket] is a low-level interface to a socket, exposing the raw |
207 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s. | 140 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s. |
208 */ | 141 */ |
209 abstract class RawSocket implements Stream<RawSocketEvent> { | 142 abstract class RawSocket implements Stream<RawSocketEvent> { |
210 /** | 143 /** |
211 * Creates a new socket connection to the host and port and returns a [Future] | 144 * Creates a new socket connection to the host and port and returns a [Future] |
212 * that will complete with either a [RawSocket] once connected or an error | 145 * that will complete with either a [RawSocket] once connected or an error |
213 * if the host-lookup or connection failed. | 146 * if the host-lookup or connection failed. |
214 * | |
215 * [host] can either be a [String] or an [InternetAddress]. If [host] is a | |
216 * [String], [connect] will perform a [InternetAddress.lookup] and use | |
217 * the first value in the list. | |
218 */ | 147 */ |
219 external static Future<RawSocket> connect(host, int port); | 148 external static Future<RawSocket> connect(String host, int port); |
220 | 149 |
221 /** | 150 /** |
222 * Returns the number of received and non-read bytes in the socket that | 151 * Returns the number of received and non-read bytes in the socket that |
223 * can be read. | 152 * can be read. |
224 */ | 153 */ |
225 int available(); | 154 int available(); |
226 | 155 |
227 /** | 156 /** |
228 * Read up to [len] bytes from the socket. This function is | 157 * Read up to [len] bytes from the socket. This function is |
229 * non-blocking and will only return data if data is available. The | 158 * non-blocking and will only return data if data is available. The |
(...skipping 15 matching lines...) Expand all Loading... |
245 * Returns the port used by this socket. | 174 * Returns the port used by this socket. |
246 */ | 175 */ |
247 int get port; | 176 int get port; |
248 | 177 |
249 /** | 178 /** |
250 * Returns the remote port connected to by this socket. | 179 * Returns the remote port connected to by this socket. |
251 */ | 180 */ |
252 int get remotePort; | 181 int get remotePort; |
253 | 182 |
254 /** | 183 /** |
255 * Returns the [InternetAddress] used to connect this socket. | 184 * Returns the host used to connect this socket. |
256 */ | 185 */ |
257 InternetAddress get address; | 186 String get host; |
258 | 187 |
259 /** | 188 /** |
260 * Returns the remote host connected to by this socket. | 189 * Returns the remote host connected to by this socket. |
261 */ | 190 */ |
262 String get remoteHost; | 191 String get remoteHost; |
263 | 192 |
264 /** | 193 /** |
265 * Closes the socket. Calling [close] will never throw an exception | 194 * Closes the socket. Calling [close] will never throw an exception |
266 * and calling it several times is supported. Calling [close] can result in | 195 * and calling it several times is supported. Calling [close] can result in |
267 * a [RawSocketEvent.READ_CLOSED] event. | 196 * a [RawSocketEvent.READ_CLOSED] event. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 /** | 231 /** |
303 * A high-level class for communicating over a TCP socket. The [Socket] exposes | 232 * A high-level class for communicating over a TCP socket. The [Socket] exposes |
304 * both a [Stream] and a [IOSink] interface, making it ideal for | 233 * both a [Stream] and a [IOSink] interface, making it ideal for |
305 * using together with other [Stream]s. | 234 * using together with other [Stream]s. |
306 */ | 235 */ |
307 abstract class Socket implements Stream<List<int>>, IOSink { | 236 abstract class Socket implements Stream<List<int>>, IOSink { |
308 /** | 237 /** |
309 * Creats a new socket connection to the host and port and returns a [Future] | 238 * Creats a new socket connection to the host and port and returns a [Future] |
310 * that will complete with either a [Socket] once connected or an error | 239 * that will complete with either a [Socket] once connected or an error |
311 * if the host-lookup or connection failed. | 240 * if the host-lookup or connection failed. |
312 * | |
313 * [host] can either be a [String] or an [InternetAddress]. If [host] is a | |
314 * [String], [connect] will perform a [InternetAddress.lookup] and use | |
315 * the first value in the list. | |
316 */ | 241 */ |
317 external static Future<Socket> connect(host, int port); | 242 external static Future<Socket> connect(String host, int port); |
318 | 243 |
319 /** | 244 /** |
320 * Destroy the socket in both directions. Calling [destroy] will make the | 245 * Destroy the socket in both directions. Calling [destroy] will make the |
321 * send a close event on the stream and will no longer react on data being | 246 * send a close event on the stream and will no longer react on data being |
322 * piped to it. | 247 * piped to it. |
323 * | 248 * |
324 * Call [close](inherited from [IOSink]) to only close the [Socket] | 249 * Call [close](inherited from [IOSink]) to only close the [Socket] |
325 * for sending data. | 250 * for sending data. |
326 */ | 251 */ |
327 void destroy(); | 252 void destroy(); |
(...skipping 10 matching lines...) Expand all Loading... |
338 * Returns the port used by this socket. | 263 * Returns the port used by this socket. |
339 */ | 264 */ |
340 int get port; | 265 int get port; |
341 | 266 |
342 /** | 267 /** |
343 * Returns the remote port connected to by this socket. | 268 * Returns the remote port connected to by this socket. |
344 */ | 269 */ |
345 int get remotePort; | 270 int get remotePort; |
346 | 271 |
347 /** | 272 /** |
348 * Returns the [InternetAddress] used to connect this socket. | 273 * Returns the host used to connect this socket. |
349 */ | 274 */ |
350 InternetAddress get address; | 275 String get host; |
351 | 276 |
352 /** | 277 /** |
353 * Returns the remote host connected to by this socket. | 278 * Returns the remote host connected to by this socket. |
354 */ | 279 */ |
355 String get remoteHost; | 280 String get remoteHost; |
356 } | 281 } |
357 | 282 |
358 | 283 |
359 class SocketIOException implements Exception { | 284 class SocketIOException implements Exception { |
360 const SocketIOException([String this.message = "", | 285 const SocketIOException([String this.message = "", |
361 OSError this.osError = null]); | 286 OSError this.osError = null]); |
362 String toString() { | 287 String toString() { |
363 StringBuffer sb = new StringBuffer(); | 288 StringBuffer sb = new StringBuffer(); |
364 sb.write("SocketIOException"); | 289 sb.write("SocketIOException"); |
365 if (!message.isEmpty) { | 290 if (!message.isEmpty) { |
366 sb.write(": $message"); | 291 sb.write(": $message"); |
367 if (osError != null) { | 292 if (osError != null) { |
368 sb.write(" ($osError)"); | 293 sb.write(" ($osError)"); |
369 } | 294 } |
370 } else if (osError != null) { | 295 } else if (osError != null) { |
371 sb.write(": $osError"); | 296 sb.write(": $osError"); |
372 } | 297 } |
373 return sb.toString(); | 298 return sb.toString(); |
374 } | 299 } |
375 final String message; | 300 final String message; |
376 final OSError osError; | 301 final OSError osError; |
377 } | 302 } |
OLD | NEW |