OLD | NEW |
| (Empty) |
1 // Copyright 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 "mojo/public/cpp/test_support/test_utils.h" | |
6 | |
7 #include "mojo/public/cpp/system/handle.h" | |
8 #include "mojo/public/cpp/system/message_pipe.h" | |
9 #include "mojo/public/cpp/system/wait.h" | |
10 | |
11 namespace mojo { | |
12 namespace test { | |
13 | |
14 bool WriteTextMessage(const MessagePipeHandle& handle, | |
15 const std::string& text) { | |
16 MojoResult rv = | |
17 WriteMessageRaw(handle, text.data(), static_cast<uint32_t>(text.size()), | |
18 nullptr, 0u, MOJO_WRITE_MESSAGE_FLAG_NONE); | |
19 return rv == MOJO_RESULT_OK; | |
20 } | |
21 | |
22 bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text) { | |
23 MojoResult rv; | |
24 bool did_wait = false; | |
25 | |
26 uint32_t num_bytes = 0u; | |
27 uint32_t num_handles = 0u; | |
28 for (;;) { | |
29 rv = ReadMessageRaw(handle, nullptr, &num_bytes, nullptr, &num_handles, | |
30 MOJO_READ_MESSAGE_FLAG_NONE); | |
31 if (rv == MOJO_RESULT_SHOULD_WAIT) { | |
32 if (did_wait) { | |
33 assert(false); // Looping endlessly!? | |
34 return false; | |
35 } | |
36 rv = Wait(handle, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, | |
37 nullptr); | |
38 if (rv != MOJO_RESULT_OK) | |
39 return false; | |
40 did_wait = true; | |
41 } else { | |
42 assert(!num_handles); | |
43 break; | |
44 } | |
45 } | |
46 | |
47 text->resize(num_bytes); | |
48 rv = ReadMessageRaw(handle, &text->at(0), &num_bytes, nullptr, &num_handles, | |
49 MOJO_READ_MESSAGE_FLAG_NONE); | |
50 return rv == MOJO_RESULT_OK; | |
51 } | |
52 | |
53 bool DiscardMessage(const MessagePipeHandle& handle) { | |
54 MojoResult rv = ReadMessageRaw(handle, nullptr, nullptr, nullptr, nullptr, | |
55 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); | |
56 return rv == MOJO_RESULT_OK; | |
57 } | |
58 | |
59 } // namespace test | |
60 } // namespace mojo | |
OLD | NEW |