Skip to content

Commit

Permalink
more work on networking
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGlitch76 committed Jun 2, 2024
1 parent cdf34b5 commit d525446
Show file tree
Hide file tree
Showing 33 changed files with 403 additions and 839 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@
* @see PacketByteBufs
*/
public interface PacketSender<C> {
/**
* Makes a packet for a channel.
*
* @param channelName the identifier of the channel
* @param buf the content of the packet
*/
Packet<?> createPacket(CustomPayload.Id<?> channelName, PacketByteBuf buf);

/**
* Makes a packet for a channel.
*
Expand All @@ -61,33 +53,6 @@ public interface PacketSender<C> {
*/
void sendPacket(Packet<?> packet, @Nullable PacketSendListener listener);

/**
* Sends a packet to a channel.
*
* @param channel the identifier of the channel
* @param buf the content of the packet
*/
default void sendPacket(CustomPayload.Id<?> channel, PacketByteBuf buf) {
Objects.requireNonNull(channel, "Channel cannot be null");
Objects.requireNonNull(buf, "Payload cannot be null");

this.sendPacket(this.createPacket(channel, buf));
}

/**
* Sends a packet to a channel.
*
* @param channel the identifier of the channel
* @param buf the content of the packet
* @param listener an optional listener containing callbacks to execute after the packet is sent, may be {@code null}
*/
default void sendPacket(CustomPayload.Id<?> channel, PacketByteBuf buf, @Nullable PacketSendListener listener) {
Objects.requireNonNull(channel, "Channel cannot be null");
Objects.requireNonNull(buf, "Payload cannot be null");

this.sendPacket(this.createPacket(channel, buf), listener);
}

/**
* Sends a packet to a channel.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
* Copyright 2023 The Quilt Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.networking.api;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.payload.CustomPayload;

import org.jetbrains.annotations.ApiStatus;
import org.quiltmc.qsl.networking.impl.PayloadTypeRegistryImpl;

/**
* A registry for payload types.
*/
@ApiStatus.NonExtendable
public interface PayloadTypeRegistry<B extends PacketByteBuf> {
/**
* Registers a custom payload type.
*
* <p>This must be done on both the sending and receiving side, usually during mod initialization
* and <strong>before registering a packet handler</strong>.
*
* @param id the id of the payload type
* @param codec the codec for the payload type
* @param <T> the payload type
* @return the registered payload type
*/
<T extends CustomPayload> CustomPayload.Type<? super B, T> register(CustomPayload.Id<T> id, PacketCodec<? super B, T> codec);

/**
* @return the {@link PayloadTypeRegistry} instance for the client to server configuration channel.
*/
static PayloadTypeRegistry<PacketByteBuf> configurationC2S() {
return PayloadTypeRegistryImpl.CONFIGURATION_C2S;
}

/**
* @return the {@link PayloadTypeRegistry} instance for the server to client configuration channel.
*/
static PayloadTypeRegistry<PacketByteBuf> configurationS2C() {
return PayloadTypeRegistryImpl.CONFIGURATION_S2C;
}

/**
* @return the {@link PayloadTypeRegistry} instance for the client to server play channel.
*/
static PayloadTypeRegistry<RegistryByteBuf> playC2S() {
return PayloadTypeRegistryImpl.PLAY_C2S;
}

/**
* @return the {@link PayloadTypeRegistry} instance for the server to client play channel.
*/
static PayloadTypeRegistry<RegistryByteBuf> playS2C() {
return PayloadTypeRegistryImpl.PLAY_S2C;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import net.minecraft.util.Identifier;

import org.quiltmc.qsl.networking.api.client.ClientConfigurationNetworking;
import org.quiltmc.qsl.networking.impl.payload.PacketByteBufPayload;
import org.quiltmc.qsl.networking.impl.server.ServerNetworkingImpl;
import org.quiltmc.qsl.networking.mixin.accessor.AbstractServerPacketHandlerAccessor;

Expand Down Expand Up @@ -66,25 +65,6 @@ public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPay
return ServerNetworkingImpl.CONFIGURATION.registerGlobalReceiver(channelName, channelHandler);
}

/**
* Registers a handler to a channel.
* A global receiver is registered to all connections, in the present and future.
* <p>
* If a handler is already registered to the {@code channel}, this method will return {@code false}, and no change will be made.
* Use {@link #unregisterReceiver(ServerConfigurationNetworkHandler, CustomPayload.Id)} to unregister the existing handler.
*
* @param channelName the identifier of the channel
* @param channelHandler the handler
* @return {@code false} if a handler is already registered to the channel, otherwise {@code true}
* @see ServerConfigurationNetworking#unregisterGlobalReceiver(CustomPayload.Id)
* @see ServerConfigurationNetworking#registerReceiver(ServerConfigurationNetworkHandler, Identifier, ChannelReceiver)
* @deprecated use {@link ServerConfigurationNetworking#registerGlobalReceiver(CustomPayload.Id, CustomChannelReceiver)}
*/
@Deprecated
public static boolean registerGlobalReceiver(Identifier channelName, ChannelReceiver channelHandler) {
return ServerNetworkingImpl.CONFIGURATION.registerGlobalReceiver(new CustomPayload.Id<>(channelName), channelHandler);
}

/**
* Removes the handler of a channel.
* A global receiver is registered to all connections, in the present and future.
Expand Down Expand Up @@ -134,31 +114,6 @@ public static <T extends CustomPayload> boolean registerReceiver(ServerConfigura
return ServerNetworkingImpl.getAddon(networkHandler).registerChannel(channelName, channelHandler);
}

/**
* Registers a handler to a channel.
* This method differs from {@link ServerConfigurationNetworking#registerGlobalReceiver(Identifier, ChannelReceiver)} since
* the channel handler will only be applied to the client represented by the {@link ServerConfigurationNetworkHandler}.
* <p>
* For example, if you only register a receiver using this method when a {@linkplain ServerLoginNetworking#registerGlobalReceiver(CustomPayload.Id, ServerLoginNetworking.QueryResponseReceiver)}
* login response has been received, you should use {@link ServerConfigurationConnectionEvents#INIT} to register the channel handler.
* <p>
* If a handler is already registered to the {@code channelName}, this method will return {@code false}, and no change will be made.
* Use {@link #unregisterReceiver(ServerConfigurationNetworkHandler, CustomPayload.Id)} to unregister the existing handler.
*
* @param networkHandler the handler
* @param channelName the identifier of the channel
* @param channelHandler the handler
* @return {@code false} if a handler is already registered to the channel name, otherwise {@code true}
* @see ServerConfigurationConnectionEvents#INIT
* @deprecated use {@link ServerConfigurationNetworking#registerReceiver(ServerConfigurationNetworkHandler, CustomPayload.Id, CustomChannelReceiver)}
*/
@Deprecated
public static boolean registerReceiver(ServerConfigurationNetworkHandler networkHandler, Identifier channelName, ChannelReceiver channelHandler) {
Objects.requireNonNull(networkHandler, "Network handler cannot be null");

return ServerNetworkingImpl.getAddon(networkHandler).registerChannel(new CustomPayload.Id<>(channelName), channelHandler);
}

/**
* Removes the handler of a channel.
* <p>
Expand Down Expand Up @@ -212,20 +167,6 @@ public static boolean canSend(ServerConfigurationNetworkHandler handler, CustomP
return ServerNetworkingImpl.getAddon(handler).getSendableChannels().contains(channelName);
}

/**
* Creates a packet which may be sent to a connected client.
*
* @param channelName the channel name
* @param buf the packet byte data which represents the payload of the packet
* @return a new packet
*/
@Contract(value = "_, _ -> new", pure = true)
public static Packet<ClientCommonPacketListener> createS2CPacket(@NotNull CustomPayload.Id<?> channelName, @NotNull PacketByteBuf buf) {
Objects.requireNonNull(channelName, "Channel cannot be null");
Objects.requireNonNull(buf, "Buf cannot be null");

return ServerNetworkingImpl.createS2CPacket(channelName, buf);
}

/**
* Creates a packet from a payload which may be sent to a connected client.
Expand Down Expand Up @@ -256,26 +197,23 @@ public static PacketSender<CustomPayload> getSender(ServerConfigurationNetworkHa
* Sends a packet to a client.
*
* @param networkHandler the handler to send the packet to
* @param channelName the channel of the packet
* @param buf the payload of the packet
* @param payload to be sent
*/
public static void send(ServerConfigurationNetworkHandler networkHandler, CustomPayload.Id<?> channelName, PacketByteBuf buf) {
public static void send(ServerConfigurationNetworkHandler networkHandler, CustomPayload payload) {
Objects.requireNonNull(networkHandler, "Server configuration handler cannot be null");
Objects.requireNonNull(channelName, "Channel name cannot be null");
Objects.requireNonNull(buf, "Packet byte data cannot be null");
Objects.requireNonNull(payload, "Payload cannot be null");

networkHandler.send(createS2CPacket(channelName, buf));
networkHandler.send(createS2CPacket(payload));
}

// Helper methods

// TODO: Possible future CHASM extension method.

/**
* Returns the <i>Minecraft</i> Server of a server configuration packet handler.
*
* @param handler the server configuration packet handler
*/
// TODO: Possible future CHASM extension method.
public static MinecraftServer getServer(ServerConfigurationNetworkHandler handler) {
Objects.requireNonNull(handler, "Network handler cannot be null");

Expand Down Expand Up @@ -310,40 +248,4 @@ public interface CustomChannelReceiver<T extends CustomPayload> {
*/
void receive(MinecraftServer server, ServerConfigurationNetworkHandler handler, T payload, PacketSender<CustomPayload> responseSender);
}

/**
* This functional interface should only be used when sending a raw {@link PacketByteBuf} is necessary.
* <p>
* @deprecated use {@link CustomChannelReceiver}
*/
@Deprecated
@FunctionalInterface
public interface ChannelReceiver extends CustomChannelReceiver<PacketByteBufPayload> {
default void receive(MinecraftServer server, ServerConfigurationNetworkHandler handler, PacketByteBufPayload payload, PacketSender<CustomPayload> responseSender) {
this.receive(server, handler, payload.data(), responseSender);
}

/**
* Receives an incoming packet.
* <p>
* This method is executed on {@linkplain io.netty.channel.EventLoop netty's event loops}.
* Modification to the game should be {@linkplain net.minecraft.util.thread.ThreadExecutor#submit(Runnable) scheduled} using the provided Minecraft server instance.
* <pre>{@code
* ServerConfigurationNetworking.registerReceiver(new Identifier("mymod", "boom"), (server, handler, data, responseSender) -> {
* boolean fire = data.readBoolean();
*
* // All operations on the server or world must be executed on the server thread
* server.execute(() -> {
*
* });
* });
* }</pre>
*
* @param server the server
* @param handler the network handler that received this packet, representing the client who sent the packet
* @param buf the payload of the packet
* @param responseSender the packet sender
*/
void receive(MinecraftServer server, ServerConfigurationNetworkHandler handler, PacketByteBuf buf, PacketSender<CustomPayload> responseSender);
}
}
Loading

0 comments on commit d525446

Please sign in to comment.