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); | |
Søren Gjesse
2015/09/02 09:07:49
Shouldn't this move down to just after timer.cance
ricow1
2015/09/02 10:45:42
If I do that, and there is an error, I do a 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 if (!isInternal && !isPipe) { |
ricow1
2015/09/01 12:06:24
These "if (!isInternal && !isPipe) {" will go away
Søren Gjesse
2015/09/02 09:07:49
Please add a TODO then.
Maybe check if resourceIn
ricow1
2015/09/02 10:45:42
So the reason I did not want to do that is that I
Søren Gjesse
2015/09/02 12:24:49
I like the more verbose version with the assert.
ricow1
2015/09/02 12:45:50
Done (instead I put the assert before)
| |
579 resourceInfo.totalRead += result.length; | |
580 } | |
579 } | 581 } |
580 readCount++; | 582 if (!isInternal && !isPipe) { |
581 lastRead = timestamp; | 583 resourceInfo.readCount++; |
584 resourceInfo.lastRead = timestamp; | |
585 } | |
582 return result; | 586 return result; |
583 } | 587 } |
584 | 588 |
585 Datagram receive() { | 589 Datagram receive() { |
586 if (isClosing || isClosed) return null; | 590 if (isClosing || isClosed) return null; |
587 var result = nativeRecvFrom(); | 591 var result = nativeRecvFrom(); |
588 if (result is OSError) { | 592 if (result is OSError) { |
589 reportError(result, "Receive failed"); | 593 reportError(result, "Receive failed"); |
590 return null; | 594 return null; |
591 } | 595 } |
592 if (result != null) { | 596 if (result != null) { |
593 // Read the next available. Available is only for the next datagram, not | 597 // 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 | 598 // the sum of all datagrams pending, so we need to call after each |
595 // receive. If available becomes > 0, the _NativeSocket will continue to | 599 // receive. If available becomes > 0, the _NativeSocket will continue to |
596 // emit read events. | 600 // emit read events. |
597 available = nativeAvailable(); | 601 available = nativeAvailable(); |
598 totalRead += result.data.length; | 602 if (!isInternal && !isPipe) { |
603 resourceInfo.totalRead += result.data.length; | |
604 } | |
599 } | 605 } |
600 readCount++; | 606 if (!isInternal && !isPipe) { |
601 lastRead = timestamp; | 607 resourceInfo.readCount++; |
608 resourceInfo.lastRead = timestamp; | |
609 } | |
602 return result; | 610 return result; |
603 } | 611 } |
604 | 612 |
605 int write(List<int> buffer, int offset, int bytes) { | 613 int write(List<int> buffer, int offset, int bytes) { |
606 if (buffer is! List) throw new ArgumentError(); | 614 if (buffer is! List) throw new ArgumentError(); |
607 if (offset == null) offset = 0; | 615 if (offset == null) offset = 0; |
608 if (bytes == null) { | 616 if (bytes == null) { |
609 if (offset > buffer.length) { | 617 if (offset > buffer.length) { |
610 throw new RangeError.value(offset); | 618 throw new RangeError.value(offset); |
611 } | 619 } |
(...skipping 18 matching lines...) Expand all Loading... | |
630 result = 0; | 638 result = 0; |
631 } | 639 } |
632 // The result may be negative, if we forced a short write for testing | 640 // 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 | 641 // 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. | 642 // know if we'll receive an event. It's better to just retry. |
635 if (result >= 0 && result < bytes) { | 643 if (result >= 0 && result < bytes) { |
636 writeAvailable = false; | 644 writeAvailable = false; |
637 } | 645 } |
638 // Negate the result, as stated above. | 646 // Negate the result, as stated above. |
639 if (result < 0) result = -result; | 647 if (result < 0) result = -result; |
640 totalWritten += result; | 648 if (!isInternal && !isPipe) { |
641 writeCount++; | 649 resourceInfo.totalWritten += result; |
642 lastWrite = timestamp; | 650 resourceInfo.writeCount++; |
651 resourceInfo.lastWrite = timestamp; | |
652 } | |
643 return result; | 653 return result; |
644 } | 654 } |
645 | 655 |
646 int send(List<int> buffer, int offset, int bytes, | 656 int send(List<int> buffer, int offset, int bytes, |
647 InternetAddress address, int port) { | 657 InternetAddress address, int port) { |
648 if (isClosing || isClosed) return 0; | 658 if (isClosing || isClosed) return 0; |
649 _BufferAndStart bufferAndStart = | 659 _BufferAndStart bufferAndStart = |
650 _ensureFastAndSerializableByteData( | 660 _ensureFastAndSerializableByteData( |
651 buffer, offset, bytes); | 661 buffer, offset, bytes); |
652 var result = nativeSendTo( | 662 var result = nativeSendTo( |
653 bufferAndStart.buffer, bufferAndStart.start, bytes, | 663 bufferAndStart.buffer, bufferAndStart.start, bytes, |
654 address._in_addr, port); | 664 address._in_addr, port); |
655 if (result is OSError) { | 665 if (result is OSError) { |
656 scheduleMicrotask(() => reportError(result, "Send failed")); | 666 scheduleMicrotask(() => reportError(result, "Send failed")); |
657 result = 0; | 667 result = 0; |
658 } | 668 } |
659 totalWritten += result; | 669 if (!isInternal && !isPipe) { |
660 writeCount++; | 670 resourceInfo.totalWritten += result; |
661 lastWrite = timestamp; | 671 resourceInfo.writeCount++; |
672 resourceInfo.lastWrite = timestamp; | |
673 } | |
662 return result; | 674 return result; |
663 } | 675 } |
664 | 676 |
665 _NativeSocket accept() { | 677 _NativeSocket accept() { |
666 // Don't issue accept if we're closing. | 678 // Don't issue accept if we're closing. |
667 if (isClosing || isClosed) return null; | 679 if (isClosing || isClosed) return null; |
668 assert(available > 0); | 680 assert(available > 0); |
669 available--; | 681 available--; |
670 tokens++; | 682 tokens++; |
671 returnTokens(LISTENING_TOKEN_BATCH_SIZE); | 683 returnTokens(LISTENING_TOKEN_BATCH_SIZE); |
672 var socket = new _NativeSocket.normal(); | 684 var socket = new _NativeSocket.normal(); |
673 if (nativeAccept(socket) != true) return null; | 685 if (nativeAccept(socket) != true) return null; |
674 socket.localPort = localPort; | 686 socket.localPort = localPort; |
675 socket.localAddress = address; | 687 socket.localAddress = address; |
676 totalRead += 1; | 688 setupResourceInfo(socket); |
677 lastRead = timestamp; | 689 if (!isInternal && !isPipe) { |
690 resourceInfo.totalRead += 1; | |
691 resourceInfo.lastRead = timestamp; | |
692 } | |
678 return socket; | 693 return socket; |
679 } | 694 } |
680 | 695 |
681 int get port { | 696 int get port { |
682 if (localPort != 0) return localPort; | 697 if (localPort != 0) return localPort; |
683 if (isClosing || isClosed) throw const SocketException.closed(); | 698 if (isClosing || isClosed) throw const SocketException.closed(); |
684 var result = nativeGetPort(); | 699 var result = nativeGetPort(); |
685 if (result is OSError) throw result; | 700 if (result is OSError) throw result; |
686 return localPort = result; | 701 return localPort = result; |
687 } | 702 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
780 available = nativeAvailable(); | 795 available = nativeAvailable(); |
781 issueReadEvent(); | 796 issueReadEvent(); |
782 continue; | 797 continue; |
783 } | 798 } |
784 } | 799 } |
785 | 800 |
786 var handler = eventHandlers[i]; | 801 var handler = eventHandlers[i]; |
787 if (i == DESTROYED_EVENT) { | 802 if (i == DESTROYED_EVENT) { |
788 assert(isClosing); | 803 assert(isClosing); |
789 assert(!isClosed); | 804 assert(!isClosed); |
805 if (!isPipe && !isInternal) { | |
806 _SocketResourceInfo.SocketClosed(resourceInfo); | |
807 } | |
790 isClosed = true; | 808 isClosed = true; |
791 closeCompleter.complete(); | 809 closeCompleter.complete(); |
792 disconnectFromEventHandler(); | 810 disconnectFromEventHandler(); |
793 if (handler != null) handler(); | 811 if (handler != null) handler(); |
794 continue; | 812 continue; |
795 } | 813 } |
796 | 814 |
797 if (i == ERROR_EVENT) { | 815 if (i == ERROR_EVENT) { |
798 if (!isClosing) { | 816 if (!isClosing) { |
799 reportError(nativeGetError(), ""); | 817 reportError(nativeGetError(), ""); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
897 int fullData = (typeFlags & TYPE_TYPE_MASK) | data; | 915 int fullData = (typeFlags & TYPE_TYPE_MASK) | data; |
898 assert(!isClosing); | 916 assert(!isClosing); |
899 connectToEventHandler(); | 917 connectToEventHandler(); |
900 _EventHandler._sendData(this, eventPort.sendPort, fullData); | 918 _EventHandler._sendData(this, eventPort.sendPort, fullData); |
901 } | 919 } |
902 | 920 |
903 void connectToEventHandler() { | 921 void connectToEventHandler() { |
904 assert(!isClosed); | 922 assert(!isClosed); |
905 if (eventPort == null) { | 923 if (eventPort == null) { |
906 eventPort = new RawReceivePort(multiplex); | 924 eventPort = new RawReceivePort(multiplex); |
907 _sockets[_serviceId] = this; | 925 } |
926 if (!connectedResourceHandler) { | |
ricow1
2015/09/01 12:06:25
I am not sure this is the correct location for thi
Søren Gjesse
2015/09/02 12:24:49
I think this is fine for now.
| |
927 registerExtension('__getOpenSockets', | |
928 _SocketResourceInfo.getOpenSockets); | |
929 registerExtension('__getSocketByID', | |
930 _SocketResourceInfo.getSocketInfoMapByID); | |
931 | |
932 connectedResourceHandler = true; | |
908 } | 933 } |
909 } | 934 } |
910 | 935 |
911 void disconnectFromEventHandler() { | 936 void disconnectFromEventHandler() { |
912 assert(eventPort != null); | 937 assert(eventPort != null); |
913 eventPort.close(); | 938 eventPort.close(); |
914 eventPort = null; | 939 eventPort = null; |
915 _sockets.remove(_serviceId); | |
916 // Now that we don't track this Socket anymore, we can clear the owner | 940 // Now that we don't track this Socket anymore, we can clear the owner |
917 // field. | 941 // field. |
918 owner = null; | 942 owner = null; |
919 } | 943 } |
920 | 944 |
921 // Check whether this is an error response from a native port call. | 945 // Check whether this is an error response from a native port call. |
922 static bool isErrorResponse(response) { | 946 static bool isErrorResponse(response) { |
923 return response is List && response[0] != _SUCCESS_RESPONSE; | 947 return response is List && response[0] != _SUCCESS_RESPONSE; |
924 } | 948 } |
925 | 949 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1010 void leaveMulticast(InternetAddress addr, NetworkInterface interface) { | 1034 void leaveMulticast(InternetAddress addr, NetworkInterface interface) { |
1011 var interfaceAddr = multicastAddress(addr, interface); | 1035 var interfaceAddr = multicastAddress(addr, interface); |
1012 var interfaceIndex = interface == null ? 0 : interface.index; | 1036 var interfaceIndex = interface == null ? 0 : interface.index; |
1013 var result = nativeLeaveMulticast( | 1037 var result = nativeLeaveMulticast( |
1014 addr._in_addr, | 1038 addr._in_addr, |
1015 interfaceAddr == null ? null : interfaceAddr._in_addr, | 1039 interfaceAddr == null ? null : interfaceAddr._in_addr, |
1016 interfaceIndex); | 1040 interfaceIndex); |
1017 if (result is OSError) throw result; | 1041 if (result is OSError) throw result; |
1018 } | 1042 } |
1019 | 1043 |
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 | 1044 |
1138 void nativeSetSocketId(int id) native "Socket_SetSocketId"; | 1045 void nativeSetSocketId(int id) native "Socket_SetSocketId"; |
1139 nativeAvailable() native "Socket_Available"; | 1046 nativeAvailable() native "Socket_Available"; |
1140 nativeRead(int len) native "Socket_Read"; | 1047 nativeRead(int len) native "Socket_Read"; |
1141 nativeRecvFrom() native "Socket_RecvFrom"; | 1048 nativeRecvFrom() native "Socket_RecvFrom"; |
1142 nativeWrite(List<int> buffer, int offset, int bytes) | 1049 nativeWrite(List<int> buffer, int offset, int bytes) |
1143 native "Socket_WriteList"; | 1050 native "Socket_WriteList"; |
1144 nativeSendTo(List<int> buffer, int offset, int bytes, | 1051 nativeSendTo(List<int> buffer, int offset, int bytes, |
1145 List<int> address, int port) | 1052 List<int> address, int port) |
1146 native "Socket_SendTo"; | 1053 native "Socket_SendTo"; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1279 _referencePort.listen((sendPort) { | 1186 _referencePort.listen((sendPort) { |
1280 sendPort.send( | 1187 sendPort.send( |
1281 [_socket.address, | 1188 [_socket.address, |
1282 _socket.port, | 1189 _socket.port, |
1283 _v6Only]); | 1190 _v6Only]); |
1284 }); | 1191 }); |
1285 } | 1192 } |
1286 return new _RawServerSocketReference(_referencePort.sendPort); | 1193 return new _RawServerSocketReference(_referencePort.sendPort); |
1287 } | 1194 } |
1288 | 1195 |
1289 Map _toJSON(bool ref) => _socket._toJSON(ref); | |
1290 | |
1291 void set _owner(owner) { _socket.owner = owner; } | 1196 void set _owner(owner) { _socket.owner = owner; } |
1292 } | 1197 } |
1293 | 1198 |
1294 | 1199 |
1295 class _RawServerSocketReference implements RawServerSocketReference { | 1200 class _RawServerSocketReference implements RawServerSocketReference { |
1296 final SendPort _sendPort; | 1201 final SendPort _sendPort; |
1297 | 1202 |
1298 _RawServerSocketReference(this._sendPort); | 1203 _RawServerSocketReference(this._sendPort); |
1299 | 1204 |
1300 Future<RawServerSocket> create() { | 1205 Future<RawServerSocket> create() { |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1458 } | 1363 } |
1459 | 1364 |
1460 void _onSubscriptionStateChange() { | 1365 void _onSubscriptionStateChange() { |
1461 if (_controller.hasListener) { | 1366 if (_controller.hasListener) { |
1462 _resume(); | 1367 _resume(); |
1463 } else { | 1368 } else { |
1464 _socket.close(); | 1369 _socket.close(); |
1465 } | 1370 } |
1466 } | 1371 } |
1467 | 1372 |
1468 Map _toJSON(bool ref) => _socket._toJSON(ref); | |
1469 void set _owner(owner) { _socket.owner = owner; } | 1373 void set _owner(owner) { _socket.owner = owner; } |
1470 } | 1374 } |
1471 | 1375 |
1472 | 1376 |
1473 patch class ServerSocket { | 1377 patch class ServerSocket { |
1474 /* patch */ static Future<ServerSocket> bind(address, | 1378 /* patch */ static Future<ServerSocket> bind(address, |
1475 int port, | 1379 int port, |
1476 {int backlog: 0, | 1380 {int backlog: 0, |
1477 bool v6Only: false, | 1381 bool v6Only: false, |
1478 bool shared: false}) { | 1382 bool shared: false}) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1521 int get port => _socket.port; | 1425 int get port => _socket.port; |
1522 | 1426 |
1523 InternetAddress get address => _socket.address; | 1427 InternetAddress get address => _socket.address; |
1524 | 1428 |
1525 Future close() => _socket.close().then((_) => this); | 1429 Future close() => _socket.close().then((_) => this); |
1526 | 1430 |
1527 ServerSocketReference get reference { | 1431 ServerSocketReference get reference { |
1528 return new _ServerSocketReference(_socket.reference); | 1432 return new _ServerSocketReference(_socket.reference); |
1529 } | 1433 } |
1530 | 1434 |
1531 Map _toJSON(bool ref) => _socket._toJSON(ref); | |
1532 | |
1533 void set _owner(owner) { _socket._owner = owner; } | 1435 void set _owner(owner) { _socket._owner = owner; } |
1534 } | 1436 } |
1535 | 1437 |
1536 | 1438 |
1537 patch class Socket { | 1439 patch class Socket { |
1538 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { | 1440 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { |
1539 return RawSocket.connect(host, port, sourceAddress: sourceAddress).then( | 1441 return RawSocket.connect(host, port, sourceAddress: sourceAddress).then( |
1540 (socket) => new _Socket(socket)); | 1442 (socket) => new _Socket(socket)); |
1541 } | 1443 } |
1542 } | 1444 } |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1836 if (_detachReady != null) { | 1738 if (_detachReady != null) { |
1837 _detachReady.complete(null); | 1739 _detachReady.complete(null); |
1838 } else { | 1740 } else { |
1839 if (_raw != null) { | 1741 if (_raw != null) { |
1840 _raw.shutdown(SocketDirection.SEND); | 1742 _raw.shutdown(SocketDirection.SEND); |
1841 _disableWriteEvent(); | 1743 _disableWriteEvent(); |
1842 } | 1744 } |
1843 } | 1745 } |
1844 } | 1746 } |
1845 | 1747 |
1846 Map _toJSON(bool ref) => _raw._toJSON(ref); | |
1847 void set _owner(owner) { _raw._owner = owner; } | 1748 void set _owner(owner) { _raw._owner = owner; } |
1848 } | 1749 } |
1849 | 1750 |
1850 | 1751 |
1851 patch class RawDatagramSocket { | 1752 patch class RawDatagramSocket { |
1852 /* patch */ static Future<RawDatagramSocket> bind( | 1753 /* patch */ static Future<RawDatagramSocket> bind( |
1853 host, int port, {bool reuseAddress: true}) { | 1754 host, int port, {bool reuseAddress: true}) { |
1854 return _RawDatagramSocket.bind(host, port, reuseAddress); | 1755 return _RawDatagramSocket.bind(host, port, reuseAddress); |
1855 } | 1756 } |
1856 } | 1757 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1992 Datagram _makeDatagram(List<int> data, | 1893 Datagram _makeDatagram(List<int> data, |
1993 String address, | 1894 String address, |
1994 List<int> in_addr, | 1895 List<int> in_addr, |
1995 int port) { | 1896 int port) { |
1996 return new Datagram( | 1897 return new Datagram( |
1997 data, | 1898 data, |
1998 new _InternetAddress(address, null, in_addr), | 1899 new _InternetAddress(address, null, in_addr), |
1999 port); | 1900 port); |
2000 } | 1901 } |
2001 | 1902 |
2002 String _socketsStats() => _SocketsObservatory.toJSON(); | |
OLD | NEW |