OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'dart:isolate'; |
| 7 import 'dart:typed_data'; |
| 8 |
| 9 import 'package:mojo/public/dart/utils/lib/expect.dart'; |
| 10 import 'package:mojo/public/dart/system/lib/core.dart'; |
| 11 import 'package:mojo/public/dart/system/lib/embedder.dart' as embedder; |
| 12 |
| 13 |
| 14 ByteData byteDataOfString(String s) { |
| 15 return new ByteData.view((new Uint8List.fromList(s.codeUnits)).buffer); |
| 16 } |
| 17 |
| 18 |
| 19 String stringOfByteData(ByteData bytes) { |
| 20 return new String.fromCharCodes(bytes.buffer.asUint8List().toList()); |
| 21 } |
| 22 |
| 23 |
| 24 void rawMessagePingPongIsolate(MojoMessagePipeEndpoint endpoint) { |
| 25 int pings = 0; |
| 26 MojoHandle handle = new MojoHandle(endpoint.handle); |
| 27 handle.listen((int mojo_handle_signal) { |
| 28 if (MojoHandleSignals.isReadWrite(mojo_handle_signal)) { |
| 29 throw new Exception("Unexpected signal"); |
| 30 } else if (MojoHandleSignals.isReadable(mojo_handle_signal)) { |
| 31 // Query how many bytes are available. |
| 32 List result = endpoint.query(); |
| 33 int size = result[1]; |
| 34 |
| 35 // Read the data. |
| 36 ByteData bytes = new ByteData(size); |
| 37 endpoint.read(bytes); |
| 38 |
| 39 // Convert to string and output. |
| 40 String msg = stringOfByteData(bytes); |
| 41 Expect.equals("Ping", msg); |
| 42 pings = 10; |
| 43 handle.toggleWriteEvents(); |
| 44 } else if (MojoHandleSignals.isWritable(mojo_handle_signal)) { |
| 45 endpoint.write(byteDataOfString("Pong")); |
| 46 handle.toggleWriteEvents(); |
| 47 } |
| 48 }, onDone: () { |
| 49 handle.close(); |
| 50 Expect.equals(10, pings); |
| 51 }); |
| 52 } |
| 53 |
| 54 |
| 55 void rawMessagePingPongTest() { |
| 56 var pipe = new MojoMessagePipe(); |
| 57 |
| 58 Isolate.spawn(rawMessagePingPongIsolate, pipe.endpoints[0]).then((isolate) { |
| 59 MojoMessagePipeEndpoint endpoint = pipe.endpoints[1]; |
| 60 MojoHandle handle = new MojoHandle(endpoint.handle); |
| 61 handle.toggleWriteEvents(); |
| 62 int count = 10; |
| 63 int pongs = 0; |
| 64 handle.timeout(const Duration(seconds: 1), |
| 65 onTimeout: (es) { |
| 66 es.close(); |
| 67 }).listen((int mojo_handle_signal) { |
| 68 if (MojoHandleSignals.isReadWrite(mojo_handle_signal)) { |
| 69 throw new Exception("Unexpected signal"); |
| 70 } else if (MojoHandleSignals.isReadable(mojo_handle_signal)) { |
| 71 // Query how many bytes are available. |
| 72 List result = endpoint.query(); |
| 73 int size = result[1]; |
| 74 |
| 75 // Read the data. |
| 76 ByteData bytes = new ByteData(size); |
| 77 endpoint.read(bytes); |
| 78 |
| 79 // Convert to string and output. |
| 80 String msg = stringOfByteData(bytes); |
| 81 Expect.equals("Pong", msg); |
| 82 pongs++; |
| 83 handle.toggleWriteEvents(); |
| 84 count--; |
| 85 } else if (MojoHandleSignals.isWritable(mojo_handle_signal)) { |
| 86 if (count > 0) { |
| 87 endpoint.write(byteDataOfString("Ping")); |
| 88 } |
| 89 handle.toggleWriteEvents(); |
| 90 } |
| 91 }, onDone: () { |
| 92 handle.close(); |
| 93 Expect.equals(10, pongs); |
| 94 }); |
| 95 }); |
| 96 } |
| 97 |
| 98 |
| 99 main() { |
| 100 embedder.mojoEmbedderInit().then((status) { |
| 101 rawMessagePingPongTest(); |
| 102 }); |
| 103 } |
OLD | NEW |