| 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._closed) _closed = true; | 7 if (_socket._closed) _closed = true; |
| 8 _socket.onClosed = _onClosed; | 8 _socket.onClosed = _onClosed; |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 flush() { | 111 void flush() { |
| 112 // Nothing to do on a socket output stream. | 112 // Nothing to do on a socket output stream. |
| 113 } | 113 } |
| 114 | 114 |
| 115 void close() { | 115 void close() { |
| 116 if (_closing && _closed) return; | 116 if (_closing && _closed) return; |
| 117 if (!_pendingWrites.isEmpty()) { | 117 if (!_pendingWrites.isEmpty) { |
| 118 // Mark the socket for close when all data is written. | 118 // Mark the socket for close when all data is written. |
| 119 _closing = true; | 119 _closing = true; |
| 120 _socket._onWrite = _onWrite; | 120 _socket._onWrite = _onWrite; |
| 121 } else { | 121 } else { |
| 122 // Close the socket for writing. | 122 // Close the socket for writing. |
| 123 _socket._closeWrite(); | 123 _socket._closeWrite(); |
| 124 _closed = true; | 124 _closed = true; |
| 125 // Invoke the callback asynchronously. | 125 // Invoke the callback asynchronously. |
| 126 new Timer(0, (t) { | 126 new Timer(0, (t) { |
| 127 if (_onClosed != null) _onClosed(); | 127 if (_onClosed != null) _onClosed(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 void set onClosed(void callback()) { | 148 void set onClosed(void callback()) { |
| 149 _onClosed = callback; | 149 _onClosed = callback; |
| 150 } | 150 } |
| 151 | 151 |
| 152 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { | 152 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { |
| 153 if (_closing || _closed) throw new StreamException("Stream closed"); | 153 if (_closing || _closed) throw new StreamException("Stream closed"); |
| 154 int bytesWritten = 0; | 154 int bytesWritten = 0; |
| 155 if (_pendingWrites.isEmpty()) { | 155 if (_pendingWrites.isEmpty) { |
| 156 // If nothing is buffered write as much as possible and buffer | 156 // If nothing is buffered write as much as possible and buffer |
| 157 // the rest. | 157 // the rest. |
| 158 bytesWritten = _socket.writeList(buffer, offset, len); | 158 bytesWritten = _socket.writeList(buffer, offset, len); |
| 159 if (bytesWritten == len) return true; | 159 if (bytesWritten == len) return true; |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Place remaining data on the pending writes queue. | 162 // Place remaining data on the pending writes queue. |
| 163 int notWrittenOffset = offset + bytesWritten; | 163 int notWrittenOffset = offset + bytesWritten; |
| 164 if (copyBuffer) { | 164 if (copyBuffer) { |
| 165 List<int> newBuffer = | 165 List<int> newBuffer = |
| 166 buffer.getRange(notWrittenOffset, len - bytesWritten); | 166 buffer.getRange(notWrittenOffset, len - bytesWritten); |
| 167 _pendingWrites.add(newBuffer); | 167 _pendingWrites.add(newBuffer); |
| 168 } else { | 168 } else { |
| 169 assert(offset + len == buffer.length); | 169 assert(offset + len == buffer.length); |
| 170 _pendingWrites.add(buffer, notWrittenOffset); | 170 _pendingWrites.add(buffer, notWrittenOffset); |
| 171 } | 171 } |
| 172 _socket._onWrite = _onWrite; | 172 _socket._onWrite = _onWrite; |
| 173 return false; | 173 return false; |
| 174 } | 174 } |
| 175 | 175 |
| 176 void _onWrite() { | 176 void _onWrite() { |
| 177 // Write as much buffered data to the socket as possible. | 177 // Write as much buffered data to the socket as possible. |
| 178 while (!_pendingWrites.isEmpty()) { | 178 while (!_pendingWrites.isEmpty) { |
| 179 List<int> buffer = _pendingWrites.first; | 179 List<int> buffer = _pendingWrites.first; |
| 180 int offset = _pendingWrites.index; | 180 int offset = _pendingWrites.index; |
| 181 int bytesToWrite = buffer.length - offset; | 181 int bytesToWrite = buffer.length - offset; |
| 182 int bytesWritten; | 182 int bytesWritten; |
| 183 try { | 183 try { |
| 184 bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); | 184 bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); |
| 185 } catch (e) { | 185 } catch (e) { |
| 186 _pendingWrites.clear(); | 186 _pendingWrites.clear(); |
| 187 _onSocketError(e); | 187 _onSocketError(e); |
| 188 return; | 188 return; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 | 223 |
| 224 Socket _socket; | 224 Socket _socket; |
| 225 _BufferList _pendingWrites; | 225 _BufferList _pendingWrites; |
| 226 Function _onNoPendingWrites; | 226 Function _onNoPendingWrites; |
| 227 Function _onClosed; | 227 Function _onClosed; |
| 228 bool _closing = false; | 228 bool _closing = false; |
| 229 bool _closed = false; | 229 bool _closed = false; |
| 230 } | 230 } |
| OLD | NEW |