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

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

Issue 8386029: Consistently use setters to set event handlers in native APIs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month 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') | samples/chat/http_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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 InputStream { 5 class SocketInputStream implements InputStream {
6 SocketInputStream(Socket socket) { 6 SocketInputStream(Socket socket) {
7 _socket = socket; 7 _socket = socket;
8 } 8 }
9 9
10 List<int> read([int len]) { 10 List<int> read([int len]) {
(...skipping 23 matching lines...) Expand all
34 if (offset < 0) throw new StreamException("Illegal offset $offset"); 34 if (offset < 0) throw new StreamException("Illegal offset $offset");
35 if (len < 0) throw new StreamException("Illegal length $len"); 35 if (len < 0) throw new StreamException("Illegal length $len");
36 return _socket.readList(buffer, offset, len); 36 return _socket.readList(buffer, offset, len);
37 } 37 }
38 38
39 int available() { 39 int available() {
40 return _socket.available(); 40 return _socket.available();
41 } 41 }
42 42
43 void set dataHandler(void callback()) { 43 void set dataHandler(void callback()) {
44 _socket.setDataHandler(callback); 44 _socket.dataHandler = callback;
45 } 45 }
46 46
47 void set closeHandler(void callback()) { 47 void set closeHandler(void callback()) {
48 _socket.setCloseHandler(callback); 48 _socket.closeHandler = callback;
49 } 49 }
50 50
51 void set errorHandler(void callback()) { 51 void set errorHandler(void callback()) {
52 _socket.setErrorHandler(callback); 52 _socket.errorHandler = callback;
53 } 53 }
54 54
55 Socket _socket; 55 Socket _socket;
56 } 56 }
57 57
58 58
59 class _BufferList2 { 59 class _BufferList2 {
60 _BufferList2() { 60 _BufferList2() {
61 clear(); 61 clear();
62 } 62 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 98
99 int _length; // Total length of pending data. 99 int _length; // Total length of pending data.
100 Queue<List<int>> _buffers; 100 Queue<List<int>> _buffers;
101 int _index; // Offset into the first buffer of next write position. 101 int _index; // Offset into the first buffer of next write position.
102 } 102 }
103 103
104 104
105 class SocketOutputStream implements OutputStream { 105 class SocketOutputStream implements OutputStream {
106 SocketOutputStream(Socket socket) 106 SocketOutputStream(Socket socket)
107 : _socket = socket, _pendingWrites = new _BufferList2() { 107 : _socket = socket, _pendingWrites = new _BufferList2() {
108 _socket.setWriteHandler(_writeHandler); 108 _socket.writeHandler = _writeHandler;
109 _socket.setErrorHandler(_errorHandler); 109 _socket.errorHandler = _errorHandler;
110 } 110 }
111 111
112 bool write(List<int> buffer) { 112 bool write(List<int> buffer) {
113 return _write(buffer, 0, buffer.length, false); 113 return _write(buffer, 0, buffer.length, false);
114 } 114 }
115 115
116 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { 116 bool writeFrom(List<int> buffer, [int offset = 0, int len]) {
117 return _write(buffer, offset, (len == null) ? buffer.length : len, true); 117 return _write(buffer, offset, (len == null) ? buffer.length : len, true);
118 } 118 }
119 119
120 void end() { 120 void end() {
121 if (_ending || _ended) throw new StreamException("Stream ended"); 121 if (_ending || _ended) throw new StreamException("Stream ended");
122 _ending = true; 122 _ending = true;
123 if (_pendingWrites.isEmpty()) { 123 if (_pendingWrites.isEmpty()) {
124 close(); 124 close();
125 } 125 }
126 } 126 }
127 127
128 void close() { 128 void close() {
129 _socket.setWriteHandler(null); 129 _socket.writeHandler = null;
130 _pendingWrites.clear(); 130 _pendingWrites.clear();
131 _socket.close(); 131 _socket.close();
132 _ended = true; 132 _ended = true;
133 } 133 }
134 134
135 void set noPendingWriteHandler(void callback()) { 135 void set noPendingWriteHandler(void callback()) {
136 _noPendingWriteHandler = callback; 136 _noPendingWriteHandler = callback;
137 _socket.setWriteHandler(_writeHandler); 137 _socket.writeHandler = _writeHandler;
138 } 138 }
139 139
140 void set closeHandler(void callback()) { 140 void set closeHandler(void callback()) {
141 _socket.setCloseHandler(callback()); 141 _socket.closeHandler = callback;
142 } 142 }
143 143
144 void set errorHandler(void callback()) { 144 void set errorHandler(void callback()) {
145 _streamErrorHandler = callback(); 145 _streamErrorHandler = callback;
146 } 146 }
147 147
148 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { 148 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) {
149 if (_ending || _ended) throw new StreamException("Stream ended"); 149 if (_ending || _ended) throw new StreamException("Stream ended");
150 if (len == null) len = buffer.length; 150 if (len == null) len = buffer.length;
151 int bytesWritten = 0; 151 int bytesWritten = 0;
152 if (_pendingWrites.isEmpty()) { 152 if (_pendingWrites.isEmpty()) {
153 // If nothing is buffered write as much as possible and buffer 153 // If nothing is buffered write as much as possible and buffer
154 // the rest. 154 // the rest.
155 bytesWritten = _socket.writeList(buffer, offset, len); 155 bytesWritten = _socket.writeList(buffer, offset, len);
156 if (bytesWritten == len) return true; 156 if (bytesWritten == len) return true;
157 } 157 }
158 158
159 // Place remaining data on the pending writes queue. 159 // Place remaining data on the pending writes queue.
160 if (copyBuffer) { 160 if (copyBuffer) {
161 List<int> newBuffer = 161 List<int> newBuffer =
162 buffer.getRange(offset + bytesWritten, buffer.length); 162 buffer.getRange(offset + bytesWritten, buffer.length);
163 _pendingWrites.add(newBuffer); 163 _pendingWrites.add(newBuffer);
164 } else { 164 } else {
165 _pendingWrites.add(buffer, bytesWritten); 165 _pendingWrites.add(buffer, bytesWritten);
166 } 166 }
167 } 167 }
168 168
169 void _writeHandler() { 169 void _writeHandler() {
170 _socket.setWriteHandler(_writeHandler); 170 _socket.writeHandler = _writeHandler;
171 // Write as much buffered data to the socket as possible. 171 // Write as much buffered data to the socket as possible.
172 while (!_pendingWrites.isEmpty()) { 172 while (!_pendingWrites.isEmpty()) {
173 List<int> buffer = _pendingWrites.first; 173 List<int> buffer = _pendingWrites.first;
174 int offset = _pendingWrites.index; 174 int offset = _pendingWrites.index;
175 int bytesToWrite = buffer.length - offset; 175 int bytesToWrite = buffer.length - offset;
176 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); 176 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite);
177 _pendingWrites.removeBytes(bytesWritten); 177 _pendingWrites.removeBytes(bytesWritten);
178 if (bytesWritten < bytesToWrite) return; 178 if (bytesWritten < bytesToWrite) return;
179 } 179 }
180 180
(...skipping 11 matching lines...) Expand all
192 if (_streamErrorHandler != null) _streamErrorHandler(); 192 if (_streamErrorHandler != null) _streamErrorHandler();
193 } 193 }
194 194
195 Socket _socket; 195 Socket _socket;
196 _BufferList2 _pendingWrites; 196 _BufferList2 _pendingWrites;
197 bool _ending = false; 197 bool _ending = false;
198 bool _ended = false; 198 bool _ended = false;
199 var _noPendingWriteHandler; 199 var _noPendingWriteHandler;
200 var _streamErrorHandler; 200 var _streamErrorHandler;
201 } 201 }
OLDNEW
« no previous file with comments | « runtime/bin/socket_impl.dart ('k') | samples/chat/http_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698