com.jme3.network.base
Class MessageProtocol

java.lang.Object
  extended by com.jme3.network.base.MessageProtocol

public class MessageProtocol
extends java.lang.Object

Consolidates the conversion of messages to/from byte buffers and provides a rolling message buffer. ByteBuffers can be pushed in and messages will be extracted, accumulated, and available for retrieval. This is not thread safe and is meant to be used within a single message processing thread.

The protocol is based on a simple length + data format where two bytes represent the (short) length of the data and the rest is the raw data for the Serializers class.


Constructor Summary
MessageProtocol()
           
 
Method Summary
 int addBuffer(java.nio.ByteBuffer buffer)
          Adds the specified buffer, extracting the contained messages and making them available to getMessage().
protected  void createMessage(java.nio.ByteBuffer buffer)
          Creates a message from the properly sized byte buffer and adds it to the messages queue.
 Message getMessage()
          Retrieves and removes an extracted message from the accumulated buffer or returns null if there are no more messages.
static java.nio.ByteBuffer messageToBuffer(Message message, java.nio.ByteBuffer target)
          Converts a message to a ByteBuffer using the Serializer and the (short length) + data protocol.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageProtocol

public MessageProtocol()
Method Detail

messageToBuffer

public static java.nio.ByteBuffer messageToBuffer(Message message,
                                                  java.nio.ByteBuffer target)
Converts a message to a ByteBuffer using the Serializer and the (short length) + data protocol. If target is null then a 32k byte buffer will be created and filled.


getMessage

public Message getMessage()
Retrieves and removes an extracted message from the accumulated buffer or returns null if there are no more messages.


addBuffer

public int addBuffer(java.nio.ByteBuffer buffer)
Adds the specified buffer, extracting the contained messages and making them available to getMessage(). The left over data is buffered to be combined with future data. &

Returns:
The total number of queued messages after this call.

createMessage

protected void createMessage(java.nio.ByteBuffer buffer)
Creates a message from the properly sized byte buffer and adds it to the messages queue.