refactoring: ProtocolSplitter
This commit is contained in:
@@ -2,6 +2,7 @@ apply from: rootDir.toPath().resolve('logic.gradle').toFile()
|
||||
|
||||
dependencies {
|
||||
implementation libs.netty.transport
|
||||
implementation libs.netty.codec
|
||||
|
||||
testImplementation libs.lang3
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package mc.protocol.codec;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import mc.protocol.io.NetByteBuf;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ProtocolSplitter extends ByteToMessageDecoder {
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
|
||||
NetByteBuf netByteBuf = new NetByteBuf(in);
|
||||
netByteBuf.markReaderIndex();
|
||||
|
||||
do {
|
||||
byte[] sizePacketRaw = new byte[3];
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
sizePacketRaw[i] = netByteBuf.readByte();
|
||||
|
||||
if (sizePacketRaw[i] >= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int sizePacket = new NetByteBuf(Unpooled.wrappedBuffer(sizePacketRaw)).readVarInt();
|
||||
|
||||
if (netByteBuf.readableBytes() >= sizePacket) {
|
||||
byte[] bytes = new byte[sizePacket];
|
||||
netByteBuf.readBytes(bytes);
|
||||
out.add(Unpooled.wrappedBuffer(bytes));
|
||||
} else {
|
||||
netByteBuf.resetReaderIndex();
|
||||
break;
|
||||
}
|
||||
} while (netByteBuf.readableBytes() > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package mc.protocol.codec;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ProtocolSplitterTest {
|
||||
|
||||
byte[] expectedPacket1Bytes;
|
||||
byte[] expectedPacket2Bytes;
|
||||
byte[] bytes;
|
||||
|
||||
@Test
|
||||
void split() {
|
||||
prepare();
|
||||
|
||||
ByteBuf inputBuffer = Unpooled.wrappedBuffer(bytes);
|
||||
List<Object> outputObject = new ArrayList<>();
|
||||
|
||||
ProtocolSplitter protocolSplitter = new ProtocolSplitter();
|
||||
protocolSplitter.decode(null, inputBuffer, outputObject);
|
||||
|
||||
assertEquals(2, outputObject.size());
|
||||
assertTrue(outputObject.get(0) instanceof ByteBuf);
|
||||
assertArrayEquals(expectedPacket1Bytes, ((ByteBuf)outputObject.get(0)).array());
|
||||
assertTrue(outputObject.get(1) instanceof ByteBuf);
|
||||
assertArrayEquals(expectedPacket2Bytes, ((ByteBuf)outputObject.get(1)).array());
|
||||
}
|
||||
|
||||
private void prepare() {
|
||||
/*
|
||||
+-------------------------------------------------+
|
||||
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
|
||||
+--------+-------------------------------------------------+----------------+
|
||||
|00000000| 10 00 d4 02 09 31 32 37 2e 30 2e 30 2e 31 63 dd |.....127.0.0.1c.|
|
||||
|00000010| 01 01 00 |... |
|
||||
+--------+-------------------------------------------------+----------------+
|
||||
*/
|
||||
expectedPacket1Bytes = new byte[]{
|
||||
0x00, (byte) 0xd4, 0x02, 0x09, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x63,
|
||||
(byte) 0xdd, 0x01
|
||||
};
|
||||
expectedPacket2Bytes = new byte[]{
|
||||
0x00
|
||||
};
|
||||
bytes = new byte[2 + expectedPacket1Bytes.length + expectedPacket2Bytes.length];
|
||||
bytes[0] = 0x10;
|
||||
System.arraycopy(expectedPacket1Bytes, 0,
|
||||
bytes, 1, expectedPacket1Bytes.length);
|
||||
bytes[1 + expectedPacket1Bytes.length] = 0x01;
|
||||
System.arraycopy(expectedPacket2Bytes, 0,
|
||||
bytes, 2 + expectedPacket1Bytes.length, expectedPacket2Bytes.length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user