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