OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 // This file provides a C++ wrapping around the Mojo C API for message pipes, | |
6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more | |
7 // strongly-typed representations of |MojoHandle|s. | |
8 // | |
9 // Please see "mojo/public/c/include/mojo/system/message_pipe.h" for complete | |
10 // documentation of the API. | |
11 | |
12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | |
13 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | |
14 | |
15 #include <assert.h> | |
16 #include <mojo/system/message_pipe.h> | |
17 | |
18 #include "mojo/public/cpp/system/handle.h" | |
19 #include "mojo/public/cpp/system/macros.h" | |
20 | |
21 namespace mojo { | |
22 | |
23 // A strongly-typed representation of a |MojoHandle| to one end of a message | |
24 // pipe. | |
25 class MessagePipeHandle : public Handle { | |
26 public: | |
27 MessagePipeHandle() {} | |
28 explicit MessagePipeHandle(MojoHandle value) : Handle(value) {} | |
29 | |
30 // Copying and assignment allowed. | |
31 }; | |
32 | |
33 static_assert(sizeof(MessagePipeHandle) == sizeof(Handle), | |
34 "Bad size for C++ MessagePipeHandle"); | |
35 | |
36 typedef ScopedHandleBase<MessagePipeHandle> ScopedMessagePipeHandle; | |
37 static_assert(sizeof(ScopedMessagePipeHandle) == sizeof(MessagePipeHandle), | |
38 "Bad size for C++ ScopedMessagePipeHandle"); | |
39 | |
40 // Creates a message pipe. See |MojoCreateMessagePipe()| for complete | |
41 // documentation. | |
42 inline MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options, | |
43 ScopedMessagePipeHandle* message_pipe0, | |
44 ScopedMessagePipeHandle* message_pipe1) { | |
45 assert(message_pipe0); | |
46 assert(message_pipe1); | |
47 MessagePipeHandle handle0; | |
48 MessagePipeHandle handle1; | |
49 MojoResult rv = MojoCreateMessagePipe( | |
50 options, handle0.mutable_value(), handle1.mutable_value()); | |
51 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
52 // will be used). | |
53 message_pipe0->reset(handle0); | |
54 message_pipe1->reset(handle1); | |
55 return rv; | |
56 } | |
57 | |
58 // The following "...Raw" versions fully expose the underlying API, and don't | |
59 // help with ownership of handles (especially when writing messages). It is | |
60 // expected that in most cases these methods will be called through generated | |
61 // bindings anyway. | |
62 // TODO(vtl): Write friendlier versions of these functions (using scoped | |
63 // handles and/or vectors) if there is a demonstrated need for them. | |
64 | |
65 // Writes to a message pipe. If handles are attached, on success the handles | |
66 // will no longer be valid (the receiver will receive equivalent, but logically | |
67 // different, handles). See |MojoWriteMessage()| for complete documentation. | |
68 inline MojoResult WriteMessageRaw(MessagePipeHandle message_pipe, | |
69 const void* bytes, | |
70 uint32_t num_bytes, | |
71 const MojoHandle* handles, | |
72 uint32_t num_handles, | |
73 MojoWriteMessageFlags flags) { | |
74 return MojoWriteMessage( | |
75 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); | |
76 } | |
77 | |
78 // Reads from a message pipe. See |MojoReadMessage()| for complete | |
79 // documentation. | |
80 inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe, | |
81 void* bytes, | |
82 uint32_t* num_bytes, | |
83 MojoHandle* handles, | |
84 uint32_t* num_handles, | |
85 MojoReadMessageFlags flags) { | |
86 return MojoReadMessage( | |
87 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); | |
88 } | |
89 | |
90 // A wrapper class that automatically creates a message pipe and owns both | |
91 // handles. | |
92 class MessagePipe { | |
93 public: | |
94 MessagePipe(); | |
95 explicit MessagePipe(const MojoCreateMessagePipeOptions& options); | |
96 ~MessagePipe(); | |
97 | |
98 ScopedMessagePipeHandle handle0; | |
99 ScopedMessagePipeHandle handle1; | |
100 }; | |
101 | |
102 inline MessagePipe::MessagePipe() { | |
103 MojoResult result = CreateMessagePipe(nullptr, &handle0, &handle1); | |
104 MOJO_ALLOW_UNUSED_LOCAL(result); | |
105 assert(result == MOJO_RESULT_OK); | |
106 } | |
107 | |
108 inline MessagePipe::MessagePipe(const MojoCreateMessagePipeOptions& options) { | |
109 MojoResult result = CreateMessagePipe(&options, &handle0, &handle1); | |
110 MOJO_ALLOW_UNUSED_LOCAL(result); | |
111 assert(result == MOJO_RESULT_OK); | |
112 } | |
113 | |
114 inline MessagePipe::~MessagePipe() { | |
115 } | |
116 | |
117 } // namespace mojo | |
118 | |
119 #endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | |
OLD | NEW |