разделил encoder на две части
This commit is contained in:
@@ -10,7 +10,6 @@ import io.netty.handler.codec.MessageToByteEncoder;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import mc.core.network.NetOutputStream;
|
import mc.core.network.NetOutputStream;
|
||||||
import mc.core.network.SCPacket;
|
import mc.core.network.SCPacket;
|
||||||
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
|
|
||||||
import mc.core.network.proto_1_12_2.State;
|
import mc.core.network.proto_1_12_2.State;
|
||||||
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetOutputStream;
|
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetOutputStream;
|
||||||
|
|
||||||
@@ -18,17 +17,6 @@ import static mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_STATE;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
||||||
private static int sizeVarInt(final int value) {
|
|
||||||
byte size = 0;
|
|
||||||
int v = value;
|
|
||||||
|
|
||||||
do {
|
|
||||||
v >>>= 7;
|
|
||||||
size++;
|
|
||||||
} while (v != 0);
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, SCPacket packet, ByteBuf out) {
|
protected void encode(ChannelHandlerContext ctx, SCPacket packet, ByteBuf out) {
|
||||||
@@ -41,13 +29,8 @@ public class PacketEncoder extends MessageToByteEncoder<SCPacket> {
|
|||||||
|
|
||||||
log.debug("Send {}:{}", state, packet);
|
log.debug("Send {}:{}", state, packet);
|
||||||
|
|
||||||
NetOutputStream netStream = new ByteArrayOutputNetStream();
|
NetOutputStream netStream = new WrapperNetOutputStream(out);
|
||||||
packet.writeSelf(netStream);
|
|
||||||
byte[] bytes = ((ByteArrayOutputNetStream) netStream).toByteArray();
|
|
||||||
netStream = new WrapperNetOutputStream(out);
|
|
||||||
|
|
||||||
netStream.writeVarInt(bytes.length + sizeVarInt(id));
|
|
||||||
netStream.writeVarInt(id);
|
netStream.writeVarInt(id);
|
||||||
netStream.writeBytes(bytes);
|
packet.writeSelf(netStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package mc.core.network.proto_1_12_2.netty;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.handler.codec.MessageToByteEncoder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import mc.core.network.proto_1_12_2.netty.wrappers.WrapperNetOutputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Подсчет размера отправляемого пакета
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class PacketPostEncoder extends MessageToByteEncoder<ByteBuf> {
|
||||||
|
|
||||||
|
private static int getVarIntSize(int input) {
|
||||||
|
for (int i = 1; i < 5; ++i) {
|
||||||
|
if ((input & -1 << i * 7) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
|
||||||
|
final int pktSize = msg.readableBytes();
|
||||||
|
final int sizeOfPktSize = getVarIntSize(pktSize);
|
||||||
|
|
||||||
|
if (sizeOfPktSize > 3) {
|
||||||
|
log.warn("unable to fit {} into {}", pktSize, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
out.ensureWritable(sizeOfPktSize + pktSize);
|
||||||
|
(new WrapperNetOutputStream(out)).writeVarInt(pktSize);
|
||||||
|
out.writeBytes(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user