Search in sources :

Example 1 with TransactionResult

use of org.spongepowered.common.network.channel.TransactionResult in project SpongeCommon by SpongePowered.

the class SpongePacketChannel method handleResponsePacket.

private void handleResponsePacket(final EngineConnection connection, final int transactionId, @Nullable final ChannelBuf payload, final int dynamicOpcode) {
    final TransactionStore store = ConnectionUtil.getTransactionStore(connection);
    final TransactionStore.Entry stored = store.remove(transactionId);
    if (stored == null) {
        return;
    }
    final TransactionData<RequestPacket<Packet>, Packet> transactionData = (TransactionData<RequestPacket<Packet>, Packet>) stored.getData();
    final TransactionResult result = payload == null ? TransactionResult.failure(new NoResponseException()) : TransactionResult.success(payload);
    this.handleTransactionResponse(connection, transactionData, result, dynamicOpcode);
}
Also used : TransactionStore(org.spongepowered.common.network.channel.TransactionStore) RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) RequestPacket(org.spongepowered.api.network.channel.packet.RequestPacket) NoResponseException(org.spongepowered.api.network.channel.NoResponseException)

Example 2 with TransactionResult

use of org.spongepowered.common.network.channel.TransactionResult in project SpongeCommon by SpongePowered.

the class SpongeRawLoginDataChannel method sendTo.

@Override
public CompletableFuture<ChannelBuf> sendTo(final EngineConnection connection, final Consumer<ChannelBuf> payload) {
    ConnectionUtil.checkHandshakePhase(connection);
    final CompletableFuture<ChannelBuf> future = new CompletableFuture<>();
    final ChannelBuf buf;
    try {
        buf = this.parent.encodePayload(payload);
    } catch (final Throwable t) {
        this.parent.handleException(connection, t, future);
        return future;
    }
    final TransactionStore transactionStore = ConnectionUtil.getTransactionStore(connection);
    final int transactionId = transactionStore.nextId();
    final Consumer<TransactionResult> resultConsumer = result -> {
        if (result.isSuccess()) {
            future.complete(result.getPayload());
        } else {
            this.parent.handleException(connection, result.getCause(), future);
        }
    };
    final Packet<?> mcPacket = PacketUtil.createLoginPayloadRequest(this.parent.key(), buf, transactionId);
    PacketSender.sendTo(connection, mcPacket, sendFuture -> {
        if (sendFuture.isSuccess()) {
            transactionStore.put(transactionId, this.parent, resultConsumer);
        } else {
            // The packet already failed before it could reach the client
            future.completeExceptionally(sendFuture.cause());
        }
    });
    return future;
}
Also used : PacketUtil(org.spongepowered.common.network.channel.PacketUtil) SpongeChannel(org.spongepowered.common.network.channel.SpongeChannel) TransactionStore(org.spongepowered.common.network.channel.TransactionStore) EngineConnection(org.spongepowered.api.network.EngineConnection) ChannelException(org.spongepowered.api.network.channel.ChannelException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EngineConnectionSide(org.spongepowered.api.network.EngineConnectionSide) CompletableFuture(java.util.concurrent.CompletableFuture) PacketSender(org.spongepowered.common.network.channel.PacketSender) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf) Objects(java.util.Objects) Consumer(java.util.function.Consumer) RawDataChannel(org.spongepowered.api.network.channel.raw.RawDataChannel) ConnectionUtil(org.spongepowered.common.network.channel.ConnectionUtil) Map(java.util.Map) Packet(net.minecraft.network.protocol.Packet) RawHandshakeDataRequestResponse(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) RawHandshakeDataRequestHandler(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestHandler) RawHandshakeDataChannel(org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataChannel) TransactionStore(org.spongepowered.common.network.channel.TransactionStore) CompletableFuture(java.util.concurrent.CompletableFuture) TransactionResult(org.spongepowered.common.network.channel.TransactionResult) ChannelBuf(org.spongepowered.api.network.channel.ChannelBuf)

Aggregations

TransactionResult (org.spongepowered.common.network.channel.TransactionResult)2 TransactionStore (org.spongepowered.common.network.channel.TransactionStore)2 Map (java.util.Map)1 Objects (java.util.Objects)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Consumer (java.util.function.Consumer)1 Packet (net.minecraft.network.protocol.Packet)1 EngineConnection (org.spongepowered.api.network.EngineConnection)1 EngineConnectionSide (org.spongepowered.api.network.EngineConnectionSide)1 ChannelBuf (org.spongepowered.api.network.channel.ChannelBuf)1 ChannelException (org.spongepowered.api.network.channel.ChannelException)1 NoResponseException (org.spongepowered.api.network.channel.NoResponseException)1 Packet (org.spongepowered.api.network.channel.packet.Packet)1 RequestPacket (org.spongepowered.api.network.channel.packet.RequestPacket)1 RawDataChannel (org.spongepowered.api.network.channel.raw.RawDataChannel)1 RawHandshakeDataChannel (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataChannel)1 RawHandshakeDataRequestHandler (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestHandler)1 RawHandshakeDataRequestResponse (org.spongepowered.api.network.channel.raw.handshake.RawHandshakeDataRequestResponse)1 ConnectionUtil (org.spongepowered.common.network.channel.ConnectionUtil)1