Search in sources :

Example 1 with IMessage

use of com.minecolonies.api.network.IMessage 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 2 with IMessage

use of com.minecolonies.api.network.IMessage in project minecolonies by Minecolonies.

the class SplitPacketMessage method onExecute.

@Override
public void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer) {
    try {
        // Sync on the message cache since this is still on the Netty thread.
        synchronized (Network.getNetwork().getMessageCache()) {
            Network.getNetwork().getMessageCache().get(this.communicationId, Maps::newConcurrentMap).put(this.packetIndex, this.payload);
        }
        if (!this.terminator) {
            // We are not the last message stop executing.
            return;
        }
        // No need to sync again, since we are now the last packet to arrive.
        // All data gets sorted and appended.
        final byte[] packetData = Network.getNetwork().getMessageCache().get(this.communicationId, Maps::newConcurrentMap).entrySet().stream().sorted(Map.Entry.comparingByKey()).map(Map.Entry::getValue).reduce(new byte[0], Bytes::concat);
        // Grab the entry from the inner message id.
        final NetworkChannel.NetworkingMessageEntry<?> messageEntry = Network.getNetwork().getMessagesTypes().get(this.innerMessageId);
        // Create a message.
        final IMessage message = messageEntry.getCreator().get();
        // Create a new buffer that reads from the packet data and then deserialize the inner message.
        final ByteBuf buffer = Unpooled.wrappedBuffer(packetData);
        message.fromBytes(new PacketBuffer(buffer));
        buffer.release();
        // Execute the message.
        final LogicalSide packetOrigin = ctxIn.getDirection().getOriginationSide();
        if (message.getExecutionSide() != null && packetOrigin.equals(message.getExecutionSide())) {
            Log.getLogger().warn("Receving {} at wrong side!", message.getClass().getName());
            return;
        }
        // boolean param MUST equals true if packet arrived at logical server
        ctxIn.enqueueWork(() -> message.onExecute(ctxIn, packetOrigin.equals(LogicalSide.CLIENT)));
    } catch (ExecutionException e) {
        Log.getLogger().error("Failed to handle split packet.", e);
    }
}
Also used : Bytes(com.google.common.primitives.Bytes) NetworkChannel(com.minecolonies.coremod.network.NetworkChannel) IMessage(com.minecolonies.api.network.IMessage) LogicalSide(net.minecraftforge.fml.LogicalSide) ByteBuf(io.netty.buffer.ByteBuf) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 3 with IMessage

use of com.minecolonies.api.network.IMessage in project minecolonies by ldtteam.

the class SplitPacketMessage method onExecute.

@Override
public void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer) {
    try {
        // Sync on the message cache since this is still on the Netty thread.
        synchronized (Network.getNetwork().getMessageCache()) {
            Network.getNetwork().getMessageCache().get(this.communicationId, Maps::newConcurrentMap).put(this.packetIndex, this.payload);
        }
        if (!this.terminator) {
            // We are not the last message stop executing.
            return;
        }
        // No need to sync again, since we are now the last packet to arrive.
        // All data gets sorted and appended.
        final byte[] packetData = Network.getNetwork().getMessageCache().get(this.communicationId, Maps::newConcurrentMap).entrySet().stream().sorted(Map.Entry.comparingByKey()).map(Map.Entry::getValue).reduce(new byte[0], Bytes::concat);
        // Grab the entry from the inner message id.
        final NetworkChannel.NetworkingMessageEntry<?> messageEntry = Network.getNetwork().getMessagesTypes().get(this.innerMessageId);
        // Create a message.
        final IMessage message = messageEntry.getCreator().get();
        // Create a new buffer that reads from the packet data and then deserialize the inner message.
        final ByteBuf buffer = Unpooled.wrappedBuffer(packetData);
        message.fromBytes(new PacketBuffer(buffer));
        buffer.release();
        // Execute the message.
        final LogicalSide packetOrigin = ctxIn.getDirection().getOriginationSide();
        if (message.getExecutionSide() != null && packetOrigin.equals(message.getExecutionSide())) {
            Log.getLogger().warn("Receving {} at wrong side!", message.getClass().getName());
            return;
        }
        // boolean param MUST equals true if packet arrived at logical server
        ctxIn.enqueueWork(() -> message.onExecute(ctxIn, packetOrigin.equals(LogicalSide.CLIENT)));
    } catch (ExecutionException e) {
        Log.getLogger().error("Failed to handle split packet.", e);
    }
}
Also used : Bytes(com.google.common.primitives.Bytes) NetworkChannel(com.minecolonies.coremod.network.NetworkChannel) IMessage(com.minecolonies.api.network.IMessage) LogicalSide(net.minecraftforge.fml.LogicalSide) ByteBuf(io.netty.buffer.ByteBuf) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 4 with IMessage

use of com.minecolonies.api.network.IMessage 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

IMessage (com.minecolonies.api.network.IMessage)4 LogicalSide (net.minecraftforge.fml.LogicalSide)4 Bytes (com.google.common.primitives.Bytes)2 NetworkChannel (com.minecolonies.coremod.network.NetworkChannel)2 SplitPacketMessage (com.minecolonies.coremod.network.messages.splitting.SplitPacketMessage)2 ByteBuf (io.netty.buffer.ByteBuf)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 PacketBuffer (net.minecraft.network.PacketBuffer)2 Context (net.minecraftforge.fml.network.NetworkEvent.Context)2