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([String address = "127.0.0.1", | 6 /* patch */ static Future<RawServerSocket> bind([String address = "127.0.0.1", |
7 int port = 0, | 7 int port = 0, |
8 int backlog = 0]) { | 8 int backlog = 0]) { |
9 return _RawServerSocket.bind(address, port, backlog); | 9 return _RawServerSocket.bind(address, port, backlog); |
10 } | 10 } |
(...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 | 764 |
765 | 765 |
766 class _Socket extends Stream<List<int>> implements Socket { | 766 class _Socket extends Stream<List<int>> implements Socket { |
767 RawSocket _raw; // Set to null when the raw socket is closed. | 767 RawSocket _raw; // Set to null when the raw socket is closed. |
768 bool _closed = false; // Set to true when the raw socket is closed. | 768 bool _closed = false; // Set to true when the raw socket is closed. |
769 StreamController _controller; | 769 StreamController _controller; |
770 bool _controllerClosed = false; | 770 bool _controllerClosed = false; |
771 _SocketStreamConsumer _consumer; | 771 _SocketStreamConsumer _consumer; |
772 IOSink _sink; | 772 IOSink _sink; |
773 var _subscription; | 773 var _subscription; |
| 774 var _detachReady; |
774 | 775 |
775 _Socket(RawSocket this._raw) { | 776 _Socket(RawSocket this._raw) { |
776 _controller = new StreamController<List<int>>( | 777 _controller = new StreamController<List<int>>( |
777 onListen: _onSubscriptionStateChange, | 778 onListen: _onSubscriptionStateChange, |
778 onCancel: _onSubscriptionStateChange, | 779 onCancel: _onSubscriptionStateChange, |
779 onPause: _onPauseStateChange, | 780 onPause: _onPauseStateChange, |
780 onResume: _onPauseStateChange); | 781 onResume: _onPauseStateChange); |
781 _consumer = new _SocketStreamConsumer(this); | 782 _consumer = new _SocketStreamConsumer(this); |
782 _sink = new IOSink(_consumer); | 783 _sink = new IOSink(_consumer); |
783 | 784 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
844 | 845 |
845 bool setOption(SocketOption option, bool enabled) { | 846 bool setOption(SocketOption option, bool enabled) { |
846 if (_raw == null) return false; | 847 if (_raw == null) return false; |
847 return _raw.setOption(option, enabled); | 848 return _raw.setOption(option, enabled); |
848 } | 849 } |
849 | 850 |
850 int get port => _raw.port; | 851 int get port => _raw.port; |
851 String get remoteHost => _raw.remoteHost; | 852 String get remoteHost => _raw.remoteHost; |
852 int get remotePort => _raw.remotePort; | 853 int get remotePort => _raw.remotePort; |
853 | 854 |
| 855 Future _detachRaw() { |
| 856 _detachReady = new Completer(); |
| 857 _sink.close(); |
| 858 return _detachReady.future.then((_) { |
| 859 assert(_consumer.buffer == null); |
| 860 var raw = _raw; |
| 861 _raw = null; |
| 862 return [raw, _subscription]; |
| 863 }); |
| 864 } |
| 865 |
854 // Ensure a subscription on the raw socket. Both the stream and the | 866 // Ensure a subscription on the raw socket. Both the stream and the |
855 // consumer needs a subscription as they share the error and done | 867 // consumer needs a subscription as they share the error and done |
856 // events from the raw socket. | 868 // events from the raw socket. |
857 void _ensureRawSocketSubscription() { | 869 void _ensureRawSocketSubscription() { |
858 if (_subscription == null && _raw != null) { | 870 if (_subscription == null && _raw != null) { |
859 _subscription = _raw.listen(_onData, | 871 _subscription = _raw.listen(_onData, |
860 onError: _onError, | 872 onError: _onError, |
861 onDone: _onDone, | 873 onDone: _onDone, |
862 cancelOnError: true); | 874 cancelOnError: true); |
863 } | 875 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 _raw.writeEventsEnabled = true; | 943 _raw.writeEventsEnabled = true; |
932 } | 944 } |
933 | 945 |
934 void _disableWriteEvent() { | 946 void _disableWriteEvent() { |
935 if (_raw != null) { | 947 if (_raw != null) { |
936 _raw.writeEventsEnabled = false; | 948 _raw.writeEventsEnabled = false; |
937 } | 949 } |
938 } | 950 } |
939 | 951 |
940 void _consumerDone() { | 952 void _consumerDone() { |
941 if (_raw != null) { | 953 if (_detachReady != null) { |
942 _raw.shutdown(SocketDirection.SEND); | 954 _detachReady.complete(null); |
943 _disableWriteEvent(); | 955 } else { |
| 956 if (_raw != null) { |
| 957 _raw.shutdown(SocketDirection.SEND); |
| 958 _disableWriteEvent(); |
| 959 } |
944 } | 960 } |
945 } | 961 } |
946 } | 962 } |
947 | 963 |
948 | 964 |
949 class _SecureSocket extends _Socket implements SecureSocket { | 965 class _SecureSocket extends _Socket implements SecureSocket { |
950 _SecureSocket(RawSecureSocket raw) : super(raw); | 966 _SecureSocket(RawSecureSocket raw) : super(raw); |
951 | 967 |
952 void set onBadCertificate(bool callback(X509Certificate certificate)) { | 968 void set onBadCertificate(bool callback(X509Certificate certificate)) { |
953 if (_raw == null) { | 969 if (_raw == null) { |
954 throw new StateError("onBadCertificate called on destroyed SecureSocket"); | 970 throw new StateError("onBadCertificate called on destroyed SecureSocket"); |
955 } | 971 } |
956 _raw.onBadCertificate = callback; | 972 _raw.onBadCertificate = callback; |
957 } | 973 } |
958 | 974 |
959 X509Certificate get peerCertificate { | 975 X509Certificate get peerCertificate { |
960 if (_raw == null) { | 976 if (_raw == null) { |
961 throw new StateError("peerCertificate called on destroyed SecureSocket"); | 977 throw new StateError("peerCertificate called on destroyed SecureSocket"); |
962 } | 978 } |
963 return _raw.peerCertificate; | 979 return _raw.peerCertificate; |
964 } | 980 } |
965 } | 981 } |
OLD | NEW |