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