@ -288,9 +288,9 @@ public class Framer {
* MACs .
* MACs .
*
*
* @param message The message to frame .
* @param message The message to frame .
* @return The framed message , as byte buffer .
* @param output The { @link ByteBuf } to write framed data to .
* /
* /
public synchronized ByteBuf frame ( final MessageData message ) {
public synchronized void frame ( final MessageData message , final ByteBuf output ) {
Preconditions . checkArgument (
Preconditions . checkArgument (
message . getSize ( ) < LENGTH_MAX_MESSAGE_FRAME , "Message size in excess of maximum length." ) ;
message . getSize ( ) < LENGTH_MAX_MESSAGE_FRAME , "Message size in excess of maximum length." ) ;
// Compress message
// Compress message
@ -306,24 +306,22 @@ public class Framer {
// Construct new, compressed message
// Construct new, compressed message
final ByteBuf compressedBuf = NetworkMemoryPool . allocate ( compressed . length ) ;
final ByteBuf compressedBuf = NetworkMemoryPool . allocate ( compressed . length ) ;
compressedBuf . writeBytes ( compressed ) ;
compressedBuf . writeBytes ( compressed ) ;
return frameAndReleaseMessage ( new RawMessage ( message . getCode ( ) , compressedBuf ) ) ;
frameAndReleaseMessage ( new RawMessage ( message . getCode ( ) , compressedBuf ) , output ) ;
} finally {
} finally {
// We have to release the original message because frameAndRelease only released the
// We have to release the original message because frameAndRelease only released the
// compressed copy.
// compressed copy.
message . release ( ) ;
message . release ( ) ;
}
}
} else {
} else {
return frameAndReleaseMessage ( message ) ;
frameAndReleaseMessage ( message , output ) ;
}
}
}
}
@VisibleForTesting
@VisibleForTesting
ByteBuf frameAndReleaseMessage ( final MessageData message ) {
void frameAndReleaseMessage ( final MessageData message , final ByteBuf buf ) {
try {
try {
final int frameSize = message . getSize ( ) + LENGTH_MESSAGE_ID ;
final int frameSize = message . getSize ( ) + LENGTH_MESSAGE_ID ;
final int pad = padding16 ( frameSize ) ;
final int pad = padding16 ( frameSize ) ;
final int bufSize = LENGTH_FULL_HEADER + ( frameSize + pad + LENGTH_MAC ) ;
final ByteBuf buf = NetworkMemoryPool . allocate ( bufSize ) ;
final byte id = ( byte ) message . getCode ( ) ;
final byte id = ( byte ) message . getCode ( ) ;
@ -343,9 +341,6 @@ public class Framer {
hMac = Arrays . copyOf ( hMac , LENGTH_MAC ) ;
hMac = Arrays . copyOf ( hMac , LENGTH_MAC ) ;
buf . writeBytes ( h ) . writeBytes ( hMac ) ;
buf . writeBytes ( h ) . writeBytes ( hMac ) ;
// Sanity check.
assert buf . writerIndex ( ) = = LENGTH_FULL_HEADER ;
// Encrypt payload.
// Encrypt payload.
final byte [ ] f = new byte [ frameSize + pad ] ;
final byte [ ] f = new byte [ frameSize + pad ] ;
@ -367,11 +362,6 @@ public class Framer {
fMac = Arrays . copyOf ( secrets . updateEgress ( xor ( fMac , fMacSeed ) ) . getEgressMac ( ) , LENGTH_MAC ) ;
fMac = Arrays . copyOf ( secrets . updateEgress ( xor ( fMac , fMacSeed ) ) . getEgressMac ( ) , LENGTH_MAC ) ;
buf . writeBytes ( f ) . writeBytes ( fMac ) ;
buf . writeBytes ( f ) . writeBytes ( fMac ) ;
// Sanity check: all expected bytes are written.
assert buf . writerIndex ( ) = = LENGTH_FULL_HEADER + ( frameSize + pad + LENGTH_MAC ) ;
return buf ;
} finally {
} finally {
message . release ( ) ;
message . release ( ) ;
}
}