use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongePacketChannel method sendNormalPacketTo.
private <P extends Packet> void sendNormalPacketTo(final EngineConnection connection, final P packet, final CompletableFuture<Void> future) {
final SpongePacketBinding<P> binding = (SpongePacketBinding) this.requireBinding(packet.getClass());
final boolean isLoginPhase = ConnectionUtil.isLoginPhase(connection);
final EngineConnectionSide<?> side = connection.side();
final ChannelBuf payload = this.manager().getBufferAllocator().buffer();
final Supplier<net.minecraft.network.protocol.Packet<?>> mcPacketSupplier;
if (isLoginPhase) {
if (side == EngineConnectionSide.CLIENT) {
payload.writeString(this.key().formatted());
payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_NORMAL, binding.opcode()));
mcPacketSupplier = () -> PacketUtil.createLoginPayloadResponse(payload, Constants.Channels.LOGIN_PAYLOAD_TRANSACTION_ID);
} else {
payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_NORMAL, binding.opcode()));
final int transactionId = ConnectionUtil.getTransactionStore(connection).nextId();
mcPacketSupplier = () -> PacketUtil.createLoginPayloadRequest(this.key(), payload, transactionId);
}
} else {
payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_NORMAL, binding.opcode()));
mcPacketSupplier = () -> PacketUtil.createPlayPayload(this.key(), payload, side);
}
try {
this.encodePayload(payload, packet);
} catch (final Throwable ex) {
future.completeExceptionally(ex);
return;
}
final net.minecraft.network.protocol.Packet<?> mcPacket = mcPacketSupplier.get();
PacketSender.sendTo(connection, mcPacket, future);
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongePacketChannel method sendRequestPacketTo.
private <P extends RequestPacket<R>, R extends Packet> void sendRequestPacketTo(final EngineConnection connection, final P packet, final CompletableFuture<?> future, @Nullable final Consumer<R> response, @Nullable final Runnable sendSuccess) {
final SpongeTransactionalPacketBinding<P, R> binding = (SpongeTransactionalPacketBinding) this.requireBinding(packet.getClass());
final TransactionStore transactionStore = ConnectionUtil.getTransactionStore(connection);
final int transactionId = transactionStore.nextId();
final boolean isLoginPhase = ConnectionUtil.isLoginPhase(connection);
final EngineConnectionSide<?> side = connection.side();
final ChannelBuf payload = this.manager().getBufferAllocator().buffer();
final Supplier<net.minecraft.network.protocol.Packet<?>> mcPacketSupplier;
if (isLoginPhase) {
if (side == EngineConnectionSide.CLIENT) {
payload.writeString(this.key().formatted());
payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_REQUEST, transactionId));
payload.writeVarInt(binding.opcode());
mcPacketSupplier = () -> PacketUtil.createLoginPayloadResponse(payload, Constants.Channels.LOGIN_PAYLOAD_TRANSACTION_ID);
} else {
payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_REQUEST, binding.opcode()));
mcPacketSupplier = () -> PacketUtil.createLoginPayloadRequest(this.key(), payload, transactionId);
}
} else {
payload.writeVarLong(SpongePacketChannel.packTypeAndValue(SpongePacketChannel.TYPE_REQUEST, transactionId));
payload.writeVarInt(binding.opcode());
mcPacketSupplier = () -> PacketUtil.createPlayPayload(this.key(), payload, side);
}
try {
this.encodePayload(payload, packet);
} catch (final Throwable ex) {
this.handleException(connection, ex, future);
return;
}
if (response != null) {
final TransactionData<P, R> transactionData = new TransactionData<>(packet, binding, response, future);
transactionStore.put(transactionId, this, transactionData);
}
final net.minecraft.network.protocol.Packet<?> mcPacket = mcPacketSupplier.get();
PacketSender.sendTo(connection, mcPacket, sendFuture -> {
if (!sendFuture.isSuccess()) {
this.handleException(connection, sendFuture.cause(), future);
// and remove it from the store
if (response != null) {
transactionStore.remove(transactionId);
}
} else if (sendSuccess != null) {
sendSuccess.run();
}
});
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongePacketChannel method handleTransactionResponse.
private <P extends RequestPacket<R>, R extends Packet> void handleTransactionResponse(final EngineConnection connection, final TransactionData<P, R> transactionData, final TransactionResult result, final int dynamicOpcode) {
if (result.isSuccess()) {
final ChannelBuf payload = result.getPayload();
SpongePacketBinding<R> responseBinding = null;
final Supplier<R> packetSupplier;
if (dynamicOpcode != SpongePacketChannel.NO_DYNAMIC_OPCODE) {
responseBinding = (SpongePacketBinding<R>) this.requireBinding(dynamicOpcode);
packetSupplier = responseBinding.getPacketConstructor();
} else if (transactionData.binding instanceof SpongeFixedTransactionalPacketBinding) {
packetSupplier = (Supplier<R>) ((SpongeFixedTransactionalPacketBinding<RequestPacket<Packet>, Packet>) transactionData.binding).getResponsePacketConstructor();
} else {
throw new ChannelException("A fixed response was send but no fixed response was bound to the request: " + transactionData.request.getClass());
}
final R packet;
try {
packet = this.decodePayload(packetSupplier, payload);
} catch (final Throwable ex) {
this.handleException(connection, new ChannelIOException("Failed to decode packet", ex), transactionData.future);
return;
}
if (responseBinding != null) {
this.handle(connection, (SpongeHandlerPacketBinding<Packet>) responseBinding, packet);
} else {
this.handleResponse(connection, transactionData.binding, transactionData.request, packet);
}
if (transactionData.success != null) {
transactionData.success.accept(packet);
}
} else {
this.handleException(connection, result.getCause(), transactionData.future);
this.handleResponseFailure(connection, transactionData.binding, transactionData.request, result.getCause());
}
}
use of org.spongepowered.api.network.channel.ChannelBuf 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;
}
Aggregations