Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.emulation.net.codec;
- import org.jboss.netty.buffer.ChannelBuffer;
- import org.jboss.netty.buffer.ChannelBuffers;
- import org.jboss.netty.channel.Channel;
- import org.jboss.netty.channel.ChannelHandlerContext;
- import org.jboss.netty.handler.codec.frame.FrameDecoder;
- import com.emulation.model.player.Player;
- import com.emulation.net.packet.Packet;
- import com.emulation.net.packet.PacketExecute;
- import com.emulation.net.packet.PacketType;
- import com.emulation.util.Execution;
- import com.emulation.world.World;
- /**
- * Decodes a packet that will be
- * sent to the client, encoded
- * from there and then used accordingly.
- * @author Owner
- * @version 1.0
- */
- public class Decoder extends FrameDecoder {
- /**
- * Decode an Object which will be
- * of type Packet and sent to the
- * client.
- */
- @Override
- protected Object decode(ChannelHandlerContext chc, Channel channel, ChannelBuffer cb) {
- Player player = World.getWorld().getChannelLocal().get(channel);
- // ISAACCipher isaac = (ISAACCipher) player.getAttribute("decryptor");
- int opcode = -1;
- int length = -1;
- if (cb.readableBytes() >= 1) {
- opcode = cb.readByte() & 0xFF;
- // opcode = (opcode - isaac.getNextKey()) & 0xFF;
- World.getWorld().getLogger().info("Writing and decoding opcode " + opcode + "...");
- length = Packet.getPacketSizes()[opcode];
- } else {
- return null;
- }
- if (length == -1) {
- if (cb.readableBytes() >= 1) {
- length = cb.readByte() & 0xFF;
- } else {
- return null;
- }
- }
- if (cb.readableBytes() >= length) {
- byte[] data = new byte[length];
- cb.readBytes(data);
- ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(data.length);
- channelBuffer.writeBytes(data);
- Packet packet = new Packet(opcode, PacketType.FIXED, channelBuffer);
- if ((player != null) && (packet != null)) {
- Execution.getScheduledExecutorService().submit(new PacketExecute(player, packet));
- }
- opcode = -1;
- length = -1;
- return packet;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement