OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // | 5 // |
6 // Implementation of Socket and RawSocket for Mojo. | 6 // Implementation of Socket and RawSocket for Mojo. |
7 // | 7 // |
8 | 8 |
9 patch class Socket { | 9 patch class Socket { |
10 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { | 10 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { |
(...skipping 16 matching lines...) Expand all Loading... |
27 // Constructing a new MojoDataPipe allocates two handles. All failure paths | 27 // Constructing a new MojoDataPipe allocates two handles. All failure paths |
28 // must be sure that these handles are closed so we do not leak any handles. | 28 // must be sure that these handles are closed so we do not leak any handles. |
29 final _pipeOut = new MojoDataPipe(); | 29 final _pipeOut = new MojoDataPipe(); |
30 bool _outClosed = false; | 30 bool _outClosed = false; |
31 // Constructing a new MojoDataPipe allocates two handles. All failure paths | 31 // Constructing a new MojoDataPipe allocates two handles. All failure paths |
32 // must be sure that these handles are closed so we do not leak any handles. | 32 // must be sure that these handles are closed so we do not leak any handles. |
33 final _pipeIn = new MojoDataPipe(); | 33 final _pipeIn = new MojoDataPipe(); |
34 bool _inClosed = false; | 34 bool _inClosed = false; |
35 bool _readEventsEnabled = true; | 35 bool _readEventsEnabled = true; |
36 bool _writeEventsEnabled = true; | 36 bool _writeEventsEnabled = true; |
37 MojoEventStream _pipeOutEvents; | 37 MojoEventSubscription _pipeOutEvents; |
38 MojoEventStream _pipeInEvents; | 38 MojoEventSubscription _pipeInEvents; |
39 InternetAddress _localAddress; | 39 InternetAddress _localAddress; |
40 int _localPort; | 40 int _localPort; |
41 InternetAddress _remoteAddress; | 41 InternetAddress _remoteAddress; |
42 int _remotePort; | 42 int _remotePort; |
43 var _owner; | 43 var _owner; |
44 | 44 |
45 bool _trace = false; | 45 bool _trace = false; |
46 int _traceId; | 46 int _traceId; |
47 | 47 |
48 _tracePrint(String message) { | 48 _tracePrint(String message) { |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 if (_trace) { | 260 if (_trace) { |
261 _tracePrint('<- READ_CLOSED'); | 261 _tracePrint('<- READ_CLOSED'); |
262 } | 262 } |
263 _controller.add(RawSocketEvent.READ_CLOSED); | 263 _controller.add(RawSocketEvent.READ_CLOSED); |
264 // Once we are closed, stop reporting events. | 264 // Once we are closed, stop reporting events. |
265 _inClosed = true; | 265 _inClosed = true; |
266 return; | 266 return; |
267 } | 267 } |
268 } | 268 } |
269 | 269 |
270 _onInputError(e, st) { | |
271 _controller.addError(e); | |
272 _onInputDone(); | |
273 } | |
274 | |
275 _onInputDone() { | |
276 if (_inClosed) { | |
277 return; | |
278 } | |
279 if (_trace) { | |
280 _tracePrint('<- READ_CLOSED (done)'); | |
281 } | |
282 _controller.add(RawSocketEvent.READ_CLOSED); | |
283 _inClosed = true; | |
284 } | |
285 | |
286 _onOutputData(List<int> event) { | 270 _onOutputData(List<int> event) { |
287 if (_outClosed) { | 271 if (_outClosed) { |
288 return; | 272 return; |
289 } | 273 } |
290 var signalsWatched = new MojoHandleSignals(event[0]); | 274 var signalsWatched = new MojoHandleSignals(event[0]); |
291 var signalsReceived = new MojoHandleSignals(event[1]); | 275 var signalsReceived = new MojoHandleSignals(event[1]); |
292 if (_trace) { | 276 if (_trace) { |
293 _tracePrint('<- OUT: ${signalsReceived}'); | 277 _tracePrint('<- OUT: ${signalsReceived}'); |
294 } | 278 } |
295 if (signalsReceived.isPeerClosed) { | 279 if (signalsReceived.isPeerClosed) { |
296 if (_trace) { | 280 if (_trace) { |
297 _tracePrint('<- CLOSED'); | 281 _tracePrint('<- CLOSED'); |
298 } | 282 } |
299 _controller.add(RawSocketEvent.CLOSED); | 283 _controller.add(RawSocketEvent.CLOSED); |
300 // Once we are closed, stop reporting events. | 284 // Once we are closed, stop reporting events. |
301 _outClosed = true; | 285 _outClosed = true; |
302 return; | 286 return; |
303 } | 287 } |
304 if (signalsReceived.isWritable) { | 288 if (signalsReceived.isWritable) { |
305 if (_trace) { | 289 if (_trace) { |
306 _tracePrint('<- WRITE'); | 290 _tracePrint('<- WRITE'); |
307 } | 291 } |
308 _controller.add(RawSocketEvent.WRITE); | 292 _controller.add(RawSocketEvent.WRITE); |
309 } | 293 } |
310 } | 294 } |
311 | 295 |
312 _onOutputError(e, st) { | |
313 _controller.addError(e); | |
314 _onOutputDone(); | |
315 } | |
316 | |
317 _onOutputDone() { | |
318 if (_outClosed) { | |
319 return; | |
320 } | |
321 if (_trace) { | |
322 _tracePrint('<- CLOSED (done)'); | |
323 } | |
324 _controller.add(RawSocketEvent.CLOSED); | |
325 _outClosed = true; | |
326 } | |
327 | |
328 _setupIn() { | 296 _setupIn() { |
329 assert(_pipeInEvents == null); | 297 assert(_pipeInEvents == null); |
330 _pipeInEvents = new MojoEventStream(_pipeIn.consumer.handle, | 298 _pipeInEvents = new MojoEventSubscription(_pipeIn.consumer.handle, |
331 MojoHandleSignals.READABLE + | 299 MojoHandleSignals.READABLE + |
332 MojoHandleSignals.PEER_CLOSED); | 300 MojoHandleSignals.PEER_CLOSED); |
333 _pipeInEvents.listen(_onInputData, | 301 _pipeInEvents.subscribe(_onInputData); |
334 onError: _onInputError, | |
335 onDone: _onInputDone); | |
336 } | 302 } |
337 | 303 |
338 _setupOut() { | 304 _setupOut() { |
339 assert(_pipeOutEvents == null); | 305 assert(_pipeOutEvents == null); |
340 _pipeOutEvents = new MojoEventStream(_pipeOut.producer.handle, | 306 _pipeOutEvents = new MojoEventSubscription(_pipeOut.producer.handle, |
341 MojoHandleSignals.WRITABLE + | 307 MojoHandleSignals.WRITABLE + |
342 MojoHandleSignals.PEER_CLOSED); | 308 MojoHandleSignals.PEER_CLOSED); |
343 _pipeOutEvents.listen(_onOutputData, | 309 _pipeOutEvents.subscribe(_onOutputData); |
344 onError: _onOutputError, | |
345 onDone: _onOutputDone); | |
346 } | 310 } |
347 | 311 |
348 _shutdownIn([bool force = false]) { | 312 _shutdownIn([bool force = false]) { |
349 _inClosed = true; | 313 _inClosed = true; |
350 if (_trace) { | 314 if (_trace) { |
351 _tracePrint('shutdown IN'); | 315 _tracePrint('shutdown IN'); |
352 _tracePipeIn(); | 316 _tracePipeIn(); |
353 } | 317 } |
354 if (_pipeInEvents != null) { | 318 if (_pipeInEvents != null) { |
355 if (force) { | 319 if (force) { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 | 455 |
492 StreamSubscription<RawSocketEvent> listen(void onData(RawSocketEvent event), | 456 StreamSubscription<RawSocketEvent> listen(void onData(RawSocketEvent event), |
493 {Function onError, | 457 {Function onError, |
494 void onDone(), | 458 void onDone(), |
495 bool cancelOnError}) { | 459 bool cancelOnError}) { |
496 return _controller.stream.listen(onData, onError: onError, onDone: onDone, | 460 return _controller.stream.listen(onData, onError: onError, onDone: onDone, |
497 cancelOnError: cancelOnError); | 461 cancelOnError: cancelOnError); |
498 } | 462 } |
499 | 463 |
500 | 464 |
501 static _enableReadEvents(MojoEventStream stream) { | 465 static _enableReadEvents(MojoEventSubscription subscription) { |
502 if (stream == null) { | 466 if (subscription == null) { |
503 return; | 467 return; |
504 } | 468 } |
505 stream.enableSignals(MojoHandleSignals.PEER_CLOSED + | 469 subscription.enableSignals(MojoHandleSignals.PEER_CLOSED + |
506 MojoHandleSignals.READABLE); | 470 MojoHandleSignals.READABLE); |
507 } | 471 } |
508 | 472 |
509 static _enableWriteEvents(MojoEventStream stream) { | 473 static _enableWriteEvents(MojoEventSubscription subscription) { |
510 if (stream == null) { | 474 if (subscription == null) { |
511 return; | 475 return; |
512 } | 476 } |
513 stream.enableSignals(MojoHandleSignals.PEER_CLOSED + | 477 subscription.enableSignals(MojoHandleSignals.PEER_CLOSED + |
514 MojoHandleSignals.WRITABLE); | 478 MojoHandleSignals.WRITABLE); |
515 } | 479 } |
516 | 480 |
517 static _disableEvents(MojoEventStream stream) { | 481 static _disableEvents(MojoEventSubscription subscription) { |
518 if (stream == null) { | 482 if (subscription == null) { |
519 return; | 483 return; |
520 } | 484 } |
521 stream.enableSignals(MojoHandleSignals.PEER_CLOSED); | 485 subscription.enableSignals(MojoHandleSignals.PEER_CLOSED); |
522 } | 486 } |
523 | 487 |
524 _pause() { | 488 _pause() { |
525 _disableEvents(_pipeInEvents); | 489 _disableEvents(_pipeInEvents); |
526 _disableEvents(_pipeOutEvents); | 490 _disableEvents(_pipeOutEvents); |
527 } | 491 } |
528 | 492 |
529 void _resume() { | 493 void _resume() { |
530 if (_pipeInEvents != null) { | 494 if (_pipeInEvents != null) { |
531 if (_readEventsEnabled) { | 495 if (_readEventsEnabled) { |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 if (_raw != null) { | 816 if (_raw != null) { |
853 _raw.shutdown(SocketDirection.SEND); | 817 _raw.shutdown(SocketDirection.SEND); |
854 _disableWriteEvent(); | 818 _disableWriteEvent(); |
855 } | 819 } |
856 } | 820 } |
857 } | 821 } |
858 | 822 |
859 Map _toJSON(bool ref) => _raw._toJSON(ref); | 823 Map _toJSON(bool ref) => _raw._toJSON(ref); |
860 void set _owner(owner) { _raw._owner = owner; } | 824 void set _owner(owner) { _raw._owner = owner; } |
861 } | 825 } |
OLD | NEW |