Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename and document packets #87

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void handleSyncServerEncryptionSuccess() {
// TODO tell server our current dimension
}

public void handleRegionTimestamps(SRegionTimestamps packet, SyncClient client) {
public void handleRegionTimestamps(ClientboundRegionTimestampsPacket packet, SyncClient client) {
DimensionState dimension = getDimensionState();
if (dimension == null) return;
if (!dimension.dimension.location().toString().equals(packet.getDimension())) {
Expand All @@ -241,7 +241,7 @@ public void handleRegionTimestamps(SRegionTimestamps packet, SyncClient client)
}
}

client.send(new CRegionCatchup(packet.getDimension(), outdatedRegions));
client.send(new ServerboundChunkTimestampsRequestPacket(packet.getDimension(), outdatedRegions));
}

public void handleSharedChunk(ChunkTile chunkTile) {
Expand All @@ -255,7 +255,7 @@ public void handleSharedChunk(ChunkTile chunkTile) {
dimensionState.processSharedChunk(chunkTile);
}

public void handleCatchupData(SCatchup packet) {
public void handleCatchupData(ClientboundChunkTimestampsResponsePacket packet) {
var dimensionState = getDimensionState();
if (dimensionState == null) return;
debugLog("received catchup: " + packet.chunks.size() + " " + packet.chunks.get(0).syncClient.address);
Expand All @@ -276,7 +276,7 @@ public void requestCatchupData(List<CatchupChunk> chunks) {
}
for (List<CatchupChunk> chunksForServer : byServer.values()) {
SyncClient client = chunksForServer.get(0).syncClient;
client.send(new CCatchupRequest(chunksForServer));
client.send(new ServerboundCatchupRequestPacket(chunksForServer));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public ClientHandler(SyncClient client) {
public void channelRead(ChannelHandlerContext ctx, Object packet) {
try {
if (!client.isEncrypted()) {
if (packet instanceof SEncryptionRequest pktEncryptionRequest) {
if (packet instanceof ClientboundEncryptionRequestPacket pktEncryptionRequest) {
client.setUpEncryption(ctx, pktEncryptionRequest);
} else throw new Error("Expected encryption request, got " + packet);
} else if (packet instanceof ChunkTilePacket pktChunkTile) {
getMod().handleSharedChunk(pktChunkTile.chunkTile);
} else if (packet instanceof SRegionTimestamps pktRegionTimestamps) {
} else if (packet instanceof ClientboundRegionTimestampsPacket pktRegionTimestamps) {
getMod().handleRegionTimestamps(pktRegionTimestamps, client);
} else if (packet instanceof SCatchup pktCatchup) {
} else if (packet instanceof ClientboundChunkTimestampsResponsePacket pktCatchup) {
for (CatchupChunk chunk : pktCatchup.chunks) {
chunk.syncClient = this.client;
}
getMod().handleCatchupData((SCatchup) packet);
getMod().handleCatchupData((ClientboundChunkTimestampsResponsePacket) packet);
} else throw new Error("Expected packet, got " + packet);
} catch (Throwable err) {
err.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import java.util.List;

public class ServerPacketDecoder extends ReplayingDecoder<Void> {
public class ClientboundPacketDecoder extends ReplayingDecoder<Void> {
public static @Nullable Packet constructServerPacket(int id, ByteBuf buf) {
if (id == ChunkTilePacket.PACKET_ID) return ChunkTilePacket.read(buf);
if (id == SEncryptionRequest.PACKET_ID) return SEncryptionRequest.read(buf);
if (id == SCatchup.PACKET_ID) return SCatchup.read(buf);
if (id == SRegionTimestamps.PACKET_ID) return SRegionTimestamps.read(buf);
if (id == ClientboundEncryptionRequestPacket.PACKET_ID) return ClientboundEncryptionRequestPacket.read(buf);
if (id == ClientboundChunkTimestampsResponsePacket.PACKET_ID) return ClientboundChunkTimestampsResponsePacket.read(buf);
if (id == ClientboundRegionTimestampsPacket.PACKET_ID) return ClientboundRegionTimestampsPacket.read(buf);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ClientPacketEncoder extends MessageToByteEncoder<Packet> {
public class ServerboundPacketEncoder extends MessageToByteEncoder<Packet> {
public static int getClientPacketId(Packet packet) {
if (packet instanceof ChunkTilePacket) return ChunkTilePacket.PACKET_ID;
if (packet instanceof CHandshake) return CHandshake.PACKET_ID;
if (packet instanceof CEncryptionResponse) return CEncryptionResponse.PACKET_ID;
if (packet instanceof CCatchupRequest) return CCatchupRequest.PACKET_ID;
if (packet instanceof CRegionCatchup) return CRegionCatchup.PACKET_ID;
if (packet instanceof ServerboundHandshakePacket) return ServerboundHandshakePacket.PACKET_ID;
if (packet instanceof ServerboundEncryptionResponsePacket) return ServerboundEncryptionResponsePacket.PACKET_ID;
if (packet instanceof ServerboundCatchupRequestPacket) return ServerboundCatchupRequestPacket.PACKET_ID;
if (packet instanceof ServerboundChunkTimestampsRequestPacket) return ServerboundChunkTimestampsRequestPacket.PACKET_ID;
throw new IllegalArgumentException("Unknown client packet class " + packet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(
new LengthFieldPrepender(4),
new LengthFieldBasedFrameDecoder(1 << 15, 0, 4, 0, 4),
new ServerPacketDecoder(),
new ClientPacketEncoder(),
new ClientboundPacketDecoder(),
new ServerboundPacketEncoder(),
new ClientHandler(SyncClient.this));
}
});
Expand All @@ -127,7 +127,7 @@ public void initChannel(SocketChannel ch) {
channelFuture.addListener(future -> {
if (future.isSuccess()) {
logger.info("[map-sync] Connected to " + address);
channelFuture.channel().writeAndFlush(new CHandshake(
channelFuture.channel().writeAndFlush(new ServerboundHandshakePacket(
getMod().getVersion(),
Minecraft.getInstance().getUser().getName(),
gameAddress,
Expand Down Expand Up @@ -239,7 +239,7 @@ public synchronized void shutDown() {
}
}

void setUpEncryption(ChannelHandlerContext ctx, SEncryptionRequest packet) {
void setUpEncryption(ChannelHandlerContext ctx, ClientboundEncryptionRequestPacket packet) {
try {
byte[] sharedSecret = new byte[16];
ThreadLocalRandom.current().nextBytes(sharedSecret);
Expand All @@ -260,7 +260,7 @@ void setUpEncryption(ChannelHandlerContext ctx, SEncryptionRequest packet) {
session.getGameProfile(), session.getAccessToken(), shaHex);

try {
ctx.channel().writeAndFlush(new CEncryptionResponse(
ctx.channel().writeAndFlush(new ServerboundEncryptionResponsePacket(
encrypt(packet.publicKey, sharedSecret),
encrypt(packet.publicKey, packet.verifyToken)));
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | BadPaddingException |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

import javax.annotation.Nonnull;

/**
* This packet is sent in two situations:
*
* 1. Clients are relaying chunk data to each other in real time.
*
* 2. You have requested synchronisation via {@link ServerboundCatchupRequestPacket}.
*/
public class ChunkTilePacket extends Packet {
public static final int PACKET_ID = 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@

import static gjum.minecraft.mapsync.common.Utils.readStringFromBuf;

public class SCatchup extends Packet {
/**
* You'll receive this in response to a sent {@link ServerboundChunkTimestampsRequestPacket},
* containing an elaboration of chunk timestamps of all the regions you listed.
* You should respond with a {@link ServerboundCatchupRequestPacket}.
*/
public class ClientboundChunkTimestampsResponsePacket extends Packet {
public static final int PACKET_ID = 5;

/**
* sorted by newest to oldest
*/
public final @Nonnull List<CatchupChunk> chunks;

public SCatchup(@Nonnull List<CatchupChunk> chunks) {
public ClientboundChunkTimestampsResponsePacket(@Nonnull List<CatchupChunk> chunks) {
this.chunks = chunks;
}

Expand All @@ -39,7 +44,7 @@ public static Packet read(ByteBuf buf) {
dimension, chunk_x, chunk_z, timestamp);
chunks.add(chunk);
}
return new SCatchup(chunks);
return new ClientboundChunkTimestampsResponsePacket(chunks);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

public class SEncryptionRequest extends Packet {
/**
* You will receive this in response to {@link ServerboundHandshakePacket}, and
* will expect a {@link ServerboundEncryptionResponsePacket} in response.
*/
public class ClientboundEncryptionRequestPacket extends Packet {
public static final int PACKET_ID = 2;

@Nonnull
public final PublicKey publicKey;
@Nonnull
public final byte[] verifyToken;

public SEncryptionRequest(@Nonnull PublicKey publicKey, @Nonnull byte[] verifyToken) {
public ClientboundEncryptionRequestPacket(@Nonnull PublicKey publicKey, @Nonnull byte[] verifyToken) {
this.publicKey = publicKey;
this.verifyToken = verifyToken;
}

public static Packet read(ByteBuf buf) {
return new SEncryptionRequest(
return new ClientboundEncryptionRequestPacket(
readKey(buf),
readByteArray(buf));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
import gjum.minecraft.mapsync.common.net.Packet;
import io.netty.buffer.ByteBuf;

public class SRegionTimestamps extends Packet {
/**
* This is the packet for the first-stage of the synchronisation process. It's
* sent immediately after you've been authenticated. You should respond with a
* {@link ServerboundChunkTimestampsRequestPacket}.
*/
public class ClientboundRegionTimestampsPacket extends Packet {
public static final int PACKET_ID = 7;

private final String dimension;

private final RegionTimestamp[] timestamps;

public SRegionTimestamps(String dimension, RegionTimestamp[] timestamps) {
public ClientboundRegionTimestampsPacket(String dimension, RegionTimestamp[] timestamps) {
this.dimension = dimension;
this.timestamps = timestamps;
}
Expand All @@ -39,7 +44,7 @@ public static Packet read(ByteBuf buf) {
timestamps[i] = new RegionTimestamp(regionX, regionZ, timestamp);
}

return new SRegionTimestamps(dimension, timestamps);
return new ClientboundRegionTimestampsPacket(dimension, timestamps);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

import static gjum.minecraft.mapsync.common.Utils.writeStringToBuf;

public class CCatchupRequest extends Packet {
/**
* This is the final stage in the synchronisation process, sent in response to
* a received {@link ClientboundChunkTimestampsResponsePacket}. Here you list
* what chunks you'd like to receive from the server, who'll then respond with
* a bunch of {@link ChunkTilePacket}.
*/
public class ServerboundCatchupRequestPacket extends Packet {
public static final int PACKET_ID = 6;

/**
Expand All @@ -22,7 +28,7 @@ public class CCatchupRequest extends Packet {
/**
* Chunks must all be in the same dimension
*/
public CCatchupRequest(@Nonnull List<CatchupChunk> chunks) {
public ServerboundCatchupRequestPacket(@Nonnull List<CatchupChunk> chunks) {
if (chunks.isEmpty()) throw new Error("Chunks list must not be empty");
ResourceKey<Level> dim = null;
for (CatchupChunk chunk : chunks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@

import java.util.List;

public class CRegionCatchup extends Packet {
/**
* You send this in response to a {@link ClientboundRegionTimestampsPacket},
* listing all the regions you'd like the server to elaborate on. You should
* expect a {@link ClientboundChunkTimestampsResponsePacket}.
*/
public class ServerboundChunkTimestampsRequestPacket extends Packet {
public static final int PACKET_ID = 8;

private final String dimension;
private final List<RegionPos> regions;

public CRegionCatchup(String dimension, List<RegionPos> regions) {
public ServerboundChunkTimestampsRequestPacket(String dimension, List<RegionPos> regions) {
this.dimension = dimension;
this.regions = regions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import gjum.minecraft.mapsync.common.net.Packet;
import io.netty.buffer.ByteBuf;

public class CEncryptionResponse extends Packet {
/**
* This is sent to the server in response to a {@link ClientboundEncryptionRequestPacket},
* after which, if the connection persists, you are considered authenticated
* with the server. You should then receive a {@link ClientboundRegionTimestampsPacket}.
*/
public class ServerboundEncryptionResponsePacket extends Packet {
public static final int PACKET_ID = 3;

/**
Expand All @@ -15,13 +20,13 @@ public class CEncryptionResponse extends Packet {
*/
public final byte[] verifyToken;

public CEncryptionResponse(byte[] sharedSecret, byte[] verifyToken) {
public ServerboundEncryptionResponsePacket(byte[] sharedSecret, byte[] verifyToken) {
this.sharedSecret = sharedSecret;
this.verifyToken = verifyToken;
}

public static Packet read(ByteBuf buf) {
return new CEncryptionResponse(readByteArray(buf), readByteArray(buf));
return new ServerboundEncryptionResponsePacket(readByteArray(buf), readByteArray(buf));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

import static gjum.minecraft.mapsync.common.Utils.readStringFromBuf;
import static gjum.minecraft.mapsync.common.Utils.writeStringToBuf;

public class CHandshake extends Packet {
/**
* This should be sent to the server <i>IMMEDIATELY</i> upon connection. If the
* server accepts the connection, you will receive a {@link ClientboundEncryptionRequestPacket}.
*/
public class ServerboundHandshakePacket extends Packet {
public static final int PACKET_ID = 1;

public final @NotNull String modVersion;
public final @NotNull String username;
public final @NotNull String gameAddress;
public final @NotNull String world;

public CHandshake(@NotNull String modVersion, @NotNull String username, @NotNull String gameAddress, @NotNull String world) {
public ServerboundHandshakePacket(@NotNull String modVersion, @NotNull String username, @NotNull String gameAddress, @NotNull String world) {
this.modVersion = modVersion;
this.username = username;
this.gameAddress = gameAddress;
Expand Down