Guest User

Untitled

a guest
Jul 31st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. ##package com.maximemeire.rs2ls.io.tcp.upstreampackets;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import com.maximemeire.rs2ls.io.DynamicByteBuffer;
  7.  
  8. public abstract class UpstreamPacket {
  9.  
  10. public static final int VAR_BYTE_SIZED = -1;
  11. public static final int VAR_SHORT_SIZED = -2;
  12.  
  13. /**
  14. * A <code>Hashmap</code> containing all kinds of attributes related to
  15. * this <code>AbstractUpstreamPacket</code>.
  16. */
  17. private final Map<String, Object> attributes = new HashMap<String, Object>();
  18.  
  19. /**
  20. * Length value that will be set using the PREMATURE_LENGTH from its childs.
  21. */
  22. private int matureLength;
  23.  
  24. public abstract int getOpcode();
  25.  
  26. public abstract int getPrematureLength();
  27.  
  28. public void setMatureLength(int matureLength) {
  29. this.matureLength = matureLength;
  30. }
  31.  
  32. public int getMatureLength() {
  33. return matureLength;
  34. }
  35.  
  36. public abstract UpstreamPacket getNewInstance();
  37.  
  38. /**
  39. * Creates an <code>AbstractUpstreamPacket</code>.
  40. */
  41. public UpstreamPacket() {
  42. }
  43.  
  44. public void setAttribute(String attribute, Object value) {
  45. attributes.put(attribute, value);
  46. }
  47.  
  48. public final Object getAttribute(String attribute) {
  49. return attributes.get(attribute);
  50. }
  51.  
  52. public abstract UpstreamPacket decode(DynamicByteBuffer in);
  53.  
  54. public abstract void handlePacket();
  55.  
  56. }
  57. ##package com.maximemeire.rs2ls.io.tcp.upstreampackets;
  58.  
  59. import com.maximemeire.rs2ls.io.DynamicByteBuffer;
  60.  
  61. public class LoginRequest extends UpstreamPacket {
  62.  
  63. public static final int PREMATURE_LENGTH = VAR_BYTE_SIZED;
  64.  
  65. public static final int OPCODE = 1;
  66.  
  67. public static final String USERNAME = "u";
  68. public static final String PASSWORD = "p";
  69.  
  70. public LoginRequest() {
  71. super();
  72. }
  73.  
  74. @Override
  75. public UpstreamPacket getNewInstance() {
  76. return new LoginRequest();
  77. }
  78.  
  79. @Override
  80. public int getPrematureLength() {
  81. return PREMATURE_LENGTH;
  82. }
  83.  
  84. @Override
  85. public int getOpcode() {
  86. return OPCODE;
  87. }
  88.  
  89. @Override
  90. public UpstreamPacket decode(DynamicByteBuffer in) {
  91. setAttribute(USERNAME, in.readString());
  92. setAttribute(PASSWORD, in.readString());
  93. return this;
  94. }
  95.  
  96. @Override
  97. public void handlePacket() {
  98. // TODO Auto-generated method stub
  99.  
  100. }
  101.  
  102. }
  103. ##package com.maximemeire.rs2ls.io.tcp.handlers;
  104.  
  105. import org.jboss.netty.buffer.ChannelBuffer;
  106. import org.jboss.netty.channel.Channel;
  107. import org.jboss.netty.channel.ChannelHandlerContext;
  108. import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
  109.  
  110. import com.maximemeire.rs2ls.io.DynamicByteBuffer;
  111. import com.maximemeire.rs2ls.io.tcp.upstreampackets.UpstreamPacket;
  112. import com.maximemeire.rs2ls.io.tcp.upstreampackets.LoginRequest;
  113.  
  114. public class UpstreamPacketDecoder extends ReplayingDecoder<UpstreamPacketDecoderState> {
  115.  
  116. public static final UpstreamPacket[] UPSTREAM_PACKET_TEMPLATES = new UpstreamPacket[256];
  117.  
  118. public UpstreamPacketDecoder() {
  119. super(UpstreamPacketDecoderState.READ_OPCODE);
  120. }
  121.  
  122. static {
  123. UPSTREAM_PACKET_TEMPLATES[1] = new LoginRequest();
  124. }
  125.  
  126. @Override
  127. protected Object decode(ChannelHandlerContext ctx, Channel channel,
  128. ChannelBuffer buffer, UpstreamPacketDecoderState state) throws Exception {
  129. DynamicByteBuffer in = (DynamicByteBuffer) buffer;
  130. UpstreamPacket upstream = null;
  131. switch (state) {
  132. case READ_OPCODE:
  133. upstream = getUpstreamPacketTemplate(in.readOpcode());
  134. if (upstream == null) {
  135. return null;
  136. }
  137. checkpoint(UpstreamPacketDecoderState.READ_LENGTH);
  138. case READ_LENGTH:
  139. switch (upstream.getOpcode()) {
  140. case -2:
  141. upstream.setMatureLength(in.readShort());
  142. break;
  143. case -1:
  144. upstream.setMatureLength(in.readByte());
  145. break;
  146. default:
  147. upstream.setMatureLength(upstream.getPrematureLength());
  148. break;
  149. }
  150. checkpoint(UpstreamPacketDecoderState.READ_BODY);
  151. case READ_BODY:
  152. checkpoint(UpstreamPacketDecoderState.READ_OPCODE);
  153. return upstream.decode(in);
  154. }
  155. return null;
  156. }
  157.  
  158. public static UpstreamPacket getUpstreamPacketTemplate(int opcode) {
  159. return UPSTREAM_PACKET_TEMPLATES[opcode].getNewInstance();
  160. }
  161.  
  162. }
  163. ##package com.maximemeire.rs2ls.io.tcp.handlers;
  164.  
  165. import org.jboss.netty.channel.Channel;
  166. import org.jboss.netty.channel.ChannelHandlerContext;
  167. import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
  168.  
  169. public class UpstreamPacketQueueAppender extends OneToOneDecoder {
  170.  
  171. @Override
  172. protected Object decode(ChannelHandlerContext ctx, Channel channel,
  173. Object msg) throws Exception {
  174. // Object 'msg' added to ctx.getAttachment()'s queue
  175. return null;
  176. }
  177.  
  178. }
Add Comment
Please, Sign In to add comment