use of org.spongepowered.api.network.EngineConnection in project Sponge by SpongePowered.
the class ClientHandshakePacketListenerImplMixin_Vanilla method vanilla$handleRequestPayload.
// @formatter:on
@Inject(method = "handleCustomQuery", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/Connection;send(Lnet/minecraft/network/protocol/Packet;)V"), cancellable = true)
private void vanilla$handleRequestPayload(final ClientboundCustomQueryPacket packet, final CallbackInfo ci) {
ci.cancel();
final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
this.minecraft.execute(() -> {
final EngineConnection connection = (EngineConnection) this;
if (!channelRegistry.handleLoginRequestPayload(connection, packet)) {
PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, packet.getTransactionId()));
}
});
}
use of org.spongepowered.api.network.EngineConnection 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);
}
}
}
use of org.spongepowered.api.network.EngineConnection 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;
}
use of org.spongepowered.api.network.EngineConnection in project SpongeCommon by SpongePowered.
the class ServerGamePacketListenerImplMixin_Vanilla method vanilla$onHandleCustomPayload.
@Inject(method = "handleCustomPayload", at = @At(value = "HEAD"))
private void vanilla$onHandleCustomPayload(final ServerboundCustomPayloadPacket packet, final CallbackInfo ci) {
// For some reason, "ServerboundCustomPayloadPacket" is released in the processPacket
// method of its class, only applicable to this packet, so just retain here.
((ServerboundCustomPayloadPacketAccessor) packet).accessor$data().retain();
final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
this.server.execute(() -> channelRegistry.handlePlayPayload((EngineConnection) this, packet));
}
use of org.spongepowered.api.network.EngineConnection in project SpongeCommon by SpongePowered.
the class ServerLoginPacketListenerImplMixin_Vanilla method onResponsePayload.
@Inject(method = "handleCustomQueryPacket", at = @At("HEAD"), cancellable = true)
private void onResponsePayload(final ServerboundCustomQueryPacket packet, final CallbackInfo ci) {
ci.cancel();
final SpongeChannelManager channelRegistry = (SpongeChannelManager) Sponge.channelManager();
this.server.execute(() -> channelRegistry.handleLoginResponsePayload((EngineConnection) this, packet));
}
Aggregations