OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 part of core; | 5 part of core; |
6 | 6 |
7 class MojoEventStream extends Stream<List<int>> { | 7 class MojoEventStream extends Stream<List<int>> { |
8 // The underlying Mojo handle. | 8 // The underlying Mojo handle. |
9 MojoHandle _handle; | 9 MojoHandle _handle; |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 _sendPort = _receivePort.sendPort; | 57 _sendPort = _receivePort.sendPort; |
58 _controller = new StreamController( | 58 _controller = new StreamController( |
59 sync: true, | 59 sync: true, |
60 onListen: _onSubscriptionStateChange, | 60 onListen: _onSubscriptionStateChange, |
61 onCancel: _onSubscriptionStateChange, | 61 onCancel: _onSubscriptionStateChange, |
62 onPause: _onPauseStateChange, | 62 onPause: _onPauseStateChange, |
63 onResume: _onPauseStateChange); | 63 onResume: _onPauseStateChange); |
64 _controller.addStream(_receivePort).whenComplete(_controller.close); | 64 _controller.addStream(_receivePort).whenComplete(_controller.close); |
65 | 65 |
66 if (_signals != MojoHandleSignals.NONE) { | 66 if (_signals != MojoHandleSignals.NONE) { |
67 var res = MojoHandleWatcher.add(_handle, _sendPort, _signals.value); | 67 var res = new MojoResult( |
| 68 MojoHandleWatcher.add(_handle.h, _sendPort, _signals.value)); |
68 if (!res.isOk) { | 69 if (!res.isOk) { |
69 throw "MojoHandleWatcher add failed: $res"; | 70 throw "MojoHandleWatcher add failed: $res"; |
70 } | 71 } |
71 } | 72 } |
72 | 73 |
73 _isListening = true; | 74 _isListening = true; |
74 return _controller.stream.listen(onData, | 75 return _controller.stream.listen(onData, |
75 onError: onError, onDone: onDone, cancelOnError: cancelOnError); | 76 onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
76 } | 77 } |
77 | 78 |
78 void enableSignals(MojoHandleSignals signals) { | 79 void enableSignals(MojoHandleSignals signals) { |
79 _signals = signals; | 80 _signals = signals; |
80 if (_isListening) { | 81 if (_isListening) { |
81 var res = MojoHandleWatcher.add(_handle, _sendPort, signals.value); | 82 var res = new MojoResult( |
| 83 MojoHandleWatcher.add(_handle.h, _sendPort, signals.value)); |
82 if (!res.isOk) { | 84 if (!res.isOk) { |
83 throw "MojoHandleWatcher add failed: $res"; | 85 throw "MojoHandleWatcher add failed: $res"; |
84 } | 86 } |
85 } | 87 } |
86 } | 88 } |
87 | 89 |
88 void enableReadEvents() => | 90 void enableReadEvents() => |
89 enableSignals(MojoHandleSignals.PEER_CLOSED_READABLE); | 91 enableSignals(MojoHandleSignals.PEER_CLOSED_READABLE); |
90 void enableWriteEvents() => enableSignals(MojoHandleSignals.WRITABLE); | 92 void enableWriteEvents() => enableSignals(MojoHandleSignals.WRITABLE); |
91 void enableAllEvents() => enableSignals(MojoHandleSignals.READWRITE); | 93 void enableAllEvents() => enableSignals(MojoHandleSignals.READWRITE); |
92 | 94 |
93 Future _handleWatcherClose() { | 95 Future _handleWatcherClose() { |
94 assert(_handle != null); | 96 assert(_handle != null); |
95 return MojoHandleWatcher.close(_handle, wait: true).then((_) { | 97 assert(MojoHandle._removeUnclosedHandle(_handle)); |
| 98 return MojoHandleWatcher.close(_handle.h, wait: true).then((r) { |
96 if (_receivePort != null) { | 99 if (_receivePort != null) { |
97 _receivePort.close(); | 100 _receivePort.close(); |
98 _receivePort = null; | 101 _receivePort = null; |
99 } | 102 } |
| 103 return new MojoResult(r); |
100 }); | 104 }); |
101 } | 105 } |
102 | 106 |
103 void _localClose() { | 107 void _localClose() { |
104 assert(_handle != null); | 108 assert(_handle != null); |
105 _handle.close(); | 109 _handle.close(); |
106 _handle = null; | 110 _handle = null; |
107 if (_receivePort != null) { | 111 if (_receivePort != null) { |
108 _receivePort.close(); | 112 _receivePort.close(); |
109 _receivePort = null; | 113 _receivePort = null; |
110 } | 114 } |
111 } | 115 } |
112 | 116 |
113 void _onSubscriptionStateChange() { | 117 void _onSubscriptionStateChange() { |
114 if (!_controller.hasListener) { | 118 if (!_controller.hasListener) { |
115 close(); | 119 close(); |
116 } | 120 } |
117 } | 121 } |
118 | 122 |
119 void _onPauseStateChange() { | 123 void _onPauseStateChange() { |
120 if (_controller.isPaused) { | 124 if (_controller.isPaused) { |
121 var res = MojoHandleWatcher.remove(_handle); | 125 var res = new MojoResult(MojoHandleWatcher.remove(_handle.h)); |
122 if (!res.isOk) { | 126 if (!res.isOk) { |
123 throw "MojoHandleWatcher add failed: $res"; | 127 throw "MojoHandleWatcher add failed: $res"; |
124 } | 128 } |
125 } else { | 129 } else { |
126 var res = MojoHandleWatcher.add(_handle, _sendPort, _signals.value); | 130 var res = new MojoResult( |
| 131 MojoHandleWatcher.add(_handle.h, _sendPort, _signals.value)); |
127 if (!res.isOk) { | 132 if (!res.isOk) { |
128 throw "MojoHandleWatcher add failed: $res"; | 133 throw "MojoHandleWatcher add failed: $res"; |
129 } | 134 } |
130 } | 135 } |
131 } | 136 } |
132 | 137 |
133 bool get readyRead => _handle.readyRead; | 138 bool get readyRead => _handle.readyRead; |
134 bool get readyWrite => _handle.readyWrite; | 139 bool get readyWrite => _handle.readyWrite; |
135 | 140 |
136 String toString() => "$_handle"; | 141 String toString() => "$_handle"; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 void handleWrite() {} | 239 void handleWrite() {} |
235 | 240 |
236 MojoMessagePipeEndpoint get endpoint => _endpoint; | 241 MojoMessagePipeEndpoint get endpoint => _endpoint; |
237 bool get isOpen => _isOpen; | 242 bool get isOpen => _isOpen; |
238 bool get isInHandler => _isInHandler; | 243 bool get isInHandler => _isInHandler; |
239 bool get isBound => _endpoint != null; | 244 bool get isBound => _endpoint != null; |
240 | 245 |
241 String toString() => "MojoEventStreamListener(" | 246 String toString() => "MojoEventStreamListener(" |
242 "isOpen: $isOpen, isBound: $isBound, endpoint: $_endpoint)"; | 247 "isOpen: $isOpen, isBound: $isBound, endpoint: $_endpoint)"; |
243 } | 248 } |
OLD | NEW |