Added a magic number to the factorial protocol
This commit is contained in:
parent
ce0d983825
commit
0b16006fd7
@ -20,12 +20,14 @@ import java.math.BigInteger;
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.channel.Channel;
|
||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||
import org.jboss.netty.handler.codec.frame.CorruptedFrameException;
|
||||
import org.jboss.netty.handler.codec.frame.FrameDecoder;
|
||||
|
||||
/**
|
||||
* Decodes the binary representation of a {@link BigInteger} with 32-bit
|
||||
* integer length prefix into a Java {@link BigInteger} instance. For example,
|
||||
* { 0, 0, 0, 1, 42 } will be decoded into new BigInteger("42").
|
||||
* Decodes the binary representation of a {@link BigInteger} prepended
|
||||
* with a magic number ('F' or 0x46) and a 32-bit integer length prefix into a
|
||||
* {@link BigInteger} instance. For example, { 'F', 0, 0, 0, 1, 42 } will be
|
||||
* decoded into new BigInteger("42").
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
@ -38,20 +40,27 @@ public class BigIntegerDecoder extends FrameDecoder {
|
||||
protected Object decode(
|
||||
ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
|
||||
// Wait until the length prefix is available.
|
||||
if (buffer.readableBytes() < 4) {
|
||||
if (buffer.readableBytes() < 5) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int dataLength = buffer.getInt(buffer.readerIndex());
|
||||
buffer.markReaderIndex();
|
||||
|
||||
// Check the magic number.
|
||||
int magicNumber = buffer.readUnsignedByte();
|
||||
if (magicNumber != 'F') {
|
||||
buffer.resetReaderIndex();
|
||||
throw new CorruptedFrameException(
|
||||
"Invalid magic number: " + magicNumber);
|
||||
}
|
||||
|
||||
// Wait until the whole data is available.
|
||||
if (buffer.readableBytes() < dataLength + 4) {
|
||||
int dataLength = buffer.readInt();
|
||||
if (buffer.readableBytes() < dataLength) {
|
||||
buffer.resetReaderIndex();
|
||||
return null;
|
||||
}
|
||||
|
||||
// Skip the length field because we know it already.
|
||||
buffer.skipBytes(4);
|
||||
|
||||
// Convert the received data into a new BigInteger.
|
||||
byte[] decoded = new byte[dataLength];
|
||||
buffer.readBytes(decoded);
|
||||
|
@ -24,8 +24,9 @@ import org.jboss.netty.channel.ChannelHandlerContext;
|
||||
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
|
||||
|
||||
/**
|
||||
* Encodes a {@link Number} into the binary representation with a 32-bit length
|
||||
* prefix. For example, 42 will be encoded to { 0, 0, 0, 1, 42 }.
|
||||
* Encodes a {@link Number} into the binary representation prepended with
|
||||
* a magic number ('F' or 0x46) and a 32-bit length prefix. For example, 42
|
||||
* will be encoded to { 'F', 0, 0, 0, 1, 42 }.
|
||||
*
|
||||
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
|
||||
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
|
||||
@ -55,10 +56,11 @@ public class NumberEncoder extends OneToOneEncoder {
|
||||
byte[] data = v.toByteArray();
|
||||
int dataLength = data.length;
|
||||
|
||||
// Construct a message with a length header.
|
||||
// Construct a message.
|
||||
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
|
||||
buf.writeInt(dataLength);
|
||||
buf.writeBytes(data);
|
||||
buf.writeByte((byte) 'F'); // magic number
|
||||
buf.writeInt(dataLength); // data length
|
||||
buf.writeBytes(data); // data
|
||||
|
||||
// Return the constructed message.
|
||||
return buf;
|
||||
|
Loading…
Reference in New Issue
Block a user