Search in sources :

Example 11 with ChannelBuf

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

the class SpongeChannelManager method handleLoginRequestPayload.

private boolean handleLoginRequestPayload(final EngineConnection connection, final ResourceKey channelKey, final int transactionId, final ChannelBuf payload) {
    if (channelKey.equals(Constants.Channels.SPONGE_CLIENT_TYPE)) {
        final ClientType clientType = ((MinecraftBridge) Sponge.client()).bridge$getClientType();
        final ChannelBuf responsePayload = this.bufferAllocator.buffer();
        responsePayload.writeString(clientType.getName());
        final Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(responsePayload, transactionId);
        PacketSender.sendTo(connection, mcPacket);
        return true;
    }
    if (channelKey.equals(Constants.Channels.SPONGE_CHANNEL_REGISTRY)) {
        this.handleChannelRegistry(connection, payload);
        // Respond with registered channels
        final ChannelBuf responsePayload = this.encodeChannelRegistry();
        final Packet<?> mcPacket = PacketUtil.createLoginPayloadResponse(responsePayload, transactionId);
        PacketSender.sendTo(connection, mcPacket);
        return true;
    }
    ResourceKey actualChannelKey = channelKey;
    ChannelBuf actualPayload = payload;
    if (channelKey.equals(Constants.Channels.FML_LOGIN_WRAPPER_CHANNEL)) {
        actualChannelKey = ResourceKey.resolve(payload.readString());
        final int length = payload.readVarInt();
        actualPayload = payload.readSlice(length);
    }
    final SpongeChannel channel = this.channels.get(actualChannelKey);
    if (channel != null) {
        channel.handleLoginRequestPayload(connection, transactionId, actualPayload);
        return true;
    }
    return false;
}
Also used : ClientType(org.spongepowered.common.entity.player.ClientType) MinecraftBridge(org.spongepowered.common.bridge.client.MinecraftBridge) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) ResourceKey(org.spongepowered.api.ResourceKey)

Example 12 with ChannelBuf

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

the class SpongeChannelManager method sendLoginChannelRegistry.

/**
 * Sends the login channel registrations. The client will respond back with
 * the registrations and the returned future will be completed.
 *
 * @param connection The connection to send the registrations to
 */
public CompletableFuture<Void> sendLoginChannelRegistry(final EngineConnection connection) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    final TransactionStore store = ConnectionUtil.getTransactionStore(connection);
    final int transactionId = store.nextId();
    store.put(transactionId, null, new ChannelRegistrySyncFuture(future));
    final ChannelBuf payload = this.encodeChannelRegistry();
    final Packet<?> mcPacket = PacketUtil.createLoginPayloadRequest(Constants.Channels.SPONGE_CHANNEL_REGISTRY, payload, transactionId);
    PacketSender.sendTo(connection, mcPacket, sendFuture -> {
        if (!sendFuture.isSuccess()) {
            future.completeExceptionally(sendFuture.cause());
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 13 with ChannelBuf

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

the class SpongeChannelManager method handleLoginRequestPayload.

public boolean handleLoginRequestPayload(final EngineConnection connection, final ClientboundCustomQueryPacket packet) {
    // Server -> Client request
    final ClientboundCustomQueryPacketAccessor accessor = (ClientboundCustomQueryPacketAccessor) packet;
    final ResourceKey channel = (ResourceKey) (Object) accessor.accessor$identifier();
    final int transactionId = accessor.accessor$transactionId();
    final ChannelBuf payload = (ChannelBuf) accessor.accessor$data();
    try {
        return this.handleLoginRequestPayload(connection, channel, transactionId, payload);
    } finally {
        ChannelBuffers.release(payload);
    }
}
Also used : ClientboundCustomQueryPacketAccessor(org.spongepowered.common.accessor.network.protocol.login.ClientboundCustomQueryPacketAccessor) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) ResourceKey(org.spongepowered.api.ResourceKey)

Example 14 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 ClientboundCustomPayloadPacket packet) {
    final ClientboundCustomPayloadPacketAccessor accessor = (ClientboundCustomPayloadPacketAccessor) packet;
    final ResourceKey channel = (ResourceKey) (Object) accessor.accessor$identifier();
    final ChannelBuf payload = (ChannelBuf) accessor.accessor$data();
    return this.handlePlayPayload(connection, channel, payload);
}
Also used : ClientboundCustomPayloadPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ClientboundCustomPayloadPacketAccessor) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) ResourceKey(org.spongepowered.api.ResourceKey)

Example 15 with ChannelBuf

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

the class SpongeChannelManager method requestClientType.

public CompletableFuture<Void> requestClientType(final EngineConnection connection) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    final TransactionStore store = ConnectionUtil.getTransactionStore(connection);
    final int transactionId = store.nextId();
    store.put(transactionId, null, new ClientTypeSyncFuture(future));
    final ChannelBuf payload = this.bufferAllocator.buffer();
    final Packet<?> mcPacket = PacketUtil.createLoginPayloadRequest(Constants.Channels.SPONGE_CLIENT_TYPE, payload, transactionId);
    PacketSender.sendTo(connection, mcPacket, sendFuture -> {
        if (!sendFuture.isSuccess()) {
            future.completeExceptionally(sendFuture.cause());
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) 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