com.perforce.p4java.impl.mapbased.rpc.packet
Class RpcPacketPreamble

java.lang.Object
  extended by com.perforce.p4java.impl.mapbased.rpc.packet.RpcPacketPreamble

public class RpcPacketPreamble
extends Object

The five byte preamble appended to every text packet payload.

The format (as divined from the C++ API) is designed to provide a very simple sanity check checksum and encode the length in bytes of the accompanying RPC payload:

      byte[1] = ( payload_length / 0x1 ) % 0x100;
      byte[2] = ( payload_length / 0x100 ) % 0x100;
      byte[3] = ( payload_length / 0x10000 ) % 0x100;
      byte[4] = ( payload_length / 0x1000000 ) % 0x100;
      byte[0] = byte[1] ^ byte[2] ^ byte[3] ^ byte[4];
 
This can generally only be calculated after the other packet elements have been serialized, which is irritating but not too annoying.


Field Summary
static int RPC_PREAMBLE_CHKSUM_SIZE
          Size in bytes of the preamble checksum.
static int RPC_PREAMBLE_SIZE
          The size in bytes of the standard text packet RPC packet preamble.
 
Method Summary
static RpcPacketPreamble constructPreamble(ByteBuffer payload)
          Calculate and construct a suitable preamble for the passed-in payload buffer.
static RpcPacketPreamble constructPreamble(int payloadLength)
          Calculate and construct a suitable preamble for the passed-in payload buffer length.
 int getPayloadSize()
          Return the payload size (in bytes) specified by this preamble.
 boolean isValidChecksum()
          Return true iff the simple checksum checks out.
 ByteBuffer marshal()
          Return a ByteBuffer representing the marshaled version of this preamble.
 byte[] marshalAsBytes()
           
static RpcPacketPreamble retrievePreamble(byte[] bytes)
          Retrieve the preamble from raw bytes.
static RpcPacketPreamble retrievePreamble(ByteBuffer payload)
          Retrieve a preamble from the passed-in payload byte buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RPC_PREAMBLE_CHKSUM_SIZE

public static final int RPC_PREAMBLE_CHKSUM_SIZE
Size in bytes of the preamble checksum. This is a very fundamental value; changing it will probably cause total destruction within P4Java...

See Also:
Constant Field Values

RPC_PREAMBLE_SIZE

public static final int RPC_PREAMBLE_SIZE
The size in bytes of the standard text packet RPC packet preamble.

See Also:
Constant Field Values
Method Detail

constructPreamble

public static RpcPacketPreamble constructPreamble(ByteBuffer payload)
Calculate and construct a suitable preamble for the passed-in payload buffer. Does not affect the incoming buffer at all (i.e. marks and limits, etc. are unaffected). Assumes the payload starts at buffer byte position zero and uses the buffer's limit as the length.

Parameters:
payload - non-null byte buffer representing the payload
Returns:
new RpcPacketPreamble for the payload

constructPreamble

public static RpcPacketPreamble constructPreamble(int payloadLength)
Calculate and construct a suitable preamble for the passed-in payload buffer length.


retrievePreamble

public static RpcPacketPreamble retrievePreamble(ByteBuffer payload)
Retrieve a preamble from the passed-in payload byte buffer. Will move the byte buffer pointer accordingly.

Parameters:
payload - non-null payload
Returns:
new RpcPacketPreamble as retrieved from the payload buffer.

retrievePreamble

public static RpcPacketPreamble retrievePreamble(byte[] bytes)
Retrieve the preamble from raw bytes. Most sanity checking is done in the RpcPacketPreamble constructor.


getPayloadSize

public int getPayloadSize()
Return the payload size (in bytes) specified by this preamble.

Returns:
the associated payload size in bytes.

isValidChecksum

public boolean isValidChecksum()
Return true iff the simple checksum checks out.


marshal

public ByteBuffer marshal()
Return a ByteBuffer representing the marshaled version of this preamble. Buffer will need to be flipped before sending...

Returns:
non-null ByteBuffer ready for sending

marshalAsBytes

public byte[] marshalAsBytes()


Copyright © 2015 Perforce Software. All Rights Reserved.