Added debug levels. Code style

This commit is contained in:
Nanit
2020-11-26 20:35:12 +02:00
parent bc05228da3
commit 7cb3846848
10 changed files with 69 additions and 48 deletions

View File

@@ -22,7 +22,7 @@ public class VarIntByteDecoder implements ByteProcessor {
return true;
}
public int getReadVarint() {
public int getReadVarInt() {
return readVarInt;
}

View File

@@ -16,31 +16,27 @@ public class VarIntFrameDecoder extends ByteToMessageDecoder {
return;
}
final VarIntByteDecoder reader = new VarIntByteDecoder();
VarIntByteDecoder reader = new VarIntByteDecoder();
int varIntEnd = in.forEachByte(reader);
int varintEnd = in.forEachByte(reader);
if (varintEnd == -1) {
return;
}
if (varIntEnd == -1) return;
if (reader.getResult() == VarIntByteDecoder.DecodeResult.SUCCESS) {
int readVarint = reader.getReadVarint();
int readVarInt = reader.getReadVarInt();
int bytesRead = reader.getBytesRead();
if (readVarint < 0) {
Logger.error("BAD_LENGTH_CACHED");
} else if (readVarint == 0) {
// skip over the empty packet and ignore it
in.readerIndex(varintEnd + 1);
if (readVarInt < 0) {
Logger.error("[VarIntFrameDecoder] Bad data length");
} else if (readVarInt == 0) {
in.readerIndex(varIntEnd + 1);
} else {
int minimumRead = bytesRead + readVarint;
int minimumRead = bytesRead + readVarInt;
if (in.isReadable(minimumRead)) {
out.add(in.retainedSlice(varintEnd + 1, readVarint));
out.add(in.retainedSlice(varIntEnd + 1, readVarInt));
in.skipBytes(minimumRead);
}
}
} else if (reader.getResult() == VarIntByteDecoder.DecodeResult.TOO_BIG) {
Logger.error("Too big");
Logger.error("[VarIntFrameDecoder] Too big data");
}
}
}

View File

@@ -10,14 +10,14 @@ import ru.nanit.limbo.protocol.ByteMessage;
public class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) throws Exception {
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) throws Exception {
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) {
int anticipatedRequiredCapacity = 5 + msg.readableBytes();
return ctx.alloc().heapBuffer(anticipatedRequiredCapacity);
}