Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: mojo/system/message_in_transit.h

Issue 60103005: Mojo: First stab at making MessagePipes work across OS pipes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 kTypeMessagePipeEndpoint = 0;
24 // Messages that are forwarded to |MessagePipe|s.
25 static const Type kTypeMessagePipe = 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 |kTypeMessagePipeEndpoint|:
31 static const Subtype kSubtypeMessagePipeEndpointData = 0;
32 // Subtypes for type |kTypeMessagePipe|:
33 static const Subtype kSubtypeMessagePipePeerClosed = 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_source_id(EndpointId source_id) { source_id_ = source_id; }
74 void set_destination_id(EndpointId destination_id) {
75 destination_id_ = destination_id;
76 }
77
44 // TODO(vtl): Add whatever's necessary to transport handles. 78 // TODO(vtl): Add whatever's necessary to transport handles.
45 79
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|. 80 // Rounds |n| up to a multiple of |kMessageAlignment|.
51 static inline size_t RoundUpMessageAlignment(size_t n) { 81 static inline size_t RoundUpMessageAlignment(size_t n) {
52 return (n + kMessageAlignment - 1) & ~(kMessageAlignment - 1); 82 return (n + kMessageAlignment - 1) & ~(kMessageAlignment - 1);
53 } 83 }
54 84
55 private: 85 private:
56 explicit MessageInTransit(uint32_t size) 86 explicit MessageInTransit(uint32_t size, Type type, Subtype subtype)
57 : size_(size), reserved_(0), user_1_(0), user_2_(0) {} 87 : size_(size),
88 type_(type),
89 subtype_(subtype),
90 source_id_(kInvalidEndpointId),
91 destination_id_(kInvalidEndpointId) {}
58 92
59 // "Header" for the data. 93 // "Header" for the data.
60 uint32_t size_; 94 uint32_t size_;
61 uint32_t reserved_; 95 Type type_;
62 uint32_t user_1_; 96 Subtype subtype_;
63 uint32_t user_2_; 97 EndpointId source_id_;
98 EndpointId destination_id_;
64 99
65 // Intentionally unimplemented (and private): Use |Destroy()| instead (which 100 // Intentionally unimplemented (and private): Use |Destroy()| instead (which
66 // simply frees the memory). 101 // simply frees the memory).
67 ~MessageInTransit(); 102 ~MessageInTransit();
68 103
69 DISALLOW_COPY_AND_ASSIGN(MessageInTransit); 104 DISALLOW_COPY_AND_ASSIGN(MessageInTransit);
70 }; 105 };
71 106
72 // The size of |MessageInTransit| must be appropriate to maintain alignment of 107 // The size of |MessageInTransit| must be appropriate to maintain alignment of
73 // the following data. 108 // the following data.
74 COMPILE_ASSERT(sizeof(MessageInTransit) == 16, MessageInTransit_has_wrong_size); 109 COMPILE_ASSERT(sizeof(MessageInTransit) == 16, MessageInTransit_has_wrong_size);
75 110
76 } // namespace system 111 } // namespace system
77 } // namespace mojo 112 } // namespace mojo
78 113
79 #endif // MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_ 114 #endif // MOJO_SYSTEM_MESSAGE_IN_TRANSIT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698