1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/public/bindings/sync_dispatcher.h"
#include <stdlib.h>
#include "mojo/public/bindings/message.h"
namespace mojo {
bool WaitForMessageAndDispatch(MessagePipeHandle handle,
mojo::MessageReceiver* receiver) {
uint32_t num_bytes = 0, num_handles = 0;
while (true) {
MojoResult rv = ReadMessageRaw(handle,
NULL,
&num_bytes,
NULL,
&num_handles,
MOJO_READ_MESSAGE_FLAG_NONE);
if (rv == MOJO_RESULT_RESOURCE_EXHAUSTED)
break;
if (rv != MOJO_RESULT_SHOULD_WAIT)
return false;
rv = Wait(handle, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE);
if (rv != MOJO_RESULT_OK)
return false;
}
Message message;
message.AllocUninitializedData(num_bytes);
message.mutable_handles()->resize(num_handles);
MojoResult rv = ReadMessageRaw(
handle,
message.mutable_data(),
&num_bytes,
message.mutable_handles()->empty() ? NULL :
reinterpret_cast<MojoHandle*>(&message.mutable_handles()->front()),
&num_handles,
MOJO_READ_MESSAGE_FLAG_NONE);
if (rv != MOJO_RESULT_OK)
return false;
return receiver->Accept(&message);
}
} // namespace mojo
|