Search in sources :

Example 1 with ChannelIOException

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

the class SpongeBasicPacketChannel method handleTransactionResponse.

private <P extends RequestPacket<R>, R extends Packet> void handleTransactionResponse(final EngineConnection connection, final TransactionData<P, R> transactionData, final TransactionResult result) {
    final SpongeTransactionalPacketBinding<P, R> binding = transactionData.binding;
    final P request = transactionData.request;
    if (result.isSuccess()) {
        ChannelBuf responsePayload = result.getPayload();
        // FML wrapper channel key
        responsePayload.readString();
        final int actualLength = responsePayload.readVarInt();
        responsePayload = responsePayload.readSlice(actualLength);
        try {
            final int opcode = this.readOpcode(responsePayload);
            final Supplier<R> responseConstructor;
            if (binding instanceof SpongeFixedTransactionalPacketBinding) {
                // The response packet uses the same binding and opcode,
                // so the returned opcode can be ignored.
                responseConstructor = ((SpongeFixedTransactionalPacketBinding<P, R>) binding).getResponsePacketConstructor();
            } else {
                // The response type was registered separately to a different
                // opcode, so lookup using the opcode.
                final SpongeHandlerPacketBinding<R> responseBinding = (SpongeHandlerPacketBinding<R>) this.byOpcode.get(opcode);
                if (responseBinding == null) {
                    throw new ChannelIOException("Unknown packet opcode: " + opcode);
                }
                responseConstructor = responseBinding.getPacketConstructor();
            }
            final R responsePacket = this.decodePayload(responseConstructor, responsePayload);
            if (transactionData.success != null) {
                transactionData.success.accept(responsePacket);
            }
            this.handleResponse(connection, binding, request, responsePacket);
        } catch (final Throwable ex) {
            this.handleException(connection, ex, transactionData.future);
        }
    } else {
        this.handleException(connection, result.getCause(), transactionData.future);
        this.handleResponseFailure(connection, binding, request, result.getCause());
    }
}
Also used : ChannelIOException(org.spongepowered.api.network.channel.ChannelIOException) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 2 with ChannelIOException

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

the class SpongePacketChannel method handlePlayPayload.

@Override
protected void handlePlayPayload(final EngineConnection connection, final ChannelBuf payload) {
    final long typeAndValue = payload.readVarLong();
    final int type = SpongePacketChannel.extractType(typeAndValue);
    final int value = SpongePacketChannel.extractValue(typeAndValue);
    if (type == SpongePacketChannel.TYPE_NORMAL) {
        this.handleNormalPacket(connection, value, payload);
    } else if (type == SpongePacketChannel.TYPE_REQUEST) {
        final int opcode = payload.readVarInt();
        this.handleRequestPacket(connection, opcode, value, payload);
    } else if (type == SpongePacketChannel.TYPE_RESPONSE) {
        this.handleResponsePacket(connection, value, payload, SpongePacketChannel.NO_DYNAMIC_OPCODE);
    } else if (type == SpongePacketChannel.TYPE_NO_RESPONSE) {
        this.handleResponsePacket(connection, value, null, SpongePacketChannel.NO_DYNAMIC_OPCODE);
    } else if (type == SpongePacketChannel.TYPE_DYNAMIC_RESPONSE) {
        final int opcode = payload.readVarInt();
        this.handleResponsePacket(connection, value, payload, opcode);
    } else {
        this.handleException(connection, new ChannelIOException("Unknown packet type: " + type), null);
    }
}
Also used : ChannelIOException(org.spongepowered.api.network.channel.ChannelIOException)

Example 3 with ChannelIOException

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

the class SpongePacketChannel method sendResponsePacketTo.

private <P extends RequestPacket<R>, R extends Packet> void sendResponsePacketTo(final EngineConnection connection, @Nullable final SpongeTransactionalPacketBinding<P, R> requestBinding, @Nullable final R packet, final int transactionId) {
    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 (packet == null || requestBinding instanceof SpongeFixedTransactionalPacketBinding) {
        final int type = packet == null ? SpongePacketChannel.TYPE_NO_RESPONSE : SpongePacketChannel.TYPE_RESPONSE;
        if (isLoginPhase) {
            if (side == EngineConnectionSide.CLIENT) {
                payload.writeVarLong(SpongePacketChannel.packTypeAndValue(type, 0));
                mcPacketSupplier = () -> PacketUtil.createLoginPayloadResponse(payload, transactionId);
            } else {
                payload.writeVarLong(SpongePacketChannel.packTypeAndValue(type, transactionId));
                mcPacketSupplier = () -> PacketUtil.createLoginPayloadRequest(this.key(), payload, Constants.Channels.LOGIN_PAYLOAD_TRANSACTION_ID);
            }
        } else {
            payload.writeVarLong(SpongePacketChannel.packTypeAndValue(type, transactionId));
            mcPacketSupplier = () -> PacketUtil.createPlayPayload(this.key(), payload, side);
        }
    } else {
        // Dynamic opcode
        final int opcode = this.requireBinding(packet.getClass()).opcode();
        if (isLoginPhase) {
            if (side == EngineConnectionSide.CLIENT) {
                payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_DYNAMIC_RESPONSE, opcode));
                mcPacketSupplier = () -> PacketUtil.createLoginPayloadResponse(payload, transactionId);
            } else {
                payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_DYNAMIC_RESPONSE, transactionId));
                payload.writeVarInt(opcode);
                mcPacketSupplier = () -> PacketUtil.createLoginPayloadRequest(this.key(), payload, Constants.Channels.LOGIN_PAYLOAD_TRANSACTION_ID);
            }
        } else {
            payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_DYNAMIC_RESPONSE, transactionId));
            payload.writeVarInt(opcode);
            mcPacketSupplier = () -> PacketUtil.createPlayPayload(this.key(), payload, side);
        }
    }
    try {
        this.encodePayload(payload, packet);
    } catch (final Throwable ex) {
        this.handleException(connection, new ChannelIOException("Failed to encode request response", ex), null);
        return;
    }
    final net.minecraft.network.protocol.Packet<?> mcPacket = mcPacketSupplier.get();
    PacketSender.sendTo(connection, mcPacket);
}
Also used : RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) ChannelIOException(org.spongepowered.api.network.channel.ChannelIOException) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 4 with ChannelIOException

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

the class SpongePacketChannel method handleTransactionResponse.

@Override
protected void handleTransactionResponse(final EngineConnection connection, final Object stored, final TransactionResult result) {
    if (result.isSuccess()) {
        final ChannelBuf payload = result.getPayload();
        final long typeAndValue = payload.readVarLong();
        final int type = SpongePacketChannel.extractType(typeAndValue);
        final int value = SpongePacketChannel.extractValue(typeAndValue);
        if (type == SpongePacketChannel.TYPE_RESPONSE || type == SpongePacketChannel.TYPE_NO_RESPONSE || type == SpongePacketChannel.TYPE_DYNAMIC_RESPONSE) {
            final TransactionData<RequestPacket<Packet>, Packet> transactionData = (TransactionData<RequestPacket<Packet>, Packet>) stored;
            if (type == SpongePacketChannel.TYPE_RESPONSE) {
                this.handleTransactionResponse(connection, transactionData, result, SpongePacketChannel.NO_DYNAMIC_OPCODE);
            } else if (type == SpongePacketChannel.TYPE_NO_RESPONSE) {
                this.handleTransactionResponse(connection, transactionData, TransactionResult.failure(new NoResponseException()), SpongePacketChannel.NO_DYNAMIC_OPCODE);
            } else {
                this.handleTransactionResponse(connection, transactionData, result, value);
            }
        } else {
            this.handleException(connection, new ChannelIOException("Unknown packet type: " + type), null);
        }
    }
}
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) NoResponseException(org.spongepowered.api.network.channel.NoResponseException)

Example 5 with ChannelIOException

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

Aggregations

ChannelIOException (org.spongepowered.api.network.channel.ChannelIOException)8 ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)5 Packet (org.spongepowered.api.network.channel.packet.Packet)5 RequestPacket (org.spongepowered.api.network.channel.packet.RequestPacket)5 ChannelException (org.spongepowered.api.network.channel.ChannelException)3 Supplier (java.util.function.Supplier)1 EngineConnection (org.spongepowered.api.network.EngineConnection)1 NoResponseException (org.spongepowered.api.network.channel.NoResponseException)1 RequestPacketHandler (org.spongepowered.api.network.channel.packet.RequestPacketHandler)1