Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: runtime/bin/socket_stream_impl.dart

Issue 10351009: Implement onClosed on a socket output stream (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/bin/websocket_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class _SocketInputStream implements SocketInputStream { 5 class _SocketInputStream implements SocketInputStream {
6 _SocketInputStream(Socket socket) : _socket = socket { 6 _SocketInputStream(Socket socket) : _socket = socket {
7 if (_socket._id == -1) _closed = true; 7 if (_socket._id == -1) _closed = true;
8 _socket.onClosed = _onClosed; 8 _socket.onClosed = _onClosed;
9 } 9 }
10 10
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 bool write(List<int> buffer, [bool copyBuffer = true]) { 102 bool write(List<int> buffer, [bool copyBuffer = true]) {
103 return _write(buffer, 0, buffer.length, copyBuffer); 103 return _write(buffer, 0, buffer.length, copyBuffer);
104 } 104 }
105 105
106 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { 106 bool writeFrom(List<int> buffer, [int offset = 0, int len]) {
107 return _write( 107 return _write(
108 buffer, offset, (len == null) ? buffer.length - offset : len, true); 108 buffer, offset, (len == null) ? buffer.length - offset : len, true);
109 } 109 }
110 110
111 void close() { 111 void close() {
112 if (_closing && _closed) return;
112 if (!_pendingWrites.isEmpty()) { 113 if (!_pendingWrites.isEmpty()) {
113 // Mark the socket for close when all data is written. 114 // Mark the socket for close when all data is written.
114 _closing = true; 115 _closing = true;
115 _socket._onWrite = _onWrite; 116 _socket._onWrite = _onWrite;
116 } else { 117 } else {
117 // Close the socket for writing. 118 // Close the socket for writing.
118 _socket._closeWrite(); 119 _socket._closeWrite();
119 _closed = true; 120 _closed = true;
121 // Invoke the callback asynchronously.
122 new Timer(0, (t) {
123 if (_onClosed != null) _onClosed();
124 });
120 } 125 }
121 } 126 }
122 127
123 void destroy() { 128 void destroy() {
124 _socket.onWrite = null; 129 _socket.onWrite = null;
125 _pendingWrites.clear(); 130 _pendingWrites.clear();
126 _socket.close(); 131 _socket.close();
127 _closed = true; 132 _closed = true;
128 } 133 }
129 134
130 void set onNoPendingWrites(void callback()) { 135 void set onNoPendingWrites(void callback()) {
131 _onNoPendingWrites = callback; 136 _onNoPendingWrites = callback;
132 if (_onNoPendingWrites != null) { 137 if (_onNoPendingWrites != null) {
133 _socket._onWrite = _onWrite; 138 _socket._onWrite = _onWrite;
134 } 139 }
135 } 140 }
136 141
142 void set onClosed(void callback()) {
143 _onClosed = callback;
144 }
145
137 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { 146 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) {
138 if (_closing || _closed) throw new StreamException("Stream closed"); 147 if (_closing || _closed) throw new StreamException("Stream closed");
139 int bytesWritten = 0; 148 int bytesWritten = 0;
140 if (_pendingWrites.isEmpty()) { 149 if (_pendingWrites.isEmpty()) {
141 // If nothing is buffered write as much as possible and buffer 150 // If nothing is buffered write as much as possible and buffer
142 // the rest. 151 // the rest.
143 bytesWritten = _socket.writeList(buffer, offset, len); 152 bytesWritten = _socket.writeList(buffer, offset, len);
144 if (bytesWritten == len) return true; 153 if (bytesWritten == len) return true;
145 } 154 }
146 155
(...skipping 22 matching lines...) Expand all
169 if (bytesWritten < bytesToWrite) { 178 if (bytesWritten < bytesToWrite) {
170 _socket._onWrite = _onWrite; 179 _socket._onWrite = _onWrite;
171 return; 180 return;
172 } 181 }
173 } 182 }
174 183
175 // All buffered data was written. 184 // All buffered data was written.
176 if (_closing) { 185 if (_closing) {
177 _socket._closeWrite(); 186 _socket._closeWrite();
178 _closed = true; 187 _closed = true;
188 if (_onClosed != null) {
189 _onClosed();
190 }
179 } else { 191 } else {
180 if (_onNoPendingWrites != null) _onNoPendingWrites(); 192 if (_onNoPendingWrites != null) _onNoPendingWrites();
181 } 193 }
182 if (_onNoPendingWrites == null) { 194 if (_onNoPendingWrites == null) {
183 _socket._onWrite = null; 195 _socket._onWrite = null;
184 } else { 196 } else {
185 _socket._onWrite = _onWrite; 197 _socket._onWrite = _onWrite;
186 } 198 }
187 } 199 }
188 200
189 bool _onSocketError(e) { 201 bool _onSocketError(e) {
190 close(); 202 close();
191 if (_onError != null) { 203 if (_onError != null) {
192 _onError(e); 204 _onError(e);
193 return true; 205 return true;
194 } else { 206 } else {
195 return false; 207 return false;
196 } 208 }
197 } 209 }
198 210
199 Socket _socket; 211 Socket _socket;
200 _BufferList _pendingWrites; 212 _BufferList _pendingWrites;
201 Function _onNoPendingWrites; 213 Function _onNoPendingWrites;
214 Function _onClosed;
202 bool _closing = false; 215 bool _closing = false;
203 bool _closed = false; 216 bool _closed = false;
204 } 217 }
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/websocket_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698