Archived
0

Merge branch 'dev/pool' into dev/world

# Conflicts:
#	protocol/src/main/java/mc/protocol/packets/play/client/CPlayerPositionAndLookPacket.java
#	protocol/src/main/java/mc/protocol/packets/play/client/PlayerLookPacket.java
#	protocol/src/main/java/mc/protocol/packets/play/client/PlayerPositionPacket.java
#	protocol/src/main/java/mc/protocol/packets/play/server/ChunkDataPacket.java
#	protocol/src/test/java/mc/protocol/buffer/NetByteBufReadTest.java
#	protocol/src/test/java/mc/protocol/buffer/NetByteBufWriteTest.java
#	server/src/main/resources/logback-default.xml
This commit is contained in:
2021-07-17 19:10:08 +03:00
32 changed files with 187 additions and 95 deletions

View File

@@ -3,7 +3,6 @@ apply from: rootDir.toPath().resolve('logic.gradle').toFile()
dependencies { dependencies {
api project(':utils') api project(':utils')
implementation libs.objpool
implementation libs.netty.transport implementation libs.netty.transport
implementation libs.netty.codec implementation libs.netty.codec
implementation libs.json implementation libs.json

View File

@@ -3,13 +3,11 @@ package mc.protocol.buffer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import lombok.experimental.Delegate; import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.protocol.model.text.Text; import mc.protocol.model.text.Text;
import mc.protocol.model.text.TextSerializer; import mc.protocol.model.text.TextSerializer;
import mc.protocol.pool.Passivable;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
@@ -50,15 +48,10 @@ import java.util.UUID;
@RequiredArgsConstructor @RequiredArgsConstructor
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
@ToString @ToString
public class NetByteBuf extends ByteBuf implements Passivable { public abstract class NetByteBuf extends ByteBuf {
@Delegate @Delegate
private ByteBuf byteBuf; protected ByteBuf byteBuf;
public NetByteBuf setByteBuf(ByteBuf byteBuf) {
this.byteBuf = byteBuf;
return this;
}
public void writeUnsignedByte(int value) { public void writeUnsignedByte(int value) {
byteBuf.writeByte((byte)(value & 0xFF)); byteBuf.writeByte((byte)(value & 0xFF));
@@ -182,11 +175,6 @@ public class NetByteBuf extends ByteBuf implements Passivable {
} }
//endregion //endregion
@Override
public void passivate() {
this.byteBuf = null;
}
public static int readVarInt(ByteBuf byteBuf) { public static int readVarInt(ByteBuf byteBuf) {
int numRead = 0; int numRead = 0;
int result = 0; int result = 0;

View File

@@ -8,7 +8,6 @@ import mc.protocol.ProtocolAttributes;
import mc.protocol.State; import mc.protocol.State;
import mc.protocol.packets.ClientSidePacket; import mc.protocol.packets.ClientSidePacket;
import mc.protocol.pool.ProtocolObjectPool; import mc.protocol.pool.ProtocolObjectPool;
import org.apache.commons.pool2.ObjectPool;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@@ -22,11 +21,11 @@ public class ProtocolInboundHandler extends SimpleChannelInboundHandler<ClientSi
private final ProtocolHandlersBus protocolHandlersBus; private final ProtocolHandlersBus protocolHandlersBus;
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, ClientSidePacket packet) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, ClientSidePacket packet) {
State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get()); State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get());
protocolHandlersBus.process(state, ctx, packet); protocolHandlersBus.process(state, ctx, packet);
ProtocolObjectPool.getPacketPool().returnObject(packet); ProtocolObjectPool.packet().returnObject(packet);
} }
@Override @Override

View File

@@ -27,9 +27,9 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
} }
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get()); State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get());
NetByteBuf netByteBuf = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(in); NetByteBuf netByteBuf = ProtocolObjectPool.netByteBuf().borrowObject(in);
int packetId = netByteBuf.readVarInt(); int packetId = netByteBuf.readVarInt();
Class<? extends ClientSidePacket> packetClass = state.getClientSidePacketById(packetId); Class<? extends ClientSidePacket> packetClass = state.getClientSidePacketById(packetId);
@@ -37,7 +37,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
log.warn("Unknown packet: State {} ; Id 0x{}", state, packetIdAsHexcode(packetId)); log.warn("Unknown packet: State {} ; Id 0x{}", state, packetIdAsHexcode(packetId));
if (readUnknownPackets) { if (readUnknownPackets) {
UnknownPacket unknownPacket = ProtocolObjectPool.getPacketPool().borrowObject(UnknownPacket.class); UnknownPacket unknownPacket = ProtocolObjectPool.packet().borrowObject(UnknownPacket.class);
unknownPacket.setState(state); unknownPacket.setState(state);
unknownPacket.setId(packetId); unknownPacket.setId(packetId);
unknownPacket.setDataSize(netByteBuf.readableBytes()); unknownPacket.setDataSize(netByteBuf.readableBytes());
@@ -47,7 +47,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
netByteBuf.skipBytes(netByteBuf.readableBytes()); netByteBuf.skipBytes(netByteBuf.readableBytes());
} }
} else { } else {
ClientSidePacket packet = ProtocolObjectPool.getPacketPool().borrowObject(packetClass); ClientSidePacket packet = ProtocolObjectPool.packet().borrowObject(packetClass);
packet.readSelf(netByteBuf); packet.readSelf(netByteBuf);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("IN: {}:{}", state, packet); log.debug("IN: {}:{}", state, packet);
@@ -55,7 +55,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
out.add(packet); out.add(packet);
} }
ProtocolObjectPool.getNetByteBufPool().returnObject(netByteBuf); ProtocolObjectPool.netByteBuf().returnObject(netByteBuf);
} }
private static String packetIdAsHexcode(int packetId) { private static String packetIdAsHexcode(int packetId) {

View File

@@ -1,7 +1,6 @@
package mc.protocol.handler.codec; package mc.protocol.handler.codec;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -29,15 +28,15 @@ public class ProtocolEncoder extends MessageToByteEncoder<ServerSidePacket> {
log.debug("OUT: {}:{}", state, packet); log.debug("OUT: {}:{}", state, packet);
} }
NetByteBuf buffer = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer()); NetByteBuf buffer = ProtocolObjectPool.netByteBuf().borrowObject();
buffer.writeVarInt(packetId); buffer.writeVarInt(packetId);
packet.writeSelf(buffer); packet.writeSelf(buffer);
NetByteBuf netByteBuf = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(out); NetByteBuf netByteBuf = ProtocolObjectPool.netByteBuf().borrowObject(out);
netByteBuf.writeVarInt(buffer.readableBytes()); netByteBuf.writeVarInt(buffer.readableBytes());
netByteBuf.writeBytes(buffer); netByteBuf.writeBytes(buffer);
ProtocolObjectPool.getNetByteBufPool().returnObject(netByteBuf); ProtocolObjectPool.netByteBuf().returnObject(netByteBuf);
ProtocolObjectPool.getNetByteBufPool().returnObject(buffer); ProtocolObjectPool.netByteBuf().returnObject(buffer);
} }
} }

View File

@@ -13,7 +13,7 @@ public class ProtocolSplitter extends ByteToMessageDecoder {
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
NetByteBuf netByteBuf = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(in); NetByteBuf netByteBuf = ProtocolObjectPool.netByteBuf().borrowObject(in);
netByteBuf.markReaderIndex(); netByteBuf.markReaderIndex();
do { do {
@@ -38,6 +38,6 @@ public class ProtocolSplitter extends ByteToMessageDecoder {
} }
} while (netByteBuf.readableBytes() > 0); } while (netByteBuf.readableBytes() > 0);
ProtocolObjectPool.getNetByteBufPool().returnObject(netByteBuf); ProtocolObjectPool.netByteBuf().returnObject(netByteBuf);
} }
} }

View File

@@ -1,7 +1,7 @@
package mc.protocol.packets; package mc.protocol.packets;
import mc.protocol.buffer.NetByteBuf; import mc.protocol.buffer.NetByteBuf;
import mc.protocol.pool.Passivable; import mc.utils.pool.Passivable;
/** /**
* Пакеты отправляемые клиентом. * Пакеты отправляемые клиентом.

View File

@@ -43,12 +43,12 @@ public class CPlayerPositionAndLookPacket implements ClientSidePacket {
double x = netByteBuf.readDouble(); double x = netByteBuf.readDouble();
double y = netByteBuf.readDouble(); double y = netByteBuf.readDouble();
double z = netByteBuf.readDouble(); double z = netByteBuf.readDouble();
this.position = ProtocolObjectPool.getLocationPool().borrowObject(); this.position = ProtocolObjectPool.location().borrowObject();
position.set(x, y, z); position.set(x, y, z);
float yaw = netByteBuf.readFloat(); float yaw = netByteBuf.readFloat();
float pitch = netByteBuf.readFloat(); float pitch = netByteBuf.readFloat();
this.look = ProtocolObjectPool.getLookPool().borrowObject(); this.look = ProtocolObjectPool.look().borrowObject();
this.look.set(yaw, pitch); this.look.set(yaw, pitch);
this.onGround = netByteBuf.readBoolean(); this.onGround = netByteBuf.readBoolean();
@@ -57,11 +57,11 @@ public class CPlayerPositionAndLookPacket implements ClientSidePacket {
@Override @Override
public void passivate() { public void passivate() {
this.position.set(0, 0, 0); this.position.set(0, 0, 0);
ProtocolObjectPool.getLocationPool().returnObject(this.position); ProtocolObjectPool.location().returnObject(this.position);
this.position = null; this.position = null;
this.look.set(0, 0); this.look.set(0, 0);
ProtocolObjectPool.getLookPool().returnObject(this.look); ProtocolObjectPool.look().returnObject(this.look);
this.look = null; this.look = null;
this.onGround = false; this.onGround = false;

View File

@@ -36,7 +36,7 @@ public class PlayerLookPacket implements ClientSidePacket {
public void readSelf(NetByteBuf netByteBuf) { public void readSelf(NetByteBuf netByteBuf) {
float yaw = netByteBuf.readFloat(); float yaw = netByteBuf.readFloat();
float pitch = netByteBuf.readFloat(); float pitch = netByteBuf.readFloat();
this.look = ProtocolObjectPool.getLookPool().borrowObject(); this.look = ProtocolObjectPool.look().borrowObject();
this.look.set(yaw, pitch); this.look.set(yaw, pitch);
this.onGround = netByteBuf.readBoolean(); this.onGround = netByteBuf.readBoolean();
@@ -45,7 +45,7 @@ public class PlayerLookPacket implements ClientSidePacket {
@Override @Override
public void passivate() { public void passivate() {
this.look.set(0, 0); this.look.set(0, 0);
ProtocolObjectPool.getLookPool().returnObject(this.look); ProtocolObjectPool.look().returnObject(this.look);
this.look = null; this.look = null;
this.onGround = false; this.onGround = false;

View File

@@ -39,7 +39,7 @@ public class PlayerPositionPacket implements ClientSidePacket {
double x = netByteBuf.readDouble(); double x = netByteBuf.readDouble();
double y = netByteBuf.readDouble(); double y = netByteBuf.readDouble();
double z = netByteBuf.readDouble(); double z = netByteBuf.readDouble();
this.position = ProtocolObjectPool.getLocationPool().borrowObject(); this.position = ProtocolObjectPool.location().borrowObject();
this.position.set(x, y, z); this.position.set(x, y, z);
this.onGround = netByteBuf.readBoolean(); this.onGround = netByteBuf.readBoolean();
@@ -48,7 +48,7 @@ public class PlayerPositionPacket implements ClientSidePacket {
@Override @Override
public void passivate() { public void passivate() {
this.position.set(0, 0, 0); this.position.set(0, 0, 0);
ProtocolObjectPool.getLocationPool().returnObject(this.position); ProtocolObjectPool.location().returnObject(this.position);
this.position = null; this.position = null;
this.onGround = false; this.onGround = false;

View File

@@ -93,17 +93,17 @@ public class ChunkDataPacket implements ServerSidePacket {
netByteBuf.writeVarInt(0); // Number of block entities netByteBuf.writeVarInt(0); // Number of block entities
// Block entities (NBT's) // Block entities (NBT's)
ProtocolObjectPool.getNetByteBufPool().returnObject(data); ProtocolObjectPool.netByteBuf().returnObject(data);
} }
private NetByteBuf createDataStructure() { private NetByteBuf createDataStructure() {
NetByteBuf dataStructure = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer()); NetByteBuf dataStructure = ProtocolObjectPool.netByteBuf().borrowObject(Unpooled.buffer());
for (int h = 0; h < 16; h++) { for (int h = 0; h < 16; h++) {
ChunkSection section = chunk.getSection(h); ChunkSection section = chunk.getSection(h);
NetByteBuf data = ChunkSerializeUtil.serializeSection(section); NetByteBuf data = ChunkSerializeUtil.serializeSection(section);
dataStructure.writeBytes(data); // Data dataStructure.writeBytes(data); // Data
ProtocolObjectPool.getNetByteBufPool().returnObject(data); ProtocolObjectPool.netByteBuf().returnObject(data);
} }
// <Biomes> // <Biomes>

View File

@@ -0,0 +1,39 @@
package mc.protocol.pool;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import lombok.SneakyThrows;
import mc.protocol.buffer.NetByteBuf;
import mc.utils.pool.ObjectPool;
import mc.utils.pool.PassivablePooledObjectFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class NetByteBufObjectPool implements ObjectPool<NetByteBuf> {
private final org.apache.commons.pool2.ObjectPool apacheObjectPool;
NetByteBufObjectPool() {
this.apacheObjectPool = new GenericObjectPool<>(new PassivablePooledObjectFactory<>(PooledNetByteBuf.class));
}
@Override
public NetByteBuf borrowObject() {
return borrowObject(PooledByteBufAllocator.DEFAULT.directBuffer());
}
@SneakyThrows
public NetByteBuf borrowObject(ByteBuf byteBuf) {
PooledNetByteBuf pooledNetByteBuf = (PooledNetByteBuf) apacheObjectPool.borrowObject();
pooledNetByteBuf.init(byteBuf);
return pooledNetByteBuf;
}
@Override
@SneakyThrows
public void returnObject(NetByteBuf netByteBuf) {
if (netByteBuf instanceof PooledNetByteBuf) {
apacheObjectPool.returnObject(netByteBuf);
}
}
}

View File

@@ -0,0 +1,18 @@
package mc.protocol.pool;
import io.netty.buffer.ByteBuf;
import mc.protocol.buffer.NetByteBuf;
import mc.utils.pool.Passivable;
public class PooledNetByteBuf extends NetByteBuf implements Passivable {
void init(ByteBuf byteBuf) {
this.byteBuf = byteBuf.retain();
}
@Override
public void passivate() {
this.byteBuf.release();
this.byteBuf = null;
}
}

View File

@@ -1,25 +1,36 @@
package mc.protocol.pool; package mc.protocol.pool;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import mc.protocol.buffer.NetByteBuf;
import mc.protocol.model.Location; import mc.protocol.model.Location;
import mc.protocol.model.Look; import mc.protocol.model.Look;
import mc.protocol.packets.ClientSidePacket; import mc.protocol.packets.ClientSidePacket;
import mc.utils.pool.MultiObjectPool;
import mc.utils.pool.ObjectPool;
import mc.utils.pool.PassivableMultiObjectPool;
import mc.utils.pool.SimpleObjectPool;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ProtocolObjectPool { public final class ProtocolObjectPool {
@Getter
private static final ObjectPool<Location> locationPool = new SimpleObjectPool<>(Location.class); private static final ObjectPool<Location> locationPool = new SimpleObjectPool<>(Location.class);
@Getter
private static final ObjectPool<Look> lookPool = new SimpleObjectPool<>(Look.class); private static final ObjectPool<Look> lookPool = new SimpleObjectPool<>(Look.class);
private static final NetByteBufObjectPool netByteBufPool = new NetByteBufObjectPool();
@Getter
private static final ObjectPool<NetByteBuf> netByteBufPool = new PassivableObjectPool<>(NetByteBuf.class);
@Getter
private static final MultiObjectPool<ClientSidePacket> packetPool = new PassivableMultiObjectPool<>(); private static final MultiObjectPool<ClientSidePacket> packetPool = new PassivableMultiObjectPool<>();
public static ObjectPool<Location> location() {
return locationPool;
}
public static ObjectPool<Look> look() {
return lookPool;
}
public static NetByteBufObjectPool netByteBuf() {
return netByteBufPool;
}
public static MultiObjectPool<ClientSidePacket> packet() {
return packetPool;
}
} }

View File

@@ -0,0 +1,11 @@
package mc.protocol.pool;
import io.netty.buffer.ByteBuf;
import mc.protocol.buffer.NetByteBuf;
public class UnpooledNetByteBuf extends NetByteBuf {
public UnpooledNetByteBuf(ByteBuf byteBuf) {
this.byteBuf = byteBuf;
}
}

View File

@@ -35,7 +35,7 @@ public final class ChunkSerializeUtil {
} }
} }
NetByteBuf result = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer()); NetByteBuf result = ProtocolObjectPool.netByteBuf().borrowObject(Unpooled.buffer());
result.writeUnsignedByte(BITS_PER_BLOCK); // Bits Per Block result.writeUnsignedByte(BITS_PER_BLOCK); // Bits Per Block
result.writeVarInt(0); // Palette, Direct mode result.writeVarInt(0); // Palette, Direct mode
result.writeVarInt(blockArray.size()); // Data Array Length result.writeVarInt(blockArray.size()); // Data Array Length

View File

@@ -1,6 +1,7 @@
package mc.protocol.buffer; package mc.protocol.buffer;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import mc.protocol.pool.UnpooledNetByteBuf;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
@@ -33,7 +34,7 @@ class NetByteBufReadTest {
void readBoolean(byte sourceByte, boolean expectedValue) { void readBoolean(byte sourceByte, boolean expectedValue) {
baos.write(sourceByte); baos.write(sourceByte);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(expectedValue, netByteBuf.readBoolean()); assertEquals(expectedValue, netByteBuf.readBoolean());
} }
@@ -44,7 +45,7 @@ class NetByteBufReadTest {
random.nextBytes(bytes); random.nextBytes(bytes);
baos.write(bytes[0]); baos.write(bytes[0]);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(bytes[0], netByteBuf.readByte()); assertEquals(bytes[0], netByteBuf.readByte());
} }
@@ -54,7 +55,7 @@ class NetByteBufReadTest {
void readUnsignedByte(byte sourceByte, int expectedValue) { void readUnsignedByte(byte sourceByte, int expectedValue) {
baos.write(sourceByte); baos.write(sourceByte);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(expectedValue, netByteBuf.readUnsignedByte()); assertEquals(expectedValue, netByteBuf.readUnsignedByte());
} }
@@ -64,7 +65,7 @@ class NetByteBufReadTest {
int value = Integer.valueOf(random.nextInt()).shortValue(); int value = Integer.valueOf(random.nextInt()).shortValue();
new DataOutputStream(baos).writeShort(value); new DataOutputStream(baos).writeShort(value);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(value, netByteBuf.readShort()); assertEquals(value, netByteBuf.readShort());
} }
@@ -74,7 +75,7 @@ class NetByteBufReadTest {
int value = 32768; int value = 32768;
new DataOutputStream(baos).writeShort(value); new DataOutputStream(baos).writeShort(value);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(value, netByteBuf.readUnsignedShort()); assertEquals(value, netByteBuf.readUnsignedShort());
} }
@@ -89,7 +90,7 @@ class NetByteBufReadTest {
baos.write(bytes); baos.write(bytes);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(string, netByteBuf.readString()); assertEquals(string, netByteBuf.readString());
} }
@@ -105,7 +106,7 @@ class NetByteBufReadTest {
baos.write(bytes); baos.write(bytes);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertThrows(NetIOException.class, () -> netByteBuf.readString(length)); assertThrows(NetIOException.class, () -> netByteBuf.readString(length));
} }
@@ -124,7 +125,7 @@ class NetByteBufReadTest {
baos.write(bytes); baos.write(bytes);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertThrows(NetIOException.class, () -> netByteBuf.readString(-1)); assertThrows(NetIOException.class, () -> netByteBuf.readString(-1));
} }
@@ -134,7 +135,7 @@ class NetByteBufReadTest {
void readVarInt(byte[] sourceBytes, int expectedValue) throws IOException { void readVarInt(byte[] sourceBytes, int expectedValue) throws IOException {
baos.write(sourceBytes); baos.write(sourceBytes);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(expectedValue, netByteBuf.readVarInt()); assertEquals(expectedValue, netByteBuf.readVarInt());
} }
@@ -143,7 +144,7 @@ class NetByteBufReadTest {
void readVarInt_tooBig() throws IOException { void readVarInt_tooBig() throws IOException {
baos.write(new byte[]{ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x0F }); baos.write(new byte[]{ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x0F });
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(-1, netByteBuf.readVarInt()); assertEquals(-1, netByteBuf.readVarInt());
} }
@@ -153,7 +154,7 @@ class NetByteBufReadTest {
void readVarLong(byte[] sourceBytes, long expectedValue) throws IOException { void readVarLong(byte[] sourceBytes, long expectedValue) throws IOException {
baos.write(sourceBytes); baos.write(sourceBytes);
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(expectedValue, netByteBuf.readVarLong()); assertEquals(expectedValue, netByteBuf.readVarLong());
} }
@@ -164,7 +165,7 @@ class NetByteBufReadTest {
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0x0F }); (byte) 0xFF, (byte) 0x0F });
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(-1, netByteBuf.readVarLong()); assertEquals(-1, netByteBuf.readVarLong());
} }
@@ -196,7 +197,7 @@ class NetByteBufReadTest {
(byte) (leastSignificantBits & 0xFF) (byte) (leastSignificantBits & 0xFF)
}); });
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(uuid, netByteBuf.readUUID()); assertEquals(uuid, netByteBuf.readUUID());
} }
@@ -208,7 +209,7 @@ class NetByteBufReadTest {
baos.write(bytes); baos.write(bytes);
byte[] actualBytes = new byte[128]; byte[] actualBytes = new byte[128];
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
assertEquals(bytes.length, netByteBuf.readableBytes()); assertEquals(bytes.length, netByteBuf.readableBytes());
@@ -225,7 +226,7 @@ class NetByteBufReadTest {
baos.write(bytes); baos.write(bytes);
byte[] buff = new byte[128]; byte[] buff = new byte[128];
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray())); NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
netByteBuf.readBytes(buff, 3, 11); netByteBuf.readBytes(buff, 3, 11);
byte[] expectedBytes = new byte[11]; byte[] expectedBytes = new byte[11];

View File

@@ -2,6 +2,7 @@ package mc.protocol.buffer;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import mc.protocol.pool.UnpooledNetByteBuf;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -30,7 +31,7 @@ class NetByteBufWriteTest {
@MethodSource("paramsWriteBoolean") @MethodSource("paramsWriteBoolean")
void writeBoolean(boolean sourceValue, byte expectedByte) { void writeBoolean(boolean sourceValue, byte expectedByte) {
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeBoolean(sourceValue); netByteBuf.writeBoolean(sourceValue);
@@ -41,7 +42,7 @@ class NetByteBufWriteTest {
@MethodSource("paramsWriteByte") @MethodSource("paramsWriteByte")
void writeByte(byte sourceValue, byte expectedByte) { void writeByte(byte sourceValue, byte expectedByte) {
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeByte(sourceValue); netByteBuf.writeByte(sourceValue);
@@ -51,7 +52,7 @@ class NetByteBufWriteTest {
@Test @Test
void writeUnsignedByte() { void writeUnsignedByte() {
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeUnsignedByte(129); netByteBuf.writeUnsignedByte(129);
@@ -62,7 +63,7 @@ class NetByteBufWriteTest {
@MethodSource("paramsWriteString") @MethodSource("paramsWriteString")
void writeString(String string, int exceptedLength) { void writeString(String string, int exceptedLength) {
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeString(string); netByteBuf.writeString(string);
@@ -81,11 +82,11 @@ class NetByteBufWriteTest {
String overSizeString = RandomStringUtils.randomAscii(Short.MAX_VALUE + Short.MAX_VALUE); String overSizeString = RandomStringUtils.randomAscii(Short.MAX_VALUE + Short.MAX_VALUE);
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeString(overSizeString); netByteBuf.writeString(overSizeString);
NetByteBuf netByteBuf2 = new NetByteBuf().setByteBuf(byteBuf.copy()); NetByteBuf netByteBuf2 = new UnpooledNetByteBuf(byteBuf.copy());
String actualString = netByteBuf2.readString(); String actualString = netByteBuf2.readString();
String expectedString = overSizeString.substring(0, Short.MAX_VALUE); String expectedString = overSizeString.substring(0, Short.MAX_VALUE);
@@ -97,7 +98,7 @@ class NetByteBufWriteTest {
@MethodSource("paramsWriteVarInt") @MethodSource("paramsWriteVarInt")
void writeVarInt(int sourceValue, byte[] expectedBytes) { void writeVarInt(int sourceValue, byte[] expectedBytes) {
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeVarInt(sourceValue); netByteBuf.writeVarInt(sourceValue);
byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array(); byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array();
@@ -109,7 +110,7 @@ class NetByteBufWriteTest {
@MethodSource({ "paramsWriteVarInt", "paramsWriteVarLong" }) @MethodSource({ "paramsWriteVarInt", "paramsWriteVarLong" })
void writeVarLong(long sourceValue, byte[] expectedBytes) { void writeVarLong(long sourceValue, byte[] expectedBytes) {
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeVarLong(sourceValue); netByteBuf.writeVarLong(sourceValue);
byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array(); byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array();
@@ -122,7 +123,7 @@ class NetByteBufWriteTest {
final UUID uuid = UUID.randomUUID(); final UUID uuid = UUID.randomUUID();
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeUUID(uuid); netByteBuf.writeUUID(uuid);
@@ -158,7 +159,7 @@ class NetByteBufWriteTest {
random.nextBytes(bytes); random.nextBytes(bytes);
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeBytes(bytes); netByteBuf.writeBytes(bytes);
byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array(); byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array();
@@ -172,7 +173,7 @@ class NetByteBufWriteTest {
random.nextBytes(bytes); random.nextBytes(bytes);
ByteBuf byteBuf = Unpooled.buffer(); ByteBuf byteBuf = Unpooled.buffer();
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf); NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
netByteBuf.writeBytes(bytes, 3, 11); netByteBuf.writeBytes(bytes, 3, 11);

View File

@@ -129,7 +129,9 @@ public class ScenarioLogin implements PacketScenario {
private void sendPlayerPositionAndLook(Player player) { private void sendPlayerPositionAndLook(Player player) {
var playerPositionAndLookPacket = new SPlayerPositionAndLookPacket(); var playerPositionAndLookPacket = new SPlayerPositionAndLookPacket();
playerPositionAndLookPacket.setPosition(player.getLocation()); playerPositionAndLookPacket.setPosition(player.getLocation());
playerPositionAndLookPacket.setLook(new Look().set(0f, 0f)); Look look = new Look();
look.set(0f, 0f);
playerPositionAndLookPacket.setLook(look);
playerPositionAndLookPacket.setTeleportId(random.nextInt()); playerPositionAndLookPacket.setTeleportId(random.nextInt());
player.getCtx().write(playerPositionAndLookPacket); player.getCtx().write(playerPositionAndLookPacket);

View File

@@ -6,7 +6,10 @@ import mc.protocol.model.Location;
@UtilityClass @UtilityClass
public class LocationUtils { public class LocationUtils {
//TODO заменить другими инструментами
public Location toChunkXZ(Location location) { public Location toChunkXZ(Location location) {
return new Location().set((int) location.getX() >> 4, 0d, (int) location.getZ() >> 4); Location location0 = new Location();
location0.set((int) location.getX() >> 4, 0d, (int) location.getZ() >> 4);
return location0;
} }
} }

View File

@@ -20,14 +20,18 @@ public class SomeChunkSection implements ChunkSection {
public Block getBlock(int x, int y, int z) { public Block getBlock(int x, int y, int z) {
if (this.y == 15) { if (this.y == 15) {
if (y == 0) { if (y == 0) {
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> { Location location1 = new Location();
location1.set(x, y, z);
return blocks.computeIfAbsent(location1, location -> {
SomeBlock block = new SomeBlock(); SomeBlock block = new SomeBlock();
block.setLocation(location); block.setLocation(location);
block.setId(3); block.setId(3);
return block; return block;
}); });
} else { } else {
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> { Location location1 = new Location();
location1.set(x, y, z);
return blocks.computeIfAbsent(location1, location -> {
SomeBlock block = new SomeBlock(); SomeBlock block = new SomeBlock();
block.setLocation(location); block.setLocation(location);
block.setId(0); block.setId(0);
@@ -35,7 +39,9 @@ public class SomeChunkSection implements ChunkSection {
}); });
} }
} else { } else {
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> { Location location1 = new Location();
location1.set(x, y, z);
return blocks.computeIfAbsent(location1, location -> {
SomeBlock block = new SomeBlock(); SomeBlock block = new SomeBlock();
block.setLocation(location); block.setLocation(location);
block.setId(1); block.setId(1);

View File

@@ -9,7 +9,7 @@ import mc.utils.Table;
public class SomeWorld implements World { public class SomeWorld implements World {
private static final Location spawn = ProtocolObjectPool.getLocationPool().borrowObject().set(-790d, 256d, -263d + 16d); private static final Location spawn;
private final Table<Integer, Integer, Chunk> chunkTable = new Table<>(); private final Table<Integer, Integer, Chunk> chunkTable = new Table<>();
@Override @Override
@@ -36,4 +36,9 @@ public class SomeWorld implements World {
return chunk; return chunk;
} }
static {
spawn = new Location();
spawn.set(-790d, 256d, -263d + 16d);
}
} }

View File

@@ -28,4 +28,10 @@
<!--<logger name="io.netty.handler.logging.LoggingHandler" level="debug" additivity="false"> <!--<logger name="io.netty.handler.logging.LoggingHandler" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/> <appender-ref ref="CONSOLE"/>
</logger>--> </logger>-->
<!--<logger name="mc.protocol.handler.codec.ProtocolEncoder" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="mc.protocol.handler.codec.ProtocolDecoder" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>-->
</configuration> </configuration>

View File

@@ -1 +1,5 @@
apply from: rootDir.toPath().resolve('logic.gradle').toFile() apply from: rootDir.toPath().resolve('logic.gradle').toFile()
dependencies {
api libs.objpool
}

View File

@@ -1,4 +1,4 @@
package mc.protocol.pool; package mc.utils.pool;
public interface MultiObjectPool<T> { public interface MultiObjectPool<T> {

View File

@@ -1,4 +1,4 @@
package mc.protocol.pool; package mc.utils.pool;
public interface ObjectPool<T> { public interface ObjectPool<T> {

View File

@@ -1,4 +1,4 @@
package mc.protocol.pool; package mc.utils.pool;
public interface Passivable { public interface Passivable {

View File

@@ -1,4 +1,4 @@
package mc.protocol.pool; package mc.utils.pool;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@@ -1,4 +1,4 @@
package mc.protocol.pool; package mc.utils.pool;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPool;

View File

@@ -1,10 +1,10 @@
package mc.protocol.pool; package mc.utils.pool;
import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.PooledObject;
class PassivablePooledObjectFactory<T extends Passivable> extends SimplePooledObjectFactory<T> { public class PassivablePooledObjectFactory<T extends Passivable> extends SimplePooledObjectFactory<T> {
PassivablePooledObjectFactory(Class<T> clazz) { public PassivablePooledObjectFactory(Class<T> clazz) {
super(clazz); super(clazz);
} }

View File

@@ -1,4 +1,4 @@
package mc.protocol.pool; package mc.utils.pool;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPool;

View File

@@ -1,4 +1,4 @@
package mc.protocol.pool; package mc.utils.pool;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.pool2.BasePooledObjectFactory; import org.apache.commons.pool2.BasePooledObjectFactory;