Encoder for binary protocol

brunodecarvalho kindernade at gmail.com
Sat Jul 24 13:01:26 EDT 2010


Deb,


If I understood correctly, your StringBuffer already contains the encoded
message and you only need to transfer the content bytes to a ChannelBuffer,
avoiding memory copies?

If so, is it an application requirement to encode messages to a StringBuffer
and receive StringBuffers? Why not have your messages as something like

public class YourMessage {
  private byte code;
  private long length; // only if message is allowed to have lengths >
Integer.MAX_VALUE
  private int opcode;
  private String a;
  private int b;
  ...

  ...
}

and then implementing a OneToOneEncoder (and a ReplayingDecoder) that
convert YourMessage into a ChannelBuffer (and vice-versa)?

Your pipeline would go:

...
pipeline.addLast("encoder", new YourMessageEncoder());
pipeline.addLast("decoder", new YourMessageDecoder());
pipeline.addLast("handler", new YourAppHandler());

and you would always write YourMessage instances to Netty's Channels and
receive YourMessage in your app handler, by far easier to handle than a
StringBuffer (or the faster non-threadsafe version, StringBuilder) ;)

Pardon me if I didn't understand your question correctly.


Cheers,
  Bruno
-- 
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Encoder-for-binary-protocol-tp5332889p5333227.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list