Archived
0

refactoring: ProtocolSplitter

This commit is contained in:
2021-05-09 23:28:36 +03:00
parent 7f7fefdc98
commit 50fc39e924
4 changed files with 62 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ ext {
libs.netty = [
transport: "io.netty:netty-transport:${netty_version}",
codec : "io.netty:netty-codec:${netty_version}",
handler : "io.netty:netty-handler:${netty_version}"
]

View File

@@ -2,6 +2,7 @@ apply from: rootDir.toPath().resolve('logic.gradle').toFile()
dependencies {
implementation libs.netty.transport
implementation libs.netty.codec
testImplementation libs.lang3
}

View File

@@ -1,4 +1,4 @@
package mc.protocol.io.codec;
package mc.protocol.codec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

View File

@@ -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);
}
}