Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package testNettyServer;
  2.  
  3. import java.util.List;
  4.  
  5. import io.netty.buffer.ByteBuf;
  6. import io.netty.buffer.Unpooled;
  7. import io.netty.channel.ChannelFutureListener;
  8. import io.netty.channel.ChannelHandlerContext;
  9. import io.netty.channel.ChannelPipeline;
  10. import io.netty.handler.codec.ByteToMessageDecoder;
  11. import io.netty.util.CharsetUtil;
  12. import static io.netty.buffer.Unpooled.*;
  13.  
  14. public class PacketDecoder extends ByteToMessageDecoder
  15. {
  16.    
  17.     int readd = 0;
  18.     boolean isFlashPolicy = true;
  19.  
  20.     @Override
  21.     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
  22.     {
  23.         if (in.writerIndex()-readd<2)
  24.         {
  25.             return;
  26.         }
  27.  
  28.         if(isFlashPolicy)
  29.         {
  30.             isFlashPolicy=false;
  31.             final int magic1 = in.getUnsignedByte(in.readerIndex());
  32.             final int magic2 = in.getUnsignedByte(in.readerIndex() + 1);
  33.             boolean isFlashPolicyRequest = (magic1 == '<' && magic2 == 'p');
  34.  
  35.             if (isFlashPolicyRequest)
  36.             {
  37.                 String XML = "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>";
  38.                 ByteBuf policyResponse = Unpooled.copiedBuffer(XML, CharsetUtil.UTF_8);
  39.  
  40.                 in.skipBytes(in.readableBytes());
  41.  
  42.                 removeAllPipelineHandlers(ctx.pipeline());
  43.                 ctx.writeAndFlush(policyResponse).addListener(ChannelFutureListener.CLOSE);
  44.  
  45.                 return;
  46.             }
  47.         }
  48.  
  49.         while((in.writerIndex()-readd>=2)&&((in.getShort(readd)>0 && in.writerIndex()-readd >= in.getShort(readd))||in.getShort(readd)<0))
  50.         {
  51.             ByteBuf bbf;
  52.  
  53.             if(in.getShort(readd)<=0)
  54.             {
  55.                 bbf = buffer(2);
  56.                 in.getBytes(readd,bbf);
  57.                 readd+=2;
  58.                 out.add(new PacketRequest(bbf));
  59.             }
  60.             else
  61.             {
  62.                 bbf = buffer(in.getShort(readd));
  63.                 in.getBytes(readd,bbf);
  64.                 readd+=in.getShort(readd);
  65.                 out.add(new PacketRequest(bbf));
  66.             }
  67.         }
  68.  
  69.         if(readd-in.readerIndex()>100 && readd==in.writerIndex())
  70.         {
  71.             in.clear();
  72.             readd=0;
  73.         }
  74.     }
  75.  
  76.     private void removeAllPipelineHandlers(ChannelPipeline pipeline)
  77.     {
  78.         while (pipeline.first() != null)
  79.         {
  80.             pipeline.removeFirst();
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement