use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeChannelManager method handleLoginResponsePayload.
public void handleLoginResponsePayload(final EngineConnection connection, final ServerboundCustomQueryPacket packet) {
// Client -> Server response
final ServerboundCustomQueryPacketAccessor accessor = (ServerboundCustomQueryPacketAccessor) packet;
final int transactionId = accessor.accessor$transactionId();
final ChannelBuf payload = (ChannelBuf) accessor.accessor$data();
try {
this.handleLoginResponsePayload(connection, transactionId, payload);
} finally {
if (payload != null) {
ChannelBuffers.release(payload);
}
}
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeChannelManager method sendChannelRegistrations.
public void sendChannelRegistrations(final EngineConnection connection) {
final ChannelBuf payload = RegisterChannelUtil.encodePayload(this.channels.keySet());
final Packet<?> mcPacket = PacketUtil.createPlayPayload(Constants.Channels.REGISTER_KEY, payload, connection.side());
PacketSender.sendTo(connection, mcPacket);
}
use of org.spongepowered.api.network.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class AbstractPacketChannel method encodePayload.
protected void encodePayload(final ChannelBuf payload, final Packet packet) {
final ChannelBuf packetContent = this.manager().getBufferAllocator().buffer();
try {
this.encodePayloadUnsafe(packetContent, packet);
ChannelBuffers.write(payload, packetContent);
} catch (final Throwable ex) {
ChannelBuffers.release(payload);
throw ex;
} finally {
ChannelBuffers.release(packetContent);
}
}
use of org.spongepowered.api.network.channel.ChannelBuf 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.channel.ChannelBuf in project SpongeCommon by SpongePowered.
the class SpongeBasicPacketChannel method encodeLoginPayload.
// This only exists for forge compatibility
private ChannelBuf encodeLoginPayload(final int opcode, final Packet packet) {
final ChannelBuf loginPayload = this.manager().getBufferAllocator().buffer();
final ChannelBuf payload = this.manager().getBufferAllocator().buffer();
try {
this.encodePayloadUnsafe(payload, packet);
loginPayload.writeString(this.key().formatted());
loginPayload.writeVarInt(payload.available() + 1);
loginPayload.writeByte((byte) opcode);
ChannelBuffers.write(loginPayload, payload);
} finally {
ChannelBuffers.release(payload);
}
return loginPayload;
}
Aggregations