| 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 return _RawServerSocket.bind(address, port, backlog, v6Only); | 10 return _RawServerSocket.bind(address, port, backlog, v6Only); |
| (...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 } | 708 } |
| 709 | 709 |
| 710 | 710 |
| 711 class _RawSocket extends Stream<RawSocketEvent> | 711 class _RawSocket extends Stream<RawSocketEvent> |
| 712 implements RawSocket { | 712 implements RawSocket { |
| 713 final _NativeSocket _socket; | 713 final _NativeSocket _socket; |
| 714 StreamController<RawSocketEvent> _controller; | 714 StreamController<RawSocketEvent> _controller; |
| 715 bool _readEventsEnabled = true; | 715 bool _readEventsEnabled = true; |
| 716 bool _writeEventsEnabled = true; | 716 bool _writeEventsEnabled = true; |
| 717 | 717 |
| 718 // Flag to handle Ctrl-D closing of stdio on Mac OS. |
| 719 bool _isMacOSTerminalInput = false; |
| 720 |
| 718 static Future<RawSocket> connect(host, int port) { | 721 static Future<RawSocket> connect(host, int port) { |
| 719 return _NativeSocket.connect(host, port) | 722 return _NativeSocket.connect(host, port) |
| 720 .then((socket) => new _RawSocket(socket)); | 723 .then((socket) => new _RawSocket(socket)); |
| 721 } | 724 } |
| 722 | 725 |
| 723 _RawSocket(this._socket) { | 726 _RawSocket(this._socket) { |
| 724 _controller = new StreamController(sync: true, | 727 _controller = new StreamController(sync: true, |
| 725 onListen: _onSubscriptionStateChange, | 728 onListen: _onSubscriptionStateChange, |
| 726 onCancel: _onSubscriptionStateChange, | 729 onCancel: _onSubscriptionStateChange, |
| 727 onPause: _onPauseStateChange, | 730 onPause: _onPauseStateChange, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 747 var native = new _NativeSocket.pipe(); | 750 var native = new _NativeSocket.pipe(); |
| 748 native.isClosedRead = true; | 751 native.isClosedRead = true; |
| 749 if (fd != null) _getStdioHandle(native, fd); | 752 if (fd != null) _getStdioHandle(native, fd); |
| 750 return new _RawSocket(native); | 753 return new _RawSocket(native); |
| 751 } | 754 } |
| 752 | 755 |
| 753 factory _RawSocket._readPipe(int fd) { | 756 factory _RawSocket._readPipe(int fd) { |
| 754 var native = new _NativeSocket.pipe(); | 757 var native = new _NativeSocket.pipe(); |
| 755 native.isClosedWrite = true; | 758 native.isClosedWrite = true; |
| 756 if (fd != null) _getStdioHandle(native, fd); | 759 if (fd != null) _getStdioHandle(native, fd); |
| 757 return new _RawSocket(native); | 760 var result = new _RawSocket(native); |
| 761 result._isMacOSTerminalInput = |
| 762 Platform.isMacOS && |
| 763 _StdIOUtils._socketType(result) == _STDIO_HANDLE_TYPE_TERMINAL; |
| 764 return result; |
| 758 } | 765 } |
| 759 | 766 |
| 760 StreamSubscription<RawSocketEvent> listen(void onData(RawSocketEvent event), | 767 StreamSubscription<RawSocketEvent> listen(void onData(RawSocketEvent event), |
| 761 {void onError(Object error), | 768 {void onError(Object error), |
| 762 void onDone(), | 769 void onDone(), |
| 763 bool cancelOnError}) { | 770 bool cancelOnError}) { |
| 764 return _controller.stream.listen( | 771 return _controller.stream.listen( |
| 765 onData, | 772 onData, |
| 766 onError: onError, | 773 onError: onError, |
| 767 onDone: onDone, | 774 onDone: onDone, |
| 768 cancelOnError: cancelOnError); | 775 cancelOnError: cancelOnError); |
| 769 } | 776 } |
| 770 | 777 |
| 771 int available() => _socket.available(); | 778 int available() => _socket.available(); |
| 772 | 779 |
| 773 List<int> read([int len]) => _socket.read(len); | 780 List<int> read([int len]) { |
| 781 if (_isMacOSTerminalInput) { |
| 782 var available = available(); |
| 783 if (available == 0) return null; |
| 784 var data = _socket.read(len); |
| 785 if (data == null || data.length < available) { |
| 786 // Reading less than available from a Mac OS terminal indicate Ctrl-D. |
| 787 // This is interpreted as read closed. |
| 788 runAsync(() => _controller.add(RawSocketEvent.READ_CLOSED)); |
| 789 } |
| 790 return data; |
| 791 } else { |
| 792 return _socket.read(len); |
| 793 } |
| 794 } |
| 774 | 795 |
| 775 int write(List<int> buffer, [int offset, int count]) => | 796 int write(List<int> buffer, [int offset, int count]) => |
| 776 _socket.write(buffer, offset, count); | 797 _socket.write(buffer, offset, count); |
| 777 | 798 |
| 778 void close() => _socket.close(); | 799 void close() => _socket.close(); |
| 779 | 800 |
| 780 void shutdown(SocketDirection direction) => _socket.shutdown(direction); | 801 void shutdown(SocketDirection direction) => _socket.shutdown(direction); |
| 781 | 802 |
| 782 int get port => _socket.port; | 803 int get port => _socket.port; |
| 783 | 804 |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1156 if (_detachReady != null) { | 1177 if (_detachReady != null) { |
| 1157 _detachReady.complete(null); | 1178 _detachReady.complete(null); |
| 1158 } else { | 1179 } else { |
| 1159 if (_raw != null) { | 1180 if (_raw != null) { |
| 1160 _raw.shutdown(SocketDirection.SEND); | 1181 _raw.shutdown(SocketDirection.SEND); |
| 1161 _disableWriteEvent(); | 1182 _disableWriteEvent(); |
| 1162 } | 1183 } |
| 1163 } | 1184 } |
| 1164 } | 1185 } |
| 1165 } | 1186 } |
| OLD | NEW |