Search in sources :

Example 1 with ChannelException

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

the class SpongePacketChannel method handleRequestPacket.

private <C extends EngineConnection> void handleRequestPacket(final C connection, final int opcode, final int transactionId, final ChannelBuf payload) {
    final SpongePacketBinding<Packet> binding = this.requireBinding(opcode);
    final Packet request;
    try {
        request = this.decodePayload(binding.getPacketConstructor(), payload);
    } catch (final Throwable ex) {
        this.sendResponsePacketTo(connection, null, null, transactionId);
        this.handleException(connection, new ChannelIOException("Failed to decode request packet", ex), null);
        return;
    }
    boolean success = false;
    Throwable responseFailure = null;
    // TODO: Send cause of failure somehow?
    if (binding instanceof SpongeTransactionalPacketBinding) {
        final RequestPacketHandler<RequestPacket<Packet>, Packet, C> handler = ((SpongeTransactionalPacketBinding) binding).getRequestHandler(connection);
        if (handler != null) {
            final SpongeRequestPacketResponse<Packet> requestPacketResponse = new SpongeRequestPacketResponse<Packet>() {

                @Override
                protected void fail0(final ChannelException exception) {
                    // TODO: Use response failure?
                    SpongePacketChannel.this.sendResponsePacketTo(connection, null, null, transactionId);
                }

                @Override
                protected void success0(final Packet response) {
                    SpongePacketChannel.this.sendResponsePacketTo(connection, (SpongeTransactionalPacketBinding) binding, response, transactionId);
                }
            };
            try {
                handler.handleRequest((RequestPacket) request, connection, requestPacketResponse);
                success = true;
            } catch (final Throwable ex) {
                this.handleException(connection, new ChannelException("Failed to handle request packet", ex), null);
                responseFailure = ex;
            }
        }
    }
    if (!success) {
        // TODO: Use response failure?
        this.sendResponsePacketTo(connection, null, null, transactionId);
    }
}
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) ChannelException(org.spongepowered.api.network.channel.ChannelException)

Example 2 with ChannelException

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

the class SpongeRawLoginDataChannel method handleRequestPayload.

<C extends EngineConnection> void handleRequestPayload(final C connection, final ChannelBuf payload, final int transactionId) {
    final RawHandshakeDataRequestHandler<? super C> handler = this.getRequestHandler(connection);
    final RawHandshakeDataRequestResponse response = new RawHandshakeDataRequestResponse() {

        private boolean completed;

        private void checkCompleted() {
            if (this.completed) {
                throw new ChannelException("The request response was already completed.");
            }
            this.completed = true;
        }

        @Override
        public void fail(final ChannelException exception) {
            Objects.requireNonNull(exception, "exception");
            this.checkCompleted();
            PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
        }

        @Override
        public void success(final Consumer<ChannelBuf> response) {
            Objects.requireNonNull(response, "response");
            this.checkCompleted();
            final ChannelBuf payload;
            try {
                payload = SpongeRawLoginDataChannel.this.parent.encodePayload(response);
            } catch (final Throwable t) {
                SpongeRawLoginDataChannel.this.parent.handleException(connection, new ChannelException("Failed to encode login data response", t), null);
                PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
                return;
            }
            PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(payload, transactionId));
        }
    };
    boolean success = false;
    if (handler != null) {
        try {
            handler.handleRequest(payload, connection, response);
            success = true;
        } catch (final Throwable t) {
            this.parent.handleException(connection, new ChannelException("Failed to handle login data request", t), null);
        }
    }
    if (!success) {
        PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
    }
}
Also used : Consumer(java.util.function.Consumer) RawHandshakeDataRequestResponse(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) ChannelException(org.spongepowered.api.network.channel.ChannelException)

Example 3 with ChannelException

use of org.spongepowered.api.network.channel.ChannelException 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 4 with ChannelException

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

Aggregations

ChannelException (org.spongepowered.api.network.channel.ChannelException)4 ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)3 ChannelIOException (org.spongepowered.api.network.channel.ChannelIOException)3 Packet (org.spongepowered.api.network.channel.packet.Packet)3 RequestPacket (org.spongepowered.api.network.channel.packet.RequestPacket)3 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 EngineConnection (org.spongepowered.api.network.EngineConnection)1 RequestPacketHandler (org.spongepowered.api.network.channel.packet.RequestPacketHandler)1 RawHandshakeDataRequestResponse (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse)1