Archived
0

refactoring: NetByteBuf

This commit is contained in:
2021-05-09 22:50:30 +03:00
parent 72b06bca7b
commit 7f7fefdc98
8 changed files with 35 additions and 17 deletions

View File

@@ -13,8 +13,6 @@ ext {
lombok : 'org.projectlombok:lombok:1.18.12', lombok : 'org.projectlombok:lombok:1.18.12',
annotations: 'com.google.code.findbugs:jsr305:3.0.2', annotations: 'com.google.code.findbugs:jsr305:3.0.2',
lang3 : 'org.apache.commons:commons-lang3:3.11', lang3 : 'org.apache.commons:commons-lang3:3.11',
netty : ["io.netty:netty-transport:${netty_version}",
"io.netty:netty-handler:${netty_version}"],
reactor : 'io.projectreactor:reactor-core:3.4.5', reactor : 'io.projectreactor:reactor-core:3.4.5',
yaml : 'org.yaml:snakeyaml:1.28', yaml : 'org.yaml:snakeyaml:1.28',
json : 'com.eclipsesource.minimal-json:minimal-json:0.9.5', json : 'com.eclipsesource.minimal-json:minimal-json:0.9.5',
@@ -23,6 +21,11 @@ ext {
objpool : 'org.apache.commons:commons-pool2:2.9.0' objpool : 'org.apache.commons:commons-pool2:2.9.0'
] ]
libs.netty = [
transport: "io.netty:netty-transport:${netty_version}",
handler : "io.netty:netty-handler:${netty_version}"
]
libs.logger = [ libs.logger = [
slf4j : ["org.slf4j:slf4j-api:${slf4j_version}", slf4j : ["org.slf4j:slf4j-api:${slf4j_version}",
"org.slf4j:jcl-over-slf4j:${slf4j_version}"], "org.slf4j:jcl-over-slf4j:${slf4j_version}"],

View File

@@ -1,6 +1,7 @@
//file:noinspection GrUnresolvedAccess //file:noinspection GrUnresolvedAccess
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'java-library' apply plugin: 'java-library'
apply plugin: 'jacoco'
apply from: rootDir.toPath().resolve('libs.gradle').toFile() apply from: rootDir.toPath().resolve('libs.gradle').toFile()
String getProperty1(String propertyName1, String propertyName2) { String getProperty1(String propertyName1, String propertyName2) {
@@ -41,3 +42,7 @@ dependencies {
test { test {
useJUnitPlatform() useJUnitPlatform()
} }
jacoco {
toolVersion = '0.8.7'
}

View File

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

View File

@@ -32,8 +32,6 @@ import java.util.UUID;
* | | | | этого числа). | * | | | | этого числа). |
* | VarInt | >= 1 ; <= 5 | Число от -2147483648 и 2147483647 | 32-bit число с плавающей размерностью от 1 до 5 байт | * | VarInt | >= 1 ; <= 5 | Число от -2147483648 и 2147483647 | 32-bit число с плавающей размерностью от 1 до 5 байт |
* | VarLong | >= 1 ; <= 10 | Число от -9223372036854775808 и 9223372036854775807 | 64-bit число с плавающей размерностью от 1 до 10 байт | * | VarLong | >= 1 ; <= 10 | Число от -9223372036854775808 и 9223372036854775807 | 64-bit число с плавающей размерностью от 1 до 10 байт |
* | Position | 8 | 64-bit число разделённое на три части: x, y, z | Кодируется формулой: |
* | | | | ((x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF) |
* *
* [1] - <a href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format">Single-precision floating-point format</a> * [1] - <a href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format">Single-precision floating-point format</a>
* [2] - <a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">Double-precision floating-point format</a> * [2] - <a href="https://en.wikipedia.org/wiki/Double-precision_floating-point_format">Double-precision floating-point format</a>
@@ -67,9 +65,9 @@ public class NetByteBuf extends ByteBuf {
if (length == 0) { if (length == 0) {
return ""; return "";
} else if (length > maxLength) { } else if (length > maxLength) {
throw new DecoderException("String length exceeds maximum length: " + length + " > " + maxLength); throw new NetIOException("String length exceeds maximum length: " + length + " > " + maxLength);
} else if (length < 0) { } else if (length < 0) {
throw new DecoderException("String length less zero!"); throw new NetIOException("String length less zero!");
} }
byte[] bytes = new byte[length * 4]; byte[] bytes = new byte[length * 4];

View File

@@ -0,0 +1,8 @@
package mc.protocol.io;
public class NetIOException extends RuntimeException {
public NetIOException(String message) {
super(message);
}
}

View File

@@ -16,7 +16,6 @@ import java.util.UUID;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
class NetByteBufReadTest { class NetByteBufReadTest {
@@ -108,7 +107,7 @@ class NetByteBufReadTest {
NetByteBuf netByteBuf = new NetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new NetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertThrows(DecoderException.class, () -> netByteBuf.readString(length)); assertThrows(NetIOException.class, () -> netByteBuf.readString(length));
} }
@Test @Test
@@ -127,7 +126,7 @@ class NetByteBufReadTest {
NetByteBuf netByteBuf = new NetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new NetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertThrows(DecoderException.class, () -> netByteBuf.readString(-1)); assertThrows(NetIOException.class, () -> netByteBuf.readString(-1));
} }
@ParameterizedTest @ParameterizedTest

View File

@@ -48,6 +48,16 @@ class NetByteBufWriteTest {
assertEquals(expectedByte, byteBuf.array()[0]); assertEquals(expectedByte, byteBuf.array()[0]);
} }
@Test
void writeUnsignedByte() {
ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf(byteBuf);
netByteBuf.writeUnsignedByte(129);
assertEquals(129, netByteBuf.readUnsignedByte());
}
@ParameterizedTest @ParameterizedTest
@MethodSource("paramsWriteString") @MethodSource("paramsWriteString")
void writeString(String string, int exceptedLength) { void writeString(String string, int exceptedLength) {

View File

@@ -1,8 +0,0 @@
package mc.protocol.io;
public class DecoderException extends RuntimeException {
public DecoderException(String message) {
super(message);
}
}