Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.rse.network.codec;
- import io.netty.buffer.ByteBuf;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.handler.codec.ByteToMessageDecoder;
- import java.util.List;
- import org.rse.network.packets.incoming.Incoming;
- public final class RS2GameDecoder extends ByteToMessageDecoder {
- private int opcode = -1;
- private int size = -1;
- @Override
- protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
- if (opcode == -1) {
- if (buffer.readableBytes() >= 1) {
- opcode = buffer.readByte() & 0xFF;
- size = Incoming.SIZES[opcode];
- } else {
- return;
- }
- }
- switch (size) {
- case -1:
- if (buffer.readableBytes() < 1)
- return;
- size = buffer.readByte() & 0xff;
- break;
- case -2:
- if (buffer.readableBytes() < 2)
- return;
- size = buffer.readShort() & 0xffff;
- break;
- }
- if (buffer.readableBytes() >= size) {
- if (Incoming.isIgnored(opcode)) {
- buffer.skipBytes(size);
- opcode = size = -1;
- return;
- }
- byte[] incomingPayload = new byte[size];
- buffer.readBytes(incomingPayload);
- try {
- out.add(new Message(opcode, incomingPayload));
- return;
- } finally {
- opcode = -1;
- size = -1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement