| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:isolate'; | 6 import 'dart:isolate'; |
| 7 import 'dart:mojo_core' as core; | 7 import 'dart:mojo_core' as core; |
| 8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
| 9 | 9 |
| 10 ByteData byteDataOfString(String s) { | 10 ByteData byteDataOfString(String s) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 assert(result != null); | 29 assert(result != null); |
| 30 assert(size == result.bytesRead); | 30 assert(size == result.bytesRead); |
| 31 | 31 |
| 32 // Convert to a string and check. | 32 // Convert to a string and check. |
| 33 String msg = stringOfByteData(bytes); | 33 String msg = stringOfByteData(bytes); |
| 34 assert(expected == msg); | 34 assert(expected == msg); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void pipeTestIsolate(core.MojoMessagePipeEndpoint endpoint) { | 37 void pipeTestIsolate(core.MojoMessagePipeEndpoint endpoint) { |
| 38 var handle = new core.MojoHandle(endpoint.handle); | 38 var handle = new core.MojoHandle(endpoint.handle); |
| 39 handle.listen((int signal) { | 39 handle.listen((List<int> event) { |
| 40 if (core.MojoHandleSignals.isReadWrite(signal)) { | 40 var mojoSignals = new core.MojoHandleSignals(event[1]); |
| 41 if (mojoSignals.isReadWrite) { |
| 41 throw 'We should only be reading or writing, not both.'; | 42 throw 'We should only be reading or writing, not both.'; |
| 42 } else if (core.MojoHandleSignals.isReadable(signal)) { | 43 } else if (mojoSignals.isReadable) { |
| 43 expectStringFromEndpoint("Ping", endpoint); | 44 expectStringFromEndpoint("Ping", endpoint); |
| 44 handle.enableWriteEvents(); | 45 handle.enableWriteEvents(); |
| 45 } else if (core.MojoHandleSignals.isWritable(signal)) { | 46 } else if (mojoSignals.isWritable) { |
| 46 endpoint.write(byteDataOfString("Pong")); | 47 endpoint.write(byteDataOfString("Pong")); |
| 47 handle.disableWriteEvents(); | 48 handle.enableReadEvents(); |
| 48 } else if (core.MojoHandleSignals.isNone(signal)) { | 49 } else if (mojoSignals.isPeerClosed) { |
| 49 handle.close(); | 50 handle.close(); |
| 50 } else { | 51 } else { |
| 51 throw 'Unexpected signal.'; | 52 throw 'Unexpected event.'; |
| 52 } | 53 } |
| 53 }); | 54 }); |
| 54 } | 55 } |
| 55 | 56 |
| 56 main() { | 57 main() { |
| 57 var pipe = new core.MojoMessagePipe(); | 58 var pipe = new core.MojoMessagePipe(); |
| 58 var endpoint = pipe.endpoints[0]; | 59 var endpoint = pipe.endpoints[0]; |
| 59 var handle = new core.MojoHandle(endpoint.handle); | 60 var handle = new core.MojoHandle(endpoint.handle); |
| 60 Isolate.spawn(pipeTestIsolate, pipe.endpoints[1]).then((_) { | 61 Isolate.spawn(pipeTestIsolate, pipe.endpoints[1]).then((_) { |
| 61 handle.enableWriteEvents(); | 62 handle.listen((List<int> event) { |
| 62 handle.listen((int signal) { | 63 var mojoSignals = new core.MojoHandleSignals(event[1]); |
| 63 if (core.MojoHandleSignals.isReadWrite(signal)) { | 64 if (mojoSignals.isReadWrite) { |
| 64 throw 'We should only be reading or writing, not both.'; | 65 throw 'We should only be reading or writing, not both.'; |
| 65 } else if (core.MojoHandleSignals.isReadable(signal)) { | 66 } else if (mojoSignals.isReadable) { |
| 66 expectStringFromEndpoint("Pong", endpoint); | 67 expectStringFromEndpoint("Pong", endpoint); |
| 67 handle.close(); | 68 handle.close(); |
| 68 } else if (core.MojoHandleSignals.isWritable(signal)) { | 69 } else if (mojoSignals.isWritable) { |
| 69 endpoint.write(byteDataOfString("Ping")); | 70 endpoint.write(byteDataOfString("Ping")); |
| 70 handle.disableWriteEvents(); | 71 handle.enableReadEvents(); |
| 71 } else if (core.MojoHandleSignals.isNone(signal)) { | 72 } else if (mojoSignals.isPeerClosed) { |
| 72 throw 'This end should close first.'; | 73 throw 'This end should close first.'; |
| 73 } else { | 74 } else { |
| 74 throw 'Unexpected signal.'; | 75 throw 'Unexpected event.'; |
| 75 } | 76 } |
| 76 }); | 77 }); |
| 78 handle.enableWriteEvents(); |
| 77 }); | 79 }); |
| 78 } | 80 } |
| OLD | NEW |