OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 BASE_MACH_IPC_MAC_H_ | 5 #ifndef BASE_MACH_IPC_MAC_H_ |
6 #define BASE_MACH_IPC_MAC_H_ | 6 #define BASE_MACH_IPC_MAC_H_ |
7 | 7 |
8 #include <mach/mach.h> | 8 #include <mach/mach.h> |
9 #include <mach/message.h> | 9 #include <mach/message.h> |
10 #include <servers/bootstrap.h> | 10 #include <servers/bootstrap.h> |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // // send to already named port | 56 // // send to already named port |
57 // MachPortSender sender("com.Google.MyService"); | 57 // MachPortSender sender("com.Google.MyService"); |
58 // MachSendMessage message(57); // our message ID is 57 | 58 // MachSendMessage message(57); // our message ID is 57 |
59 // | 59 // |
60 // // add some ports to be translated for us | 60 // // add some ports to be translated for us |
61 // message.AddDescriptor(mach_task_self()); // our task | 61 // message.AddDescriptor(mach_task_self()); // our task |
62 // message.AddDescriptor(mach_thread_self()); // this thread | 62 // message.AddDescriptor(mach_thread_self()); // this thread |
63 // | 63 // |
64 // char messageString[] = "Hello server!\n"; | 64 // char messageString[] = "Hello server!\n"; |
65 // message.SetData(messageString, strlen(messageString)+1); | 65 // message.SetData(messageString, strlen(messageString)+1); |
66 // | 66 // // timeout 1000ms |
67 // kern_return_t result = sender.SendMessage(message, 1000); // timeout 1000m
s | 67 // kern_return_t result = sender.SendMessage(message, 1000); |
68 // | 68 // |
69 | 69 |
70 #define PRINT_MACH_RESULT(result_, message_) \ | 70 #define PRINT_MACH_RESULT(result_, message_) \ |
71 printf(message_" %s (%d)\n", mach_error_string(result_), result_ ); | 71 printf(message_" %s (%d)\n", mach_error_string(result_), result_ ); |
72 | 72 |
73 //============================================================================== | 73 //============================================================================== |
74 // A wrapper class for mach_msg_port_descriptor_t (with same memory layout) | 74 // A wrapper class for mach_msg_port_descriptor_t (with same memory layout) |
75 // with convenient constructors and accessors | 75 // with convenient constructors and accessors |
76 class MachMsgPortDescriptor : public mach_msg_port_descriptor_t { | 76 class MachMsgPortDescriptor : public mach_msg_port_descriptor_t { |
77 public: | 77 public: |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 187 |
188 // Constructor for use with preallocate storage. | 188 // Constructor for use with preallocate storage. |
189 // storage_length must be >= EmptyMessageSize() | 189 // storage_length must be >= EmptyMessageSize() |
190 MachMessage(void *storage, size_t storage_length); | 190 MachMessage(void *storage, size_t storage_length); |
191 | 191 |
192 friend class ReceivePort; | 192 friend class ReceivePort; |
193 friend class MachPortSender; | 193 friend class MachPortSender; |
194 | 194 |
195 // Represents raw data in our message | 195 // Represents raw data in our message |
196 struct MessageDataPacket { | 196 struct MessageDataPacket { |
197 int32_t id; // little-endian | 197 int32_t id; // little-endian |
198 int32_t data_length;» // little-endian | 198 int32_t data_length; // little-endian |
199 u_int8_t data[1]; // actual size limited by storage_length_bytes_ | 199 u_int8_t data[1]; // actual size limited by storage_length_bytes_ |
200 }; | 200 }; |
201 | 201 |
202 MessageDataPacket* GetDataPacket(); | 202 MessageDataPacket* GetDataPacket(); |
203 | 203 |
204 void SetDescriptorCount(int n); | 204 void SetDescriptorCount(int n); |
205 void SetDescriptor(int n, const MachMsgPortDescriptor &desc); | 205 void SetDescriptor(int n, const MachMsgPortDescriptor &desc); |
206 | 206 |
207 // Returns total message size setting msgh_size in the header to this value | 207 // Returns total message size setting msgh_size in the header to this value |
208 int CalculateSize(); | 208 int CalculateSize(); |
209 | 209 |
210 // Returns total storage size that this object can grow to, this is inclusive | 210 // Returns total storage size that this object can grow to, this is inclusive |
211 // of the mach header. | 211 // of the mach header. |
212 size_t MaxSize() const { return storage_length_bytes_; } | 212 size_t MaxSize() const { return storage_length_bytes_; } |
213 | 213 |
214 protected: | 214 protected: |
215 mach_msg_header_t *Head() { return &(storage_->head); } | 215 mach_msg_header_t *Head() { return &(storage_->head); } |
216 | 216 |
217 private: | 217 private: |
218 struct MachMessageData { | 218 struct MachMessageData { |
219 mach_msg_header_t head; | 219 mach_msg_header_t head; |
220 mach_msg_body_t body; | 220 mach_msg_body_t body; |
221 // descriptors and data may be embedded here. | 221 // descriptors and data may be embedded here. |
222 u_int8_t padding[1024]; | 222 u_int8_t padding[1024]; |
223 }; | 223 }; |
224 | 224 |
225 // kEmptyMessageSize needs to have the definition of MachMessageData before it.
NNN | 225 // kEmptyMessageSize needs to have the definition of MachMessageData before |
| 226 // it. |
226 public: | 227 public: |
227 // The size of an empty message with no data. | 228 // The size of an empty message with no data. |
228 static const size_t kEmptyMessageSize = sizeof(mach_msg_header_t) + | 229 static const size_t kEmptyMessageSize = sizeof(mach_msg_header_t) + |
229 sizeof(mach_msg_body_t) + | 230 sizeof(mach_msg_body_t) + |
230 sizeof(MessageDataPacket); | 231 sizeof(MessageDataPacket); |
231 | 232 |
232 private: | 233 private: |
233 MachMessageData *storage_; | 234 MachMessageData *storage_; |
234 size_t storage_length_bytes_; | 235 size_t storage_length_bytes_; |
235 bool own_storage_; // Is storage owned by this object? | 236 bool own_storage_; // Is storage owned by this object? |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 mach_msg_timeout_t timeout); | 311 mach_msg_timeout_t timeout); |
311 | 312 |
312 private: | 313 private: |
313 mach_port_t send_port_; | 314 mach_port_t send_port_; |
314 kern_return_t init_result_; | 315 kern_return_t init_result_; |
315 | 316 |
316 DISALLOW_COPY_AND_ASSIGN(MachPortSender); | 317 DISALLOW_COPY_AND_ASSIGN(MachPortSender); |
317 }; | 318 }; |
318 | 319 |
319 #endif // BASE_MACH_IPC_MAC_H_ | 320 #endif // BASE_MACH_IPC_MAC_H_ |
OLD | NEW |