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 #ifndef MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | |
6 #define MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | |
7 | |
8 #include <assert.h> | |
9 | |
10 #include "mojo/public/c/system/message_pipe.h" | |
11 #include "mojo/public/cpp/system/handle.h" | |
12 #include "mojo/public/cpp/system/macros.h" | |
13 | |
14 namespace mojo { | |
15 | |
16 // MessagePipeHandle ----------------------------------------------------------- | |
17 | |
18 class MessagePipeHandle : public Handle { | |
19 public: | |
20 MessagePipeHandle() {} | |
21 explicit MessagePipeHandle(MojoHandle value) : Handle(value) {} | |
22 | |
23 // Copying and assignment allowed. | |
24 }; | |
25 | |
26 static_assert(sizeof(MessagePipeHandle) == sizeof(Handle), | |
27 "Bad size for C++ MessagePipeHandle"); | |
28 | |
29 typedef ScopedHandleBase<MessagePipeHandle> ScopedMessagePipeHandle; | |
30 static_assert(sizeof(ScopedMessagePipeHandle) == sizeof(MessagePipeHandle), | |
31 "Bad size for C++ ScopedMessagePipeHandle"); | |
32 | |
33 inline MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options, | |
34 ScopedMessagePipeHandle* message_pipe0, | |
35 ScopedMessagePipeHandle* message_pipe1) { | |
36 assert(message_pipe0); | |
37 assert(message_pipe1); | |
38 MessagePipeHandle handle0; | |
39 MessagePipeHandle handle1; | |
40 MojoResult rv = MojoCreateMessagePipe( | |
41 options, handle0.mutable_value(), handle1.mutable_value()); | |
42 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
43 // will be used). | |
44 message_pipe0->reset(handle0); | |
45 message_pipe1->reset(handle1); | |
46 return rv; | |
47 } | |
48 | |
49 // These "raw" versions fully expose the underlying API, but don't help with | |
50 // ownership of handles (especially when writing messages). | |
51 // TODO(vtl): Write "baked" versions. | |
52 inline MojoResult WriteMessageRaw(MessagePipeHandle message_pipe, | |
53 const void* bytes, | |
54 uint32_t num_bytes, | |
55 const MojoHandle* handles, | |
56 uint32_t num_handles, | |
57 MojoWriteMessageFlags flags) { | |
58 return MojoWriteMessage( | |
59 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); | |
60 } | |
61 | |
62 inline MojoResult ReadMessageRaw(MessagePipeHandle message_pipe, | |
63 void* bytes, | |
64 uint32_t* num_bytes, | |
65 MojoHandle* handles, | |
66 uint32_t* num_handles, | |
67 MojoReadMessageFlags flags) { | |
68 return MojoReadMessage( | |
69 message_pipe.value(), bytes, num_bytes, handles, num_handles, flags); | |
70 } | |
71 | |
72 // A wrapper class that automatically creates a message pipe and owns both | |
73 // handles. | |
74 class MessagePipe { | |
75 public: | |
76 MessagePipe(); | |
77 explicit MessagePipe(const MojoCreateMessagePipeOptions& options); | |
78 ~MessagePipe(); | |
79 | |
80 ScopedMessagePipeHandle handle0; | |
81 ScopedMessagePipeHandle handle1; | |
82 }; | |
83 | |
84 inline MessagePipe::MessagePipe() { | |
85 MojoResult result = CreateMessagePipe(nullptr, &handle0, &handle1); | |
86 MOJO_ALLOW_UNUSED_LOCAL(result); | |
87 assert(result == MOJO_RESULT_OK); | |
88 } | |
89 | |
90 inline MessagePipe::MessagePipe(const MojoCreateMessagePipeOptions& options) { | |
91 MojoResult result = CreateMessagePipe(&options, &handle0, &handle1); | |
92 MOJO_ALLOW_UNUSED_LOCAL(result); | |
93 assert(result == MOJO_RESULT_OK); | |
94 } | |
95 | |
96 inline MessagePipe::~MessagePipe() { | |
97 } | |
98 | |
99 } // namespace mojo | |
100 | |
101 #endif // MOJO_PUBLIC_CPP_SYSTEM_MESSAGE_PIPE_H_ | |
OLD | NEW |