Guest User

Untitled

a guest
Jun 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package co.xxx.protocol.handler.codec;
  2.  
  3. import co.xxx.protocol.handler.codec.command.CommandFactory;
  4. import org.jboss.netty.buffer.ChannelBuffer;
  5. import org.jboss.netty.channel.Channel;
  6. import org.jboss.netty.channel.ChannelHandlerContext;
  7. import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
  8. import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
  9.  
  10. /**
  11.  * Created by IntelliJ IDEA.
  12.  * User: Keyston
  13.  * Date: 12/1/11
  14.  * Time: 2:31 PM
  15.  */
  16. public class ProtocolDecoder extends ReplayingDecoder<ProtocolDecoder.DecoderState>
  17. {
  18.  
  19.  
  20.     private Header header;
  21.  
  22.  
  23.     public ProtocolDecoder()
  24.     {
  25.         super(true);
  26.         this.reset();
  27.  
  28.     }
  29.  
  30.  
  31.     private void reset()
  32.     {
  33.  
  34.         header = new Header();
  35.  
  36.         checkpoint(DecoderState.READ_PROTOCOL_ID);
  37.     }
  38.  
  39.     @Override
  40.     protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, DecoderState state) throws Exception
  41.     {
  42.         switch (state) {
  43.             case READ_PROTOCOL_ID:
  44.  
  45.                 byte[] bytes = new byte[4];
  46.                 buffer.readBytes(bytes);
  47.                 header.protocolId = new String(bytes);
  48.                 checkpoint(DecoderState.READ_SIZE);
  49.             case READ_SIZE:
  50.                 header.structureSize = buffer.readShort();
  51.  
  52.                 checkpoint(DecoderState.READ_STATUS);
  53.             case READ_STATUS:
  54.                 // status, not used
  55.                 buffer.readInt();
  56.                 checkpoint(DecoderState.READ_COMMAND);
  57.             case READ_COMMAND:
  58.                 header.command = Command.fromShort(buffer.readShort());
  59.                 checkpoint(DecoderState.READ_MESSAGE_ID);
  60.             case READ_MESSAGE_ID:
  61.                 header.messageId = buffer.readLong();
  62.                 checkpoint(DecoderState.READ_SESSION_ID);
  63.             case READ_SESSION_ID:
  64.                 header.sessionId = buffer.readLong();
  65.                 checkpoint(DecoderState.READ_BODY);
  66.             case READ_BODY:
  67.  
  68.                 SimpleChannelUpstreamHandler handler = CommandFactory.get(header.command);
  69.                 if (ctx.getPipeline().getNames().indexOf("0") != -1) {
  70.  
  71.                     ctx.getPipeline().addAfter("0", "bodyDecoder", handler);
  72.                 } else {
  73.                     ctx.getPipeline().addLast("bodyDecoder", handler);
  74.                 }
  75.                 ctx.getPipeline().remove(this);
  76.                 if (handler != null) {
  77.                     // TODO dispatch error and close channel
  78.                 }
  79.  
  80.  
  81.                 try {
  82.                     if (buffer.readable()) {
  83.                         return new Object[]{
  84.                                 header,
  85.                                 buffer.readBytes(super.actualReadableBytes())
  86.                         };
  87.                     } else {
  88.                         return header;
  89.                     }
  90.                 } finally {
  91.  
  92.                     this.reset();
  93.                 }
  94.  
  95.  
  96.             default:
  97.                 throw new Error("Shouldn't reach here");
  98.  
  99.  
  100.         }
  101.     }
  102.  
  103.  
  104.     public enum DecoderState
  105.     {
  106.         READ_PROTOCOL_ID,
  107.         READ_SIZE,
  108.         READ_STATUS,
  109.         READ_COMMAND,
  110.         READ_MESSAGE_ID,
  111.         READ_SESSION_ID,
  112.         READ_BODY
  113.     }
  114. }
Add Comment
Please, Sign In to add comment