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

Side by Side Diff: runtime/bin/socket_patch.dart

Issue 18154005: Add 'address' and 'port' to SocketException, and include them in toString(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/io/socket.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 patch class RawServerSocket { 5 patch class RawServerSocket {
6 /* patch */ static Future<RawServerSocket> bind(address, 6 /* patch */ static Future<RawServerSocket> bind(address,
7 int port, 7 int port,
8 {int backlog: 0, 8 {int backlog: 0,
9 bool v6Only: false}) { 9 bool v6Only: false}) {
10 return _RawServerSocket.bind(address, port, backlog, v6Only); 10 return _RawServerSocket.bind(address, port, backlog, v6Only);
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 220
221 // Native port for socket services. 221 // Native port for socket services.
222 static SendPort socketService; 222 static SendPort socketService;
223 223
224 static Future<List<InternetAddress>> lookup( 224 static Future<List<InternetAddress>> lookup(
225 String host, {InternetAddressType type: InternetAddressType.ANY}) { 225 String host, {InternetAddressType type: InternetAddressType.ANY}) {
226 ensureSocketService(); 226 ensureSocketService();
227 return socketService.call([HOST_NAME_LOOKUP, host, type._value]) 227 return socketService.call([HOST_NAME_LOOKUP, host, type._value])
228 .then((response) { 228 .then((response) {
229 if (isErrorResponse(response)) { 229 if (isErrorResponse(response)) {
230 throw createError(response, "Failed host name lookup"); 230 throw createError(response, "Failed host lookup: '$host'");
231 } else { 231 } else {
232 return response.skip(1).map((result) { 232 return response.skip(1).map((result) {
233 var type = new InternetAddressType._from(result[0]); 233 var type = new InternetAddressType._from(result[0]);
234 return new _InternetAddress(type, result[1], host, result[2]); 234 return new _InternetAddress(type, result[1], host, result[2]);
235 }).toList(); 235 }).toList();
236 } 236 }
237 }); 237 });
238 } 238 }
239 239
240 static Future<InternetAddress> reverseLookup(InternetAddress addr) { 240 static Future<InternetAddress> reverseLookup(InternetAddress addr) {
241 ensureSocketService(); 241 ensureSocketService();
242 return socketService.call([REVERSE_LOOKUP, addr._sockaddr_storage]) 242 return socketService.call([REVERSE_LOOKUP, addr._sockaddr_storage])
243 .then((response) { 243 .then((response) {
244 if (isErrorResponse(response)) { 244 if (isErrorResponse(response)) {
245 throw createError(response, "Failed host name lookup"); 245 throw createError(response, "Failed reverse host lookup", addr);
246 } else { 246 } else {
247 return addr._cloneWithNewHost(response); 247 return addr._cloneWithNewHost(response);
248 } 248 }
249 }); 249 });
250 } 250 }
251 251
252 static Future<List<NetworkInterface>> listInterfaces({ 252 static Future<List<NetworkInterface>> listInterfaces({
253 bool includeLoopback: false, 253 bool includeLoopback: false,
254 bool includeLinkLocal: false, 254 bool includeLinkLocal: false,
255 InternetAddressType type: InternetAddressType.ANY}) { 255 InternetAddressType type: InternetAddressType.ANY}) {
(...skipping 24 matching lines...) Expand all
280 }); 280 });
281 } 281 }
282 282
283 static Future<_NativeSocket> connect(host, int port) { 283 static Future<_NativeSocket> connect(host, int port) {
284 return new Future.value(host) 284 return new Future.value(host)
285 .then((host) { 285 .then((host) {
286 if (host is _InternetAddress) return host; 286 if (host is _InternetAddress) return host;
287 return lookup(host) 287 return lookup(host)
288 .then((list) { 288 .then((list) {
289 if (list.length == 0) { 289 if (list.length == 0) {
290 throw createError(response, "Failed host name lookup"); 290 throw createError(response, "Failed host lookup: '$host'");
291 } 291 }
292 return list[0]; 292 return list[0];
293 }); 293 });
294 }) 294 })
295 .then((address) { 295 .then((address) {
296 ensureSocketService(); 296 ensureSocketService();
297 var socket = new _NativeSocket.normal(); 297 var socket = new _NativeSocket.normal();
298 socket.address = address; 298 socket.address = address;
299 var result = socket.nativeCreateConnect( 299 var result = socket.nativeCreateConnect(
300 address._sockaddr_storage, port); 300 address._sockaddr_storage, port);
301 if (result is OSError) { 301 if (result is OSError) {
302 throw createError(result, "Connection failed"); 302 throw createError(result, "Connection failed", address, port);
303 } else { 303 } else {
304 var completer = new Completer(); 304 var completer = new Completer();
305 // Setup handlers for receiving the first write event which 305 // Setup handlers for receiving the first write event which
306 // indicate that the socket is fully connected. 306 // indicate that the socket is fully connected.
307 socket.setHandlers( 307 socket.setHandlers(
308 write: () { 308 write: () {
309 socket.setListening(read: false, write: false); 309 socket.setListening(read: false, write: false);
310 completer.complete(socket); 310 completer.complete(socket);
311 }, 311 },
312 error: (e) { 312 error: (e) {
313 socket.close(); 313 socket.close();
314 completer.completeError(createError(e, "Connection failed")); 314 completer.completeError(
315 createError(e, "Connection failed", address, port));
315 } 316 }
316 ); 317 );
317 socket.setListening(read: false, write: true); 318 socket.setListening(read: false, write: true);
318 return completer.future; 319 return completer.future;
319 } 320 }
320 }); 321 });
321 } 322 }
322 323
323 static Future<_NativeSocket> bind(host, 324 static Future<_NativeSocket> bind(host,
324 int port, 325 int port,
325 int backlog, 326 int backlog,
326 bool v6Only) { 327 bool v6Only) {
327 return new Future.value(host) 328 return new Future.value(host)
328 .then((host) { 329 .then((host) {
329 if (host is _InternetAddress) return host; 330 if (host is _InternetAddress) return host;
330 return lookup(host) 331 return lookup(host)
331 .then((list) { 332 .then((list) {
332 if (list.length == 0) { 333 if (list.length == 0) {
333 throw createError(response, "Failed host name lookup"); 334 throw createError(response, "Failed host lookup: '$host'");
334 } 335 }
335 return list[0]; 336 return list[0];
336 }); 337 });
337 }) 338 })
338 .then((address) { 339 .then((address) {
339 var socket = new _NativeSocket.listen(); 340 var socket = new _NativeSocket.listen();
340 socket.address = address; 341 socket.address = address;
341 var result = socket.nativeCreateBindListen(address._sockaddr_storage, 342 var result = socket.nativeCreateBindListen(address._sockaddr_storage,
342 port, 343 port,
343 backlog, 344 backlog,
344 v6Only); 345 v6Only);
345 if (result is OSError) { 346 if (result is OSError) {
346 throw new SocketException( 347 throw new SocketException("Failed to create server socket",
347 "Failed to create server socket", result); 348 osError: result,
349 address: address,
350 port: port);
348 } 351 }
349 if (port != 0) socket.localPort = port; 352 if (port != 0) socket.localPort = port;
350 return socket; 353 return socket;
351 }); 354 });
352 } 355 }
353 356
354 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET { 357 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET {
355 eventHandlers = new List(EVENT_COUNT + 1); 358 eventHandlers = new List(EVENT_COUNT + 1);
356 _EventHandler._start(); 359 _EventHandler._start();
357 } 360 }
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 } 595 }
593 } 596 }
594 597
595 // Check whether this is an error response from a native port call. 598 // Check whether this is an error response from a native port call.
596 static bool isErrorResponse(response) { 599 static bool isErrorResponse(response) {
597 return response is List && response[0] != _SUCCESS_RESPONSE; 600 return response is List && response[0] != _SUCCESS_RESPONSE;
598 } 601 }
599 602
600 // Create the appropriate error/exception from different returned 603 // Create the appropriate error/exception from different returned
601 // error objects. 604 // error objects.
602 static createError(error, String message) { 605 static createError(error,
606 String message,
607 [InternetAddress address,
608 int port]) {
603 if (error is OSError) { 609 if (error is OSError) {
604 return new SocketException(message, error); 610 return new SocketException(
611 message, osError: error, address: address, port: port);
605 } else if (error is List) { 612 } else if (error is List) {
606 assert(isErrorResponse(error)); 613 assert(isErrorResponse(error));
607 switch (error[0]) { 614 switch (error[0]) {
608 case _ILLEGAL_ARGUMENT_RESPONSE: 615 case _ILLEGAL_ARGUMENT_RESPONSE:
609 return new ArgumentError(); 616 return new ArgumentError();
610 case _OSERROR_RESPONSE: 617 case _OSERROR_RESPONSE:
611 return new SocketException( 618 return new SocketException(message,
612 message, new OSError(error[2], error[1])); 619 osError: new OSError(error[2], error[1]),
620 address: address,
621 port: port);
613 default: 622 default:
614 return new Exception("Unknown error"); 623 return new Exception("Unknown error");
615 } 624 }
616 } else { 625 } else {
617 return new SocketException(message); 626 return new SocketException(message, address: address, port: port);
618 } 627 }
619 } 628 }
620 629
621 void reportError(error, String message) { 630 void reportError(error, String message) {
622 var e = createError(error, message); 631 var e = createError(error, message, address, localPort);
623 // Invoke the error handler if any. 632 // Invoke the error handler if any.
624 if (eventHandlers[ERROR_EVENT] != null) { 633 if (eventHandlers[ERROR_EVENT] != null) {
625 eventHandlers[ERROR_EVENT](e); 634 eventHandlers[ERROR_EVENT](e);
626 } 635 }
627 // For all errors we close the socket 636 // For all errors we close the socket
628 close(); 637 close();
629 } 638 }
630 639
631 bool setOption(SocketOption option, bool enabled) { 640 bool setOption(SocketOption option, bool enabled) {
632 if (option is! SocketOption) throw new ArgumentError(options); 641 if (option is! SocketOption) throw new ArgumentError(options);
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
1197 if (_detachReady != null) { 1206 if (_detachReady != null) {
1198 _detachReady.complete(null); 1207 _detachReady.complete(null);
1199 } else { 1208 } else {
1200 if (_raw != null) { 1209 if (_raw != null) {
1201 _raw.shutdown(SocketDirection.SEND); 1210 _raw.shutdown(SocketDirection.SEND);
1202 _disableWriteEvent(); 1211 _disableWriteEvent();
1203 } 1212 }
1204 } 1213 }
1205 } 1214 }
1206 } 1215 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/io/socket.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698