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

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: Addressed review comments 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 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 59
60 void set onData(void callback()) { 60 void set onData(void callback()) {
61 _socket._onData = callback; 61 _socket._onData = callback;
62 } 62 }
63 63
64 void set onClosed(void callback()) { 64 void set onClosed(void callback()) {
65 _clientCloseHandler = callback; 65 _clientCloseHandler = callback;
66 _socket._onClosed = _onClosed; 66 _socket._onClosed = _onClosed;
67 } 67 }
68 68
69 void set onError(void callback()) {
70 _socket.onError = callback;
71 }
72
73 void _onClosed() { 69 void _onClosed() {
74 _closed = true; 70 _closed = true;
75 if (_clientCloseHandler !== null) { 71 if (_clientCloseHandler !== null) {
76 _clientCloseHandler(); 72 _clientCloseHandler();
77 } 73 }
78 } 74 }
79 75
76 void set onError(void callback(Exception e)) {
77 _errorCallback = callback;
78 }
79
80 void _onError(Exception e) {
81 close();
82 if (_errorCallback != null) _errorCallback(e);
83 }
84
80 Socket _socket; 85 Socket _socket;
86 bool _closed = false;
81 Function _clientCloseHandler; 87 Function _clientCloseHandler;
82 bool _closed = false; 88 Function _errorCallback;
83 } 89 }
84 90
85 91
86 class _SocketOutputStream 92 class _SocketOutputStream
87 extends _BaseOutputStream implements SocketOutputStream { 93 extends _BaseOutputStream implements SocketOutputStream {
88 _SocketOutputStream(Socket socket) 94 _SocketOutputStream(Socket socket)
89 : _socket = socket, _pendingWrites = new _BufferList(); 95 : _socket = socket, _pendingWrites = new _BufferList();
90 96
91 bool write(List<int> buffer, [bool copyBuffer = true]) { 97 bool write(List<int> buffer, [bool copyBuffer = true]) {
92 return _write(buffer, 0, buffer.length, copyBuffer); 98 return _write(buffer, 0, buffer.length, copyBuffer);
(...skipping 23 matching lines...) Expand all
116 _closed = true; 122 _closed = true;
117 } 123 }
118 124
119 void set onNoPendingWrites(void callback()) { 125 void set onNoPendingWrites(void callback()) {
120 _onNoPendingWrites = callback; 126 _onNoPendingWrites = callback;
121 if (_onNoPendingWrites != null) { 127 if (_onNoPendingWrites != null) {
122 _socket._onWrite = _onWrite; 128 _socket._onWrite = _onWrite;
123 } 129 }
124 } 130 }
125 131
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) { 132 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) {
136 if (_closing || _closed) throw new StreamException("Stream closed"); 133 if (_closing || _closed) throw new StreamException("Stream closed");
137 int bytesWritten = 0; 134 int bytesWritten = 0;
138 if (_pendingWrites.isEmpty()) { 135 if (_pendingWrites.isEmpty()) {
139 // If nothing is buffered write as much as possible and buffer 136 // If nothing is buffered write as much as possible and buffer
140 // the rest. 137 // the rest.
141 bytesWritten = _socket.writeList(buffer, offset, len); 138 bytesWritten = _socket.writeList(buffer, offset, len);
142 if (bytesWritten == len) return true; 139 if (bytesWritten == len) return true;
143 } 140 }
144 141
(...skipping 28 matching lines...) Expand all
173 // All buffered data was written. 170 // All buffered data was written.
174 if (_closing) { 171 if (_closing) {
175 _socket._closeWrite(); 172 _socket._closeWrite();
176 _closed = true; 173 _closed = true;
177 } else { 174 } else {
178 if (_onNoPendingWrites != null) _onNoPendingWrites(); 175 if (_onNoPendingWrites != null) _onNoPendingWrites();
179 } 176 }
180 if (_onNoPendingWrites == null) _socket._onWrite = null; 177 if (_onNoPendingWrites == null) _socket._onWrite = null;
181 } 178 }
182 179
183 void _onError() { 180 void set onError(void callback(Exception e)) {
181 _errorCallback = callback;
182 }
183
184 void _onError(Exception e) {
184 close(); 185 close();
185 if (_streamErrorHandler != null) _streamErrorHandler(); 186 if (_errorCallback != null) _errorCallback(e);
186 } 187 }
187 188
188 Socket _socket; 189 Socket _socket;
189 _BufferList _pendingWrites; 190 _BufferList _pendingWrites;
190 var _onNoPendingWrites; 191 var _onNoPendingWrites;
191 var _streamErrorHandler; 192 Function _errorCallback;
192 bool _closing = false; 193 bool _closing = false;
193 bool _closed = false; 194 bool _closed = false;
194 } 195 }
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