OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_EDK_SYSTEM_PORTS_MESSAGE_H_ |
| 6 #define MOJO_EDK_SYSTEM_PORTS_MESSAGE_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "mojo/edk/system/ports/name.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace edk { |
| 15 namespace ports { |
| 16 |
| 17 // A message consists of a header (array of bytes), payload (array of bytes) |
| 18 // and an array of ports. The header is used by the Node implementation. |
| 19 // |
| 20 // This class is designed to be subclassed, and the subclass is responsible for |
| 21 // providing the underlying storage. The header size will be aligned, and it |
| 22 // should be followed in memory by the array of ports and finally the payload. |
| 23 // |
| 24 // NOTE: This class does not manage the lifetime of the ports it references. |
| 25 class Message { |
| 26 public: |
| 27 virtual ~Message() {} |
| 28 |
| 29 // Inspect the message at |bytes| and return the size of each section. |
| 30 static void Parse(const void* bytes, |
| 31 size_t num_bytes, |
| 32 size_t* num_header_bytes, |
| 33 size_t* num_payload_bytes, |
| 34 size_t* num_ports_bytes); |
| 35 |
| 36 void* mutable_header_bytes() { return start_; } |
| 37 const void* header_bytes() const { return start_; } |
| 38 size_t num_header_bytes() const { return num_header_bytes_; } |
| 39 |
| 40 void* mutable_payload_bytes() { |
| 41 return start_ + num_header_bytes_ + num_ports_bytes_; |
| 42 } |
| 43 const void* payload_bytes() const { |
| 44 return const_cast<Message*>(this)->mutable_payload_bytes(); |
| 45 } |
| 46 size_t num_payload_bytes() const { return num_payload_bytes_; } |
| 47 |
| 48 PortName* mutable_ports() { |
| 49 return reinterpret_cast<PortName*>(start_ + num_header_bytes_); |
| 50 } |
| 51 const PortName* ports() const { |
| 52 return const_cast<Message*>(this)->mutable_ports(); |
| 53 } |
| 54 size_t num_ports_bytes() const { return num_ports_bytes_; } |
| 55 size_t num_ports() const { return num_ports_bytes_ / sizeof(PortName); } |
| 56 |
| 57 protected: |
| 58 // Constructs a new Message base for a user message. |
| 59 // |
| 60 // Note: You MUST call InitializeUserMessageHeader() before this Message is |
| 61 // ready for transmission. |
| 62 Message(size_t num_payload_bytes, size_t num_ports); |
| 63 |
| 64 // Constructs a new Message base for an internal message. Do NOT call |
| 65 // InitializeUserMessageHeader() when using this constructor. |
| 66 Message(size_t num_header_bytes, |
| 67 size_t num_payload_bytes, |
| 68 size_t num_ports_bytes); |
| 69 |
| 70 Message(const Message& other) = delete; |
| 71 void operator=(const Message& other) = delete; |
| 72 |
| 73 // Initializes the header in a newly allocated message buffer to carry a |
| 74 // user message. |
| 75 void InitializeUserMessageHeader(void* start); |
| 76 |
| 77 // Note: storage is [header][ports][payload]. |
| 78 char* start_ = nullptr; |
| 79 size_t num_ports_ = 0; |
| 80 size_t num_header_bytes_ = 0; |
| 81 size_t num_ports_bytes_ = 0; |
| 82 size_t num_payload_bytes_ = 0; |
| 83 }; |
| 84 |
| 85 using ScopedMessage = scoped_ptr<Message>; |
| 86 |
| 87 } // namespace ports |
| 88 } // namespace edk |
| 89 } // namespace mojo |
| 90 |
| 91 #endif // MOJO_EDK_SYSTEM_PORTS_MESSAGE_H_ |
OLD | NEW |