fix: чтение пакетов
This commit is contained in:
@@ -49,7 +49,7 @@ public abstract class NetInputStream extends InputStream {
|
||||
|
||||
byte[] bytes = new byte[length * 4];
|
||||
int readbleBytes = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
for (int i = 0; i < length && readableBytes() > 0; i++) {
|
||||
byte b = readByte();
|
||||
bytes[readbleBytes++] = b;
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package mc.protocol.io.coder;
|
||||
|
||||
import mc.protocol.io.NetInputStream;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class ByteArrayNetInputStream extends NetInputStream {
|
||||
|
||||
private final ByteBuffer byteBuffer;
|
||||
private int index = 0;
|
||||
|
||||
public ByteArrayNetInputStream(byte[] buffer) {
|
||||
byteBuffer = ByteBuffer.wrap(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markReadIndex() {
|
||||
index = byteBuffer.position();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetReadIndex() {
|
||||
byteBuffer.position(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readableBytes() {
|
||||
return byteBuffer.limit(); //TODO нужно простестировать
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte readByte() {
|
||||
return byteBuffer.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readBytes(byte[] buffer, int offset, int lengtn) {
|
||||
return readableBytes() - byteBuffer.get(buffer, offset, lengtn).remaining();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readShort() {
|
||||
return byteBuffer.getShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readInt() {
|
||||
return byteBuffer.getInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long readLong() {
|
||||
return byteBuffer.getLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float readFloat() {
|
||||
return byteBuffer.getFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDouble() {
|
||||
return byteBuffer.getDouble();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package mc.protocol.io;
|
||||
package mc.protocol.io.coder;
|
||||
|
||||
import mc.protocol.io.NetOutputStream;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
@@ -10,9 +10,6 @@ public class ProtocolDecoder {
|
||||
private final PacketDirection direction;
|
||||
|
||||
public Packet decode(State state, NetInputStream netInputStream) {
|
||||
//TODO необходим механизм пропуска необработанных байтов
|
||||
int sizePacket = netInputStream.readVarInt();
|
||||
|
||||
int packetId = netInputStream.readVarInt();
|
||||
Class<? extends Packet> packetClass = state.getPacketById(direction, packetId);
|
||||
if (packetClass == null) {
|
||||
@@ -20,7 +17,7 @@ public class ProtocolDecoder {
|
||||
UnknownPacket packet = new UnknownPacket(
|
||||
state,
|
||||
packetId,
|
||||
sizePacket - Utils.sizeOfVarInt(packetId)
|
||||
netInputStream.readableBytes()
|
||||
);
|
||||
packet.readSelf(netInputStream);
|
||||
return packet;
|
||||
|
||||
@@ -4,7 +4,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import mc.protocol.Packet;
|
||||
import mc.protocol.PacketDirection;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.io.ByteArrayNetOutputStream;
|
||||
import mc.protocol.io.NetOutputStream;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
32
src/main/java/mc/protocol/io/coder/ProtocolSplitter.java
Normal file
32
src/main/java/mc/protocol/io/coder/ProtocolSplitter.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package mc.protocol.io.coder;
|
||||
|
||||
import mc.protocol.io.NetInputStream;
|
||||
|
||||
public class ProtocolSplitter {
|
||||
|
||||
public NetInputStream split(NetInputStream netInputStream) {
|
||||
netInputStream.markReadIndex();
|
||||
byte[] buff = new byte[3];
|
||||
|
||||
for (int i = 0; i < buff.length; ++i) {
|
||||
buff[i] = netInputStream.readByte();
|
||||
|
||||
if (buff[i] >= 0) {
|
||||
NetInputStream nis = new ByteArrayNetInputStream(buff);
|
||||
int sizePacket = nis.readVarInt();
|
||||
|
||||
if (netInputStream.readableBytes() >= sizePacket) {
|
||||
buff = new byte[sizePacket];
|
||||
netInputStream.readBytes(buff);
|
||||
|
||||
return new ByteArrayNetInputStream(buff);
|
||||
}
|
||||
|
||||
netInputStream.resetReadIndex();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user