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 contains types/constants and functions specific to message pipes. |
| 6 |
| 7 // Note: This header should be compilable as C. |
| 8 |
| 9 #ifndef MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
| 10 #define MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
| 11 |
| 12 #include "mojo/public/c/system/system_export.h" |
| 13 #include "mojo/public/c/system/types.h" |
| 14 |
| 15 // |MojoWriteMessageFlags|: Used to specify different modes to |
| 16 // |MojoWriteMessage()|. |
| 17 // |MOJO_WRITE_MESSAGE_FLAG_NONE| - No flags; default mode. |
| 18 |
| 19 typedef uint32_t MojoWriteMessageFlags; |
| 20 |
| 21 #ifdef __cplusplus |
| 22 const MojoWriteMessageFlags MOJO_WRITE_MESSAGE_FLAG_NONE = 0; |
| 23 #else |
| 24 #define MOJO_WRITE_MESSAGE_FLAG_NONE ((MojoWriteMessageFlags) 0) |
| 25 #endif |
| 26 |
| 27 // |MojoReadMessageFlags|: Used to specify different modes to |
| 28 // |MojoReadMessage()|. |
| 29 // |MOJO_READ_MESSAGE_FLAG_NONE| - No flags; default mode. |
| 30 // |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| - If the message is unable to be read |
| 31 // for whatever reason (e.g., the caller-supplied buffer is too small), |
| 32 // discard the message (i.e., simply dequeue it). |
| 33 |
| 34 typedef uint32_t MojoReadMessageFlags; |
| 35 |
| 36 #ifdef __cplusplus |
| 37 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_NONE = 0; |
| 38 const MojoReadMessageFlags MOJO_READ_MESSAGE_FLAG_MAY_DISCARD = 1 << 0; |
| 39 #else |
| 40 #define MOJO_READ_MESSAGE_FLAG_NONE ((MojoReadMessageFlags) 0) |
| 41 #define MOJO_READ_MESSAGE_FLAG_MAY_DISCARD ((MojoReadMessageFlags) 1 << 0) |
| 42 #endif |
| 43 |
| 44 #ifdef __cplusplus |
| 45 extern "C" { |
| 46 #endif |
| 47 |
| 48 // Note: See the comment in functions.h about the meaning of the "optional" |
| 49 // label for pointer parameters. |
| 50 |
| 51 // Creates a message pipe, which is a bidirectional communication channel for |
| 52 // framed data (i.e., messages). Messages can contain plain data and/or Mojo |
| 53 // handles. On success, |*message_pipe_handle0| and |*message_pipe_handle1| are |
| 54 // set to handles for the two endpoints (ports) for the message pipe. |
| 55 // |
| 56 // Returns: |
| 57 // |MOJO_RESULT_OK| on success. |
| 58 // |MOJO_RESULT_INVALID_ARGUMENT| if |message_pipe_handle0| and/or |
| 59 // |message_pipe_handle1| do not appear to be valid pointers. |
| 60 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if a process/system/quota/etc. limit has |
| 61 // been reached. |
| 62 // |
| 63 // TODO(vtl): Add an options struct pointer argument. |
| 64 MOJO_SYSTEM_EXPORT MojoResult MojoCreateMessagePipe( |
| 65 MojoHandle* message_pipe_handle0, // Out. |
| 66 MojoHandle* message_pipe_handle1); // Out. |
| 67 |
| 68 // Writes a message to the message pipe endpoint given by |message_pipe_handle|, |
| 69 // with message data specified by |bytes| of size |num_bytes| and attached |
| 70 // handles specified by |handles| of count |num_handles|, and options specified |
| 71 // by |flags|. If there is no message data, |bytes| may be null, in which case |
| 72 // |num_bytes| must be zero. If there are no attached handles, |handles| may be |
| 73 // null, in which case |num_handles| must be zero. |
| 74 // |
| 75 // If handles are attached, on success the handles will no longer be valid (the |
| 76 // receiver will receive equivalent, but logically different, handles). Handles |
| 77 // to be sent should not be in simultaneous use (e.g., on another thread). |
| 78 // |
| 79 // Returns: |
| 80 // |MOJO_RESULT_OK| on success (i.e., the message was enqueued). |
| 81 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid (e.g., if |
| 82 // |message_pipe_handle| is not a valid handle, or some of the |
| 83 // requirements above are not satisfied). |
| 84 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if some system limit has been reached, or |
| 85 // the number of handles to send is too large (TODO(vtl): reconsider the |
| 86 // latter case). |
| 87 // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed. |
| 88 // Note that closing an endpoint is not necessarily synchronous (e.g., |
| 89 // across processes), so this function may be succeed even if the other |
| 90 // endpoint has been closed (in which case the message would be dropped). |
| 91 // |MOJO_RESULT_BUSY| if some handle to be sent is currently in use. |
| 92 // |
| 93 // TODO(vtl): Add a notion of capacity for message pipes, and return |
| 94 // |MOJO_RESULT_SHOULD_WAIT| if the message pipe is full. |
| 95 MOJO_SYSTEM_EXPORT MojoResult MojoWriteMessage( |
| 96 MojoHandle message_pipe_handle, |
| 97 const void* bytes, // Optional. |
| 98 uint32_t num_bytes, |
| 99 const MojoHandle* handles, // Optional. |
| 100 uint32_t num_handles, |
| 101 MojoWriteMessageFlags flags); |
| 102 |
| 103 // Reads a message from the message pipe endpoint given by |
| 104 // |message_pipe_handle|; also usable to query the size of the next message or |
| 105 // discard the next message. |bytes|/|*num_bytes| indicate the buffer/buffer |
| 106 // size to receive the message data (if any) and |handles|/|*num_handles| |
| 107 // indicate the buffer/maximum handle count to receive the attached handles (if |
| 108 // any). |
| 109 // |
| 110 // |num_bytes| and |num_handles| are optional "in-out" parameters. If non-null, |
| 111 // on return |*num_bytes| and |*num_handles| will usually indicate the number |
| 112 // of bytes and number of attached handles in the "next" message, respectively, |
| 113 // whether that message was read or not. (If null, the number of bytes/handles |
| 114 // is treated as zero.) |
| 115 // |
| 116 // If |bytes| is null, then |*num_bytes| must be zero, and similarly for |
| 117 // |handles| and |*num_handles|. |
| 118 // |
| 119 // Partial reads are NEVER done. Either a full read is done and |MOJO_RESULT_OK| |
| 120 // returned, or the read is NOT done and |MOJO_RESULT_RESOURCE_EXHAUSTED| is |
| 121 // returned (if |MOJO_READ_MESSAGE_FLAG_MAY_DISCARD| was set, the message is |
| 122 // also discarded in this case). |
| 123 // |
| 124 // Returns: |
| 125 // |MOJO_RESULT_OK| on success (i.e., a message was actually read). |
| 126 // |MOJO_RESULT_INVALID_ARGUMENT| if some argument was invalid. |
| 127 // |MOJO_RESULT_FAILED_PRECONDITION| if the other endpoint has been closed. |
| 128 // |MOJO_RESULT_RESOURCE_EXHAUSTED| if one of the buffers to receive the |
| 129 // message/attached handles (|bytes|/|*num_bytes| or |
| 130 // |handles|/|*num_handles|) was too small. (TODO(vtl): Reconsider this |
| 131 // error code; should distinguish this from the hitting-system-limits |
| 132 // case.) |
| 133 // |MOJO_RESULT_SHOULD_WAIT| if no message was available to be read. |
| 134 MOJO_SYSTEM_EXPORT MojoResult MojoReadMessage( |
| 135 MojoHandle message_pipe_handle, |
| 136 void* bytes, // Optional out. |
| 137 uint32_t* num_bytes, // Optional in/out. |
| 138 MojoHandle* handles, // Optional out. |
| 139 uint32_t* num_handles, // Optional in/out. |
| 140 MojoReadMessageFlags flags); |
| 141 |
| 142 #ifdef __cplusplus |
| 143 } // extern "C" |
| 144 #endif |
| 145 |
| 146 #endif // MOJO_PUBLIC_C_SYSTEM_MESSAGE_PIPE_H_ |
OLD | NEW |