Search in sources :

Example 1 with RawHandshakeDataRequestResponse

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

Aggregations

Consumer (java.util.function.Consumer)1 ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)1 ChannelException (org.spongepowered.api.network.channel.ChannelException)1 RawHandshakeDataRequestResponse (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse)1