Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.redrune.network.world.codec.io;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.Unpooled;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.handler.codec.ByteToMessageDecoder;
- import org.redrune.cache.crypto.ISAACCipher;
- import org.redrune.network.NetworkConstants;
- import org.redrune.network.NetworkSession;
- import org.redrune.network.world.WorldSession;
- import org.redrune.network.world.packet.Packet;
- import org.redrune.network.world.packet.Packet.PacketType;
- import org.redrune.utility.rs.NetworkUtils;
- import java.util.List;
- /**
- * Decodes a received packet.
- *
- * @author Tyluur <itstyluur@gmail.com>
- * @author Emperor
- * @since 7/19/17
- */
- public class RSPacketDecoder extends ByteToMessageDecoder {
- /**
- * Constructs a new {@code RS2GameDecoder} {@code Object}.
- *
- * @param session
- * The networkSession.
- */
- public RSPacketDecoder(NetworkSession session) {
- session.getChannel().attr(NetworkConstants.SESSION_KEY).setIfAbsent(session);
- }
- @Override
- protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
- while (in.readableBytes() > 0 && session.getChannel().isActive()) {
- // the session
- WorldSession session = (WorldSession) ctx.channel().attr(NetworkConstants.SESSION_KEY).get();
- // the cipher
- final ISAACCipher cipher = session.getInCipher();
- // the opcode of the packet
- int opcode = (in.readByte() - cipher.take()) & 0xFF;
- if (opcode < 0 || opcode >= NetworkUtils.PACKET_SIZES.length) {
- System.err.println("packet_opcode [" + opcode + "] out of bounds");
- in.discardReadBytes();
- return;
- }
- int length = NetworkUtils.PACKET_SIZES[opcode];
- System.err.println("received_packet [opcode=" + opcode + ", length=" + length + "]");
- switch (length) {
- case -1:
- length = in.readByte() & 0xFF;
- break;
- case -2:
- length = in.readShort() & 0xFFFF;
- break;
- case -3:
- length = in.readInt();
- break;
- case -4:
- length = in.readableBytes();
- System.err.println("unknown_packet_size [opcode=" + opcode + ", length=" + length + ", guessedLength=" + in.readableBytes() + "]");
- break;
- }
- byte[] payload = new byte[length];
- in.readBytes(payload);
- out.add(new Packet(opcode, PacketType.STANDARD, Unpooled.wrappedBuffer(payload)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement