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));
});
}
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);
}
}
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);
}
}
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));
});
}
Aggregations