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.closeHandler = _closeHandler; | 8 _socket.onClosed = _onClosed; |
9 } | 9 } |
10 | 10 |
11 List<int> read([int len]) { | 11 List<int> read([int len]) { |
12 int bytesToRead = available(); | 12 int bytesToRead = available(); |
13 if (bytesToRead == 0) return null; | 13 if (bytesToRead == 0) return null; |
14 if (len !== null) { | 14 if (len !== null) { |
15 if (len <= 0) { | 15 if (len <= 0) { |
16 throw new StreamException("Illegal length $len"); | 16 throw new StreamException("Illegal length $len"); |
17 } else if (bytesToRead > len) { | 17 } else if (bytesToRead > len) { |
18 bytesToRead = len; | 18 bytesToRead = len; |
(...skipping 31 matching lines...) Loading... |
50 } | 50 } |
51 | 51 |
52 void close() { | 52 void close() { |
53 if (!_closed) { | 53 if (!_closed) { |
54 _socket.close(); | 54 _socket.close(); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 bool get closed() => _closed; | 58 bool get closed() => _closed; |
59 | 59 |
60 void set dataHandler(void callback()) { | 60 void set onData(void callback()) { |
61 _socket._dataHandler = callback; | 61 _socket._onData = callback; |
62 } | 62 } |
63 | 63 |
64 void set closeHandler(void callback()) { | 64 void set onClosed(void callback()) { |
65 _clientCloseHandler = callback; | 65 _clientCloseHandler = callback; |
66 _socket._closeHandler = _closeHandler; | 66 _socket._onClosed = _onClosed; |
67 } | 67 } |
68 | 68 |
69 void set errorHandler(void callback()) { | 69 void set onError(void callback()) { |
70 _socket.errorHandler = callback; | 70 _socket.onError = callback; |
71 } | 71 } |
72 | 72 |
73 void _closeHandler() { | 73 void _onClosed() { |
74 _closed = true; | 74 _closed = true; |
75 if (_clientCloseHandler !== null) { | 75 if (_clientCloseHandler !== null) { |
76 _clientCloseHandler(); | 76 _clientCloseHandler(); |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 Socket _socket; | 80 Socket _socket; |
81 Function _clientCloseHandler; | 81 Function _clientCloseHandler; |
82 bool _closed = false; | 82 bool _closed = false; |
83 } | 83 } |
84 | 84 |
85 | 85 |
86 class _SocketOutputStream implements SocketOutputStream { | 86 class _SocketOutputStream implements SocketOutputStream { |
87 _SocketOutputStream(Socket socket) | 87 _SocketOutputStream(Socket socket) |
88 : _socket = socket, _pendingWrites = new _BufferList(); | 88 : _socket = socket, _pendingWrites = new _BufferList(); |
89 | 89 |
90 bool write(List<int> buffer, [bool copyBuffer = true]) { | 90 bool write(List<int> buffer, [bool copyBuffer = true]) { |
91 return _write(buffer, 0, buffer.length, copyBuffer); | 91 return _write(buffer, 0, buffer.length, copyBuffer); |
92 } | 92 } |
93 | 93 |
94 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { | 94 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { |
95 return _write( | 95 return _write( |
96 buffer, offset, (len == null) ? buffer.length - offset : len, true); | 96 buffer, offset, (len == null) ? buffer.length - offset : len, true); |
97 } | 97 } |
98 | 98 |
99 void close() { | 99 void close() { |
100 if (!_pendingWrites.isEmpty()) { | 100 if (!_pendingWrites.isEmpty()) { |
101 // Mark the socket for close when all data is written. | 101 // Mark the socket for close when all data is written. |
102 _closing = true; | 102 _closing = true; |
103 _socket._writeHandler = _writeHandler; | 103 _socket._onWrite = _onWrite; |
104 } else { | 104 } else { |
105 // Close the socket for writing. | 105 // Close the socket for writing. |
106 _socket._closeWrite(); | 106 _socket._closeWrite(); |
107 _closed = true; | 107 _closed = true; |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 void destroy() { | 111 void destroy() { |
112 _socket.writeHandler = null; | 112 _socket.onWrite = null; |
113 _pendingWrites.clear(); | 113 _pendingWrites.clear(); |
114 _socket.close(); | 114 _socket.close(); |
115 _closed = true; | 115 _closed = true; |
116 } | 116 } |
117 | 117 |
118 void set noPendingWriteHandler(void callback()) { | 118 void set onNoPendingWrites(void callback()) { |
119 _noPendingWriteHandler = callback; | 119 _onNoPendingWrites = callback; |
120 if (_noPendingWriteHandler != null) { | 120 if (_onNoPendingWrites != null) { |
121 _socket._writeHandler = _writeHandler; | 121 _socket._onWrite = _onWrite; |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 void set errorHandler(void callback()) { | 125 void set onError(void callback()) { |
126 _streamErrorHandler = callback; | 126 _streamErrorHandler = callback; |
127 if (_streamErrorHandler != null) { | 127 if (_streamErrorHandler != null) { |
128 _socket.errorHandler = _errorHandler; | 128 _socket.onError = _onError; |
129 } else { | 129 } else { |
130 _socket.errorHandler = null; | 130 _socket.onError = null; |
131 } | 131 } |
132 } | 132 } |
133 | 133 |
134 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { | 134 bool _write(List<int> buffer, int offset, int len, bool copyBuffer) { |
135 if (_closing || _closed) throw new StreamException("Stream closed"); | 135 if (_closing || _closed) throw new StreamException("Stream closed"); |
136 int bytesWritten = 0; | 136 int bytesWritten = 0; |
137 if (_pendingWrites.isEmpty()) { | 137 if (_pendingWrites.isEmpty()) { |
138 // If nothing is buffered write as much as possible and buffer | 138 // If nothing is buffered write as much as possible and buffer |
139 // the rest. | 139 // the rest. |
140 bytesWritten = _socket.writeList(buffer, offset, len); | 140 bytesWritten = _socket.writeList(buffer, offset, len); |
141 if (bytesWritten == len) return true; | 141 if (bytesWritten == len) return true; |
142 } | 142 } |
143 | 143 |
144 // Place remaining data on the pending writes queue. | 144 // Place remaining data on the pending writes queue. |
145 int notWrittenOffset = offset + bytesWritten; | 145 int notWrittenOffset = offset + bytesWritten; |
146 if (copyBuffer) { | 146 if (copyBuffer) { |
147 List<int> newBuffer = | 147 List<int> newBuffer = |
148 buffer.getRange(notWrittenOffset, len - bytesWritten); | 148 buffer.getRange(notWrittenOffset, len - bytesWritten); |
149 _pendingWrites.add(newBuffer); | 149 _pendingWrites.add(newBuffer); |
150 } else { | 150 } else { |
151 assert(offset + len == buffer.length); | 151 assert(offset + len == buffer.length); |
152 _pendingWrites.add(buffer, notWrittenOffset); | 152 _pendingWrites.add(buffer, notWrittenOffset); |
153 } | 153 } |
154 _socket._writeHandler = _writeHandler; | 154 _socket._onWrite = _onWrite; |
155 return false; | 155 return false; |
156 } | 156 } |
157 | 157 |
158 void _writeHandler() { | 158 void _onWrite() { |
159 // Write as much buffered data to the socket as possible. | 159 // Write as much buffered data to the socket as possible. |
160 while (!_pendingWrites.isEmpty()) { | 160 while (!_pendingWrites.isEmpty()) { |
161 List<int> buffer = _pendingWrites.first; | 161 List<int> buffer = _pendingWrites.first; |
162 int offset = _pendingWrites.index; | 162 int offset = _pendingWrites.index; |
163 int bytesToWrite = buffer.length - offset; | 163 int bytesToWrite = buffer.length - offset; |
164 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); | 164 int bytesWritten = _socket.writeList(buffer, offset, bytesToWrite); |
165 _pendingWrites.removeBytes(bytesWritten); | 165 _pendingWrites.removeBytes(bytesWritten); |
166 if (bytesWritten < bytesToWrite) { | 166 if (bytesWritten < bytesToWrite) { |
167 _socket._writeHandler = _writeHandler; | 167 _socket._onWrite = _onWrite; |
168 return; | 168 return; |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
172 // All buffered data was written. | 172 // All buffered data was written. |
173 if (_closing) { | 173 if (_closing) { |
174 _socket._closeWrite(); | 174 _socket._closeWrite(); |
175 _closed = true; | 175 _closed = true; |
176 } else { | 176 } else { |
177 if (_noPendingWriteHandler != null) _noPendingWriteHandler(); | 177 if (_onNoPendingWrites != null) _onNoPendingWrites(); |
178 } | 178 } |
179 if (_noPendingWriteHandler == null) _socket._writeHandler = null; | 179 if (_onNoPendingWrites == null) _socket._onWrite = null; |
180 } | 180 } |
181 | 181 |
182 void _errorHandler() { | 182 void _onError() { |
183 close(); | 183 close(); |
184 if (_streamErrorHandler != null) _streamErrorHandler(); | 184 if (_streamErrorHandler != null) _streamErrorHandler(); |
185 } | 185 } |
186 | 186 |
187 Socket _socket; | 187 Socket _socket; |
188 _BufferList _pendingWrites; | 188 _BufferList _pendingWrites; |
189 var _noPendingWriteHandler; | 189 var _onNoPendingWrites; |
190 var _streamErrorHandler; | 190 var _streamErrorHandler; |
191 bool _closing = false; | 191 bool _closing = false; |
192 bool _closed = false; | 192 bool _closed = false; |
193 } | 193 } |
OLD | NEW |