OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include <stdio.h> |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "mojo/public/system/core.h" |
| 9 #include "mojo/system/core_impl.h" |
| 10 |
| 11 char* ReadStringFromPipe(mojo::Handle pipe) { |
| 12 uint32_t len = 0; |
| 13 char* buf = NULL; |
| 14 MojoResult result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, |
| 15 MOJO_READ_MESSAGE_FLAG_NONE); |
| 16 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 17 buf = new char[len]; |
| 18 result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, |
| 19 MOJO_READ_MESSAGE_FLAG_NONE); |
| 20 } |
| 21 if (result < MOJO_RESULT_OK) { |
| 22 // Failure.. |
| 23 if (buf) |
| 24 delete[] buf; |
| 25 return NULL; |
| 26 } |
| 27 return buf; |
| 28 } |
| 29 |
| 30 class SampleMessageWaiter { |
| 31 public: |
| 32 explicit SampleMessageWaiter(mojo::Handle pipe) : pipe_(pipe) {} |
| 33 ~SampleMessageWaiter() {} |
| 34 |
| 35 void Read() { |
| 36 char* string = ReadStringFromPipe(pipe_); |
| 37 if (string) { |
| 38 printf("Read string from pipe: %s\n", string); |
| 39 delete[] string; |
| 40 string = NULL; |
| 41 } |
| 42 } |
| 43 |
| 44 void WaitAndRead() { |
| 45 MojoResult result = mojo::Wait(pipe_, MOJO_WAIT_FLAG_READABLE, 100); |
| 46 if (result < MOJO_RESULT_OK) { |
| 47 // Failure... |
| 48 } |
| 49 |
| 50 Read(); |
| 51 } |
| 52 |
| 53 private: |
| 54 |
| 55 mojo::Handle pipe_; |
| 56 DISALLOW_COPY_AND_ASSIGN(SampleMessageWaiter); |
| 57 }; |
| 58 |
| 59 extern "C" __declspec(dllexport) MojoResult __cdecl MojoMain( |
| 60 mojo::Handle pipe) { |
| 61 SampleMessageWaiter(pipe).WaitAndRead(); |
| 62 return MOJO_RESULT_OK; |
| 63 } |
OLD | NEW |