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 class InternetAddressType { |
| 9 static const InternetAddressType IPv4 = const InternetAddressType._(0); |
| 10 static const InternetAddressType IPv6 = const InternetAddressType._(1); |
| 11 static const InternetAddressType ANY = const InternetAddressType._(-1); |
| 12 |
| 13 final int _value; |
| 14 |
| 15 const InternetAddressType._(String this._value); |
| 16 |
| 17 String get name { |
| 18 switch (_value) { |
| 19 case -1: return "ANY"; |
| 20 case 0: return "IPv4"; |
| 21 case 1: return "IPv6"; |
| 22 default: throw new ArgumentError("Invalid InternetAddress"); |
| 23 } |
| 24 } |
| 25 |
| 26 String toString() => "InternetAddressType($name)"; |
| 27 |
| 28 int get hashCode => _value; |
| 29 |
| 30 bool operator==(other) => _value == other._value; |
| 31 } |
| 32 |
| 33 abstract class InternetAddress { |
| 34 InternetAddressType type; |
| 35 String get address; |
| 36 String get host; |
| 37 |
| 38 external static Future<List<InternetAddress>> lookup( |
| 39 String host, {InternetAddressType type: InternetAddressType.ANY}); |
| 40 } |
| 41 |
7 /** | 42 /** |
8 * The RawServerSocket is a server socket, providing a stream of low-level | 43 * The RawServerSocket is a server socket, providing a stream of low-level |
9 * [RawSocket]s. | 44 * [RawSocket]s. |
10 * | 45 * |
11 * See [RawSocket] for more info. | 46 * See [RawSocket] for more info. |
12 */ | 47 */ |
13 abstract class RawServerSocket implements Stream<RawSocket> { | 48 abstract class RawServerSocket implements Stream<RawSocket> { |
14 /** | 49 /** |
15 * Returns a future for a [:RawServerSocket:]. When the future | 50 * Returns a future for a [:RawServerSocket:]. When the future |
16 * completes the server socket is bound to the given [address] and | 51 * completes the server socket is bound to the given [address] and |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 172 |
138 /** | 173 /** |
139 * The [RawSocket] is a low-level interface to a socket, exposing the raw | 174 * 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. | 175 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s. |
141 */ | 176 */ |
142 abstract class RawSocket implements Stream<RawSocketEvent> { | 177 abstract class RawSocket implements Stream<RawSocketEvent> { |
143 /** | 178 /** |
144 * Creates a new socket connection to the host and port and returns a [Future] | 179 * 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 | 180 * that will complete with either a [RawSocket] once connected or an error |
146 * if the host-lookup or connection failed. | 181 * if the host-lookup or connection failed. |
| 182 * |
| 183 * [host] can either be a [String] or an [InternetAddress]. If [host] is a |
| 184 * [String], [connect] will perform a [InternetAddress.lookup] and use |
| 185 * the first value in the list. |
147 */ | 186 */ |
148 external static Future<RawSocket> connect(String host, int port); | 187 external static Future<RawSocket> connect(host, int port); |
149 | 188 |
150 /** | 189 /** |
151 * Returns the number of received and non-read bytes in the socket that | 190 * Returns the number of received and non-read bytes in the socket that |
152 * can be read. | 191 * can be read. |
153 */ | 192 */ |
154 int available(); | 193 int available(); |
155 | 194 |
156 /** | 195 /** |
157 * Read up to [len] bytes from the socket. This function is | 196 * Read up to [len] bytes from the socket. This function is |
158 * non-blocking and will only return data if data is available. The | 197 * 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. | 213 * Returns the port used by this socket. |
175 */ | 214 */ |
176 int get port; | 215 int get port; |
177 | 216 |
178 /** | 217 /** |
179 * Returns the remote port connected to by this socket. | 218 * Returns the remote port connected to by this socket. |
180 */ | 219 */ |
181 int get remotePort; | 220 int get remotePort; |
182 | 221 |
183 /** | 222 /** |
184 * Returns the host used to connect this socket. | 223 * Returns the [InternetAddress] used to connect this socket. |
185 */ | 224 */ |
186 String get host; | 225 InternetAddress get address; |
187 | 226 |
188 /** | 227 /** |
189 * Returns the remote host connected to by this socket. | 228 * Returns the remote host connected to by this socket. |
190 */ | 229 */ |
191 String get remoteHost; | 230 String get remoteHost; |
192 | 231 |
193 /** | 232 /** |
194 * Closes the socket. Calling [close] will never throw an exception | 233 * Closes the socket. Calling [close] will never throw an exception |
195 * and calling it several times is supported. Calling [close] can result in | 234 * and calling it several times is supported. Calling [close] can result in |
196 * a [RawSocketEvent.READ_CLOSED] event. | 235 * a [RawSocketEvent.READ_CLOSED] event. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 /** | 270 /** |
232 * A high-level class for communicating over a TCP socket. The [Socket] exposes | 271 * 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 | 272 * both a [Stream] and a [IOSink] interface, making it ideal for |
234 * using together with other [Stream]s. | 273 * using together with other [Stream]s. |
235 */ | 274 */ |
236 abstract class Socket implements Stream<List<int>>, IOSink { | 275 abstract class Socket implements Stream<List<int>>, IOSink { |
237 /** | 276 /** |
238 * Creats a new socket connection to the host and port and returns a [Future] | 277 * 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 | 278 * that will complete with either a [Socket] once connected or an error |
240 * if the host-lookup or connection failed. | 279 * if the host-lookup or connection failed. |
| 280 * |
| 281 * [host] can either be a [String] or an [InternetAddress]. If [host] is a |
| 282 * [String], [connect] will perform a [InternetAddress.lookup] and use |
| 283 * the first value in the list. |
241 */ | 284 */ |
242 external static Future<Socket> connect(String host, int port); | 285 external static Future<Socket> connect(host, int port); |
243 | 286 |
244 /** | 287 /** |
245 * Destroy the socket in both directions. Calling [destroy] will make the | 288 * 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 | 289 * send a close event on the stream and will no longer react on data being |
247 * piped to it. | 290 * piped to it. |
248 * | 291 * |
249 * Call [close](inherited from [IOSink]) to only close the [Socket] | 292 * Call [close](inherited from [IOSink]) to only close the [Socket] |
250 * for sending data. | 293 * for sending data. |
251 */ | 294 */ |
252 void destroy(); | 295 void destroy(); |
(...skipping 10 matching lines...) Expand all Loading... |
263 * Returns the port used by this socket. | 306 * Returns the port used by this socket. |
264 */ | 307 */ |
265 int get port; | 308 int get port; |
266 | 309 |
267 /** | 310 /** |
268 * Returns the remote port connected to by this socket. | 311 * Returns the remote port connected to by this socket. |
269 */ | 312 */ |
270 int get remotePort; | 313 int get remotePort; |
271 | 314 |
272 /** | 315 /** |
273 * Returns the host used to connect this socket. | 316 * Returns the [InternetAddress] used to connect this socket. |
274 */ | 317 */ |
275 String get host; | 318 InternetAddress get address; |
276 | 319 |
277 /** | 320 /** |
278 * Returns the remote host connected to by this socket. | 321 * Returns the remote host connected to by this socket. |
279 */ | 322 */ |
280 String get remoteHost; | 323 String get remoteHost; |
281 } | 324 } |
282 | 325 |
283 | 326 |
284 class SocketIOException implements Exception { | 327 class SocketIOException implements Exception { |
285 const SocketIOException([String this.message = "", | 328 const SocketIOException([String this.message = "", |
286 OSError this.osError = null]); | 329 OSError this.osError = null]); |
287 String toString() { | 330 String toString() { |
288 StringBuffer sb = new StringBuffer(); | 331 StringBuffer sb = new StringBuffer(); |
289 sb.write("SocketIOException"); | 332 sb.write("SocketIOException"); |
290 if (!message.isEmpty) { | 333 if (!message.isEmpty) { |
291 sb.write(": $message"); | 334 sb.write(": $message"); |
292 if (osError != null) { | 335 if (osError != null) { |
293 sb.write(" ($osError)"); | 336 sb.write(" ($osError)"); |
294 } | 337 } |
295 } else if (osError != null) { | 338 } else if (osError != null) { |
296 sb.write(": $osError"); | 339 sb.write(": $osError"); |
297 } | 340 } |
298 return sb.toString(); | 341 return sb.toString(); |
299 } | 342 } |
300 final String message; | 343 final String message; |
301 final OSError osError; | 344 final OSError osError; |
302 } | 345 } |
OLD | NEW |