Archived
0

разделение на серверные и клиентские пакеты

This commit is contained in:
2021-04-28 18:34:19 +03:00
parent 801e68ea55
commit f4f796c3a4
17 changed files with 72 additions and 96 deletions

View File

@@ -1,44 +1,45 @@
package mc.protocol;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import mc.protocol.packets.ClientSidePacket;
import mc.protocol.packets.Packet;
import mc.protocol.packets.PacketDirection;
import mc.protocol.packets.PingPacket;
import mc.protocol.packets.ServerSidePacket;
import mc.protocol.packets.client.HandshakePacket;
import mc.protocol.packets.client.LoginStartPacket;
import mc.protocol.packets.client.StatusServerRequest;
import mc.protocol.packets.client.StatusServerRequestPacket;
import mc.protocol.packets.server.DisconnectPacket;
import mc.protocol.packets.server.StatusServerResponse;
import mc.protocol.packets.server.StatusServerResponsePacket;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Map;
@RequiredArgsConstructor
public enum State {
HANDSHAKING(-1,
// server bound
ImmutableBiMap.of(0x00, HandshakePacket.class)
// client side
Map.of(0x00, HandshakePacket.class)
),
STATUS(1,
// server bound
ImmutableBiMap.of(
0x00, StatusServerRequest.class,
// client side
Map.of(
0x00, StatusServerRequestPacket.class,
0x01, PingPacket.class
),
// client bound
ImmutableBiMap.of(
0x00, StatusServerResponse.class,
0x01, PingPacket.class
// server side
Map.of(
StatusServerResponsePacket.class, 0x00,
PingPacket.class, 0x01
)
),
LOGIN(2,
// server bound
ImmutableBiMap.of(0x00, LoginStartPacket.class),
Map.of(0x00, LoginStartPacket.class),
// client bound
ImmutableBiMap.of(0x00, DisconnectPacket.class)
Map.of(DisconnectPacket.class, 0x00)
);
@Nullable
@@ -56,30 +57,22 @@ public enum State {
private final int id;
@Getter
private final BiMap<Integer, Class<? extends Packet>> serverBoundPackets;
private final BiMap<Integer, Class<? extends Packet>> clientBoundPackets;
private final Map<Integer, Class<? extends ClientSidePacket>> clientSidePackets;
private final Map<Class<? extends ServerSidePacket>, Integer> serverSidePackets;
State(int id, BiMap<Integer, Class<? extends Packet>> serverBoundPackets) {
State(int id, Map<Integer, Class<? extends ClientSidePacket>> clientSidePackets) {
this.id = id;
this.serverBoundPackets = serverBoundPackets;
this.clientBoundPackets = ImmutableBiMap.of();
this.clientSidePackets = clientSidePackets;
this.serverSidePackets = Collections.emptyMap();
}
@Nullable
public Class<? extends Packet> getPacketById(PacketDirection direction, int id) {
if (PacketDirection.CLIENT_BOUND == direction) {
return clientBoundPackets == null ? null : clientBoundPackets.get(id);
} else {
return serverBoundPackets == null ? null : serverBoundPackets.get(id);
}
public Class<? extends ClientSidePacket> getClientSidePacketById(int id) {
return clientSidePackets == null ? null : clientSidePackets.get(id);
}
@Nullable
public Integer getIdByPacket(PacketDirection direction, Class<? extends Packet> clazz) {
if (PacketDirection.CLIENT_BOUND == direction) {
return clientBoundPackets == null ? null : clientBoundPackets.inverse().get(clazz);
} else {
return serverBoundPackets == null ? null : serverBoundPackets.inverse().get(clazz);
}
public Integer getServerSidePacketId(Class<? extends Packet> clazz) {
return serverSidePackets == null ? null : serverSidePackets.get(clazz);
}
}

View File

@@ -84,7 +84,7 @@ public class ProtocolModule {
ImmutableMap.Builder<Class<? extends Packet>, Sinks.Many<ChannelContext>> builder = ImmutableMap.builder();
Stream.of(State.values())
.flatMap(state -> state.getServerBoundPackets().values().stream())
.flatMap(state -> state.getClientSidePackets().values().stream())
.forEach(packetClass -> builder.put(packetClass, Sinks.many().multicast().directBestEffort()));
return builder.build();

View File

@@ -8,8 +8,7 @@ import lombok.extern.slf4j.Slf4j;
import mc.protocol.NetworkAttributes;
import mc.protocol.State;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.Packet;
import mc.protocol.packets.PacketDirection;
import mc.protocol.packets.ClientSidePacket;
import mc.protocol.packets.UnknownPacket;
import java.util.List;
@@ -39,7 +38,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
NetByteBuf netByteBuf = new NetByteBuf(in);
int packetId = netByteBuf.readVarInt();
Class<? extends Packet> packetClass = state.getPacketById(PacketDirection.SERVER_BOUND, packetId);
Class<? extends ClientSidePacket> packetClass = state.getClientSidePacketById(packetId);
if (packetClass == null) {
log.warn("Unkown packet: State {} ; Id {}", state, packetId);
@@ -51,7 +50,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
netByteBuf.skipBytes(netByteBuf.readableBytes());
}
} else {
Packet packet = packetClass.getDeclaredConstructor().newInstance();
ClientSidePacket packet = packetClass.getDeclaredConstructor().newInstance();
packet.readSelf(netByteBuf);
out.add(packet);
}

View File

@@ -7,17 +7,16 @@ import io.netty.handler.codec.MessageToByteEncoder;
import mc.protocol.NetworkAttributes;
import mc.protocol.State;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.Packet;
import mc.protocol.packets.PacketDirection;
import mc.protocol.packets.ServerSidePacket;
import java.util.Objects;
public class ProtocolEncoder extends MessageToByteEncoder<Packet> {
public class ProtocolEncoder extends MessageToByteEncoder<ServerSidePacket> {
@Override
protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) {
protected void encode(ChannelHandlerContext ctx, ServerSidePacket packet, ByteBuf out) {
State state = ctx.channel().attr(NetworkAttributes.STATE).get();
int packetId = Objects.requireNonNull(state.getIdByPacket(PacketDirection.CLIENT_BOUND, packet.getClass()));
int packetId = Objects.requireNonNull(state.getServerSidePacketId(packet.getClass()));
NetByteBuf buffer = new NetByteBuf(Unpooled.buffer());
buffer.writeVarInt(packetId);

View File

@@ -0,0 +1,11 @@
package mc.protocol.packets;
import mc.protocol.io.NetByteBuf;
/**
* Пакеты отправляемые клиентом.
*/
public interface ClientSidePacket extends Packet {
void readSelf(NetByteBuf netByteBuf);
}

View File

@@ -2,7 +2,7 @@ package mc.protocol.packets;
import mc.protocol.io.NetByteBuf;
public abstract class EmptyPacket implements Packet {
public abstract class EmptyPacket implements ClientSidePacket, ServerSidePacket {
@Override
public void readSelf(NetByteBuf netByteBuf) {

View File

@@ -1,7 +1,5 @@
package mc.protocol.packets;
import mc.protocol.io.NetByteBuf;
/**
* Пакет.
*
@@ -18,7 +16,4 @@ import mc.protocol.io.NetByteBuf;
*/
public interface Packet {
void readSelf(NetByteBuf netByteBuf);
void writeSelf(NetByteBuf netByteBuf);
}

View File

@@ -1,6 +0,0 @@
package mc.protocol.packets;
public enum PacketDirection {
SERVER_BOUND, CLIENT_BOUND
}

View File

@@ -25,7 +25,7 @@ import mc.protocol.io.NetByteBuf;
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=7368#Keep_Alive">Keep Alive</a>
*/
@Data
public class PingPacket implements Packet {
public class PingPacket implements ClientSidePacket, ServerSidePacket {
private Long payload;

View File

@@ -0,0 +1,11 @@
package mc.protocol.packets;
import mc.protocol.io.NetByteBuf;
/**
* Пакеты отправляемые сервером.
*/
public interface ServerSidePacket extends Packet {
void writeSelf(NetByteBuf netByteBuf);
}

View File

@@ -7,7 +7,7 @@ import mc.protocol.io.NetByteBuf;
@Data
@ToString(exclude = "rawData")
public class UnknownPacket implements Packet {
public class UnknownPacket implements ClientSidePacket {
private final State state;
private final int id;
@@ -19,9 +19,4 @@ public class UnknownPacket implements Packet {
rawData = new byte[dataSize];
netByteBuf.readBytes(rawData);
}
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeBytes(rawData);
}
}

View File

@@ -6,7 +6,7 @@ import lombok.NoArgsConstructor;
import lombok.ToString;
import mc.protocol.State;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.Packet;
import mc.protocol.packets.ClientSidePacket;
/**
* Handshake packet.
@@ -32,7 +32,7 @@ import mc.protocol.packets.Packet;
@Getter
@EqualsAndHashCode
@ToString
public class HandshakePacket implements Packet {
public class HandshakePacket implements ClientSidePacket {
private int protocolVersion;
private String host;
@@ -47,11 +47,4 @@ public class HandshakePacket implements Packet {
nextState = State.getById(netByteBuf.readVarInt());
}
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeVarInt(protocolVersion);
netByteBuf.writeString(host);
netByteBuf.writeShort(port);
netByteBuf.writeVarInt(nextState.getId());
}
}

View File

@@ -5,7 +5,7 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.Packet;
import mc.protocol.packets.ClientSidePacket;
/**
* Login start packet.
@@ -25,7 +25,7 @@ import mc.protocol.packets.Packet;
@Getter
@EqualsAndHashCode
@ToString
public class LoginStartPacket implements Packet {
public class LoginStartPacket implements ClientSidePacket {
private String name;
@@ -34,8 +34,4 @@ public class LoginStartPacket implements Packet {
this.name = netByteBuf.readString();
}
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeString(name);
}
}

View File

@@ -13,6 +13,6 @@ import mc.protocol.packets.EmptyPacket;
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@ToString
public class StatusServerRequest extends EmptyPacket {
public class StatusServerRequestPacket extends EmptyPacket {
}

View File

@@ -3,7 +3,7 @@ package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.State;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.Packet;
import mc.protocol.packets.ServerSidePacket;
/**
* Diconnect packet.
@@ -21,7 +21,7 @@ import mc.protocol.packets.Packet;
* @see State
*/
@Data
public class DisconnectPacket implements Packet {
public class DisconnectPacket implements ServerSidePacket {
/**
* Причина отключения.
@@ -35,11 +35,6 @@ public class DisconnectPacket implements Packet {
*/
private String reason;
@Override
public void readSelf(NetByteBuf netByteBuf) {
this.reason = netByteBuf.readString();
}
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeString(reason);

View File

@@ -2,7 +2,7 @@ package mc.protocol.packets.server;
import lombok.Data;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.Packet;
import mc.protocol.packets.ServerSidePacket;
/**
* Status server packet, response.
@@ -19,7 +19,7 @@ import mc.protocol.packets.Packet;
* </pre></p>
*/
@Data
public class StatusServerResponse implements Packet {
public class StatusServerResponsePacket implements ServerSidePacket {
/**
* Информация о серере в формате JSON
@@ -50,11 +50,6 @@ public class StatusServerResponse implements Packet {
*/
private String info;
@Override
public void readSelf(NetByteBuf netByteBuf) {
info = netByteBuf.readString();
}
@Override
public void writeSelf(NetByteBuf netByteBuf) {
netByteBuf.writeString(info);