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

Side by Side Diff: sdk/lib/io/socket_stream_impl.dart

Issue 11879042: Improve the error-propagation on socket streams (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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
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 part of dart.io; 5 part of dart.io;
6 6
7 class _SocketInputStream implements InputStream { 7 class _SocketInputStream implements InputStream {
8 _SocketInputStream(Socket socket) : _socket = socket { 8 _SocketInputStream(Socket socket) : _socket = socket {
9 if (_socket._closed) _closed = true; 9 if (_socket._closed) _closed = true;
10 _socket.onClosed = _onClosed; 10 _socket.onClosed = _onClosed;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 51
52 void _onClosed() { 52 void _onClosed() {
53 _closed = true; 53 _closed = true;
54 if (_clientCloseHandler != null) { 54 if (_clientCloseHandler != null) {
55 _clientCloseHandler(); 55 _clientCloseHandler();
56 } 56 }
57 } 57 }
58 58
59 bool _onSocketError(e) { 59 bool _onSocketError(e) {
60 close(); 60 close();
61 if (_error) return true;
61 if (_onError != null) { 62 if (_onError != null) {
62 _onError(e); 63 _onError(e);
63 return true; 64 return true;
64 } else { 65 } else {
65 return false; 66 return false;
66 } 67 }
67 } 68 }
68 69
69 Socket _socket; 70 Socket _socket;
70 bool _closed = false; 71 bool _closed = false;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 if (_onNoPendingWrites != null) { 123 if (_onNoPendingWrites != null) {
123 _socket._onWrite = _onWrite; 124 _socket._onWrite = _onWrite;
124 } 125 }
125 } 126 }
126 127
127 void set onClosed(void callback()) { 128 void set onClosed(void callback()) {
128 _onClosed = callback; 129 _onClosed = callback;
129 } 130 }
130 131
131 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { 132 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) {
132 if (_closing || _closed) throw new StreamException("Stream closed"); 133 if (_closing || _closed) {
134 if (_error) return false;
135 _error = true;
136 var e = new StreamException.streamClosed();
137 if (_onError != null) {
138 _onError(e);
139 return false;
140 } else {
141 throw e;
142 }
143 }
133 int bytesWritten = 0; 144 int bytesWritten = 0;
134 if (_pendingWrites.isEmpty) { 145 if (_pendingWrites.isEmpty) {
135 // If nothing is buffered write as much as possible and buffer 146 // If nothing is buffered write as much as possible and buffer
136 // the rest. 147 // the rest.
137 bytesWritten = _socket.writeList(buffer, offset, len); 148 try {
138 if (bytesWritten == len) return true; 149 bytesWritten = _socket.writeList(buffer, offset, len);
150 if (bytesWritten == len) return true;
151 } catch (e) {
152 if (_error) return false;
153 _error = true;
154 if (_onError != null) {
155 _onError(e);
156 return false;
157 } else {
158 throw e;
159 }
160 }
139 } 161 }
140 162
141 // Place remaining data on the pending writes queue. 163 // Place remaining data on the pending writes queue.
142 int notWrittenOffset = offset + bytesWritten; 164 int notWrittenOffset = offset + bytesWritten;
143 if (copyBuffer) { 165 if (copyBuffer) {
144 List<int> newBuffer = 166 List<int> newBuffer =
145 buffer.getRange(notWrittenOffset, len - bytesWritten); 167 buffer.getRange(notWrittenOffset, len - bytesWritten);
146 _pendingWrites.add(newBuffer); 168 _pendingWrites.add(newBuffer);
147 } else { 169 } else {
148 assert(offset + len == buffer.length); 170 assert(offset + len == buffer.length);
149 _pendingWrites.add(buffer, notWrittenOffset); 171 _pendingWrites.add(buffer, notWrittenOffset);
150 } 172 }
151 _socket._onWrite = _onWrite; 173 _socket._onWrite = _onWrite;
152 return false; 174 return false;
153 } 175 }
154 176
155 void _onWrite() { 177 void _onWrite() {
156 // Write as much buffered data to the socket as possible. 178 // Write as much buffered data to the socket as possible.
157 while (!_pendingWrites.isEmpty) { 179 while (!_pendingWrites.isEmpty) {
158 List<int> buffer = _pendingWrites.first; 180 List<int> buffer = _pendingWrites.first;
159 int offset = _pendingWrites.index; 181 int offset = _pendingWrites.index;
160 int bytesToWrite = buffer.length - offset; 182 int bytesToWrite = buffer.length - offset;
161 int bytesWritten; 183 int bytesWritten;
162 try { 184 try {
163 bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); 185 bytesWritten = _socket.writeList(buffer, offset, bytesToWrite);
164 } catch (e) { 186 } catch (e) {
165 _pendingWrites.clear(); 187 _pendingWrites.clear();
166 _onSocketError(e); 188 if (_onError != null) _onError(e);
167 return; 189 return;
168 } 190 }
169 _pendingWrites.removeBytes(bytesWritten); 191 _pendingWrites.removeBytes(bytesWritten);
170 if (bytesWritten < bytesToWrite) { 192 if (bytesWritten < bytesToWrite) {
171 _socket._onWrite = _onWrite; 193 _socket._onWrite = _onWrite;
172 return; 194 return;
173 } 195 }
174 } 196 }
175 197
176 // All buffered data was written. 198 // All buffered data was written.
(...skipping 22 matching lines...) Expand all
199 throw e; 221 throw e;
200 } 222 }
201 } 223 }
202 224
203 Socket _socket; 225 Socket _socket;
204 _BufferList _pendingWrites; 226 _BufferList _pendingWrites;
205 Function _onNoPendingWrites; 227 Function _onNoPendingWrites;
206 Function _onClosed; 228 Function _onClosed;
207 bool _closing = false; 229 bool _closing = false;
208 bool _closed = false; 230 bool _closed = false;
231 bool _error = false;
209 } 232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698