use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeRawLoginDataChannel method handleRequestPayload.
<C extends EngineConnection> void handleRequestPayload(final C connection, final ChannelBuf payload, final int transactionId) {
final RawHandshakeDataRequestHandler<? super C> handler = this.getRequestHandler(connection);
final RawHandshakeDataRequestResponse response = new RawHandshakeDataRequestResponse() {
private boolean completed;
private void checkCompleted() {
if (this.completed) {
throw new ChannelException("The request response was already completed.");
}
this.completed = true;
}
@Override
public void fail(final ChannelException exception) {
Objects.requireNonNull(exception, "exception");
this.checkCompleted();
PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
}
@Override
public void success(final Consumer<ChannelBuf> response) {
Objects.requireNonNull(response, "response");
this.checkCompleted();
final ChannelBuf payload;
try {
payload = SpongeRawLoginDataChannel.this.parent.encodePayload(response);
} catch (final Throwable t) {
SpongeRawLoginDataChannel.this.parent.handleException(connection, new ChannelException("Failed to encode login data response", t), null);
PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
return;
}
PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(payload, transactionId));
}
};
boolean success = false;
if (handler != null) {
try {
handler.handleRequest(payload, connection, response);
success = true;
} catch (final Throwable t) {
this.parent.handleException(connection, new ChannelException("Failed to handle login data request", t), null);
}
}
if (!success) {
PacketSender.sendTo(connection, PacketUtil.createLoginPayloadResponse(null, transactionId));
}
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeRawDataChannel method encodePayload.
ChannelBuf encodePayload(final Consumer<ChannelBuf> payload) {
final ChannelBuf buf = this.manager().getBufferAllocator().buffer();
payload.accept(buf);
return buf;
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeRawPlayDataChannel method sendTo.
@Override
public CompletableFuture<Void> sendTo(final EngineConnection connection, final Consumer<ChannelBuf> consumer) {
Objects.requireNonNull(connection, "connection");
Objects.requireNonNull(consumer, "payload");
ConnectionUtil.checkPlayPhase(connection);
final CompletableFuture<Void> future = new CompletableFuture<>();
final ChannelBuf payload;
try {
payload = this.parent.encodePayload(consumer);
} catch (final Throwable ex) {
this.parent.handleException(connection, ex, future);
return future;
}
final Packet<?> mcPacket = PacketUtil.createPlayPayload(this.parent.key(), payload, connection.side());
PacketSender.sendTo(connection, mcPacket, future);
return future;
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeChannelManager method encodeChannelRegistry.
/**
* Encodes the sponge channel registry. Sending the channel registry will
* override all known channel entries for the connection. Sending
* "minecraft:register" packets afterwards is still possible to add channels.
*
* @return The encoded payload
*/
private ChannelBuf encodeChannelRegistry() {
final List<SpongeChannel> channels = ImmutableList.copyOf(this.channels.values());
final ChannelBuf buf = this.bufferAllocator.buffer();
buf.writeVarInt(channels.size());
for (final SpongeChannel channel : channels) {
buf.writeString(channel.key().formatted());
// The type is included to provide extra information for e.g. proxies
// who want to improve sponge support
// Not used by sponge itself
buf.writeByte((byte) channel.getType());
}
return buf;
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeChannelManager method handlePlayPayload.
public boolean handlePlayPayload(final EngineConnection connection, final ServerboundCustomPayloadPacket packet) {
final ServerboundCustomPayloadPacketAccessor accessor = (ServerboundCustomPayloadPacketAccessor) packet;
final ResourceKey channel = (ResourceKey) (Object) accessor.accessor$identifier();
final ChannelBuf payload = (ChannelBuf) accessor.accessor$data();
return this.handlePlayPayload(connection, channel, payload);
}
Aggregations