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 #if defined(OS_WIN) |
| 12 #if !defined(CDECL) |
| 13 #define CDECL __cdecl |
| 14 #endif |
| 15 #define SAMPLE_APP_EXPORT __declspec(dllexport) |
| 16 #else |
| 17 #define CDECL |
| 18 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) |
| 19 #endif |
| 20 |
| 21 char* ReadStringFromPipe(mojo::Handle pipe) { |
| 22 uint32_t len = 0; |
| 23 char* buf = NULL; |
| 24 MojoResult result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, |
| 25 MOJO_READ_MESSAGE_FLAG_NONE); |
| 26 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 27 buf = new char[len]; |
| 28 result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, |
| 29 MOJO_READ_MESSAGE_FLAG_NONE); |
| 30 } |
| 31 if (result < MOJO_RESULT_OK) { |
| 32 // Failure.. |
| 33 if (buf) |
| 34 delete[] buf; |
| 35 return NULL; |
| 36 } |
| 37 return buf; |
| 38 } |
| 39 |
| 40 class SampleMessageWaiter { |
| 41 public: |
| 42 explicit SampleMessageWaiter(mojo::Handle pipe) : pipe_(pipe) {} |
| 43 ~SampleMessageWaiter() {} |
| 44 |
| 45 void Read() { |
| 46 char* string = ReadStringFromPipe(pipe_); |
| 47 if (string) { |
| 48 printf("Read string from pipe: %s\n", string); |
| 49 delete[] string; |
| 50 string = NULL; |
| 51 } |
| 52 } |
| 53 |
| 54 void WaitAndRead() { |
| 55 MojoResult result = mojo::Wait(pipe_, MOJO_WAIT_FLAG_READABLE, 100); |
| 56 if (result < MOJO_RESULT_OK) { |
| 57 // Failure... |
| 58 } |
| 59 |
| 60 Read(); |
| 61 } |
| 62 |
| 63 private: |
| 64 |
| 65 mojo::Handle pipe_; |
| 66 DISALLOW_COPY_AND_ASSIGN(SampleMessageWaiter); |
| 67 }; |
| 68 |
| 69 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( |
| 70 mojo::Handle pipe) { |
| 71 SampleMessageWaiter(pipe).WaitAndRead(); |
| 72 return MOJO_RESULT_OK; |
| 73 } |
OLD | NEW |