Search in sources :

Example 21 with ChannelBuf

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

the class SpongePacketChannel method sendNormalPacketTo.

private <P extends Packet> void sendNormalPacketTo(final EngineConnection connection, final P packet, final CompletableFuture<Void> future) {
    final SpongePacketBinding<P> binding = (SpongePacketBinding) this.requireBinding(packet.getClass());
    final boolean isLoginPhase = ConnectionUtil.isLoginPhase(connection);
    final EngineConnectionSide<?> side = connection.side();
    final ChannelBuf payload = this.manager().getBufferAllocator().buffer();
    final Supplier<net.minecraft.network.protocol.Packet<?>> mcPacketSupplier;
    if (isLoginPhase) {
        if (side == EngineConnectionSide.CLIENT) {
            payload.writeString(this.key().formatted());
            payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_NORMAL, binding.opcode()));
            mcPacketSupplier = () -> PacketUtil.createLoginPayloadResponse(payload, Constants.Channels.LOGIN_PAYLOAD_TRANSACTION_ID);
        } else {
            payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_NORMAL, binding.opcode()));
            final int transactionId = ConnectionUtil.getTransactionStore(connection).nextId();
            mcPacketSupplier = () -> PacketUtil.createLoginPayloadRequest(this.key(), payload, transactionId);
        }
    } else {
        payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_NORMAL, binding.opcode()));
        mcPacketSupplier = () -> PacketUtil.createPlayPayload(this.key(), payload, side);
    }
    try {
        this.encodePayload(payload, packet);
    } catch (final Throwable ex) {
        future.completeExceptionally(ex);
        return;
    }
    final net.minecraft.network.protocol.Packet<?> mcPacket = mcPacketSupplier.get();
    PacketSender.sendTo(connection, mcPacket, future);
}
Also used : RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 22 with ChannelBuf

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

the class SpongePacketChannel method sendRequestPacketTo.

private <P extends RequestPacket<R>, R extends Packet> void sendRequestPacketTo(final EngineConnection connection, final P packet, final CompletableFuture<?> future, @Nullable final Consumer<R> response, @Nullable final Runnable sendSuccess) {
    final SpongeTransactionalPacketBinding<P, R> binding = (SpongeTransactionalPacketBinding) this.requireBinding(packet.getClass());
    final TransactionStore transactionStore = ConnectionUtil.getTransactionStore(connection);
    final int transactionId = transactionStore.nextId();
    final boolean isLoginPhase = ConnectionUtil.isLoginPhase(connection);
    final EngineConnectionSide<?> side = connection.side();
    final ChannelBuf payload = this.manager().getBufferAllocator().buffer();
    final Supplier<net.minecraft.network.protocol.Packet<?>> mcPacketSupplier;
    if (isLoginPhase) {
        if (side == EngineConnectionSide.CLIENT) {
            payload.writeString(this.key().formatted());
            payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_REQUEST, transactionId));
            payload.writeVarInt(binding.opcode());
            mcPacketSupplier = () -> PacketUtil.createLoginPayloadResponse(payload, Constants.Channels.LOGIN_PAYLOAD_TRANSACTION_ID);
        } else {
            payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_REQUEST, binding.opcode()));
            mcPacketSupplier = () -> PacketUtil.createLoginPayloadRequest(this.key(), payload, transactionId);
        }
    } else {
        payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_REQUEST, transactionId));
        payload.writeVarInt(binding.opcode());
        mcPacketSupplier = () -> PacketUtil.createPlayPayload(this.key(), payload, side);
    }
    try {
        this.encodePayload(payload, packet);
    } catch (final Throwable ex) {
        this.handleException(connection, ex, future);
        return;
    }
    if (response != null) {
        final TransactionData<P, R> transactionData = new TransactionData<>(packet, binding, response, future);
        transactionStore.put(transactionId, this, transactionData);
    }
    final net.minecraft.network.protocol.Packet<?> mcPacket = mcPacketSupplier.get();
    PacketSender.sendTo(connection, mcPacket, sendFuture -> {
        if (!sendFuture.isSuccess()) {
            this.handleException(connection, sendFuture.cause(), future);
            // and remove it from the store
            if (response != null) {
                transactionStore.remove(transactionId);
            }
        } else if (sendSuccess != null) {
            sendSuccess.run();
        }
    });
}
Also used : RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) TransactionStore(org.spongepowered.common.network.channel.TransactionStore)

Example 23 with ChannelBuf

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

the class SpongePacketChannel method handleTransactionResponse.

private <P extends RequestPacket<R>, R extends Packet> void handleTransactionResponse(final EngineConnection connection, final TransactionData<P, R> transactionData, final TransactionResult result, final int dynamicOpcode) {
    if (result.isSuccess()) {
        final ChannelBuf payload = result.getPayload();
        SpongePacketBinding<R> responseBinding = null;
        final Supplier<R> packetSupplier;
        if (dynamicOpcode != SpongePacketChannel.NO_DYNAMIC_OPCODE) {
            responseBinding = (SpongePacketBinding<R>) this.requireBinding(dynamicOpcode);
            packetSupplier = responseBinding.getPacketConstructor();
        } else if (transactionData.binding instanceof SpongeFixedTransactionalPacketBinding) {
            packetSupplier = (Supplier<R>) ((SpongeFixedTransactionalPacketBinding<RequestPacket<Packet>, Packet>) transactionData.binding).getResponsePacketConstructor();
        } else {
            throw new ChannelException("A fixed response was send but no fixed response was bound to the request: " + transactionData.request.getClass());
        }
        final R packet;
        try {
            packet = this.decodePayload(packetSupplier, payload);
        } catch (final Throwable ex) {
            this.handleException(connection, new ChannelIOException("Failed to decode packet", ex), transactionData.future);
            return;
        }
        if (responseBinding != null) {
            this.handle(connection, (SpongeHandlerPacketBinding<Packet>) responseBinding, packet);
        } else {
            this.handleResponse(connection, transactionData.binding, transactionData.request, packet);
        }
        if (transactionData.success != null) {
            transactionData.success.accept(packet);
        }
    } else {
        this.handleException(connection, result.getCause(), transactionData.future);
        this.handleResponseFailure(connection, transactionData.binding, transactionData.request, result.getCause());
    }
}
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) Supplier(java.util.function.Supplier) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) ChannelException(org.spongepowered.api.network.channel.ChannelException)

Example 24 with ChannelBuf

use of org.spongepowered.api.network.channel.ChannelBuf 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)

Aggregations

ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)24 Packet (org.spongepowered.api.network.channel.packet.Packet)6 RequestPacket (org.spongepowered.api.network.channel.packet.RequestPacket)6 ChannelIOException (org.spongepowered.api.network.channel.ChannelIOException)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 ResourceKey (org.spongepowered.api.ResourceKey)4 ChannelException (org.spongepowered.api.network.channel.ChannelException)4 Consumer (java.util.function.Consumer)2 EngineConnection (org.spongepowered.api.network.EngineConnection)2 RawHandshakeDataRequestResponse (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse)2 TransactionStore (org.spongepowered.common.network.channel.TransactionStore)2 Map (java.util.Map)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Supplier (java.util.function.Supplier)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 Packet (net.minecraft.network.protocol.Packet)1 EngineConnectionSide (org.spongepowered.api.network.EngineConnectionSide)1 NoResponseException (org.spongepowered.api.network.channel.NoResponseException)1 RequestPacketHandler (org.spongepowered.api.network.channel.packet.RequestPacketHandler)1