Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 } | 49 } |
| 50 | 50 |
| 51 void set errorHandler(void callback()) { | 51 void set errorHandler(void callback()) { |
| 52 _socket.setErrorHandler(callback); | 52 _socket.setErrorHandler(callback); |
| 53 } | 53 } |
| 54 | 54 |
| 55 Socket _socket; | 55 Socket _socket; |
| 56 } | 56 } |
| 57 | 57 |
| 58 | 58 |
| 59 class _BufferList2 { | |
| 60 _BufferList2() { | |
| 61 clear(); | |
| 62 } | |
| 63 | |
| 64 // Adds a new buffer to the list possibly with an offset of the | |
| 65 // first byte of interest. The offset can only be specified if the | |
| 66 // buffer list is empty. | |
| 67 void add(List<int> buffer, [int offset = 0]) { | |
| 68 assert(offset == 0 || _buffers.isEmpty()); | |
| 69 _buffers.addLast(buffer); | |
| 70 _length += buffer.length; | |
| 71 if (offset != 0) _index = offset; | |
| 72 } | |
| 73 | |
| 74 List<int> get first() => _buffers.first(); | |
| 75 int get index() => _index; | |
| 76 | |
| 77 void removeBytes(int count) { | |
| 78 int firstRemaining = first.length - _index; | |
| 79 assert(count <= firstRemaining); | |
| 80 if (count == firstRemaining) { | |
| 81 _buffers.removeFirst(); | |
| 82 _index = 0; | |
| 83 } else { | |
| 84 _index += count; | |
| 85 } | |
| 86 _length -= count; | |
| 87 } | |
| 88 | |
| 89 int get length() => _length; | |
| 90 | |
| 91 bool get empty() => _buffers.isEmpty(); | |
|
Mads Ager (google)
2011/10/31 09:56:48
I don't think empty should be a getter. I would on
Søren Gjesse
2011/11/01 08:56:46
Done.
| |
| 92 | |
| 93 void clear() { | |
| 94 _index = 0; | |
| 95 _length = 0; | |
| 96 _buffers = new Queue(); | |
| 97 } | |
| 98 | |
| 99 int _length; // Total length of pending data. | |
| 100 Queue<List<int>> _buffers; | |
| 101 int _index; // Offset into the first buffer of next write position. | |
| 102 } | |
| 103 | |
| 104 | |
| 59 class SocketOutputStream implements OutputStream { | 105 class SocketOutputStream implements OutputStream { |
| 60 SocketOutputStream(Socket socket) : _socket = socket; | 106 SocketOutputStream(Socket socket) |
| 107 : _socket = socket, _pendingWrites = new _BufferList2() { | |
|
Mads Ager (google)
2011/10/31 09:56:48
Are there two spaces between ":" and "_" here?
Søren Gjesse
2011/11/01 08:56:46
Done.
| |
| 108 _socket.setWriteHandler(_writeHandler); | |
| 109 _socket.setErrorHandler(_errorHandler); | |
|
Mads Ager (google)
2011/10/31 09:56:48
Indentation.
Søren Gjesse
2011/11/01 08:56:46
Done.
| |
| 110 } | |
| 61 | 111 |
| 62 bool write(List<int> buffer, int offset, int len, void callback()) { | 112 bool write(List<int> buffer) { |
| 63 int bytesWritten = _socket.writeList(buffer, offset, len); | 113 return _write(buffer, 0, buffer.length, false); |
| 114 } | |
| 64 | 115 |
| 65 void finishWrite() { | 116 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { |
| 66 bytesWritten += _socket.writeList( | 117 return _write(buffer, offset, (len == null) ? buffer.length : len, true); |
| 67 buffer, offset + bytesWritten, len - bytesWritten); | 118 } |
| 68 if (bytesWritten < len) { | 119 |
| 69 _socket.setWriteHandler(finishWrite); | 120 void end() { |
| 70 } else { | 121 if (_ending || _ended) throw new StreamException("Stream ended"); |
| 71 assert(bytesWritten == len); | 122 _ending = true; |
| 72 if (callback !== null) { | 123 if (_pendingWrites.empty) { |
| 73 callback(); | 124 close(); |
| 74 } | 125 } |
| 75 } | 126 } |
| 127 | |
| 128 void close() { | |
| 129 _socket.setWriteHandler(null); | |
| 130 _pendingWrites.clear(); | |
| 131 _socket.close(); | |
| 132 _ended = true; | |
| 133 } | |
| 134 | |
| 135 void set noPendingWriteHandler(void callback()) { | |
| 136 _noPendingWriteHandler = callback; | |
| 137 _socket.setWriteHandler(_writeHandler); | |
| 138 } | |
| 139 | |
| 140 void set closeHandler(void callback()) { | |
| 141 _socket.setCloseHandler(callback()); | |
| 142 } | |
| 143 | |
| 144 void set errorHandler(void callback()) { | |
| 145 _streamErrorHandler = callback(); | |
| 146 } | |
| 147 | |
| 148 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { | |
| 149 if (_ending || _ended) throw new StreamException("Stream ended"); | |
| 150 if (len == null) len = buffer.length; | |
| 151 int bytesWritten = 0; | |
| 152 if (_pendingWrites.empty) { | |
| 153 // If nothing is buffered write as much as possible and buffer | |
| 154 // the rest. | |
| 155 bytesWritten = _socket.writeList(buffer, offset, len); | |
| 156 if (bytesWritten == len) return true; | |
| 76 } | 157 } |
| 77 | 158 |
| 78 if (bytesWritten == len) { | 159 // Place remaining data on the pending writes queue. |
| 79 return true; | 160 if (copyBuffer) { |
| 161 List<int> newBuffer = | |
| 162 new List.fromList(buffer, offset + bytesWritten, buffer.length); | |
| 163 _pendingWrites.add(newBuffer); | |
| 164 } else { | |
| 165 _pendingWrites.add(buffer, bytesWritten); | |
| 80 } | 166 } |
| 81 _socket.setWriteHandler(finishWrite); | 167 } |
| 82 return false; | 168 |
| 169 void _writeHandler() { | |
| 170 _socket.setWriteHandler(_writeHandler); | |
| 171 // Write as much buffered data to the socket as possible. | |
| 172 while (!_pendingWrites.empty) { | |
| 173 List<int> buffer = _pendingWrites.first; | |
| 174 int offset = _pendingWrites.index; | |
| 175 int bytesToWrite = buffer.length - offset; | |
| 176 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); | |
| 177 _pendingWrites.removeBytes(bytesWritten); | |
| 178 if (bytesWritten < bytesToWrite) return; | |
| 179 } | |
| 180 | |
| 181 // All buffered data was written. | |
| 182 if (_ending) { | |
| 183 _socket.close(); | |
| 184 _ended = true; | |
| 185 } else { | |
| 186 if (_noPendingWriteHandler != null) _noPendingWriteHandler(); | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 void _errorHandler() { | |
| 191 close(); | |
| 192 if (_streamErrorHandler != null) _streamErrorHandler(); | |
| 83 } | 193 } |
| 84 | 194 |
| 85 Socket _socket; | 195 Socket _socket; |
| 196 _BufferList2 _pendingWrites; | |
| 197 bool _ending = false; | |
| 198 bool _ended = false; | |
| 199 var _noPendingWriteHandler; | |
| 200 var _streamErrorHandler; | |
| 86 } | 201 } |
| OLD | NEW |