OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_ | 5 #ifndef MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_ |
6 #define MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_ | 6 #define MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <stdlib.h> // For |free()|. | 9 #include <stdlib.h> // For |free()|. |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "mojo/public/system/system_export.h" | 12 #include "mojo/public/system/system_export.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 namespace system { | 15 namespace system { |
16 | 16 |
17 // This class is used to represent data in transit. It is thread-unsafe. | 17 // This class is used to represent data in transit. It is thread-unsafe. |
18 // Note: This class is POD. | 18 // Note: This class is POD. |
19 class MOJO_SYSTEM_EXPORT MessageInTransit { | 19 class MOJO_SYSTEM_EXPORT MessageInTransit { |
20 public: | 20 public: |
21 // Creates a |MessageInTransit| with the data given by |bytes|/|num_bytes|. | 21 typedef uint16_t Type; |
22 static MessageInTransit* Create(const void* bytes, uint32_t num_bytes); | 22 // Messages that are forwarded to |MessagePipeEndpoint|s. |
23 static const Type TYPE_MESSAGE_PIPE_ENDPOINT = 0; | |
darin (slow to review)
2013/11/06 18:26:00
nit: shouldn't these be formatted using kFooBar st
viettrungluu
2013/11/06 21:13:39
Fixed. (They were enum values before, until I deci
| |
24 // Messages that are forwarded to |MessagePipe|s. | |
25 static const Type TYPE_MESSAGE_PIPE = 1; | |
26 // Messages that are consumed by the channel. | |
27 static const Type TYPE_CHANNEL = 2; | |
28 | |
29 typedef uint16_t Subtype; | |
30 // Subtypes for type |TYPE_MESSAGE_PIPE_ENDPOINT|: | |
31 static const Subtype SUBTYPE_MESSAGE_PIPE_ENDPOINT_DATA = 0; | |
32 // Subtypes for type |TYPE_MESSAGE_PIPE|: | |
33 static const Subtype SUBTYPE_MESSAGE_PIPE_PEER_CLOSED = 0; | |
34 | |
35 typedef uint32_t EndpointId; | |
36 // Never a valid endpoint ID. | |
37 static const EndpointId kInvalidEndpointId = 0; | |
38 | |
39 // Messages (the header and data) must always be aligned to a multiple of this | |
40 // quantity (which must be a power of 2). | |
41 static const size_t kMessageAlignment = 8; | |
42 | |
43 // Creates a |MessageInTransit| of the given |type| and |subtype|, with the | |
44 // data given by |bytes|/|num_bytes|. | |
45 static MessageInTransit* Create(Type type, Subtype subtype, | |
46 const void* bytes, uint32_t num_bytes); | |
23 | 47 |
24 // Destroys a |MessageInTransit| created using |Create()|. | 48 // Destroys a |MessageInTransit| created using |Create()|. |
25 inline void Destroy() { | 49 inline void Destroy() { |
26 // No need to call the destructor, since we're POD. | 50 // No need to call the destructor, since we're POD. |
27 free(this); | 51 free(this); |
28 } | 52 } |
29 | 53 |
30 // Gets the size of the data (in number of bytes). | 54 // Gets the size of the data (in number of bytes). |
31 uint32_t data_size() const { | 55 uint32_t data_size() const { |
32 return size_; | 56 return size_; |
33 } | 57 } |
34 | 58 |
35 // Gets the data (of size |size()| bytes). | 59 // Gets the data (of size |size()| bytes). |
36 const void* data() const { | 60 const void* data() const { |
37 return reinterpret_cast<const char*>(this) + sizeof(*this); | 61 return reinterpret_cast<const char*>(this) + sizeof(*this); |
38 } | 62 } |
39 | 63 |
40 size_t size_with_header_and_padding() const { | 64 size_t size_with_header_and_padding() const { |
41 return RoundUpMessageAlignment(sizeof(*this) + size_); | 65 return RoundUpMessageAlignment(sizeof(*this) + size_); |
42 } | 66 } |
43 | 67 |
68 Type type() const { return type_; } | |
69 Subtype subtype() const { return subtype_; } | |
70 EndpointId source_id() const { return source_id_; } | |
71 EndpointId destination_id() const { return destination_id_; } | |
72 | |
73 void set_type(Type type) { type_ = type; } | |
darin (slow to review)
2013/11/06 18:26:00
note: set_type and set_subtype do not appear to be
viettrungluu
2013/11/06 21:13:39
Thanks, I deleted them (they were left over from w
| |
74 void set_subtype(Subtype subtype) { subtype_ = subtype; } | |
75 void set_source_id(EndpointId source_id) { source_id_ = source_id; } | |
76 void set_destination_id(EndpointId destination_id) { | |
77 destination_id_ = destination_id; | |
78 } | |
79 | |
44 // TODO(vtl): Add whatever's necessary to transport handles. | 80 // TODO(vtl): Add whatever's necessary to transport handles. |
45 | 81 |
46 // Messages (the header and data) must always be aligned to a multiple of this | |
47 // quantity (which must be a power of 2). | |
48 static const size_t kMessageAlignment = 8; | |
49 | |
50 // Rounds |n| up to a multiple of |kMessageAlignment|. | 82 // Rounds |n| up to a multiple of |kMessageAlignment|. |
51 static inline size_t RoundUpMessageAlignment(size_t n) { | 83 static inline size_t RoundUpMessageAlignment(size_t n) { |
52 return (n + kMessageAlignment - 1) & ~(kMessageAlignment - 1); | 84 return (n + kMessageAlignment - 1) & ~(kMessageAlignment - 1); |
53 } | 85 } |
54 | 86 |
55 private: | 87 private: |
56 explicit MessageInTransit(uint32_t size) | 88 explicit MessageInTransit(uint32_t size, Type type, Subtype subtype) |
57 : size_(size), reserved_(0), user_1_(0), user_2_(0) {} | 89 : size_(size), |
90 type_(type), | |
91 subtype_(subtype), | |
92 source_id_(kInvalidEndpointId), | |
93 destination_id_(kInvalidEndpointId) {} | |
58 | 94 |
59 // "Header" for the data. | 95 // "Header" for the data. |
60 uint32_t size_; | 96 uint32_t size_; |
61 uint32_t reserved_; | 97 Type type_; |
62 uint32_t user_1_; | 98 Subtype subtype_; |
63 uint32_t user_2_; | 99 EndpointId source_id_; |
100 EndpointId destination_id_; | |
64 | 101 |
65 // Intentionally unimplemented (and private): Use |Destroy()| instead (which | 102 // Intentionally unimplemented (and private): Use |Destroy()| instead (which |
66 // simply frees the memory). | 103 // simply frees the memory). |
67 ~MessageInTransit(); | 104 ~MessageInTransit(); |
68 | 105 |
69 DISALLOW_COPY_AND_ASSIGN(MessageInTransit); | 106 DISALLOW_COPY_AND_ASSIGN(MessageInTransit); |
70 }; | 107 }; |
71 | 108 |
72 // The size of |MessageInTransit| must be appropriate to maintain alignment of | 109 // The size of |MessageInTransit| must be appropriate to maintain alignment of |
73 // the following data. | 110 // the following data. |
74 COMPILE_ASSERT(sizeof(MessageInTransit) == 16, MessageInTransit_has_wrong_size); | 111 COMPILE_ASSERT(sizeof(MessageInTransit) == 16, MessageInTransit_has_wrong_size); |
75 | 112 |
76 } // namespace system | 113 } // namespace system |
77 } // namespace mojo | 114 } // namespace mojo |
78 | 115 |
79 #endif // MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_ | 116 #endif // MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_ |
OLD | NEW |