use of org.spongepowered.api.network.EngineConnection in project SpongeCommon 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 VelocityForwardingInfo method sendQuery.
public static void sendQuery(final ServerLoginPacketListenerImpl mcConn) {
final EngineConnection conn = (EngineConnection) mcConn;
VelocityChannel.CHANNEL.sendTo(conn, cbuf -> {
}).whenComplete((response, error) -> {
if (error != null) {
if (error instanceof NoResponseException) {
conn.close(Component.text("This server requires you to connect with Velocity."));
}
return;
}
if (!VelocityForwardingInfo.checkIntegrity(response)) {
conn.close(Component.text("Unable to verify player details. Is your forwarding secret correct?"));
return;
}
final ConnectionAccessor connectionAccessor = (ConnectionAccessor) mcConn.getConnection();
connectionAccessor.accessor$address(new InetSocketAddress(VelocityForwardingInfo.readAddress(response), ((InetSocketAddress) mcConn.getConnection().getRemoteAddress()).getPort()));
((ServerLoginPacketListenerImplAccessor) mcConn).accessor$gameProfile(VelocityForwardingInfo.createProfile(response));
}).exceptionally(err -> {
if (!(err instanceof NoResponseException)) {
// Handled above
VelocityForwardingInfo.LOGGER.error("Failed to process velocity forwarding info", err);
conn.close(Component.text("Invalid forwarding information received!"));
}
return null;
});
}
use of org.spongepowered.api.network.EngineConnection in project SpongeCommon by SpongePowered.
the class PacketSender method sendTo.
public static void sendTo(final EngineConnection connection, final Packet<?> packet, @Nullable final Consumer<Future<? super Void>> listener) {
final Connection networkManager = ((ConnectionHolderBridge) connection).bridge$getConnection();
GenericFutureListener<? extends Future<? super Void>> asyncListener = null;
if (listener != null) {
final EngineConnectionSide<?> side = connection.side();
// Complete the netty callback on the sync thread
asyncListener = future -> {
final BlockableEventLoop<?> executor;
if (side == EngineConnectionSide.CLIENT) {
executor = (BlockableEventLoop<?>) Sponge.client();
} else {
executor = (BlockableEventLoop<?>) Sponge.server();
}
executor.execute(() -> listener.accept(future));
};
}
networkManager.send(packet, asyncListener);
}
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;
}
Aggregations