| OLD | NEW |
| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 105 |
| 106 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { | 106 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { |
| 107 return _write( | 107 return _write( |
| 108 buffer, offset, (len == null) ? buffer.length - offset : len, true); | 108 buffer, offset, (len == null) ? buffer.length - offset : len, true); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void close() { | 111 void close() { |
| 112 if (!_pendingWrites.isEmpty()) { | 112 if (!_pendingWrites.isEmpty()) { |
| 113 // Mark the socket for close when all data is written. | 113 // Mark the socket for close when all data is written. |
| 114 _closing = true; | 114 _closing = true; |
| 115 _setupWriteHander(); | 115 _socket._onWrite = _onWrite; |
| 116 } else { | 116 } else { |
| 117 // Close the socket for writing. | 117 // Close the socket for writing. |
| 118 _socket._closeWrite(); | 118 _socket._closeWrite(); |
| 119 _closed = true; | 119 _closed = true; |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 void destroy() { | 123 void destroy() { |
| 124 _socket.onWrite = null; | 124 _socket.onWrite = null; |
| 125 _pendingWrites.clear(); | 125 _pendingWrites.clear(); |
| 126 _socket.close(); | 126 _socket.close(); |
| 127 _closed = true; | 127 _closed = true; |
| 128 } | 128 } |
| 129 | 129 |
| 130 void set onNoPendingWrites(void callback()) { | 130 void set onNoPendingWrites(void callback()) { |
| 131 if (_noPendingWritesTimer != null) { | |
| 132 _noPendingWritesTimer.cancel(); | |
| 133 _noPendingWritesTimer = null; | |
| 134 } | |
| 135 _onNoPendingWrites = callback; | 131 _onNoPendingWrites = callback; |
| 136 if (_onNoPendingWrites != null) { | 132 if (_onNoPendingWrites != null) { |
| 137 if (_pendingWrites.isEmpty()) { | 133 _socket._onWrite = _onWrite; |
| 138 _noPendingWritesTimer = new Timer(0, (t) { | |
| 139 if (_onNoPendingWrites != null) _onNoPendingWrites(); | |
| 140 }); | |
| 141 } else { | |
| 142 _setupWriteHander(); | |
| 143 } | |
| 144 } | 134 } |
| 145 } | 135 } |
| 146 | 136 |
| 147 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { | 137 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { |
| 148 if (_closing || _closed) throw new StreamException("Stream closed"); | 138 if (_closing || _closed) throw new StreamException("Stream closed"); |
| 149 int bytesWritten = 0; | 139 int bytesWritten = 0; |
| 150 if (_pendingWrites.isEmpty()) { | 140 if (_pendingWrites.isEmpty()) { |
| 151 // If nothing is buffered write as much as possible and buffer | 141 // If nothing is buffered write as much as possible and buffer |
| 152 // the rest. | 142 // the rest. |
| 153 bytesWritten = _socket.writeList(buffer, offset, len); | 143 bytesWritten = _socket.writeList(buffer, offset, len); |
| 154 if (bytesWritten == len) return true; | 144 if (bytesWritten == len) return true; |
| 155 } | 145 } |
| 156 | 146 |
| 157 // Place remaining data on the pending writes queue. | 147 // Place remaining data on the pending writes queue. |
| 158 int notWrittenOffset = offset + bytesWritten; | 148 int notWrittenOffset = offset + bytesWritten; |
| 159 if (copyBuffer) { | 149 if (copyBuffer) { |
| 160 List<int> newBuffer = | 150 List<int> newBuffer = |
| 161 buffer.getRange(notWrittenOffset, len - bytesWritten); | 151 buffer.getRange(notWrittenOffset, len - bytesWritten); |
| 162 _pendingWrites.add(newBuffer); | 152 _pendingWrites.add(newBuffer); |
| 163 } else { | 153 } else { |
| 164 assert(offset + len == buffer.length); | 154 assert(offset + len == buffer.length); |
| 165 _pendingWrites.add(buffer, notWrittenOffset); | 155 _pendingWrites.add(buffer, notWrittenOffset); |
| 166 } | 156 } |
| 167 _setupWriteHander(); | 157 _socket._onWrite = _onWrite; |
| 168 return false; | 158 return false; |
| 169 } | 159 } |
| 170 | 160 |
| 171 void _onWrite() { | 161 void _onWrite() { |
| 172 // Write as much buffered data to the socket as possible. | 162 // Write as much buffered data to the socket as possible. |
| 173 while (!_pendingWrites.isEmpty()) { | 163 while (!_pendingWrites.isEmpty()) { |
| 174 List<int> buffer = _pendingWrites.first; | 164 List<int> buffer = _pendingWrites.first; |
| 175 int offset = _pendingWrites.index; | 165 int offset = _pendingWrites.index; |
| 176 int bytesToWrite = buffer.length - offset; | 166 int bytesToWrite = buffer.length - offset; |
| 177 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); | 167 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 189 } else { | 179 } else { |
| 190 if (_onNoPendingWrites != null) _onNoPendingWrites(); | 180 if (_onNoPendingWrites != null) _onNoPendingWrites(); |
| 191 } | 181 } |
| 192 if (_onNoPendingWrites == null) { | 182 if (_onNoPendingWrites == null) { |
| 193 _socket._onWrite = null; | 183 _socket._onWrite = null; |
| 194 } else { | 184 } else { |
| 195 _socket._onWrite = _onWrite; | 185 _socket._onWrite = _onWrite; |
| 196 } | 186 } |
| 197 } | 187 } |
| 198 | 188 |
| 199 void _setupWriteHander() { | |
| 200 // Set up the callback for writing the pending data as the | |
| 201 // underlying socket becomes ready for writing. | |
| 202 if (_noPendingWritesTimer != null) { | |
| 203 _noPendingWritesTimer.cancel(); | |
| 204 _noPendingWritesTimer = null; | |
| 205 } | |
| 206 _socket._onWrite = _onWrite; | |
| 207 } | |
| 208 | |
| 209 bool _onSocketError(e) { | 189 bool _onSocketError(e) { |
| 210 close(); | 190 close(); |
| 211 if (_onError != null) { | 191 if (_onError != null) { |
| 212 _onError(e); | 192 _onError(e); |
| 213 return true; | 193 return true; |
| 214 } else { | 194 } else { |
| 215 return false; | 195 return false; |
| 216 } | 196 } |
| 217 } | 197 } |
| 218 | 198 |
| 219 Socket _socket; | 199 Socket _socket; |
| 220 _BufferList _pendingWrites; | 200 _BufferList _pendingWrites; |
| 221 Function _onNoPendingWrites; | 201 Function _onNoPendingWrites; |
| 222 Timer _noPendingWritesTimer; | |
| 223 bool _closing = false; | 202 bool _closing = false; |
| 224 bool _closed = false; | 203 bool _closed = false; |
| 225 } | 204 } |
| OLD | NEW |