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) : _socket = socket; |
7 _socket = socket; | |
8 } | |
9 | 7 |
10 List<int> read([int len]) { | 8 List<int> read([int len]) { |
11 int bytesToRead = available(); | 9 int bytesToRead = available(); |
12 if (bytesToRead == 0) return null; | 10 if (bytesToRead == 0) return null; |
13 if (len !== null) { | 11 if (len !== null) { |
14 if (len <= 0) { | 12 if (len <= 0) { |
15 throw new StreamException("Illegal length $len"); | 13 throw new StreamException("Illegal length $len"); |
16 } else if (bytesToRead > len) { | 14 } else if (bytesToRead > len) { |
17 bytesToRead = len; | 15 bytesToRead = len; |
18 } | 16 } |
(...skipping 30 matching lines...) Expand all Loading... |
49 void set errorHandler(void callback()) { | 47 void set errorHandler(void callback()) { |
50 _socket.errorHandler = callback; | 48 _socket.errorHandler = callback; |
51 } | 49 } |
52 | 50 |
53 Socket _socket; | 51 Socket _socket; |
54 } | 52 } |
55 | 53 |
56 | 54 |
57 class SocketOutputStream implements OutputStream { | 55 class SocketOutputStream implements OutputStream { |
58 SocketOutputStream(Socket socket) | 56 SocketOutputStream(Socket socket) |
59 : _socket = socket, _pendingWrites = new _BufferList() { | 57 : _socket = socket, _pendingWrites = new _BufferList(); |
60 _socket.writeHandler = _writeHandler; | |
61 _socket.errorHandler = _errorHandler; | |
62 } | |
63 | 58 |
64 bool write(List<int> buffer, [bool copyBuffer = true]) { | 59 bool write(List<int> buffer, [bool copyBuffer = true]) { |
65 return _write(buffer, 0, buffer.length, copyBuffer); | 60 return _write(buffer, 0, buffer.length, copyBuffer); |
66 } | 61 } |
67 | 62 |
68 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { | 63 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { |
69 return _write( | 64 return _write( |
70 buffer, offset, (len == null) ? buffer.length - offset : len, true); | 65 buffer, offset, (len == null) ? buffer.length - offset : len, true); |
71 } | 66 } |
72 | 67 |
(...skipping 11 matching lines...) Expand all Loading... |
84 | 79 |
85 void destroy() { | 80 void destroy() { |
86 _socket.writeHandler = null; | 81 _socket.writeHandler = null; |
87 _pendingWrites.clear(); | 82 _pendingWrites.clear(); |
88 _socket.close(); | 83 _socket.close(); |
89 _closed = true; | 84 _closed = true; |
90 } | 85 } |
91 | 86 |
92 void set noPendingWriteHandler(void callback()) { | 87 void set noPendingWriteHandler(void callback()) { |
93 _noPendingWriteHandler = callback; | 88 _noPendingWriteHandler = callback; |
94 _socket.writeHandler = _writeHandler; | 89 if (_noPendingWriteHandler != null) { |
| 90 _socket.writeHandler = _writeHandler; |
| 91 } |
95 } | 92 } |
96 | 93 |
97 void set closeHandler(void callback()) { | 94 void set closeHandler(void callback()) { |
98 _socket.closeHandler = callback; | 95 _socket.closeHandler = callback; |
99 } | 96 } |
100 | 97 |
101 void set errorHandler(void callback()) { | 98 void set errorHandler(void callback()) { |
102 _streamErrorHandler = callback; | 99 _streamErrorHandler = callback; |
| 100 if (_streamErrorHandler != null) { |
| 101 _socket.errorHandler = _errorHandler; |
| 102 } else { |
| 103 _socket.errorHandler = null; |
| 104 } |
103 } | 105 } |
104 | 106 |
105 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { | 107 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { |
106 if (_closing || _closed) throw new StreamException("Stream closed"); | 108 if (_closing || _closed) throw new StreamException("Stream closed"); |
107 int bytesWritten = 0; | 109 int bytesWritten = 0; |
108 if (_pendingWrites.isEmpty()) { | 110 if (_pendingWrites.isEmpty()) { |
109 // If nothing is buffered write as much as possible and buffer | 111 // If nothing is buffered write as much as possible and buffer |
110 // the rest. | 112 // the rest. |
111 bytesWritten = _socket.writeList(buffer, offset, len); | 113 bytesWritten = _socket.writeList(buffer, offset, len); |
112 if (bytesWritten == len) return true; | 114 if (bytesWritten == len) return true; |
113 } | 115 } |
114 | 116 |
115 // Place remaining data on the pending writes queue. | 117 // Place remaining data on the pending writes queue. |
116 int notWrittenOffset = offset + bytesWritten; | 118 int notWrittenOffset = offset + bytesWritten; |
117 if (copyBuffer) { | 119 if (copyBuffer) { |
118 List<int> newBuffer = | 120 List<int> newBuffer = |
119 buffer.getRange(notWrittenOffset, len - bytesWritten); | 121 buffer.getRange(notWrittenOffset, len - bytesWritten); |
120 _pendingWrites.add(newBuffer); | 122 _pendingWrites.add(newBuffer); |
121 } else { | 123 } else { |
122 assert(offset + len == buffer.length); | 124 assert(offset + len == buffer.length); |
123 _pendingWrites.add(buffer, notWrittenOffset); | 125 _pendingWrites.add(buffer, notWrittenOffset); |
124 } | 126 } |
| 127 _socket.writeHandler = _writeHandler; |
125 } | 128 } |
126 | 129 |
127 void _writeHandler() { | 130 void _writeHandler() { |
128 // Write as much buffered data to the socket as possible. | 131 // Write as much buffered data to the socket as possible. |
129 while (!_pendingWrites.isEmpty()) { | 132 while (!_pendingWrites.isEmpty()) { |
130 List<int> buffer = _pendingWrites.first; | 133 List<int> buffer = _pendingWrites.first; |
131 int offset = _pendingWrites.index; | 134 int offset = _pendingWrites.index; |
132 int bytesToWrite = buffer.length - offset; | 135 int bytesToWrite = buffer.length - offset; |
133 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); | 136 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); |
134 _pendingWrites.removeBytes(bytesWritten); | 137 _pendingWrites.removeBytes(bytesWritten); |
135 if (bytesWritten < bytesToWrite) { | 138 if (bytesWritten < bytesToWrite) { |
136 _socket.writeHandler = _writeHandler; | 139 _socket.writeHandler = _writeHandler; |
137 return; | 140 return; |
138 } | 141 } |
139 } | 142 } |
140 | 143 |
141 // All buffered data was written. | 144 // All buffered data was written. |
142 if (_closing) { | 145 if (_closing) { |
143 _socket._closeWrite(); | 146 _socket._closeWrite(); |
144 _closed = true; | 147 _closed = true; |
145 } else { | 148 } else { |
146 if (_noPendingWriteHandler != null) _noPendingWriteHandler(); | 149 if (_noPendingWriteHandler != null) _noPendingWriteHandler(); |
147 } | 150 } |
| 151 if (_noPendingWriteHandler == null) _socket.writeHandler = null; |
148 } | 152 } |
149 | 153 |
150 void _errorHandler() { | 154 void _errorHandler() { |
151 close(); | 155 close(); |
152 if (_streamErrorHandler != null) _streamErrorHandler(); | 156 if (_streamErrorHandler != null) _streamErrorHandler(); |
153 } | 157 } |
154 | 158 |
155 Socket _socket; | 159 Socket _socket; |
156 _BufferList _pendingWrites; | 160 _BufferList _pendingWrites; |
157 var _noPendingWriteHandler; | 161 var _noPendingWriteHandler; |
158 var _streamErrorHandler; | 162 var _streamErrorHandler; |
159 bool _closing = false; | 163 bool _closing = false; |
160 bool _closed = false; | 164 bool _closed = false; |
161 } | 165 } |
OLD | NEW |