OLD | NEW |
1 #!mojo mojo:dart_content_handler | 1 #!mojo mojo:dart_content_handler |
2 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
5 | 5 |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:core'; | 7 import 'dart:core'; |
8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
9 | 9 |
10 import 'package:mojo/application.dart'; | 10 import 'package:mojo/application.dart'; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 // * Relatedly, we should listen for _socketSender's peer being closed (also | 42 // * Relatedly, we should listen for _socketSender's peer being closed (also |
43 // _socket, I guess). | 43 // _socket, I guess). |
44 // * Handle the socket send pipe being full (currently, we assume it's never | 44 // * Handle the socket send pipe being full (currently, we assume it's never |
45 // full). | 45 // full). |
46 class Connector { | 46 class Connector { |
47 final Application _application; | 47 final Application _application; |
48 files.FileProxy _terminal; | 48 files.FileProxy _terminal; |
49 TcpConnectedSocketProxy _socket; | 49 TcpConnectedSocketProxy _socket; |
50 MojoDataPipeProducer _socketSender; | 50 MojoDataPipeProducer _socketSender; |
51 MojoDataPipeConsumer _socketReceiver; | 51 MojoDataPipeConsumer _socketReceiver; |
52 MojoEventStream _socketReceiverEventStream; | 52 MojoEventHandler _socketReceiverEventHandler; |
53 final ByteData _readBuffer; | 53 final ByteData _readBuffer; |
54 final ByteData _writeBuffer; | 54 final ByteData _writeBuffer; |
55 | 55 |
56 // TODO(vtl): Don't just hard-code buffer sizes. | 56 // TODO(vtl): Don't just hard-code buffer sizes. |
57 Connector(this._application, this._terminal) | 57 Connector(this._application, this._terminal) |
58 : _readBuffer = new ByteData(16 * 1024), | 58 : _readBuffer = new ByteData(16 * 1024), |
59 _writeBuffer = new ByteData(16 * 1024); | 59 _writeBuffer = new ByteData(16 * 1024); |
60 | 60 |
61 Future connect(NetAddress remote_address) async { | 61 Future connect(NetAddress remote_address) async { |
62 try { | 62 try { |
(...skipping 11 matching lines...) Expand all Loading... |
74 _socketReceiver = receiveDataPipe.consumer; | 74 _socketReceiver = receiveDataPipe.consumer; |
75 _socket = new TcpConnectedSocketProxy.unbound(); | 75 _socket = new TcpConnectedSocketProxy.unbound(); |
76 await boundSocket.ptr.connect(remote_address, sendDataPipe.consumer, | 76 await boundSocket.ptr.connect(remote_address, sendDataPipe.consumer, |
77 receiveDataPipe.producer, _socket); | 77 receiveDataPipe.producer, _socket); |
78 await boundSocket.close(); | 78 await boundSocket.close(); |
79 | 79 |
80 // Set up reading from the terminal. | 80 // Set up reading from the terminal. |
81 _startReadingFromTerminal(); | 81 _startReadingFromTerminal(); |
82 | 82 |
83 // Set up reading from the socket. | 83 // Set up reading from the socket. |
84 _socketReceiverEventStream = new MojoEventStream(_socketReceiver.handle); | 84 _socketReceiverEventHandler = new MojoEventHandler(_socketReceiver.handle)
; |
85 _socketReceiverEventStream.listen(_onSocketReceiverEvent); | 85 _socketReceiverEventHandler.handleEvents(_onSocketReceiverEvent); |
86 } catch (e) { | 86 } catch (e) { |
87 _shutDown(); | 87 _shutDown(); |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 void _startReadingFromTerminal() { | 91 void _startReadingFromTerminal() { |
92 // TODO(vtl): Do we have to do something on error? | 92 // TODO(vtl): Do we have to do something on error? |
93 _terminal.ptr | 93 _terminal.ptr |
94 .read(_writeBuffer.lengthInBytes, 0, files.Whence.FROM_CURRENT) | 94 .read(_writeBuffer.lengthInBytes, 0, files.Whence.FROM_CURRENT) |
95 .then(_onReadFromTerminal) | 95 .then(_onReadFromTerminal) |
(...skipping 27 matching lines...) Expand all Loading... |
123 void _onSocketReceiverEvent(List<int> event) { | 123 void _onSocketReceiverEvent(List<int> event) { |
124 var mojoSignals = new MojoHandleSignals(event[1]); | 124 var mojoSignals = new MojoHandleSignals(event[1]); |
125 var shouldShutDown = false; | 125 var shouldShutDown = false; |
126 if (mojoSignals.isReadable) { | 126 if (mojoSignals.isReadable) { |
127 var numBytesRead = _socketReceiver.read(_readBuffer); | 127 var numBytesRead = _socketReceiver.read(_readBuffer); |
128 if (_socketReceiver.status.isOk) { | 128 if (_socketReceiver.status.isOk) { |
129 assert(numBytesRead > 0); | 129 assert(numBytesRead > 0); |
130 _terminal.ptr.write(_readBuffer.buffer.asUint8List(0, numBytesRead), 0, | 130 _terminal.ptr.write(_readBuffer.buffer.asUint8List(0, numBytesRead), 0, |
131 files.Whence.FROM_CURRENT) | 131 files.Whence.FROM_CURRENT) |
132 .catchError((e) { _shutDown(); }); | 132 .catchError((e) { _shutDown(); }); |
133 _socketReceiverEventStream.enableReadEvents(); | 133 _socketReceiverEventHandler.enableReadEvents(); |
134 } else { | 134 } else { |
135 shouldShutDown = true; | 135 shouldShutDown = true; |
136 } | 136 } |
137 } else if (mojoSignals.isPeerClosed) { | 137 } else if (mojoSignals.isPeerClosed) { |
138 shouldShutDown = true; | 138 shouldShutDown = true; |
139 } else { | 139 } else { |
140 throw 'Unexpected handle event: $mojoSignals'; | 140 throw 'Unexpected handle event: $mojoSignals'; |
141 } | 141 } |
142 if (shouldShutDown) { | 142 if (shouldShutDown) { |
143 _shutDown(); | 143 _shutDown(); |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 void _shutDown() { | 147 void _shutDown() { |
148 if (_socketReceiverEventStream != null) { | 148 if (_socketReceiverEventHandler != null) { |
149 ignoreFuture(_socketReceiverEventStream.close()); | 149 ignoreFuture(_socketReceiverEventHandler.close()); |
150 _socketReceiverEventStream = null; | 150 _socketReceiverEventHandler = null; |
151 } | 151 } |
152 if (_socketSender != null) { | 152 if (_socketSender != null) { |
153 if (_socketSender.handle.isValid) | 153 if (_socketSender.handle.isValid) |
154 _socketSender.handle.close(); | 154 _socketSender.handle.close(); |
155 _socketSender = null; | 155 _socketSender = null; |
156 } | 156 } |
157 if (_socketReceiver != null) { | 157 if (_socketReceiver != null) { |
158 if (_socketReceiver.handle.isValid) | 158 if (_socketReceiver.handle.isValid) |
159 _socketReceiver.handle.close(); | 159 _socketReceiver.handle.close(); |
160 _socketReceiver = null; | 160 _socketReceiver = null; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 ApplicationConnection connection) { | 219 ApplicationConnection connection) { |
220 connection.provideService(TerminalClientName, | 220 connection.provideService(TerminalClientName, |
221 (endpoint) => new TerminalClientImpl(this, resolvedUrl, endpoint)); | 221 (endpoint) => new TerminalClientImpl(this, resolvedUrl, endpoint)); |
222 } | 222 } |
223 } | 223 } |
224 | 224 |
225 main(List args) { | 225 main(List args) { |
226 MojoHandle appHandle = new MojoHandle(args[0]); | 226 MojoHandle appHandle = new MojoHandle(args[0]); |
227 String url = args[1]; | 227 String url = args[1]; |
228 new NetcatApplication.fromHandle(appHandle) | 228 new NetcatApplication.fromHandle(appHandle) |
229 ..onError = (() { | 229 ..onError = ((Object e) { |
230 MojoHandle.reportLeakedHandles(); | 230 MojoHandle.reportLeakedHandles(); |
231 }); | 231 }); |
232 } | 232 } |
OLD | NEW |