Search in sources :

Example 16 with ChannelBuf

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

the class SpongeChannelManager method handleLoginResponsePayload.

public void handleLoginResponsePayload(final EngineConnection connection, final ServerboundCustomQueryPacket packet) {
    // Client -> Server response
    final ServerboundCustomQueryPacketAccessor accessor = (ServerboundCustomQueryPacketAccessor) packet;
    final int transactionId = accessor.accessor$transactionId();
    final ChannelBuf payload = (ChannelBuf) accessor.accessor$data();
    try {
        this.handleLoginResponsePayload(connection, transactionId, payload);
    } finally {
        if (payload != null) {
            ChannelBuffers.release(payload);
        }
    }
}
Also used : ServerboundCustomQueryPacketAccessor(org.spongepowered.common.accessor.network.protocol.login.ServerboundCustomQueryPacketAccessor) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 17 with ChannelBuf

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

the class SpongeChannelManager method sendChannelRegistrations.

public void sendChannelRegistrations(final EngineConnection connection) {
    final ChannelBuf payload = RegisterChannelUtil.encodePayload(this.channels.keySet());
    final Packet<?> mcPacket = PacketUtil.createPlayPayload(Constants.Channels.REGISTER_KEY, payload, connection.side());
    PacketSender.sendTo(connection, mcPacket);
}
Also used : ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 18 with ChannelBuf

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

the class AbstractPacketChannel method encodePayload.

protected void encodePayload(final ChannelBuf payload, final Packet packet) {
    final ChannelBuf packetContent = this.manager().getBufferAllocator().buffer();
    try {
        this.encodePayloadUnsafe(packetContent, packet);
        ChannelBuffers.write(payload, packetContent);
    } catch (final Throwable ex) {
        ChannelBuffers.release(payload);
        throw ex;
    } finally {
        ChannelBuffers.release(packetContent);
    }
}
Also used : ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Example 19 with ChannelBuf

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

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

the class SpongeBasicPacketChannel method encodeLoginPayload.

// This only exists for forge compatibility
private ChannelBuf encodeLoginPayload(final int opcode, final Packet packet) {
    final ChannelBuf loginPayload = this.manager().getBufferAllocator().buffer();
    final ChannelBuf payload = this.manager().getBufferAllocator().buffer();
    try {
        this.encodePayloadUnsafe(payload, packet);
        loginPayload.writeString(this.key().formatted());
        loginPayload.writeVarInt(payload.available() + 1);
        loginPayload.writeByte((byte) opcode);
        ChannelBuffers.write(loginPayload, payload);
    } finally {
        ChannelBuffers.release(payload);
    }
    return loginPayload;
}
Also used : 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