Advertisement
forextheblack

ConnectionDecoder

Mar 2nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package ml.neoforex.netty.codecs;
  2.  
  3. import io.netty.buffer.ByteBuf;
  4. import io.netty.channel.*;
  5. import io.netty.handler.codec.ByteToMessageDecoder;
  6. import ml.neoforex.Main;
  7. import ml.neoforex.engine.cryptography.habbo.ClientMessage;
  8. import ml.neoforex.netty.handler.ConnectionHandler;
  9. import ml.neoforex.netty.session.Session;
  10.  
  11. import java.util.List;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15.  * Created by forex on 21/02/2016.
  16.  */
  17.  
  18. public class ConnectionDecoder extends ByteToMessageDecoder {
  19.     /**
  20.      * Objects
  21.      */
  22.     private Logger logger = Logger.getLogger(ConnectionDecoder.class.getName());
  23.  
  24.     @Override
  25.     protected void decode(ChannelHandlerContext channelHandlerContext,
  26.                           ByteBuf byteBuf, List<Object> list) throws Exception {
  27.         // Get channel
  28.         Channel ch = channelHandlerContext.channel();
  29.  
  30.         // These pachet don't have header or other(Disconnect for example)
  31.         if (byteBuf.readableBytes() < 6)
  32.             return;
  33.  
  34.         // Get session
  35.         Session session = Main.getNexus().getConnectionManager().getSession(ch);
  36.  
  37.         // Check if is policy
  38.         if (byteBuf.getByte(0) == 60) {
  39.             // Send policy!
  40.             ChannelFuture channelFuture = ch.writeAndFlush(Main.getNexus().getConnectionManager().getCrossDomain);
  41.             channelFuture.addListener(ChannelFutureListener.CLOSE);
  42.  
  43.             // Clear buffer.
  44.             byteBuf.clear();
  45.         }
  46.  
  47.         // Get packet length
  48.         byte[] lenght = new byte[4];
  49.         byteBuf.readBytes(lenght);
  50.  
  51.         // Get lenght in int
  52.         ClientMessage clientMessage = new ClientMessage(lenght);
  53.         int packetLenght = clientMessage.readInt();
  54.  
  55.         // Checker
  56.         if(packetLenght < 2 || packetLenght > byteBuf.readableBytes() || session == null) {
  57.             this.logger.warning("Bad packet");
  58.             ch.close();
  59.         } else {
  60.             // Get packet in byte[]
  61.             byte[] packet = new byte[packetLenght];
  62.             byteBuf.readBytes(packet);
  63.  
  64.             // DECRYPT
  65.             if(session.getCryptoManager().isEnabled()) {
  66.                 // TO DO.
  67.             }
  68.  
  69.             // Put in ClientMessage
  70.             clientMessage = new ClientMessage(packetLenght, packet);
  71.  
  72.             // Invoke packet.
  73.             session.invoke(clientMessage);
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement