OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 package org.chromium.mojo.bindings; | 5 package org.chromium.mojo.bindings; |
6 | 6 |
7 import org.chromium.mojo.system.Handle; | 7 import org.chromium.mojo.system.Handle; |
8 import org.chromium.mojo.system.MessagePipeHandle; | 8 import org.chromium.mojo.system.MessagePipeHandle; |
9 import org.chromium.mojo.system.MessagePipeHandle.ReadMessageResult; | 9 import org.chromium.mojo.system.MessagePipeHandle.ReadMessageResult; |
10 import org.chromium.mojo.system.MojoResult; | 10 import org.chromium.mojo.system.MojoResult; |
11 | 11 |
12 import java.nio.ByteBuffer; | 12 import java.nio.ByteBuffer; |
13 import java.nio.ByteOrder; | |
13 import java.util.List; | 14 import java.util.List; |
14 | 15 |
15 import javax.annotation.Nullable; | 16 import javax.annotation.Nullable; |
16 | 17 |
17 /** | 18 /** |
18 * A raw message to be sent/received from a {@link MessagePipeHandle}. | 19 * A raw message to be sent/received from a {@link MessagePipeHandle}. |
19 */ | 20 */ |
20 public final class Message { | 21 public final class Message { |
21 | 22 |
22 /** | 23 /** |
23 * The data of the message. | 24 * The data of the message. |
24 */ | 25 */ |
25 public final ByteBuffer buffer; | 26 public final ByteBuffer buffer; |
26 | 27 |
27 /** | 28 /** |
28 * The handles of the message. | 29 * The handles of the message. |
29 */ | 30 */ |
30 public final List<? extends Handle> handles; | 31 public final List<? extends Handle> handles; |
31 | 32 |
33 private MessageHeader mHeader = null; | |
34 private Message mPayload = null; | |
35 | |
32 /** | 36 /** |
33 * Constructor. | 37 * Constructor. |
34 * | 38 * |
35 * @param buffer The buffer containing the bytes to send. This must be a dir ect buffer. | 39 * @param buffer The buffer containing the bytes to send. This must be a dir ect buffer. |
36 * @param handles The list of handles to send. | 40 * @param handles The list of handles to send. |
37 */ | 41 */ |
38 public Message(ByteBuffer buffer, List<? extends Handle> handles) { | 42 public Message(ByteBuffer buffer, List<? extends Handle> handles) { |
39 assert buffer.isDirect(); | 43 assert buffer.isDirect(); |
40 this.buffer = buffer; | 44 this.buffer = buffer; |
41 this.handles = handles; | 45 this.handles = handles; |
(...skipping 11 matching lines...) Expand all Loading... | |
53 return result.getMojoResult(); | 57 return result.getMojoResult(); |
54 } | 58 } |
55 ByteBuffer buffer = ByteBuffer.allocateDirect(result.getMessageSize()); | 59 ByteBuffer buffer = ByteBuffer.allocateDirect(result.getMessageSize()); |
56 result = handle.readMessage(buffer, result.getHandlesCount(), | 60 result = handle.readMessage(buffer, result.getHandlesCount(), |
57 MessagePipeHandle.ReadFlags.NONE); | 61 MessagePipeHandle.ReadFlags.NONE); |
58 if (receiver != null && result.getMojoResult() == MojoResult.OK) { | 62 if (receiver != null && result.getMojoResult() == MojoResult.OK) { |
59 receiver.accept(new Message(buffer, result.getHandles())); | 63 receiver.accept(new Message(buffer, result.getHandles())); |
60 } | 64 } |
61 return result.getMojoResult(); | 65 return result.getMojoResult(); |
62 } | 66 } |
67 | |
68 /** | |
69 * Returns the header of the given message. This should only be called on me ssage that are known | |
70 * to have a header. This will throw a {@link DeserializationException} it t he start of the | |
rmcilroy
2014/07/10 19:03:57
/s/it/if
qsr
2014/07/11 11:42:07
Done.
| |
71 * message is not a valid header. | |
72 */ | |
73 public MessageHeader getHeader() { | |
rmcilroy
2014/07/10 19:03:57
I'm not sure I like the idea of a Message optional
qsr
2014/07/11 11:42:07
Done, except that I need to use header in all the
| |
74 if (mHeader == null) { | |
75 mHeader = new org.chromium.mojo.bindings.MessageHeader(this); | |
76 } | |
77 return mHeader; | |
78 } | |
79 | |
80 /** | |
81 * Returns the payload of the message. This should only be called on message that are known to | |
82 * have a header. | |
83 */ | |
84 public Message getPayload() { | |
85 if (mPayload == null) { | |
86 ByteBuffer truncatedBuffer = ((ByteBuffer) buffer.position( | |
87 getHeader().getSize())).slice(); | |
88 truncatedBuffer.order(ByteOrder.nativeOrder()); | |
89 mPayload = new Message(truncatedBuffer, handles); | |
90 } | |
91 return mPayload; | |
92 } | |
93 | |
94 /** | |
95 * Reset the cache header. Used by {@link MessageHeader} when it modified th e message buffer. | |
96 */ | |
97 void resetHeader() { | |
rmcilroy
2014/07/10 19:03:57
Having to remember to resetHeader also seems error
qsr
2014/07/11 11:42:07
Done.
| |
98 mHeader = null; | |
99 mPayload = null; | |
100 } | |
101 | |
63 } | 102 } |
OLD | NEW |