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 typedef MojoResult (*MojoMainFunction)(MojoHandle pipe); | |
12 | |
13 char* ReadStringFromPipe(MojoHandle pipe) { | |
14 uint32_t len = 0; | |
15 char* buf = NULL; | |
16 MojoResult result = MojoReadMessage(pipe, buf, &len, NULL, NULL, | |
17 MOJO_READ_MESSAGE_FLAG_NONE); | |
18 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { | |
19 buf = new char[len]; | |
20 result = MojoReadMessage(pipe, buf, &len, NULL, NULL, | |
21 MOJO_READ_MESSAGE_FLAG_NONE); | |
22 } | |
23 if (result < MOJO_RESULT_OK) { | |
24 // Failure.. | |
25 if (buf) | |
26 delete[] buf; | |
27 return NULL; | |
28 } | |
29 return buf; | |
30 } | |
31 | |
32 class SampleMessageWaiter { | |
33 public: | |
34 explicit SampleMessageWaiter(MojoHandle pipe) : pipe_(pipe) {} | |
35 ~SampleMessageWaiter() {} | |
36 | |
37 void Read() { | |
38 char* string = ReadStringFromPipe(pipe_); | |
39 if (string) { | |
40 printf("Read string from pipe: %s\n", string); | |
41 delete[] string; | |
42 string = NULL; | |
43 } | |
44 } | |
45 | |
46 void WaitAndRead() { | |
47 MojoResult result = MojoWait(pipe_, MOJO_WAIT_FLAG_READABLE, 100); | |
48 if (result < MOJO_RESULT_OK) { | |
49 // Failure... | |
Ben Goodger (Google)
2013/10/03 17:45:18
a typical meme in this code. can we have a discuss
darin (slow to review)
2013/10/04 21:47:14
yeah, we should probably figure out a good canonic
| |
50 } | |
51 | |
52 Read(); | |
53 } | |
54 | |
55 private: | |
56 | |
57 MojoHandle pipe_; | |
58 DISALLOW_COPY_AND_ASSIGN(SampleMessageWaiter); | |
59 }; | |
60 | |
61 extern "C" __declspec(dllexport) MojoResult __cdecl MojoMain(MojoHandle pipe) { | |
62 SampleMessageWaiter(pipe).WaitAndRead(); | |
63 return MOJO_RESULT_NOT_FOUND; | |
64 } | |
OLD | NEW |