Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 1 | #ifndef ANDROID_PDX_ENDPOINT_H_ |
| 2 | #define ANDROID_PDX_ENDPOINT_H_ |
| 3 | |
| 4 | #include <pdx/channel_handle.h> |
| 5 | #include <pdx/file_handle.h> |
| 6 | #include <pdx/status.h> |
| 7 | |
| 8 | struct iovec; |
| 9 | |
| 10 | namespace android { |
| 11 | namespace pdx { |
| 12 | |
| 13 | class Service; |
| 14 | class Channel; |
| 15 | class Message; |
| 16 | |
| 17 | struct MessageInfo { |
| 18 | int pid{0}; |
| 19 | int tid{0}; |
| 20 | int cid{0}; |
| 21 | int mid{0}; |
| 22 | int euid{0}; |
| 23 | int egid{0}; |
| 24 | int32_t op{0}; |
| 25 | uint32_t flags{0}; |
| 26 | Service* service{nullptr}; |
| 27 | Channel* channel{nullptr}; |
| 28 | size_t send_len{0}; |
| 29 | size_t recv_len{0}; |
| 30 | size_t fd_count{0}; |
| 31 | uint64_t impulse[4] = {}; |
| 32 | }; |
| 33 | |
| 34 | // Wrapper around transport endpoint. Abstracts the underlying transport APIs in |
| 35 | // a way, that the underlying IPC can be substituted for another technology |
| 36 | // without changing the Service, Client and Message classes of this library. |
| 37 | class Endpoint { |
| 38 | public: |
| 39 | virtual ~Endpoint() = default; |
| 40 | |
| 41 | // Returns a tag that uniquely identifies a specific underlying IPC transport. |
| 42 | virtual uint32_t GetIpcTag() const = 0; |
| 43 | |
| 44 | // Associates a Service instance with an endpoint by setting the service |
| 45 | // context pointer to the address of the Service. Only one Service may be |
| 46 | // associated with a given endpoint. |
| 47 | virtual int SetService(Service* service) = 0; |
| 48 | |
| 49 | // Set the channel context for the given channel. |
| 50 | virtual int SetChannel(int channel_id, Channel* channel) = 0; |
| 51 | |
| 52 | // Close a channel, signaling the client file object and freeing the channel |
| 53 | // id. Once closed, the client side of the channel always returns the error |
| 54 | // ESHUTDOWN and signals the poll/epoll events POLLHUP and POLLFREE. |
| 55 | virtual int CloseChannel(int channel_id) = 0; |
| 56 | |
| 57 | // Update the event bits for the given channel (given by id), using the |
| 58 | // given clear and set masks. |
| 59 | virtual int ModifyChannelEvents(int channel_id, int clear_mask, |
| 60 | int set_mask) = 0; |
| 61 | |
| 62 | // Create a new channel and push it as a file descriptor to the process |
| 63 | // sending the |message|. |flags| may be set to O_NONBLOCK and/or |
| 64 | // O_CLOEXEC to control the initial behavior of the new file descriptor (the |
| 65 | // sending process may change these later using fcntl()). The internal Channel |
| 66 | // instance associated with this channel is set to |channel|, which may be |
| 67 | // nullptr. The new channel id allocated for this channel is returned in |
| 68 | // |channel_id|, which may also be nullptr if not needed. |
| 69 | virtual Status<RemoteChannelHandle> PushChannel(Message* message, int flags, |
| 70 | Channel* channel, |
| 71 | int* channel_id) = 0; |
| 72 | |
| 73 | // Check whether the |ref| is a reference to a channel to the service |
| 74 | // represented by the |endpoint|. If the channel reference in question is |
| 75 | // valid, the Channel object is returned in |channel| when non-nullptr and |
| 76 | // the channel ID is returned through the Status object. |
| 77 | virtual Status<int> CheckChannel(const Message* message, ChannelReference ref, |
| 78 | Channel** channel) = 0; |
| 79 | |
| 80 | // The default message handler. It is important that all messages |
| 81 | // (eventually) get a reply. This method should be called for any unrecognized |
| 82 | // opcodes or otherwise unhandled messages to prevent erroneous requests from |
| 83 | // blocking indefinitely. |
| 84 | virtual int DefaultHandleMessage(const MessageInfo& info) = 0; |
| 85 | |
| 86 | // Receives a message on the given endpoint file descriptor. |
| 87 | virtual int MessageReceive(Message* message) = 0; |
| 88 | |
| 89 | // Replies to the message with a return code. |
| 90 | virtual int MessageReply(Message* message, int return_code) = 0; |
| 91 | |
| 92 | // Replies to the message with a file descriptor. |
| 93 | virtual int MessageReplyFd(Message* message, unsigned int push_fd) = 0; |
| 94 | |
| 95 | // Replies to the message with a local channel handle. |
| 96 | virtual int MessageReplyChannelHandle(Message* message, |
| 97 | const LocalChannelHandle& handle) = 0; |
| 98 | |
| 99 | // Replies to the message with a borrowed local channel handle. |
| 100 | virtual int MessageReplyChannelHandle( |
| 101 | Message* message, const BorrowedChannelHandle& handle) = 0; |
| 102 | |
| 103 | // Replies to the message with a remote channel handle. |
| 104 | virtual int MessageReplyChannelHandle(Message* message, |
| 105 | const RemoteChannelHandle& handle) = 0; |
| 106 | |
| 107 | // Reads message data into an array of memory buffers. |
| 108 | virtual ssize_t ReadMessageData(Message* message, const iovec* vector, |
| 109 | size_t vector_length) = 0; |
| 110 | |
| 111 | // Sends reply data for message. |
| 112 | virtual ssize_t WriteMessageData(Message* message, const iovec* vector, |
| 113 | size_t vector_length) = 0; |
| 114 | |
| 115 | // Records a file descriptor into the message buffer and returns the remapped |
| 116 | // reference to be sent to the remote process. |
| 117 | virtual FileReference PushFileHandle(Message* message, |
| 118 | const LocalHandle& handle) = 0; |
| 119 | virtual FileReference PushFileHandle(Message* message, |
| 120 | const BorrowedHandle& handle) = 0; |
| 121 | virtual FileReference PushFileHandle(Message* message, |
| 122 | const RemoteHandle& handle) = 0; |
| 123 | virtual ChannelReference PushChannelHandle( |
| 124 | Message* message, const LocalChannelHandle& handle) = 0; |
| 125 | virtual ChannelReference PushChannelHandle( |
| 126 | Message* message, const BorrowedChannelHandle& handle) = 0; |
| 127 | virtual ChannelReference PushChannelHandle( |
| 128 | Message* message, const RemoteChannelHandle& handle) = 0; |
| 129 | |
| 130 | // Obtains a file descriptor/channel handle from a message for the given |
| 131 | // reference. |
| 132 | virtual LocalHandle GetFileHandle(Message* message, |
| 133 | FileReference ref) const = 0; |
| 134 | virtual LocalChannelHandle GetChannelHandle(Message* message, |
| 135 | ChannelReference ref) const = 0; |
| 136 | |
| 137 | // Transport-specific message state management. |
| 138 | virtual void* AllocateMessageState() = 0; |
| 139 | virtual void FreeMessageState(void* state) = 0; |
| 140 | |
| 141 | // Cancels the endpoint, unblocking any receiver threads waiting for a |
| 142 | // message. |
| 143 | virtual int Cancel() = 0; |
| 144 | }; |
| 145 | |
| 146 | } // namespace pdx |
| 147 | } // namespace android |
| 148 | |
| 149 | #endif // ANDROID_PDX_ENDPOINT_H_ |