Added a magic number to the factorial protocol

This commit is contained in:
Trustin Lee 2010-02-02 03:13:15 +00:00
parent ce0d983825
commit 0b16006fd7
2 changed files with 25 additions and 14 deletions

View File

@ -20,12 +20,14 @@ import java.math.BigInteger;
import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.CorruptedFrameException;
import org.jboss.netty.handler.codec.frame.FrameDecoder; import org.jboss.netty.handler.codec.frame.FrameDecoder;
/** /**
* Decodes the binary representation of a {@link BigInteger} with 32-bit * Decodes the binary representation of a {@link BigInteger} prepended
* integer length prefix into a Java {@link BigInteger} instance. For example, * with a magic number ('F' or 0x46) and a 32-bit integer length prefix into a
* { 0, 0, 0, 1, 42 } will be decoded into new BigInteger("42"). * {@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://www.jboss.org/netty/">The Netty Project</a>
* @author <a href="http://gleamynode.net/">Trustin Lee</a> * @author <a href="http://gleamynode.net/">Trustin Lee</a>
@ -38,20 +40,27 @@ public class BigIntegerDecoder extends FrameDecoder {
protected Object decode( protected Object decode(
ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
// Wait until the length prefix is available. // Wait until the length prefix is available.
if (buffer.readableBytes() < 4) { if (buffer.readableBytes() < 5) {
return null; 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. // Wait until the whole data is available.
if (buffer.readableBytes() < dataLength + 4) { int dataLength = buffer.readInt();
if (buffer.readableBytes() < dataLength) {
buffer.resetReaderIndex();
return null; return null;
} }
// Skip the length field because we know it already.
buffer.skipBytes(4);
// Convert the received data into a new BigInteger. // Convert the received data into a new BigInteger.
byte[] decoded = new byte[dataLength]; byte[] decoded = new byte[dataLength];
buffer.readBytes(decoded); buffer.readBytes(decoded);

View File

@ -24,8 +24,9 @@ import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
/** /**
* Encodes a {@link Number} into the binary representation with a 32-bit length * Encodes a {@link Number} into the binary representation prepended with
* prefix. For example, 42 will be encoded to { 0, 0, 0, 1, 42 }. * 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://www.jboss.org/netty/">The Netty Project</a>
* @author <a href="http://gleamynode.net/">Trustin Lee</a> * @author <a href="http://gleamynode.net/">Trustin Lee</a>
@ -55,10 +56,11 @@ public class NumberEncoder extends OneToOneEncoder {
byte[] data = v.toByteArray(); byte[] data = v.toByteArray();
int dataLength = data.length; int dataLength = data.length;
// Construct a message with a length header. // Construct a message.
ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
buf.writeInt(dataLength); buf.writeByte((byte) 'F'); // magic number
buf.writeBytes(data); buf.writeInt(dataLength); // data length
buf.writeBytes(data); // data
// Return the constructed message. // Return the constructed message.
return buf; return buf;