mirror of
https://github.com/Nan1t/NanoLimbo.git
synced 2026-02-11 03:16:14 +01:00
Moved pipeline package under connection
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
package ru.nanit.limbo.protocol.pipeline;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageDecoder;
|
||||
import ru.nanit.limbo.protocol.*;
|
||||
import ru.nanit.limbo.protocol.registry.State;
|
||||
import ru.nanit.limbo.util.Logger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PacketDecoder extends MessageToMessageDecoder<ByteBuf> {
|
||||
|
||||
private State.PacketRegistry mappings;
|
||||
|
||||
public PacketDecoder(){
|
||||
updateState(State.HANDSHAKING);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
|
||||
if (!ctx.channel().isActive() || mappings == null) return;
|
||||
|
||||
ByteMessage msg = new ByteMessage(buf);
|
||||
int packetId = msg.readVarInt();
|
||||
Packet packet = mappings.getPacket(packetId);
|
||||
|
||||
if (packet != null){
|
||||
try {
|
||||
packet.decode(msg);
|
||||
} catch (Exception e){
|
||||
Logger.warning("Cannot decode packet 0x%s: %s", Integer.toHexString(packetId), e.getMessage());
|
||||
}
|
||||
|
||||
ctx.fireChannelRead(packet);
|
||||
} else {
|
||||
Logger.warning("Undefined incoming packet: 0x" + Integer.toHexString(packetId));
|
||||
}
|
||||
}
|
||||
|
||||
public void updateState(State state){
|
||||
this.mappings = state.serverBound;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package ru.nanit.limbo.protocol.pipeline;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.Packet;
|
||||
import ru.nanit.limbo.protocol.PreRenderedPacket;
|
||||
import ru.nanit.limbo.protocol.registry.State;
|
||||
import ru.nanit.limbo.util.Logger;
|
||||
|
||||
public class PacketEncoder extends MessageToByteEncoder<Packet> {
|
||||
|
||||
private State.PacketRegistry registry;
|
||||
|
||||
public PacketEncoder(){
|
||||
updateState(State.HANDSHAKING);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception {
|
||||
if (registry == null) return;
|
||||
|
||||
ByteMessage msg = new ByteMessage(out);
|
||||
int packetId;
|
||||
|
||||
if (packet instanceof PreRenderedPacket){
|
||||
packetId = registry.getPacketId(((PreRenderedPacket)packet).getWrappedPacket().getClass());
|
||||
} else {
|
||||
packetId = registry.getPacketId(packet.getClass());
|
||||
}
|
||||
|
||||
if (packetId == -1){
|
||||
Logger.warning("Undefined packet class: %s", packet.getClass().getName());
|
||||
return;
|
||||
}
|
||||
|
||||
msg.writeVarInt(packetId);
|
||||
|
||||
try {
|
||||
packet.encode(msg);
|
||||
} catch (Exception e){
|
||||
Logger.warning("Cannot encode packet 0x%s: %s", Integer.toHexString(packetId), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void updateState(State state){
|
||||
this.registry = state.clientBound;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package ru.nanit.limbo.protocol.pipeline;
|
||||
|
||||
import io.netty.util.ByteProcessor;
|
||||
|
||||
public class VarIntByteDecoder implements ByteProcessor {
|
||||
|
||||
private int readVarInt;
|
||||
private int bytesRead;
|
||||
private DecodeResult result = DecodeResult.TOO_SHORT;
|
||||
|
||||
@Override
|
||||
public boolean process(byte k) {
|
||||
readVarInt |= (k & 0x7F) << bytesRead++ * 7;
|
||||
if (bytesRead > 3) {
|
||||
result = DecodeResult.TOO_BIG;
|
||||
return false;
|
||||
}
|
||||
if ((k & 0x80) != 128) {
|
||||
result = DecodeResult.SUCCESS;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int getReadVarInt() {
|
||||
return readVarInt;
|
||||
}
|
||||
|
||||
public int getBytesRead() {
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
public DecodeResult getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public enum DecodeResult {
|
||||
SUCCESS,
|
||||
TOO_SHORT,
|
||||
TOO_BIG
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package ru.nanit.limbo.protocol.pipeline;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import ru.nanit.limbo.util.Logger;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class VarIntFrameDecoder extends ByteToMessageDecoder {
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
|
||||
if (!ctx.channel().isActive()) {
|
||||
in.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
VarIntByteDecoder reader = new VarIntByteDecoder();
|
||||
int varIntEnd = in.forEachByte(reader);
|
||||
|
||||
if (varIntEnd == -1) return;
|
||||
|
||||
if (reader.getResult() == VarIntByteDecoder.DecodeResult.SUCCESS) {
|
||||
int readVarInt = reader.getReadVarInt();
|
||||
int bytesRead = reader.getBytesRead();
|
||||
if (readVarInt < 0) {
|
||||
Logger.error("[VarIntFrameDecoder] Bad data length");
|
||||
} else if (readVarInt == 0) {
|
||||
in.readerIndex(varIntEnd + 1);
|
||||
} else {
|
||||
int minimumRead = bytesRead + readVarInt;
|
||||
if (in.isReadable(minimumRead)) {
|
||||
out.add(in.retainedSlice(varIntEnd + 1, readVarInt));
|
||||
in.skipBytes(minimumRead);
|
||||
}
|
||||
}
|
||||
} else if (reader.getResult() == VarIntByteDecoder.DecodeResult.TOO_BIG) {
|
||||
Logger.error("[VarIntFrameDecoder] Too big data");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package ru.nanit.limbo.protocol.pipeline;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
public class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) {
|
||||
ByteMessage msg = new ByteMessage(out);
|
||||
msg.writeVarInt(buf.readableBytes());
|
||||
msg.writeBytes(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) {
|
||||
int anticipatedRequiredCapacity = 5 + msg.readableBytes();
|
||||
return ctx.alloc().heapBuffer(anticipatedRequiredCapacity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user