Search in sources :

Example 6 with ChannelBuf

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

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

the class SpongeRawDataChannel method encodePayload.

ChannelBuf encodePayload(final Consumer<ChannelBuf> payload) {
    final ChannelBuf buf = this.manager().getBufferAllocator().buffer();
    payload.accept(buf);
    return buf;
}
Also used : ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 8 with ChannelBuf

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

the class SpongeRawPlayDataChannel method sendTo.

@Override
public CompletableFuture<Void> sendTo(final EngineConnection connection, final Consumer<ChannelBuf> consumer) {
    Objects.requireNonNull(connection, "connection");
    Objects.requireNonNull(consumer, "payload");
    ConnectionUtil.checkPlayPhase(connection);
    final CompletableFuture<Void> future = new CompletableFuture<>();
    final ChannelBuf payload;
    try {
        payload = this.parent.encodePayload(consumer);
    } catch (final Throwable ex) {
        this.parent.handleException(connection, ex, future);
        return future;
    }
    final Packet<?> mcPacket = PacketUtil.createPlayPayload(this.parent.key(), payload, connection.side());
    PacketSender.sendTo(connection, mcPacket, future);
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 9 with ChannelBuf

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

the class SpongeChannelManager method encodeChannelRegistry.

/**
 * Encodes the sponge channel registry. Sending the channel registry will
 * override all known channel entries for the connection. Sending
 * "minecraft:register" packets afterwards is still possible to add channels.
 *
 * @return The encoded payload
 */
private ChannelBuf encodeChannelRegistry() {
    final List<SpongeChannel> channels = ImmutableList.copyOf(this.channels.values());
    final ChannelBuf buf = this.bufferAllocator.buffer();
    buf.writeVarInt(channels.size());
    for (final SpongeChannel channel : channels) {
        buf.writeString(channel.key().formatted());
        // The type is included to provide extra information for e.g. proxies
        // who want to improve sponge support
        // Not used by sponge itself
        buf.writeByte((byte) channel.getType());
    }
    return buf;
}
Also used : ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 10 with ChannelBuf

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

the class SpongeChannelManager method handlePlayPayload.

public boolean handlePlayPayload(final EngineConnection connection, final ServerboundCustomPayloadPacket packet) {
    final ServerboundCustomPayloadPacketAccessor accessor = (ServerboundCustomPayloadPacketAccessor) packet;
    final ResourceKey channel = (ResourceKey) (Object) accessor.accessor$identifier();
    final ChannelBuf payload = (ChannelBuf) accessor.accessor$data();
    return this.handlePlayPayload(connection, channel, payload);
}
Also used : ServerboundCustomPayloadPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundCustomPayloadPacketAccessor) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) ResourceKey(org.spongepowered.api.ResourceKey)

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