Netty optimization and Protocol help.
bantai
superman at bantai.com
Mon Aug 17 12:58:00 EDT 2009
Thanks for your reply Trustin,
I looked at the factorial example and created my decoder and encoder:
---------------------------
@ChannelPipelineCoverage("one")
public class JsonDecoder extends FrameDecoder {
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
ChannelBuffer buffer) throws Exception {
// Wait until the length prefix is available.
if (buffer.readableBytes() < 4) {
return null;
}
int dataLength = buffer.getInt(buffer.readerIndex());
// Wait until the whole data is available.
if (buffer.readableBytes() < dataLength + 4) {
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);
return new String(decoded);
}
}
---------------------------
@ChannelPipelineCoverage("all")
public class JsonEncoder extends OneToOneEncoder {
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object
msg) throws Exception {
if (!(msg instanceof String)) {
// Ignore what this encoder can't encode.
return msg;
}
byte[] data = ((String) msg).getBytes();
int dataLength = data.length;
// Construct a message with a length header.
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
buf.writeInt(dataLength);
buf.writeBytes(data);
// Return the constructed message.
return buf;
}
}
---------------------------
I also created one ChannelFactory and injected it into my JsonClient. The
initial bootstrap.connect takes about 100ms and consequent calls are very
quick.
I agree with your view on premature optimization so will have to compare my
results using HTTP and my own JSON protocol.
Thanks again for your help. Don't hesitate to add more tips :)
--
View this message in context: http://n2.nabble.com/Netty-optimization-and-Protocol-help.-tp3447755p3460557.html
Sent from the Netty User Group mailing list archive at Nabble.com.
More information about the netty-users
mailing list