Guest User

Untitled

a guest
Nov 11th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. **
  2. * RuneScape protocol packets decoder.
  3. * @author Faris
  4. */
  5. public class RSPacketDecoder implements NIODecoder {
  6.  
  7. private int opcode = -1, length = -1;
  8. private ByteBuffer buffer = ByteBuffer.allocateDirect(126);
  9.  
  10. @Override
  11. public void decode(RSChannelContext channelContext) throws IOException {
  12. /*
  13. * Read the incoming data to our local buffer.
  14. */
  15. channelContext.channel().read(buffer);
  16. buffer.flip();
  17.  
  18. /*
  19. * If buffer has no readable data, we skip packet decoding.
  20. */
  21. if (!buffer.hasRemaining()) {
  22. buffer.clear();
  23. return;
  24. }
  25.  
  26. while (buffer.hasRemaining()) {
  27. if (opcode == -1) {
  28. opcode = buffer.get() & 0xFF;
  29. opcode = opcode - channelContext.decryption().getNextKey()
  30. & 0xff;
  31. length = PACKET_SIZES[opcode];
  32. }
  33.  
  34. /*
  35. * Length -1 indicates that this packet is variable sized. There for
  36. * we need to read next byte to figure out packet's length.
  37. */
  38. if (length == -1) {
  39. if (!buffer.hasRemaining()) {
  40. buffer.clear();
  41. return;
  42. }
  43. length = buffer.get() & 0xFF;
  44. }
  45.  
  46. if (buffer.remaining() < length) {
  47. buffer.compact();
  48. return;
  49. }
  50.  
  51. Packet packet = Packet.buildPacket(buffer, opcode, length);
  52. PacketType.handlePacket(channelContext, packet);
  53.  
  54. /*
  55. * After handling the packet we reset the variables in order to
  56. * continue decoding other packets.
  57. */
  58. opcode = length = -1;
  59. buffer.compact().flip();
  60. }
  61.  
  62. /*
  63. * After all packets are handled we need to clear the buffer to be ready
  64. * to accept new incoming data.
  65. */
  66. buffer.clear();
  67. }
  68.  
  69. /**
  70. * Incoming packet sizes. Some of the packets have fixed or no size at all,
  71. * for those packets we don't need to read length byte. Yet all the packets
  72. * which have size of -1 have length indicator which we have to read in
  73. * order to figure out the size.
  74. */
  75. public static final int PACKET_SIZES[] = { 0, 0, 0, 1, -1, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 8, 0, 6, 2, 2, 0, 0, 2, 0, 6, 0, 12, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 8, 4, 0, 0, 2, 2, 6, 0, 6, 0, -1, 0, 0, 0, 0, 0, 0, 0, 12,
  78. 0, 0, 0, 0, 8, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 2, 2, 8, 6,
  79. 0, -1, 0, 6, 0, 0, 0, 0, 0, 1, 4, 6, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,
  80. -1, 0, 0, 13, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0,
  81. 0, 1, 0, 6, 0, 0, 0, -1, 0, 2, 6, 0, 4, 6, 8, 0, 6, 0, 0, 0, 2, 0,
  82. 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1, 2, 0, 2, 6, 0, 0, 0, 0, 0, 0,
  83. 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 3, 0,
  84. 2, 0, 0, 8, 1, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
  85. 0, 4, 0, 4, 0, 0, 0, 7, 8, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, -1, 0, 6,
  86. 0, 1, 0, 0, 0, 6, 0, 6, 8, 1, 0, 0, 4, 0, 0, 0, 0, -1, 0, -1, 4, 0,
  87. 0, 6, 6, 0, 0, 0 };
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment