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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * The [SecureServerSocket] is a server socket, providing a stream of high-level | 8 * The [SecureServerSocket] is a server socket, providing a stream of high-level |
9 * [Socket]s. | 9 * [Socket]s. |
10 * | 10 * |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 * | 55 * |
56 * The optional argument [shared] specifies whether additional | 56 * The optional argument [shared] specifies whether additional |
57 * SecureServerSocket objects can bind to the same combination of `address`, | 57 * SecureServerSocket objects can bind to the same combination of `address`, |
58 * `port` and `v6Only`. If `shared` is `true` and more `SecureServerSocket`s | 58 * `port` and `v6Only`. If `shared` is `true` and more `SecureServerSocket`s |
59 * from this isolate or other isolates are bound to the port, then the | 59 * from this isolate or other isolates are bound to the port, then the |
60 * incoming connections will be distributed among all the bound | 60 * incoming connections will be distributed among all the bound |
61 * `SecureServerSocket`s. Connections can be distributed over multiple | 61 * `SecureServerSocket`s. Connections can be distributed over multiple |
62 * isolates this way. | 62 * isolates this way. |
63 */ | 63 */ |
64 static Future<SecureServerSocket> bind( | 64 static Future<SecureServerSocket> bind( |
65 address, | 65 address, int port, SecurityContext context, |
66 int port, | |
67 SecurityContext context, | |
68 {int backlog: 0, | 66 {int backlog: 0, |
69 bool v6Only: false, | 67 bool v6Only: false, |
70 bool requestClientCertificate: false, | 68 bool requestClientCertificate: false, |
71 bool requireClientCertificate: false, | 69 bool requireClientCertificate: false, |
72 List<String> supportedProtocols, | 70 List<String> supportedProtocols, |
73 bool shared: false}) { | 71 bool shared: false}) { |
74 return RawSecureServerSocket.bind( | 72 return RawSecureServerSocket |
75 address, | 73 .bind(address, port, context, |
76 port, | 74 backlog: backlog, |
77 context, | 75 v6Only: v6Only, |
78 backlog: backlog, | 76 requestClientCertificate: requestClientCertificate, |
79 v6Only: v6Only, | 77 requireClientCertificate: requireClientCertificate, |
80 requestClientCertificate: requestClientCertificate, | 78 supportedProtocols: supportedProtocols, |
81 requireClientCertificate: requireClientCertificate, | 79 shared: shared) |
82 supportedProtocols: supportedProtocols, | 80 .then((serverSocket) => new SecureServerSocket._(serverSocket)); |
83 shared: shared).then( | |
84 (serverSocket) => new SecureServerSocket._(serverSocket)); | |
85 } | 81 } |
86 | 82 |
87 StreamSubscription<SecureSocket> listen(void onData(SecureSocket socket), | 83 StreamSubscription<SecureSocket> listen(void onData(SecureSocket socket), |
88 {Function onError, | 84 {Function onError, void onDone(), bool cancelOnError}) { |
89 void onDone(), | 85 return _socket.map((rawSocket) => new SecureSocket._(rawSocket)).listen( |
90 bool cancelOnError}) { | 86 onData, |
91 return _socket.map((rawSocket) => new SecureSocket._(rawSocket)) | 87 onError: onError, |
92 .listen(onData, | 88 onDone: onDone, |
93 onError: onError, | 89 cancelOnError: cancelOnError); |
94 onDone: onDone, | |
95 cancelOnError: cancelOnError); | |
96 } | 90 } |
97 | 91 |
98 /** | 92 /** |
99 * Returns the port used by this socket. | 93 * Returns the port used by this socket. |
100 */ | 94 */ |
101 int get port => _socket.port; | 95 int get port => _socket.port; |
102 | 96 |
103 /** | 97 /** |
104 * Returns the address used by this socket. | 98 * Returns the address used by this socket. |
105 */ | 99 */ |
106 InternetAddress get address => _socket.address; | 100 InternetAddress get address => _socket.address; |
107 | 101 |
108 /** | 102 /** |
109 * Closes the socket. The returned future completes when the socket | 103 * Closes the socket. The returned future completes when the socket |
110 * is fully closed and is no longer bound. | 104 * is fully closed and is no longer bound. |
111 */ | 105 */ |
112 Future<SecureServerSocket> close() => _socket.close().then((_) => this); | 106 Future<SecureServerSocket> close() => _socket.close().then((_) => this); |
113 | 107 |
114 void set _owner(owner) { _socket._owner = owner; } | 108 void set _owner(owner) { |
| 109 _socket._owner = owner; |
| 110 } |
115 } | 111 } |
116 | 112 |
117 | |
118 /** | 113 /** |
119 * The RawSecureServerSocket is a server socket, providing a stream of low-level | 114 * The RawSecureServerSocket is a server socket, providing a stream of low-level |
120 * [RawSecureSocket]s. | 115 * [RawSecureSocket]s. |
121 * | 116 * |
122 * See [RawSecureSocket] for more info. | 117 * See [RawSecureSocket] for more info. |
123 */ | 118 */ |
124 class RawSecureServerSocket extends Stream<RawSecureSocket> { | 119 class RawSecureServerSocket extends Stream<RawSecureSocket> { |
125 final RawServerSocket _socket; | 120 final RawServerSocket _socket; |
126 StreamController<RawSecureSocket> _controller; | 121 StreamController<RawSecureSocket> _controller; |
127 StreamSubscription<RawSocket> _subscription; | 122 StreamSubscription<RawSocket> _subscription; |
128 final SecurityContext _context; | 123 final SecurityContext _context; |
129 final bool requestClientCertificate; | 124 final bool requestClientCertificate; |
130 final bool requireClientCertificate; | 125 final bool requireClientCertificate; |
131 final List<String> supportedProtocols; | 126 final List<String> supportedProtocols; |
132 bool _closed = false; | 127 bool _closed = false; |
133 | 128 |
134 RawSecureServerSocket._(this._socket, | 129 RawSecureServerSocket._( |
135 this._context, | 130 this._socket, |
136 this.requestClientCertificate, | 131 this._context, |
137 this.requireClientCertificate, | 132 this.requestClientCertificate, |
138 this.supportedProtocols) { | 133 this.requireClientCertificate, |
| 134 this.supportedProtocols) { |
139 _controller = new StreamController<RawSecureSocket>( | 135 _controller = new StreamController<RawSecureSocket>( |
140 sync: true, | 136 sync: true, |
141 onListen: _onSubscriptionStateChange, | 137 onListen: _onSubscriptionStateChange, |
142 onPause: _onPauseStateChange, | 138 onPause: _onPauseStateChange, |
143 onResume: _onPauseStateChange, | 139 onResume: _onPauseStateChange, |
144 onCancel: _onSubscriptionStateChange); | 140 onCancel: _onSubscriptionStateChange); |
145 } | 141 } |
146 | 142 |
147 /** | 143 /** |
148 * Returns a future for a [RawSecureServerSocket]. When the future | 144 * Returns a future for a [RawSecureServerSocket]. When the future |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 * | 179 * |
184 * The optional argument [shared] specifies whether additional | 180 * The optional argument [shared] specifies whether additional |
185 * RawSecureServerSocket objects can bind to the same combination of | 181 * RawSecureServerSocket objects can bind to the same combination of |
186 * `address`, `port` and `v6Only`. If `shared` is `true` and more | 182 * `address`, `port` and `v6Only`. If `shared` is `true` and more |
187 * `RawSecureServerSocket`s from this isolate or other isolates are bound to | 183 * `RawSecureServerSocket`s from this isolate or other isolates are bound to |
188 * the port, then the incoming connections will be distributed among all the | 184 * the port, then the incoming connections will be distributed among all the |
189 * bound `RawSecureServerSocket`s. Connections can be distributed over | 185 * bound `RawSecureServerSocket`s. Connections can be distributed over |
190 * multiple isolates this way. | 186 * multiple isolates this way. |
191 */ | 187 */ |
192 static Future<RawSecureServerSocket> bind( | 188 static Future<RawSecureServerSocket> bind( |
193 address, | 189 address, int port, SecurityContext context, |
194 int port, | |
195 SecurityContext context, | |
196 {int backlog: 0, | 190 {int backlog: 0, |
197 bool v6Only: false, | 191 bool v6Only: false, |
198 bool requestClientCertificate: false, | 192 bool requestClientCertificate: false, |
199 bool requireClientCertificate: false, | 193 bool requireClientCertificate: false, |
200 List<String> supportedProtocols, | 194 List<String> supportedProtocols, |
201 bool shared: false}) { | 195 bool shared: false}) { |
202 return RawServerSocket.bind( | 196 return RawServerSocket |
203 address, port, backlog: backlog, v6Only: v6Only, shared: shared) | 197 .bind(address, port, backlog: backlog, v6Only: v6Only, shared: shared) |
204 .then((serverSocket) => new RawSecureServerSocket._( | 198 .then((serverSocket) => new RawSecureServerSocket._( |
205 serverSocket, | 199 serverSocket, |
206 context, | 200 context, |
207 requestClientCertificate, | 201 requestClientCertificate, |
208 requireClientCertificate, | 202 requireClientCertificate, |
209 supportedProtocols)); | 203 supportedProtocols)); |
210 } | 204 } |
211 | 205 |
212 StreamSubscription<RawSecureSocket> listen(void onData(RawSecureSocket s), | 206 StreamSubscription<RawSecureSocket> listen(void onData(RawSecureSocket s), |
213 {Function onError, | 207 {Function onError, void onDone(), bool cancelOnError}) { |
214 void onDone(), | |
215 bool cancelOnError}) { | |
216 return _controller.stream.listen(onData, | 208 return _controller.stream.listen(onData, |
217 onError: onError, | 209 onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
218 onDone: onDone, | |
219 cancelOnError: cancelOnError); | |
220 } | 210 } |
221 | 211 |
222 /** | 212 /** |
223 * Returns the port used by this socket. | 213 * Returns the port used by this socket. |
224 */ | 214 */ |
225 int get port => _socket.port; | 215 int get port => _socket.port; |
226 | 216 |
227 /** | 217 /** |
228 * Returns the address used by this socket. | 218 * Returns the address used by this socket. |
229 */ | 219 */ |
(...skipping 10 matching lines...) Expand all Loading... |
240 | 230 |
241 void _onData(RawSocket connection) { | 231 void _onData(RawSocket connection) { |
242 var remotePort; | 232 var remotePort; |
243 try { | 233 try { |
244 remotePort = connection.remotePort; | 234 remotePort = connection.remotePort; |
245 } catch (e) { | 235 } catch (e) { |
246 // If connection is already closed, remotePort throws an exception. | 236 // If connection is already closed, remotePort throws an exception. |
247 // Do nothing - connection is closed. | 237 // Do nothing - connection is closed. |
248 return; | 238 return; |
249 } | 239 } |
250 _RawSecureSocket.connect( | 240 _RawSecureSocket |
251 connection.address, | 241 .connect(connection.address, remotePort, |
252 remotePort, | 242 context: _context, |
253 context: _context, | 243 is_server: true, |
254 is_server: true, | 244 socket: connection, |
255 socket: connection, | 245 requestClientCertificate: requestClientCertificate, |
256 requestClientCertificate: requestClientCertificate, | 246 requireClientCertificate: requireClientCertificate, |
257 requireClientCertificate: requireClientCertificate, | 247 supportedProtocols: supportedProtocols) |
258 supportedProtocols: supportedProtocols) | 248 .then((RawSecureSocket secureConnection) { |
259 .then((RawSecureSocket secureConnection) { | |
260 if (_closed) { | 249 if (_closed) { |
261 secureConnection.close(); | 250 secureConnection.close(); |
262 } else { | 251 } else { |
263 _controller.add(secureConnection); | 252 _controller.add(secureConnection); |
264 } | 253 } |
265 }).catchError((e, s) { | 254 }).catchError((e, s) { |
266 if (!_closed) { | 255 if (!_closed) { |
267 _controller.addError(e, s); | 256 _controller.addError(e, s); |
268 } | 257 } |
269 }); | 258 }); |
270 } | 259 } |
271 | 260 |
272 void _onPauseStateChange() { | 261 void _onPauseStateChange() { |
273 if (_controller.isPaused) { | 262 if (_controller.isPaused) { |
274 _subscription.pause(); | 263 _subscription.pause(); |
275 } else { | 264 } else { |
276 _subscription.resume(); | 265 _subscription.resume(); |
277 } | 266 } |
278 } | 267 } |
279 | 268 |
280 void _onSubscriptionStateChange() { | 269 void _onSubscriptionStateChange() { |
281 if (_controller.hasListener) { | 270 if (_controller.hasListener) { |
282 _subscription = _socket.listen(_onData, | 271 _subscription = _socket.listen(_onData, |
283 onError: _controller.addError, | 272 onError: _controller.addError, onDone: _controller.close); |
284 onDone: _controller.close); | |
285 } else { | 273 } else { |
286 close(); | 274 close(); |
287 } | 275 } |
288 } | 276 } |
289 | 277 |
290 void set _owner(owner) { | 278 void set _owner(owner) { |
291 (_socket as dynamic)._owner = owner; | 279 (_socket as dynamic)._owner = owner; |
292 } | 280 } |
293 } | 281 } |
294 | |
295 | |
OLD | NEW |