Search in sources :

Example 1 with SplitPacketMessage

use of com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage in project minecolonies by ldtteam.

the class NetworkChannel method handleSplitting.

/**
 * Method that handles the splitting of the message into chunks if need be.
 *
 * @param msg                  The message to split in question.
 * @param splitMessageConsumer The consumer that sends away the split parts of the message.
 */
private void handleSplitting(final IMessage msg, final Consumer<IMessage> splitMessageConsumer) {
    // Get the inner message id and check if it is known.
    final int messageId = this.messageTypeToIdMap.getOrDefault(msg.getClass(), -1);
    if (messageId == -1) {
        throw new IllegalArgumentException("The message is unknown to this channel!");
    }
    // Write the message into a buffer and copy that buffer into a byte array for processing.
    final ByteBuf buffer = Unpooled.buffer();
    final PacketBuffer innerPacketBuffer = new PacketBuffer(buffer);
    msg.toBytes(innerPacketBuffer);
    final byte[] data = buffer.array();
    buffer.release();
    // Some tracking variables.
    // Max packet size: 90% of maximum.
    // This is 90% of max packet size.
    final int max_packet_size = 943718;
    // The current index in the data array.
    int currentIndex = 0;
    // The current index for the split packets.
    int packetIndex = 0;
    // The communication id.
    final int comId = messageCounter.getAndIncrement();
    // Loop while data is available.
    while (currentIndex < data.length) {
        // Tell the network message entry that we are splitting a packet.
        this.getMessagesTypes().get(messageId).onSplitting(packetIndex);
        final int extra = Math.min(max_packet_size, data.length - currentIndex);
        // Extract the sub data array.
        final byte[] subPacketData = Arrays.copyOfRange(data, currentIndex, currentIndex + extra);
        // Construct the wrapping packet.
        final SplitPacketMessage splitPacketMessage = new SplitPacketMessage(comId, packetIndex++, (currentIndex + extra) >= data.length, messageId, subPacketData);
        // Send the wrapping packet.
        splitMessageConsumer.accept(splitPacketMessage);
        // Move our working index.
        currentIndex += extra;
    }
}
Also used : SplitPacketMessage(com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage) ByteBuf(io.netty.buffer.ByteBuf) TargetPoint(net.minecraftforge.fml.network.PacketDistributor.TargetPoint) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 2 with SplitPacketMessage

use of com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage in project minecolonies by Minecolonies.

the class NetworkChannel method setupInternalMessages.

private void setupInternalMessages() {
    rawChannel.registerMessage(0, SplitPacketMessage.class, IMessage::toBytes, (buf) -> {
        final SplitPacketMessage msg = new SplitPacketMessage();
        msg.fromBytes(buf);
        return msg;
    }, (msg, ctxIn) -> {
        final Context ctx = ctxIn.get();
        final LogicalSide packetOrigin = ctx.getDirection().getOriginationSide();
        ctx.setPacketHandled(true);
        msg.onExecute(ctx, packetOrigin.equals(LogicalSide.CLIENT));
    });
}
Also used : SplitPacketMessage(com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage) Context(net.minecraftforge.fml.network.NetworkEvent.Context) IMessage(com.minecolonies.api.network.IMessage) LogicalSide(net.minecraftforge.fml.LogicalSide)

Example 3 with SplitPacketMessage

use of com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage in project minecolonies by Minecolonies.

the class NetworkChannel method handleSplitting.

/**
 * Method that handles the splitting of the message into chunks if need be.
 *
 * @param msg                  The message to split in question.
 * @param splitMessageConsumer The consumer that sends away the split parts of the message.
 */
private void handleSplitting(final IMessage msg, final Consumer<IMessage> splitMessageConsumer) {
    // Get the inner message id and check if it is known.
    final int messageId = this.messageTypeToIdMap.getOrDefault(msg.getClass(), -1);
    if (messageId == -1) {
        throw new IllegalArgumentException("The message is unknown to this channel!");
    }
    // Write the message into a buffer and copy that buffer into a byte array for processing.
    final ByteBuf buffer = Unpooled.buffer();
    final PacketBuffer innerPacketBuffer = new PacketBuffer(buffer);
    msg.toBytes(innerPacketBuffer);
    final byte[] data = buffer.array();
    buffer.release();
    // Some tracking variables.
    // Max packet size: 90% of maximum.
    // This is 90% of max packet size.
    final int max_packet_size = 943718;
    // The current index in the data array.
    int currentIndex = 0;
    // The current index for the split packets.
    int packetIndex = 0;
    // The communication id.
    final int comId = messageCounter.getAndIncrement();
    // Loop while data is available.
    while (currentIndex < data.length) {
        // Tell the network message entry that we are splitting a packet.
        this.getMessagesTypes().get(messageId).onSplitting(packetIndex);
        final int extra = Math.min(max_packet_size, data.length - currentIndex);
        // Extract the sub data array.
        final byte[] subPacketData = Arrays.copyOfRange(data, currentIndex, currentIndex + extra);
        // Construct the wrapping packet.
        final SplitPacketMessage splitPacketMessage = new SplitPacketMessage(comId, packetIndex++, (currentIndex + extra) >= data.length, messageId, subPacketData);
        // Send the wrapping packet.
        splitMessageConsumer.accept(splitPacketMessage);
        // Move our working index.
        currentIndex += extra;
    }
}
Also used : SplitPacketMessage(com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage) ByteBuf(io.netty.buffer.ByteBuf) TargetPoint(net.minecraftforge.fml.network.PacketDistributor.TargetPoint) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 4 with SplitPacketMessage

use of com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage in project minecolonies by ldtteam.

the class NetworkChannel method setupInternalMessages.

private void setupInternalMessages() {
    rawChannel.registerMessage(0, SplitPacketMessage.class, IMessage::toBytes, (buf) -> {
        final SplitPacketMessage msg = new SplitPacketMessage();
        msg.fromBytes(buf);
        return msg;
    }, (msg, ctxIn) -> {
        final Context ctx = ctxIn.get();
        final LogicalSide packetOrigin = ctx.getDirection().getOriginationSide();
        ctx.setPacketHandled(true);
        msg.onExecute(ctx, packetOrigin.equals(LogicalSide.CLIENT));
    });
}
Also used : SplitPacketMessage(com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage) Context(net.minecraftforge.fml.network.NetworkEvent.Context) IMessage(com.minecolonies.api.network.IMessage) LogicalSide(net.minecraftforge.fml.LogicalSide)

Aggregations

SplitPacketMessage (com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage)4 IMessage (com.minecolonies.api.network.IMessage)2 ByteBuf (io.netty.buffer.ByteBuf)2 PacketBuffer (net.minecraft.network.PacketBuffer)2 LogicalSide (net.minecraftforge.fml.LogicalSide)2 Context (net.minecraftforge.fml.network.NetworkEvent.Context)2 TargetPoint (net.minecraftforge.fml.network.PacketDistributor.TargetPoint)2