| 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; | |
| 10 import org.chromium.mojo.system.MojoResult; | |
| 11 | 9 |
| 12 import java.nio.ByteBuffer; | 10 import java.nio.ByteBuffer; |
| 13 import java.util.List; | 11 import java.util.List; |
| 14 | 12 |
| 15 /** | 13 /** |
| 16 * A raw message to be sent/received from a {@link MessagePipeHandle}. | 14 * A raw message to be sent/received from a {@link MessagePipeHandle}. |
| 17 */ | 15 */ |
| 18 public final class Message { | 16 public final class Message { |
| 19 | 17 |
| 20 /** | 18 /** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 31 * Constructor. | 29 * Constructor. |
| 32 * | 30 * |
| 33 * @param buffer The buffer containing the bytes to send. This must be a dir
ect buffer. | 31 * @param buffer The buffer containing the bytes to send. This must be a dir
ect buffer. |
| 34 * @param handles The list of handles to send. | 32 * @param handles The list of handles to send. |
| 35 */ | 33 */ |
| 36 public Message(ByteBuffer buffer, List<? extends Handle> handles) { | 34 public Message(ByteBuffer buffer, List<? extends Handle> handles) { |
| 37 assert buffer.isDirect(); | 35 assert buffer.isDirect(); |
| 38 this.buffer = buffer; | 36 this.buffer = buffer; |
| 39 this.handles = handles; | 37 this.handles = handles; |
| 40 } | 38 } |
| 41 | |
| 42 /** | |
| 43 * Read a message, and pass it to the given |MessageReceiver| if not null. I
f the | |
| 44 * |MessageReceiver| is null, the message is lost. | |
| 45 * | |
| 46 * @param receiver The {@link MessageReceiver} that will receive the read {@
link Message}. Can | |
| 47 * be <code>null</code>, in which case the message is discarded. | |
| 48 */ | |
| 49 public static int readAndDispatchMessage(MessagePipeHandle handle, MessageRe
ceiver receiver) { | |
| 50 // TODO(qsr) Allow usage of a pool of pre-allocated buffer for performan
ce. | |
| 51 ReadMessageResult result = handle.readMessage(null, 0, MessagePipeHandle
.ReadFlags.NONE); | |
| 52 if (result.getMojoResult() != MojoResult.RESOURCE_EXHAUSTED) { | |
| 53 return result.getMojoResult(); | |
| 54 } | |
| 55 ByteBuffer buffer = ByteBuffer.allocateDirect(result.getMessageSize()); | |
| 56 result = handle.readMessage(buffer, result.getHandlesCount(), | |
| 57 MessagePipeHandle.ReadFlags.NONE); | |
| 58 if (receiver != null && result.getMojoResult() == MojoResult.OK) { | |
| 59 receiver.accept(new Message(buffer, result.getHandles())); | |
| 60 } | |
| 61 return result.getMojoResult(); | |
| 62 } | |
| 63 } | 39 } |
| OLD | NEW |