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 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 bool shared: false}) { | 10 bool shared: false}) { |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 static const int PROTOCOL_IPV4 = 1 << 0; | 273 static const int PROTOCOL_IPV4 = 1 << 0; |
274 static const int PROTOCOL_IPV6 = 1 << 1; | 274 static const int PROTOCOL_IPV6 = 1 << 1; |
275 | 275 |
276 static const int NORMAL_TOKEN_BATCH_SIZE = 8; | 276 static const int NORMAL_TOKEN_BATCH_SIZE = 8; |
277 static const int LISTENING_TOKEN_BATCH_SIZE = 2; | 277 static const int LISTENING_TOKEN_BATCH_SIZE = 2; |
278 | 278 |
279 static const Duration _RETRY_DURATION = const Duration(milliseconds: 250); | 279 static const Duration _RETRY_DURATION = const Duration(milliseconds: 250); |
280 static const Duration _RETRY_DURATION_LOOPBACK = | 280 static const Duration _RETRY_DURATION_LOOPBACK = |
281 const Duration(milliseconds: 25); | 281 const Duration(milliseconds: 25); |
282 | 282 |
283 // Use default Map so we keep order. | |
284 static Map<int, _NativeSocket> _sockets = new Map<int, _NativeSocket>(); | |
285 | |
286 // Socket close state | 283 // Socket close state |
287 bool isClosed = false; | 284 bool isClosed = false; |
288 bool isClosing = false; | 285 bool isClosing = false; |
289 bool isClosedRead = false; | 286 bool isClosedRead = false; |
290 bool closedReadEventSent = false; | 287 bool closedReadEventSent = false; |
291 bool isClosedWrite = false; | 288 bool isClosedWrite = false; |
292 Completer closeCompleter = new Completer.sync(); | 289 Completer closeCompleter = new Completer.sync(); |
293 | 290 |
294 // Handlers and receive port for socket events from the event handler. | 291 // Handlers and receive port for socket events from the event handler. |
295 final List eventHandlers = new List(EVENT_COUNT + 1); | 292 final List eventHandlers = new List(EVENT_COUNT + 1); |
(...skipping 14 matching lines...) Expand all Loading... |
310 int tokens = 0; | 307 int tokens = 0; |
311 | 308 |
312 bool sendReadEvents = false; | 309 bool sendReadEvents = false; |
313 bool readEventIssued = false; | 310 bool readEventIssued = false; |
314 | 311 |
315 bool sendWriteEvents = false; | 312 bool sendWriteEvents = false; |
316 bool writeEventIssued = false; | 313 bool writeEventIssued = false; |
317 bool writeAvailable = false; | 314 bool writeAvailable = false; |
318 | 315 |
319 static final Stopwatch sw = new Stopwatch()..start(); | 316 static final Stopwatch sw = new Stopwatch()..start(); |
320 // Statistics. | 317 |
321 int totalRead = 0; | 318 static bool connectedResourceHandler = false; |
322 int totalWritten = 0; | 319 _ReadWriteResourceInfo resourceInfo; |
323 int readCount = 0; | |
324 int writeCount = 0; | |
325 double lastRead; | |
326 double lastWrite; | |
327 | 320 |
328 // The owner object is the object that the Socket is being used by, e.g. | 321 // The owner object is the object that the Socket is being used by, e.g. |
329 // a HttpServer, a WebSocket connection, a process pipe, etc. | 322 // a HttpServer, a WebSocket connection, a process pipe, etc. |
330 Object owner; | 323 Object owner; |
331 | 324 |
332 static double get timestamp => sw.elapsedMicroseconds / 1000000.0; | 325 static double get timestamp => sw.elapsedMicroseconds / 1000000.0; |
333 | 326 |
334 static Future<List<InternetAddress>> lookup( | 327 static Future<List<InternetAddress>> lookup( |
335 String host, {InternetAddressType type: InternetAddressType.ANY}) { | 328 String host, {InternetAddressType type: InternetAddressType.ANY}) { |
336 return _IOService._dispatch(_SOCKET_LOOKUP, [host, type._value]) | 329 return _IOService._dispatch(_SOCKET_LOOKUP, [host, type._value]) |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 connectNext(); | 427 connectNext(); |
435 } else { | 428 } else { |
436 // Query the local port, for error messages. | 429 // Query the local port, for error messages. |
437 socket.port; | 430 socket.port; |
438 // Set up timer for when we should retry the next address | 431 // Set up timer for when we should retry the next address |
439 // (if any). | 432 // (if any). |
440 var duration = address.isLoopback ? | 433 var duration = address.isLoopback ? |
441 _RETRY_DURATION_LOOPBACK : | 434 _RETRY_DURATION_LOOPBACK : |
442 _RETRY_DURATION; | 435 _RETRY_DURATION; |
443 var timer = new Timer(duration, connectNext); | 436 var timer = new Timer(duration, connectNext); |
| 437 setupResourceInfo(socket); |
| 438 |
444 connecting[socket] = timer; | 439 connecting[socket] = timer; |
445 // Setup handlers for receiving the first write event which | 440 // Setup handlers for receiving the first write event which |
446 // indicate that the socket is fully connected. | 441 // indicate that the socket is fully connected. |
447 socket.setHandlers( | 442 socket.setHandlers( |
448 write: () { | 443 write: () { |
449 timer.cancel(); | 444 timer.cancel(); |
450 socket.setListening(read: false, write: false); | 445 socket.setListening(read: false, write: false); |
451 completer.complete(socket); | 446 completer.complete(socket); |
452 connecting.remove(socket); | 447 connecting.remove(socket); |
453 connecting.forEach((s, t) { | 448 connecting.forEach((s, t) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 .then((list) { | 480 .then((list) { |
486 if (list.length == 0) { | 481 if (list.length == 0) { |
487 throw createError(response, "Failed host lookup: '$host'"); | 482 throw createError(response, "Failed host lookup: '$host'"); |
488 } | 483 } |
489 return list[0]; | 484 return list[0]; |
490 }); | 485 }); |
491 }) | 486 }) |
492 .then((address) { | 487 .then((address) { |
493 var socket = new _NativeSocket.listen(); | 488 var socket = new _NativeSocket.listen(); |
494 socket.localAddress = address; | 489 socket.localAddress = address; |
495 | |
496 var result = socket.nativeCreateBindListen(address._in_addr, | 490 var result = socket.nativeCreateBindListen(address._in_addr, |
497 port, | 491 port, |
498 backlog, | 492 backlog, |
499 v6Only, | 493 v6Only, |
500 shared); | 494 shared); |
501 if (result is OSError) { | 495 if (result is OSError) { |
502 throw new SocketException("Failed to create server socket", | 496 throw new SocketException("Failed to create server socket", |
503 osError: result, | 497 osError: result, |
504 address: address, | 498 address: address, |
505 port: port); | 499 port: port); |
506 } | 500 } |
507 if (port != 0) socket.localPort = port; | 501 if (port != 0) socket.localPort = port; |
| 502 setupResourceInfo(socket); |
508 socket.connectToEventHandler(); | 503 socket.connectToEventHandler(); |
509 return socket; | 504 return socket; |
510 }); | 505 }); |
511 } | 506 } |
512 | 507 |
| 508 static void setupResourceInfo(_NativeSocket socket) { |
| 509 socket.resourceInfo = new _SocketResourceInfo(socket); |
| 510 } |
| 511 |
513 static Future<_NativeSocket> bindDatagram( | 512 static Future<_NativeSocket> bindDatagram( |
514 host, int port, bool reuseAddress) { | 513 host, int port, bool reuseAddress) { |
515 return new Future.value(host) | 514 return new Future.value(host) |
516 .then((host) { | 515 .then((host) { |
517 if (host is _InternetAddress) return host; | 516 if (host is _InternetAddress) return host; |
518 return lookup(host) | 517 return lookup(host) |
519 .then((list) { | 518 .then((list) { |
520 if (list.length == 0) { | 519 if (list.length == 0) { |
521 throw createError(response, "Failed host lookup: '$host'"); | 520 throw createError(response, "Failed host lookup: '$host'"); |
522 } | 521 } |
523 return list[0]; | 522 return list[0]; |
524 }); | 523 }); |
525 }) | 524 }) |
526 .then((address) { | 525 .then((address) { |
527 var socket = new _NativeSocket.datagram(address); | 526 var socket = new _NativeSocket.datagram(address); |
528 var result = socket.nativeCreateBindDatagram( | 527 var result = socket.nativeCreateBindDatagram( |
529 address._in_addr, port, reuseAddress); | 528 address._in_addr, port, reuseAddress); |
530 if (result is OSError) { | 529 if (result is OSError) { |
531 throw new SocketException("Failed to create datagram socket", | 530 throw new SocketException("Failed to create datagram socket", |
532 osError: result, | 531 osError: result, |
533 address: address, | 532 address: address, |
534 port: port); | 533 port: port); |
535 } | 534 } |
536 if (port != 0) socket.localPort = port; | 535 if (port != 0) socket.localPort = port; |
| 536 setupResourceInfo(socket); |
537 return socket; | 537 return socket; |
538 }); | 538 }); |
539 } | 539 } |
540 | 540 |
541 _NativeSocket.datagram(this.localAddress) | 541 _NativeSocket.datagram(this.localAddress) |
542 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_UDP_SOCKET; | 542 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_UDP_SOCKET; |
543 | 543 |
544 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET | TYPE_TCP_SOCKET; | 544 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET | TYPE_TCP_SOCKET; |
545 | 545 |
546 _NativeSocket.listen() : typeFlags = TYPE_LISTENING_SOCKET | TYPE_TCP_SOCKET { | 546 _NativeSocket.listen() : typeFlags = TYPE_LISTENING_SOCKET | TYPE_TCP_SOCKET { |
(...skipping 21 matching lines...) Expand all Loading... |
568 if (isClosing || isClosed) return null; | 568 if (isClosing || isClosed) return null; |
569 len = min(available, len == null ? available : len); | 569 len = min(available, len == null ? available : len); |
570 if (len == 0) return null; | 570 if (len == 0) return null; |
571 var result = nativeRead(len); | 571 var result = nativeRead(len); |
572 if (result is OSError) { | 572 if (result is OSError) { |
573 reportError(result, "Read failed"); | 573 reportError(result, "Read failed"); |
574 return null; | 574 return null; |
575 } | 575 } |
576 if (result != null) { | 576 if (result != null) { |
577 available -= result.length; | 577 available -= result.length; |
578 totalRead += result.length; | 578 // TODO(ricow): Remove when we track internal and pipe uses. |
| 579 assert(resourceInfo != null || isPipe || isInternal); |
| 580 if (resourceInfo != null) { |
| 581 resourceInfo.totalRead += result.length; |
| 582 } |
579 } | 583 } |
580 readCount++; | 584 // TODO(ricow): Remove when we track internal and pipe uses. |
581 lastRead = timestamp; | 585 assert(resourceInfo != null || isPipe || isInternal); |
| 586 if (resourceInfo != null) { |
| 587 resourceInfo.readCount++; |
| 588 resourceInfo.lastRead = timestamp; |
| 589 } |
582 return result; | 590 return result; |
583 } | 591 } |
584 | 592 |
585 Datagram receive() { | 593 Datagram receive() { |
586 if (isClosing || isClosed) return null; | 594 if (isClosing || isClosed) return null; |
587 var result = nativeRecvFrom(); | 595 var result = nativeRecvFrom(); |
588 if (result is OSError) { | 596 if (result is OSError) { |
589 reportError(result, "Receive failed"); | 597 reportError(result, "Receive failed"); |
590 return null; | 598 return null; |
591 } | 599 } |
592 if (result != null) { | 600 if (result != null) { |
593 // Read the next available. Available is only for the next datagram, not | 601 // Read the next available. Available is only for the next datagram, not |
594 // the sum of all datagrams pending, so we need to call after each | 602 // the sum of all datagrams pending, so we need to call after each |
595 // receive. If available becomes > 0, the _NativeSocket will continue to | 603 // receive. If available becomes > 0, the _NativeSocket will continue to |
596 // emit read events. | 604 // emit read events. |
597 available = nativeAvailable(); | 605 available = nativeAvailable(); |
598 totalRead += result.data.length; | 606 // TODO(ricow): Remove when we track internal and pipe uses. |
| 607 assert(resourceInfo != null || isPipe || isInternal); |
| 608 if (resourceInfo != null) { |
| 609 resourceInfo.totalRead += result.data.length; |
| 610 } |
599 } | 611 } |
600 readCount++; | 612 // TODO(ricow): Remove when we track internal and pipe uses. |
601 lastRead = timestamp; | 613 assert(resourceInfo != null || isPipe || isInternal); |
| 614 if (resourceInfo != null) { |
| 615 resourceInfo.readCount++; |
| 616 resourceInfo.lastRead = timestamp; |
| 617 } |
602 return result; | 618 return result; |
603 } | 619 } |
604 | 620 |
605 int write(List<int> buffer, int offset, int bytes) { | 621 int write(List<int> buffer, int offset, int bytes) { |
606 if (buffer is! List) throw new ArgumentError(); | 622 if (buffer is! List) throw new ArgumentError(); |
607 if (offset == null) offset = 0; | 623 if (offset == null) offset = 0; |
608 if (bytes == null) { | 624 if (bytes == null) { |
609 if (offset > buffer.length) { | 625 if (offset > buffer.length) { |
610 throw new RangeError.value(offset); | 626 throw new RangeError.value(offset); |
611 } | 627 } |
(...skipping 18 matching lines...) Expand all Loading... |
630 result = 0; | 646 result = 0; |
631 } | 647 } |
632 // The result may be negative, if we forced a short write for testing | 648 // The result may be negative, if we forced a short write for testing |
633 // purpose. In such case, don't mark writeAvailable as false, as we don't | 649 // purpose. In such case, don't mark writeAvailable as false, as we don't |
634 // know if we'll receive an event. It's better to just retry. | 650 // know if we'll receive an event. It's better to just retry. |
635 if (result >= 0 && result < bytes) { | 651 if (result >= 0 && result < bytes) { |
636 writeAvailable = false; | 652 writeAvailable = false; |
637 } | 653 } |
638 // Negate the result, as stated above. | 654 // Negate the result, as stated above. |
639 if (result < 0) result = -result; | 655 if (result < 0) result = -result; |
640 totalWritten += result; | 656 // TODO(ricow): Remove when we track internal and pipe uses. |
641 writeCount++; | 657 assert(resourceInfo != null || isPipe || isInternal); |
642 lastWrite = timestamp; | 658 if (resourceInfo != null) { |
| 659 resourceInfo.totalWritten += result; |
| 660 resourceInfo.writeCount++; |
| 661 resourceInfo.lastWrite = timestamp; |
| 662 } |
643 return result; | 663 return result; |
644 } | 664 } |
645 | 665 |
646 int send(List<int> buffer, int offset, int bytes, | 666 int send(List<int> buffer, int offset, int bytes, |
647 InternetAddress address, int port) { | 667 InternetAddress address, int port) { |
648 if (isClosing || isClosed) return 0; | 668 if (isClosing || isClosed) return 0; |
649 _BufferAndStart bufferAndStart = | 669 _BufferAndStart bufferAndStart = |
650 _ensureFastAndSerializableByteData( | 670 _ensureFastAndSerializableByteData( |
651 buffer, offset, bytes); | 671 buffer, offset, bytes); |
652 var result = nativeSendTo( | 672 var result = nativeSendTo( |
653 bufferAndStart.buffer, bufferAndStart.start, bytes, | 673 bufferAndStart.buffer, bufferAndStart.start, bytes, |
654 address._in_addr, port); | 674 address._in_addr, port); |
655 if (result is OSError) { | 675 if (result is OSError) { |
656 scheduleMicrotask(() => reportError(result, "Send failed")); | 676 scheduleMicrotask(() => reportError(result, "Send failed")); |
657 result = 0; | 677 result = 0; |
658 } | 678 } |
659 totalWritten += result; | 679 // TODO(ricow): Remove when we track internal and pipe uses. |
660 writeCount++; | 680 assert(resourceInfo != null || isPipe || isInternal); |
661 lastWrite = timestamp; | 681 if (resourceInfo != null) { |
| 682 resourceInfo.totalWritten += result; |
| 683 resourceInfo.writeCount++; |
| 684 resourceInfo.lastWrite = timestamp; |
| 685 } |
662 return result; | 686 return result; |
663 } | 687 } |
664 | 688 |
665 _NativeSocket accept() { | 689 _NativeSocket accept() { |
666 // Don't issue accept if we're closing. | 690 // Don't issue accept if we're closing. |
667 if (isClosing || isClosed) return null; | 691 if (isClosing || isClosed) return null; |
668 assert(available > 0); | 692 assert(available > 0); |
669 available--; | 693 available--; |
670 tokens++; | 694 tokens++; |
671 returnTokens(LISTENING_TOKEN_BATCH_SIZE); | 695 returnTokens(LISTENING_TOKEN_BATCH_SIZE); |
672 var socket = new _NativeSocket.normal(); | 696 var socket = new _NativeSocket.normal(); |
673 if (nativeAccept(socket) != true) return null; | 697 if (nativeAccept(socket) != true) return null; |
674 socket.localPort = localPort; | 698 socket.localPort = localPort; |
675 socket.localAddress = address; | 699 socket.localAddress = address; |
676 totalRead += 1; | 700 setupResourceInfo(socket); |
677 lastRead = timestamp; | 701 // TODO(ricow): Remove when we track internal and pipe uses. |
| 702 assert(resourceInfo != null || isPipe || isInternal); |
| 703 if (resourceInfo != null) { |
| 704 resourceInfo.totalRead += 1; |
| 705 resourceInfo.lastRead = timestamp; |
| 706 } |
678 return socket; | 707 return socket; |
679 } | 708 } |
680 | 709 |
681 int get port { | 710 int get port { |
682 if (localPort != 0) return localPort; | 711 if (localPort != 0) return localPort; |
683 if (isClosing || isClosed) throw const SocketException.closed(); | 712 if (isClosing || isClosed) throw const SocketException.closed(); |
684 var result = nativeGetPort(); | 713 var result = nativeGetPort(); |
685 if (result is OSError) throw result; | 714 if (result is OSError) throw result; |
686 return localPort = result; | 715 return localPort = result; |
687 } | 716 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 available = nativeAvailable(); | 809 available = nativeAvailable(); |
781 issueReadEvent(); | 810 issueReadEvent(); |
782 continue; | 811 continue; |
783 } | 812 } |
784 } | 813 } |
785 | 814 |
786 var handler = eventHandlers[i]; | 815 var handler = eventHandlers[i]; |
787 if (i == DESTROYED_EVENT) { | 816 if (i == DESTROYED_EVENT) { |
788 assert(isClosing); | 817 assert(isClosing); |
789 assert(!isClosed); | 818 assert(!isClosed); |
| 819 // TODO(ricow): Remove/update when we track internal and pipe uses. |
| 820 assert(resourceInfo != null || isPipe || isInternal); |
| 821 if (resourceInfo != null) { |
| 822 _SocketResourceInfo.SocketClosed(resourceInfo); |
| 823 } |
790 isClosed = true; | 824 isClosed = true; |
791 closeCompleter.complete(); | 825 closeCompleter.complete(); |
792 disconnectFromEventHandler(); | 826 disconnectFromEventHandler(); |
793 if (handler != null) handler(); | 827 if (handler != null) handler(); |
794 continue; | 828 continue; |
795 } | 829 } |
796 | 830 |
797 if (i == ERROR_EVENT) { | 831 if (i == ERROR_EVENT) { |
798 if (!isClosing) { | 832 if (!isClosing) { |
799 reportError(nativeGetError(), ""); | 833 reportError(nativeGetError(), ""); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
897 int fullData = (typeFlags & TYPE_TYPE_MASK) | data; | 931 int fullData = (typeFlags & TYPE_TYPE_MASK) | data; |
898 assert(!isClosing); | 932 assert(!isClosing); |
899 connectToEventHandler(); | 933 connectToEventHandler(); |
900 _EventHandler._sendData(this, eventPort.sendPort, fullData); | 934 _EventHandler._sendData(this, eventPort.sendPort, fullData); |
901 } | 935 } |
902 | 936 |
903 void connectToEventHandler() { | 937 void connectToEventHandler() { |
904 assert(!isClosed); | 938 assert(!isClosed); |
905 if (eventPort == null) { | 939 if (eventPort == null) { |
906 eventPort = new RawReceivePort(multiplex); | 940 eventPort = new RawReceivePort(multiplex); |
907 _sockets[_serviceId] = this; | 941 } |
| 942 if (!connectedResourceHandler) { |
| 943 registerExtension('__getOpenSockets', |
| 944 _SocketResourceInfo.getOpenSockets); |
| 945 registerExtension('__getSocketByID', |
| 946 _SocketResourceInfo.getSocketInfoMapByID); |
| 947 |
| 948 connectedResourceHandler = true; |
908 } | 949 } |
909 } | 950 } |
910 | 951 |
911 void disconnectFromEventHandler() { | 952 void disconnectFromEventHandler() { |
912 assert(eventPort != null); | 953 assert(eventPort != null); |
913 eventPort.close(); | 954 eventPort.close(); |
914 eventPort = null; | 955 eventPort = null; |
915 _sockets.remove(_serviceId); | |
916 // Now that we don't track this Socket anymore, we can clear the owner | 956 // Now that we don't track this Socket anymore, we can clear the owner |
917 // field. | 957 // field. |
918 owner = null; | 958 owner = null; |
919 } | 959 } |
920 | 960 |
921 // Check whether this is an error response from a native port call. | 961 // Check whether this is an error response from a native port call. |
922 static bool isErrorResponse(response) { | 962 static bool isErrorResponse(response) { |
923 return response is List && response[0] != _SUCCESS_RESPONSE; | 963 return response is List && response[0] != _SUCCESS_RESPONSE; |
924 } | 964 } |
925 | 965 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1010 void leaveMulticast(InternetAddress addr, NetworkInterface interface) { | 1050 void leaveMulticast(InternetAddress addr, NetworkInterface interface) { |
1011 var interfaceAddr = multicastAddress(addr, interface); | 1051 var interfaceAddr = multicastAddress(addr, interface); |
1012 var interfaceIndex = interface == null ? 0 : interface.index; | 1052 var interfaceIndex = interface == null ? 0 : interface.index; |
1013 var result = nativeLeaveMulticast( | 1053 var result = nativeLeaveMulticast( |
1014 addr._in_addr, | 1054 addr._in_addr, |
1015 interfaceAddr == null ? null : interfaceAddr._in_addr, | 1055 interfaceAddr == null ? null : interfaceAddr._in_addr, |
1016 interfaceIndex); | 1056 interfaceIndex); |
1017 if (result is OSError) throw result; | 1057 if (result is OSError) throw result; |
1018 } | 1058 } |
1019 | 1059 |
1020 String get _serviceTypePath => 'io/sockets'; | |
1021 String get _serviceTypeName => 'Socket'; | |
1022 | |
1023 String _JSONKind() { | |
1024 return isListening ? "Listening" : | |
1025 isPipe ? "Pipe" : | |
1026 isInternal ? "Internal" : "Normal"; | |
1027 } | |
1028 | |
1029 Map _toJSONPipe(bool ref) { | |
1030 var name = 'Anonymous Pipe'; | |
1031 var r = { | |
1032 'id': _servicePath, | |
1033 'type': _serviceType(ref), | |
1034 'name': name, | |
1035 'user_name': name, | |
1036 'kind': _JSONKind(), | |
1037 }; | |
1038 if (ref) { | |
1039 return r; | |
1040 } | |
1041 r['readClosed'] = isClosedRead; | |
1042 r['writeClosed'] = isClosedWrite; | |
1043 r['closing'] = isClosing; | |
1044 r['fd'] = nativeGetSocketId(); | |
1045 if (owner != null) { | |
1046 r['owner'] = owner._toJSON(true); | |
1047 } | |
1048 return r; | |
1049 } | |
1050 | |
1051 Map _toJSONInternal(bool ref) { | |
1052 var name = 'Internal'; | |
1053 var r = { | |
1054 'id': _servicePath, | |
1055 'type': _serviceType(ref), | |
1056 'name': name, | |
1057 'user_name': name, | |
1058 'kind': _JSONKind(), | |
1059 }; | |
1060 if (ref) { | |
1061 return r; | |
1062 } | |
1063 r['closing'] = isClosing; | |
1064 r['fd'] = nativeGetSocketId(); | |
1065 if (owner != null) { | |
1066 r['owner'] = owner._toJSON(true); | |
1067 } | |
1068 return r; | |
1069 } | |
1070 | |
1071 Map _toJSONNetwork(bool ref) { | |
1072 var name = '${address.host}:$port'; | |
1073 if (isTcp && !isListening) name += " <-> ${remoteAddress.host}:$remotePort"; | |
1074 var r = { | |
1075 'id': _servicePath, | |
1076 'type': _serviceType(ref), | |
1077 'name': name, | |
1078 'user_name': name, | |
1079 'kind': _JSONKind(), | |
1080 }; | |
1081 if (ref) { | |
1082 return r; | |
1083 } | |
1084 var protocol = isTcp ? "TCP" : isUdp ? "UDP" : null; | |
1085 var localAddress; | |
1086 var localPort; | |
1087 var rAddress; | |
1088 var rPort; | |
1089 try { | |
1090 localAddress = address.address; | |
1091 } catch (e) { } | |
1092 try { | |
1093 localPort = port; | |
1094 } catch (e) { } | |
1095 try { | |
1096 rAddress = this.remoteAddress.address; | |
1097 } catch (e) { } | |
1098 try { | |
1099 rPort = remotePort; | |
1100 } catch (e) { } | |
1101 r['localAddress'] = localAddress; | |
1102 r['localPort'] = localPort; | |
1103 r['remoteAddress'] = rAddress; | |
1104 r['remotePort'] = rPort; | |
1105 r['protocol'] = protocol; | |
1106 r['readClosed'] = isClosedRead; | |
1107 r['writeClosed'] = isClosedWrite; | |
1108 r['closing'] = isClosing; | |
1109 r['listening'] = isListening; | |
1110 r['fd'] = nativeGetSocketId(); | |
1111 if (owner != null) { | |
1112 r['owner'] = owner._toJSON(true); | |
1113 } | |
1114 return r; | |
1115 } | |
1116 | |
1117 Map _toJSON(bool ref) { | |
1118 var map; | |
1119 if (isPipe) { | |
1120 map = _toJSONPipe(ref); | |
1121 } else if (isInternal) { | |
1122 map = _toJSONInternal(ref); | |
1123 } else { | |
1124 map = _toJSONNetwork(ref); | |
1125 } | |
1126 if (!ref) { | |
1127 map['available'] = available; | |
1128 map['totalRead'] = totalRead; | |
1129 map['totalWritten'] = totalWritten; | |
1130 map['readCount'] = totalWritten; | |
1131 map['writeCount'] = writeCount; | |
1132 map['lastRead'] = lastRead; | |
1133 map['lastWrite'] = lastWrite; | |
1134 } | |
1135 return map; | |
1136 } | |
1137 | 1060 |
1138 void nativeSetSocketId(int id) native "Socket_SetSocketId"; | 1061 void nativeSetSocketId(int id) native "Socket_SetSocketId"; |
1139 nativeAvailable() native "Socket_Available"; | 1062 nativeAvailable() native "Socket_Available"; |
1140 nativeRead(int len) native "Socket_Read"; | 1063 nativeRead(int len) native "Socket_Read"; |
1141 nativeRecvFrom() native "Socket_RecvFrom"; | 1064 nativeRecvFrom() native "Socket_RecvFrom"; |
1142 nativeWrite(List<int> buffer, int offset, int bytes) | 1065 nativeWrite(List<int> buffer, int offset, int bytes) |
1143 native "Socket_WriteList"; | 1066 native "Socket_WriteList"; |
1144 nativeSendTo(List<int> buffer, int offset, int bytes, | 1067 nativeSendTo(List<int> buffer, int offset, int bytes, |
1145 List<int> address, int port) | 1068 List<int> address, int port) |
1146 native "Socket_SendTo"; | 1069 native "Socket_SendTo"; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1279 _referencePort.listen((sendPort) { | 1202 _referencePort.listen((sendPort) { |
1280 sendPort.send( | 1203 sendPort.send( |
1281 [_socket.address, | 1204 [_socket.address, |
1282 _socket.port, | 1205 _socket.port, |
1283 _v6Only]); | 1206 _v6Only]); |
1284 }); | 1207 }); |
1285 } | 1208 } |
1286 return new _RawServerSocketReference(_referencePort.sendPort); | 1209 return new _RawServerSocketReference(_referencePort.sendPort); |
1287 } | 1210 } |
1288 | 1211 |
1289 Map _toJSON(bool ref) => _socket._toJSON(ref); | |
1290 | |
1291 void set _owner(owner) { _socket.owner = owner; } | 1212 void set _owner(owner) { _socket.owner = owner; } |
1292 } | 1213 } |
1293 | 1214 |
1294 | 1215 |
1295 class _RawServerSocketReference implements RawServerSocketReference { | 1216 class _RawServerSocketReference implements RawServerSocketReference { |
1296 final SendPort _sendPort; | 1217 final SendPort _sendPort; |
1297 | 1218 |
1298 _RawServerSocketReference(this._sendPort); | 1219 _RawServerSocketReference(this._sendPort); |
1299 | 1220 |
1300 Future<RawServerSocket> create() { | 1221 Future<RawServerSocket> create() { |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1458 } | 1379 } |
1459 | 1380 |
1460 void _onSubscriptionStateChange() { | 1381 void _onSubscriptionStateChange() { |
1461 if (_controller.hasListener) { | 1382 if (_controller.hasListener) { |
1462 _resume(); | 1383 _resume(); |
1463 } else { | 1384 } else { |
1464 _socket.close(); | 1385 _socket.close(); |
1465 } | 1386 } |
1466 } | 1387 } |
1467 | 1388 |
1468 Map _toJSON(bool ref) => _socket._toJSON(ref); | |
1469 void set _owner(owner) { _socket.owner = owner; } | 1389 void set _owner(owner) { _socket.owner = owner; } |
1470 } | 1390 } |
1471 | 1391 |
1472 | 1392 |
1473 patch class ServerSocket { | 1393 patch class ServerSocket { |
1474 /* patch */ static Future<ServerSocket> bind(address, | 1394 /* patch */ static Future<ServerSocket> bind(address, |
1475 int port, | 1395 int port, |
1476 {int backlog: 0, | 1396 {int backlog: 0, |
1477 bool v6Only: false, | 1397 bool v6Only: false, |
1478 bool shared: false}) { | 1398 bool shared: false}) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1521 int get port => _socket.port; | 1441 int get port => _socket.port; |
1522 | 1442 |
1523 InternetAddress get address => _socket.address; | 1443 InternetAddress get address => _socket.address; |
1524 | 1444 |
1525 Future close() => _socket.close().then((_) => this); | 1445 Future close() => _socket.close().then((_) => this); |
1526 | 1446 |
1527 ServerSocketReference get reference { | 1447 ServerSocketReference get reference { |
1528 return new _ServerSocketReference(_socket.reference); | 1448 return new _ServerSocketReference(_socket.reference); |
1529 } | 1449 } |
1530 | 1450 |
1531 Map _toJSON(bool ref) => _socket._toJSON(ref); | |
1532 | |
1533 void set _owner(owner) { _socket._owner = owner; } | 1451 void set _owner(owner) { _socket._owner = owner; } |
1534 } | 1452 } |
1535 | 1453 |
1536 | 1454 |
1537 patch class Socket { | 1455 patch class Socket { |
1538 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { | 1456 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { |
1539 return RawSocket.connect(host, port, sourceAddress: sourceAddress).then( | 1457 return RawSocket.connect(host, port, sourceAddress: sourceAddress).then( |
1540 (socket) => new _Socket(socket)); | 1458 (socket) => new _Socket(socket)); |
1541 } | 1459 } |
1542 } | 1460 } |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1836 if (_detachReady != null) { | 1754 if (_detachReady != null) { |
1837 _detachReady.complete(null); | 1755 _detachReady.complete(null); |
1838 } else { | 1756 } else { |
1839 if (_raw != null) { | 1757 if (_raw != null) { |
1840 _raw.shutdown(SocketDirection.SEND); | 1758 _raw.shutdown(SocketDirection.SEND); |
1841 _disableWriteEvent(); | 1759 _disableWriteEvent(); |
1842 } | 1760 } |
1843 } | 1761 } |
1844 } | 1762 } |
1845 | 1763 |
1846 Map _toJSON(bool ref) => _raw._toJSON(ref); | |
1847 void set _owner(owner) { _raw._owner = owner; } | 1764 void set _owner(owner) { _raw._owner = owner; } |
1848 } | 1765 } |
1849 | 1766 |
1850 | 1767 |
1851 patch class RawDatagramSocket { | 1768 patch class RawDatagramSocket { |
1852 /* patch */ static Future<RawDatagramSocket> bind( | 1769 /* patch */ static Future<RawDatagramSocket> bind( |
1853 host, int port, {bool reuseAddress: true}) { | 1770 host, int port, {bool reuseAddress: true}) { |
1854 return _RawDatagramSocket.bind(host, port, reuseAddress); | 1771 return _RawDatagramSocket.bind(host, port, reuseAddress); |
1855 } | 1772 } |
1856 } | 1773 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1992 Datagram _makeDatagram(List<int> data, | 1909 Datagram _makeDatagram(List<int> data, |
1993 String address, | 1910 String address, |
1994 List<int> in_addr, | 1911 List<int> in_addr, |
1995 int port) { | 1912 int port) { |
1996 return new Datagram( | 1913 return new Datagram( |
1997 data, | 1914 data, |
1998 new _InternetAddress(address, null, in_addr), | 1915 new _InternetAddress(address, null, in_addr), |
1999 port); | 1916 port); |
2000 } | 1917 } |
2001 | 1918 |
2002 String _socketsStats() => _SocketsObservatory.toJSON(); | |
OLD | NEW |