Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##package com.maximemeire.rs2ls.io.tcp.upstreampackets;
- import java.util.HashMap;
- import java.util.Map;
- import com.maximemeire.rs2ls.io.DynamicByteBuffer;
- public abstract class UpstreamPacket {
- public static final int VAR_BYTE_SIZED = -1;
- public static final int VAR_SHORT_SIZED = -2;
- /**
- * A <code>Hashmap</code> containing all kinds of attributes related to
- * this <code>AbstractUpstreamPacket</code>.
- */
- private final Map<String, Object> attributes = new HashMap<String, Object>();
- /**
- * Length value that will be set using the PREMATURE_LENGTH from its childs.
- */
- private int matureLength;
- public abstract int getOpcode();
- public abstract int getPrematureLength();
- public void setMatureLength(int matureLength) {
- this.matureLength = matureLength;
- }
- public int getMatureLength() {
- return matureLength;
- }
- public abstract UpstreamPacket getNewInstance();
- /**
- * Creates an <code>AbstractUpstreamPacket</code>.
- */
- public UpstreamPacket() {
- }
- public void setAttribute(String attribute, Object value) {
- attributes.put(attribute, value);
- }
- public final Object getAttribute(String attribute) {
- return attributes.get(attribute);
- }
- public abstract UpstreamPacket decode(DynamicByteBuffer in);
- public abstract void handlePacket();
- }
- ##package com.maximemeire.rs2ls.io.tcp.upstreampackets;
- import com.maximemeire.rs2ls.io.DynamicByteBuffer;
- public class LoginRequest extends UpstreamPacket {
- public static final int PREMATURE_LENGTH = VAR_BYTE_SIZED;
- public static final int OPCODE = 1;
- public static final String USERNAME = "u";
- public static final String PASSWORD = "p";
- public LoginRequest() {
- super();
- }
- @Override
- public UpstreamPacket getNewInstance() {
- return new LoginRequest();
- }
- @Override
- public int getPrematureLength() {
- return PREMATURE_LENGTH;
- }
- @Override
- public int getOpcode() {
- return OPCODE;
- }
- @Override
- public UpstreamPacket decode(DynamicByteBuffer in) {
- setAttribute(USERNAME, in.readString());
- setAttribute(PASSWORD, in.readString());
- return this;
- }
- @Override
- public void handlePacket() {
- // TODO Auto-generated method stub
- }
- }
- ##package com.maximemeire.rs2ls.io.tcp.handlers;
- import org.jboss.netty.buffer.ChannelBuffer;
- import org.jboss.netty.channel.Channel;
- import org.jboss.netty.channel.ChannelHandlerContext;
- import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
- import com.maximemeire.rs2ls.io.DynamicByteBuffer;
- import com.maximemeire.rs2ls.io.tcp.upstreampackets.UpstreamPacket;
- import com.maximemeire.rs2ls.io.tcp.upstreampackets.LoginRequest;
- public class UpstreamPacketDecoder extends ReplayingDecoder<UpstreamPacketDecoderState> {
- public static final UpstreamPacket[] UPSTREAM_PACKET_TEMPLATES = new UpstreamPacket[256];
- public UpstreamPacketDecoder() {
- super(UpstreamPacketDecoderState.READ_OPCODE);
- }
- static {
- UPSTREAM_PACKET_TEMPLATES[1] = new LoginRequest();
- }
- @Override
- protected Object decode(ChannelHandlerContext ctx, Channel channel,
- ChannelBuffer buffer, UpstreamPacketDecoderState state) throws Exception {
- DynamicByteBuffer in = (DynamicByteBuffer) buffer;
- UpstreamPacket upstream = null;
- switch (state) {
- case READ_OPCODE:
- upstream = getUpstreamPacketTemplate(in.readOpcode());
- if (upstream == null) {
- return null;
- }
- checkpoint(UpstreamPacketDecoderState.READ_LENGTH);
- case READ_LENGTH:
- switch (upstream.getOpcode()) {
- case -2:
- upstream.setMatureLength(in.readShort());
- break;
- case -1:
- upstream.setMatureLength(in.readByte());
- break;
- default:
- upstream.setMatureLength(upstream.getPrematureLength());
- break;
- }
- checkpoint(UpstreamPacketDecoderState.READ_BODY);
- case READ_BODY:
- checkpoint(UpstreamPacketDecoderState.READ_OPCODE);
- return upstream.decode(in);
- }
- return null;
- }
- public static UpstreamPacket getUpstreamPacketTemplate(int opcode) {
- return UPSTREAM_PACKET_TEMPLATES[opcode].getNewInstance();
- }
- }
- ##package com.maximemeire.rs2ls.io.tcp.handlers;
- import org.jboss.netty.channel.Channel;
- import org.jboss.netty.channel.ChannelHandlerContext;
- import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
- public class UpstreamPacketQueueAppender extends OneToOneDecoder {
- @Override
- protected Object decode(ChannelHandlerContext ctx, Channel channel,
- Object msg) throws Exception {
- // Object 'msg' added to ctx.getAttachment()'s queue
- return null;
- }
- }
Add Comment
Please, Sign In to add comment