Network throttle: Force ME update packets to be sent once every N ms. (#110)

* Network throttle: Force ME update packets to be sent once every 50ms.

1. Item and fluid packets are separated to their own classes, as they are
not even allowed to be mixed together in the first place. This will enforce a strict
separation of the two.

2. New network worker thread for handling ME inventory updates. Instead of
creating packets directly, classes should schedule packet(s) to be sent at the
next 50ms boundary. The packet has a limit of 256 items to prevent killing the
client, but this can be changed in the config. In fact, the packet send rate is
also configurable.

3. Upon opening a terminal GUI, ME update packets are split. Only the last received
packet will trigger a terminal sorting, so if the ME system is large, it may take
several seconds (!) to load all the items.

* Update SPacketMEFluidInvUpdate.java

---------

Co-authored-by: Martin Robertz <[email protected]>
This commit is contained in:
firenooandMartin Robertz authored and GitHub committed 2023-04-01 14:04:39 +02:00
1 parent 54781c290e
commit e1b3f20078
10 files changed
+200 -90

No files matched your search

@@ -1,7 +1,8 @@
package com.glodblock.github.client.gui.container;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
@@ -43,7 +44,7 @@ import com.glodblock.github.client.gui.container.base.FCContainerMonitor;
import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.network.CPacketFluidUpdate;
import com.glodblock.github.network.SPacketFluidUpdate;
import com.glodblock.github.network.SPacketMEFluidInvUpdate;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
import com.glodblock.github.util.Util;
public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
@@ -126,33 +127,22 @@ public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
protected void processItemList() {
if (!this.fluids.isEmpty()) {
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();
SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate();
List<IAEFluidStack> toSend = new ArrayList<>();
for (final IAEFluidStack is : this.fluids) {
final IAEFluidStack send = monitorCache.findPrecise(is);
try {
if (send != null) {
packet.appendFluid(send);
} else {
is.setStackSize(0);
packet.appendFluid(is);
}
} catch (BufferOverflowException e) {
for (final Object c : this.crafters) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
packet = new SPacketMEFluidInvUpdate();
if (send != null) {
packet.appendFluid(send);
} else {
is.setStackSize(0);
packet.appendFluid(is);
}
if (send != null) {
toSend.add(send);
} else {
is.setStackSize(0);
toSend.add(is);
}
}
for (final Object c : this.crafters) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
if (c instanceof EntityPlayerMP) {
SPacketMEUpdateBuffer.scheduleFluidUpdate((EntityPlayerMP) c, toSend);
}
}
this.fluids.resetStatus();
}
}
@@ -160,17 +150,11 @@ public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayerMP && this.monitor != null) {
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();
SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate();
List<IAEFluidStack> toSend = new ArrayList<>();
for (final IAEFluidStack is : monitorCache) {
try {
packet.appendFluid(is);
} catch (BufferOverflowException e) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
packet = new SPacketMEFluidInvUpdate();
packet.appendFluid(is);
}
toSend.add(is);
}
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
SPacketMEUpdateBuffer.scheduleFluidUpdate((EntityPlayerMP) c, toSend);
}
}
@@ -1,6 +1,7 @@
package com.glodblock.github.client.gui.container;
import java.nio.BufferOverflowException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
@@ -25,9 +26,8 @@ import appeng.container.slot.SlotRestrictedInput;
import appeng.me.helpers.ChannelPowerSrc;
import appeng.util.Platform;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.base.FCContainerMonitor;
import com.glodblock.github.network.SPacketMEItemInvUpdate;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
@@ -95,34 +95,19 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
protected void processItemList() {
if (!this.items.isEmpty()) {
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
List<IAEItemStack> toSend = new ArrayList<>();
for (final IAEItemStack is : this.items) {
final IAEItemStack send = monitorCache.findPrecise(is);
try {
if (send != null) {
packet.appendItem(send);
} else {
is.setStackSize(0);
packet.appendItem(is);
}
} catch (BufferOverflowException e) {
for (final Object c : this.crafters) {
if (c instanceof EntityPlayerMP) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
}
packet = new SPacketMEItemInvUpdate();
if (send != null) {
packet.appendItem(send);
} else {
is.setStackSize(0);
packet.appendItem(is);
}
if (send != null) {
toSend.add(send);
} else {
is.setStackSize(0);
toSend.add(is);
}
}
for (final Object c : this.crafters) {
if (c instanceof EntityPlayerMP) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
if (c instanceof EntityPlayer) {
SPacketMEUpdateBuffer.scheduleItemUpdate((EntityPlayerMP) c, toSend);
}
}
this.items.resetStatus();
@@ -133,17 +118,11 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayer && this.monitor != null) {
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
List<IAEItemStack> toSend = new ArrayList<>();
for (final IAEItemStack is : monitorCache) {
try {
packet.appendItem(is);
} catch (BufferOverflowException e) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
packet = new SPacketMEItemInvUpdate();
packet.appendItem(is);
}
toSend.add(is);
}
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
SPacketMEUpdateBuffer.scheduleItemUpdate((EntityPlayerMP) c, toSend);
}
}
@@ -152,6 +131,9 @@ public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {
super.removeCraftingFromCrafters(c);
if (this.crafters.isEmpty() && this.monitor != null) {
this.monitor.removeListener(this);
if (c instanceof EntityPlayerMP) {
SPacketMEUpdateBuffer.clear((EntityPlayerMP) c);
}
}
}
@@ -30,6 +30,7 @@ import appeng.util.IConfigManagerHost;
import appeng.util.Platform;
import com.glodblock.github.inventory.item.IWirelessTerminal;
import com.glodblock.github.network.SPacketMEUpdateBuffer;
public abstract class FCContainerMonitor<T extends IAEStack<T>> extends FCBaseContainer
implements IConfigManagerHost, IConfigurableObject, IMEMonitorHandlerReceiver<T> {
@@ -158,6 +159,10 @@ public abstract class FCContainerMonitor<T extends IAEStack<T>> extends FCBaseCo
super.onContainerClosed(player);
if (this.monitor != null) {
this.monitor.removeListener(this);
if (player instanceof EntityPlayerMP && Platform.isServer()) {
SPacketMEUpdateBuffer.clear((EntityPlayerMP) player);
}
}
}