Search in sources :

Example 6 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project Sponge by SpongePowered.

the class ClientHandshakePacketListenerImplMixin_Vanilla method vanilla$handleRequestPayload.

// @formatter:on
@Inject(method = "handleCustomQuery", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/Connection;send(Lnet/minecraft/network/protocol/Packet;)V"), cancellable = true)
private void vanilla$handleRequestPayload(final ClientboundCustomQueryPacket packet, final CallbackInfo ci) {
    ci.cancel();
    final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
    this.minecraft.execute(() -> {
        final EngineConnection connection = (EngineConnection) this;
        if (!channelRegistry.handleLoginRequestPayload(connection, packet)) {
            PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, packet.getTransactionId()));
        }
    });
}
Also used : SpongeChannelManager(org.spongepowered.common.network.channel.SpongeChannelManager) EngineConnection(org.spongepowered.api.network.EngineConnection) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 7 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project SpongeCommon by SpongePowered.

the class SpongeBasicPacketChannel method handleLoginRequestPayload.

@Override
protected void handleLoginRequestPayload(final EngineConnection connection, final int transactionId, final ChannelBuf payload) {
    // Is currently always executed on the client,
    // the server always expects a response
    final int opcode = this.readOpcode(payload);
    final SpongePacketBinding<Packet> binding = this.requireBinding(opcode);
    final Packet packet = this.decodePayload(() -> binding.getPacketConstructor().get(), payload);
    // A normal packet binding
    if (binding instanceof SpongeHandlerPacketBinding) {
        final SpongeHandlerPacketBinding<Packet> handlerBinding = (SpongeHandlerPacketBinding<Packet>) binding;
        this.handle(connection, handlerBinding, packet);
        // The server always expects a response
        PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
    } else {
        // A transactional packet binding
        final SpongeTransactionalPacketBinding<RequestPacket<Packet>, Packet> transactionalBinding = (SpongeTransactionalPacketBinding) binding;
        final RequestPacketHandler<Packet, Packet, EngineConnection> handler = (RequestPacketHandler<Packet, Packet, EngineConnection>) transactionalBinding.getRequestHandler(connection);
        boolean success = false;
        if (handler != null) {
            final SpongeRequestPacketResponse<Packet> response = new SpongeRequestPacketResponse<Packet>() {

                @Override
                protected void fail0(final ChannelException exception) {
                    final net.minecraft.network.protocol.Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(null, transactionId);
                    PacketSender.sendTo(connection, mcPacket);
                }

                @Override
                protected void success0(final Packet response) {
                    try {
                        final ChannelBuf responsePayload = SpongeBasicPacketChannel.this.encodeLoginPayload(transactionalBinding.opcode(), response);
                        final net.minecraft.network.protocol.Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(responsePayload, transactionId);
                        PacketSender.sendTo(connection, mcPacket);
                    } catch (final Throwable ex) {
                        SpongeBasicPacketChannel.this.handleException(connection, new ChannelIOException("Failed to encode response packet", ex), null);
                    }
                }
            };
            try {
                handler.handleRequest(packet, connection, response);
                success = true;
            } catch (final Throwable ex) {
                this.handleException(connection, new ChannelIOException("Failed to handle request packet", ex), null);
            }
        }
        if (!success) {
            final net.minecraft.network.protocol.Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(null, transactionId);
            PacketSender.sendTo(connection, mcPacket);
        }
    }
}
Also used : RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) ChannelIOException(org.spongepowered.api.network.channel.ChannelIOException) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) RequestPacketHandler(org.spongepowered.api.network.channel.packet.RequestPacketHandler) EngineConnection(org.spongepowered.api.network.EngineConnection) ChannelException(org.spongepowered.api.network.channel.ChannelException)

Example 8 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project SpongeCommon by SpongePowered.

the class SpongeRawLoginDataChannel method sendTo.

@Override
public CompletableFuture<ChannelBuf> sendTo(final EngineConnection connection, final Consumer<ChannelBuf> payload) {
    ConnectionUtil.checkHandshakePhase(connection);
    final CompletableFuture<ChannelBuf> future = new CompletableFuture<>();
    final ChannelBuf buf;
    try {
        buf = this.parent.encodePayload(payload);
    } catch (final Throwable t) {
        this.parent.handleException(connection, t, future);
        return future;
    }
    final TransactionStore transactionStore = ConnectionUtil.getTransactionStore(connection);
    final int transactionId = transactionStore.nextId();
    final Consumer<TransactionResult> resultConsumer = result -> {
        if (result.isSuccess()) {
            future.complete(result.getPayload());
        } else {
            this.parent.handleException(connection, result.getCause(), future);
        }
    };
    final Packet<?> mcPacket = PacketUtil.createLoginPayloadRequest(this.parent.key(), buf, transactionId);
    PacketSender.sendTo(connection, mcPacket, sendFuture -> {
        if (sendFuture.isSuccess()) {
            transactionStore.put(transactionId, this.parent, resultConsumer);
        } else {
            // The packet already failed before it could reach the client
            future.completeExceptionally(sendFuture.cause());
        }
    });
    return future;
}
Also used : PacketUtil(org.spongepowered.common.network.channel.PacketUtil) SpongeChannel(org.spongepowered.common.network.channel.SpongeChannel) TransactionStore(org.spongepowered.common.network.channel.TransactionStore) EngineConnection(org.spongepowered.api.network.EngineConnection) ChannelException(org.spongepowered.api.network.channel.ChannelException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EngineConnectionSide(org.spongepowered.api.network.EngineConnectionSide) CompletableFuture(java.util.concurrent.CompletableFuture) PacketSender(org.spongepowered.common.network.channel.PacketSender) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) Objects(java.util.Objects) Consumer(java.util.function.Consumer) RawDataChannel(org.spongepowered.api.network.channel.raw.RawDataChannel) ConnectionUtil(org.spongepowered.common.network.channel.ConnectionUtil) Map(java.util.Map) Packet(net.minecraft.network.protocol.Packet) RawHandshakeDataRequestResponse(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) RawHandshakeDataRequestHandler(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestHandler) RawHandshakeDataChannel(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataChannel) TransactionStore(org.spongepowered.common.network.channel.TransactionStore) CompletableFuture(java.util.concurrent.CompletableFuture) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 9 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin_Vanilla method vanilla$onHandleCustomPayload.

@Inject(method = "handleCustomPayload", at = @At(value = "HEAD"))
private void vanilla$onHandleCustomPayload(final ServerboundCustomPayloadPacket packet, final CallbackInfo ci) {
    // For some reason, "ServerboundCustomPayloadPacket" is released in the processPacket
    // method of its class, only applicable to this packet, so just retain here.
    ((ServerboundCustomPayloadPacketAccessor) packet).accessor$data().retain();
    final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
    this.server.execute(() -> channelRegistry.handlePlayPayload((EngineConnection) this, packet));
}
Also used : SpongeChannelManager(org.spongepowered.common.network.channel.SpongeChannelManager) EngineConnection(org.spongepowered.api.network.EngineConnection) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 10 with EngineConnection

use of org.spongepowered.api.network.EngineConnection in project SpongeCommon by SpongePowered.

the class ServerLoginPacketListenerImplMixin_Vanilla method onResponsePayload.

@Inject(method = "handleCustomQueryPacket", at = @At("HEAD"), cancellable = true)
private void onResponsePayload(final ServerboundCustomQueryPacket packet, final CallbackInfo ci) {
    ci.cancel();
    final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
    this.server.execute(() -> channelRegistry.handleLoginResponsePayload((EngineConnection) this, packet));
}
Also used : SpongeChannelManager(org.spongepowered.common.network.channel.SpongeChannelManager) EngineConnection(org.spongepowered.api.network.EngineConnection) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

EngineConnection (org.spongepowered.api.network.EngineConnection)14 ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)6 Inject (org.spongepowered.asm.mixin.injection.Inject)6 SpongeChannelManager (org.spongepowered.common.network.channel.SpongeChannelManager)6 ChannelException (org.spongepowered.api.network.channel.ChannelException)4 RawDataChannel (org.spongepowered.api.network.channel.raw.RawDataChannel)4 RawHandshakeDataChannel (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataChannel)4 InetAddresses (com.google.common.net.InetAddresses)2 GameProfile (com.mojang.authlib.GameProfile)2 Property (com.mojang.authlib.properties.Property)2 ByteBuf (io.netty.buffer.ByteBuf)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 StandardCharsets (java.nio.charset.StandardCharsets)2 InvalidKeyException (java.security.InvalidKeyException)2 MessageDigest (java.security.MessageDigest)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Map (java.util.Map)2 Objects (java.util.Objects)2 CompletableFuture (java.util.concurrent.CompletableFuture)2