Advertisement
Guest User

Untitled

a guest
Jan 29th, 2011
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package com.emulation.net.codec;
  2.  
  3. import org.jboss.netty.buffer.ChannelBuffer;
  4. import org.jboss.netty.buffer.ChannelBuffers;
  5. import org.jboss.netty.channel.Channel;
  6. import org.jboss.netty.channel.ChannelHandlerContext;
  7. import org.jboss.netty.handler.codec.frame.FrameDecoder;
  8.  
  9. import com.emulation.model.player.Player;
  10. import com.emulation.net.packet.Packet;
  11. import com.emulation.net.packet.PacketExecute;
  12. import com.emulation.net.packet.PacketType;
  13. import com.emulation.util.Execution;
  14. import com.emulation.world.World;
  15.  
  16. /**
  17. * Decodes a packet that will be
  18. * sent to the client, encoded
  19. * from there and then used accordingly.
  20. * @author Owner
  21. * @version 1.0
  22. */
  23. public class Decoder extends FrameDecoder {
  24.  
  25. /**
  26. * Decode an Object which will be
  27. * of type Packet and sent to the
  28. * client.
  29. */
  30. @Override
  31. protected Object decode(ChannelHandlerContext chc, Channel channel, ChannelBuffer cb) {
  32. Player player = World.getWorld().getChannelLocal().get(channel);
  33. // ISAACCipher isaac = (ISAACCipher) player.getAttribute("decryptor");
  34. int opcode = -1;
  35. int length = -1;
  36. if (cb.readableBytes() >= 1) {
  37. opcode = cb.readByte() & 0xFF;
  38. // opcode = (opcode - isaac.getNextKey()) & 0xFF;
  39. World.getWorld().getLogger().info("Writing and decoding opcode " + opcode + "...");
  40. length = Packet.getPacketSizes()[opcode];
  41. } else {
  42. return null;
  43. }
  44. if (length == -1) {
  45. if (cb.readableBytes() >= 1) {
  46. length = cb.readByte() & 0xFF;
  47. } else {
  48. return null;
  49. }
  50. }
  51. if (cb.readableBytes() >= length) {
  52. byte[] data = new byte[length];
  53. cb.readBytes(data);
  54. ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(data.length);
  55. channelBuffer.writeBytes(data);
  56. Packet packet = new Packet(opcode, PacketType.FIXED, channelBuffer);
  57. if ((player != null) && (packet != null)) {
  58. Execution.getScheduledExecutorService().submit(new PacketExecute(player, packet));
  59. }
  60. opcode = -1;
  61. length = -1;
  62. return packet;
  63. }
  64. return null;
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement