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

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

Issue 9812007: Add error handling to socket streams (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 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 | « runtime/bin/socket_impl.dart ('k') | runtime/bin/stream_util.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
5 class _SocketInputStream implements SocketInputStream { 6 class _SocketInputStream implements SocketInputStream {
6 _SocketInputStream(Socket socket) : _socket = socket { 7 _SocketInputStream(Socket socket) : _socket = socket {
7 if (_socket._id == -1) _closed = true; 8 if (_socket._id == -1) _closed = true;
8 _socket.onClosed = _onClosed; 9 _socket.onClosed = _onClosed;
9 } 10 }
10 11
11 List<int> read([int len]) { 12 List<int> read([int len]) {
12 int bytesToRead = available(); 13 int bytesToRead = available();
13 if (bytesToRead == 0) return null; 14 if (bytesToRead == 0) return null;
14 if (len !== null) { 15 if (len !== null) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 60
60 void set onData(void callback()) { 61 void set onData(void callback()) {
61 _socket._onData = callback; 62 _socket._onData = callback;
62 } 63 }
63 64
64 void set onClosed(void callback()) { 65 void set onClosed(void callback()) {
65 _clientCloseHandler = callback; 66 _clientCloseHandler = callback;
66 _socket._onClosed = _onClosed; 67 _socket._onClosed = _onClosed;
67 } 68 }
68 69
69 void set onError(void callback()) {
70 _socket.onError = callback;
71 }
72
73 void _onClosed() { 70 void _onClosed() {
74 _closed = true; 71 _closed = true;
75 if (_clientCloseHandler !== null) { 72 if (_clientCloseHandler !== null) {
76 _clientCloseHandler(); 73 _clientCloseHandler();
77 } 74 }
78 } 75 }
79 76
77 void set onError(void callback(Exception e)) {
78 _errorCallback = callback;
79 }
80
81 void _onError(Exception e) {
82 close();
83 if (_errorCallback != null) _errorCallback(e);
84 }
85
80 Socket _socket; 86 Socket _socket;
87 bool _closed = false;
81 Function _clientCloseHandler; 88 Function _clientCloseHandler;
82 bool _closed = false; 89 Function _errorCallback;
83 } 90 }
84 91
85 92
86 class _SocketOutputStream 93 class _SocketOutputStream
87 extends _BaseOutputStream implements SocketOutputStream { 94 extends _BaseOutputStream implements SocketOutputStream {
88 _SocketOutputStream(Socket socket) 95 _SocketOutputStream(Socket socket)
89 : _socket = socket, _pendingWrites = new _BufferList(); 96 : _socket = socket, _pendingWrites = new _BufferList();
90 97
91 bool write(List<int> buffer, [bool copyBuffer = true]) { 98 bool write(List<int> buffer, [bool copyBuffer = true]) {
92 return _write(buffer, 0, buffer.length, copyBuffer); 99 return _write(buffer, 0, buffer.length, copyBuffer);
(...skipping 23 matching lines...) Expand all
116 _closed = true; 123 _closed = true;
117 } 124 }
118 125
119 void set onNoPendingWrites(void callback()) { 126 void set onNoPendingWrites(void callback()) {
120 _onNoPendingWrites = callback; 127 _onNoPendingWrites = callback;
121 if (_onNoPendingWrites != null) { 128 if (_onNoPendingWrites != null) {
122 _socket._onWrite = _onWrite; 129 _socket._onWrite = _onWrite;
123 } 130 }
124 } 131 }
125 132
126 void set onError(void callback()) {
127 _streamErrorHandler = callback;
128 if (_streamErrorHandler != null) {
129 _socket.onError = _onError;
130 } else {
131 _socket.onError = null;
132 }
133 }
134
135 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { 133 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) {
136 if (_closing || _closed) throw new StreamException("Stream closed"); 134 if (_closing || _closed) throw new StreamException("Stream closed");
137 int bytesWritten = 0; 135 int bytesWritten = 0;
138 if (_pendingWrites.isEmpty()) { 136 if (_pendingWrites.isEmpty()) {
139 // If nothing is buffered write as much as possible and buffer 137 // If nothing is buffered write as much as possible and buffer
140 // the rest. 138 // the rest.
141 bytesWritten = _socket.writeList(buffer, offset, len); 139 bytesWritten = _socket.writeList(buffer, offset, len);
142 if (bytesWritten == len) return true; 140 if (bytesWritten == len) return true;
143 } 141 }
144 142
(...skipping 28 matching lines...) Expand all
173 // All buffered data was written. 171 // All buffered data was written.
174 if (_closing) { 172 if (_closing) {
175 _socket._closeWrite(); 173 _socket._closeWrite();
176 _closed = true; 174 _closed = true;
177 } else { 175 } else {
178 if (_onNoPendingWrites != null) _onNoPendingWrites(); 176 if (_onNoPendingWrites != null) _onNoPendingWrites();
179 } 177 }
180 if (_onNoPendingWrites == null) _socket._onWrite = null; 178 if (_onNoPendingWrites == null) _socket._onWrite = null;
181 } 179 }
182 180
183 void _onError() { 181 void set onError(void callback(Exception e)) {
182 _errorCallback = callback;
183 }
184
185 void _onError(Exception e) {
184 close(); 186 close();
185 if (_streamErrorHandler != null) _streamErrorHandler(); 187 if (_errorCallback != null) _errorCallback(e);
186 } 188 }
187 189
188 Socket _socket; 190 Socket _socket;
189 _BufferList _pendingWrites; 191 _BufferList _pendingWrites;
190 var _onNoPendingWrites; 192 var _onNoPendingWrites;
191 var _streamErrorHandler; 193 Function _clientCloseHandler;
Mads Ager (google) 2012/03/21 13:18:20 Where is the _clientCloseHandler used? It doesn't
Søren Gjesse 2012/03/21 14:39:46 Thanks for spotting this copy/paste error. Removed
194 Function _errorCallback;
192 bool _closing = false; 195 bool _closing = false;
193 bool _closed = false; 196 bool _closed = false;
194 } 197 }
OLDNEW
« no previous file with comments | « runtime/bin/socket_impl.dart ('k') | runtime/bin/stream_util.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698