Skip to content

Commit

Permalink
Split packets when sending items over network (#107)
Browse files Browse the repository at this point in the history
Packets are now split into smaller chunks when sent. The packet
size is adjustable in the config.
  • Loading branch information
firenoo authored Mar 23, 2023
1 parent 6a244c1 commit 9c2ef02
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 180 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/glodblock/github/FluidCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLLoadCompleteEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.*;
import cpw.mods.fml.common.network.NetworkRegistry;

@Mod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ public GuiFluidMonitor(final InventoryPlayer inventoryPlayer, final ITerminalHos
this.repo = new FluidRepo(getScrollBar(), this);
}

public void postUpdate(final List<IAEFluidStack> list) {
@Override
public void postUpdate(final List<IAEFluidStack> list, boolean resort) {
for (final IAEFluidStack is : list) {
IAEItemStack stack = AEItemStack.create(ItemFluidDrop.newDisplayStack(is.getFluidStack()));
stack.setStackSize(is.getStackSize());
this.repo.postUpdate(stack);
}
this.repo.updateView();
if (resort) {
this.repo.updateView();
}
this.setScrollBar();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ public GuiItemMonitor(final InventoryPlayer inventoryPlayer, final ITerminalHost
this.repo = new ItemRepo(getScrollBar(), this);
}

public void postUpdate(final List<IAEItemStack> list) {
@Override
public void postUpdate(final List<IAEItemStack> list, boolean resort) {
for (final IAEItemStack is : list) {
this.repo.postUpdate(is);
}
this.repo.updateView();
if (resort) {
this.repo.updateView();
}
this.setScrollBar();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void setOffsetY(int y) {
offsetY = y;
}

public abstract void postUpdate(final List<T> list);
public abstract void postUpdate(final List<T> list, boolean resort);

protected void setScrollBar() {
this.getScrollBar().setTop(18).setLeft(175).setHeight(this.rows * 18 - 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import com.glodblock.github.common.item.ItemFluidDrop;
import com.glodblock.github.network.CPacketFluidUpdate;
import com.glodblock.github.network.SPacketFluidUpdate;
import com.glodblock.github.network.SPacketMEInventoryUpdate;
import com.glodblock.github.network.SPacketMEFluidInvUpdate;
import com.glodblock.github.util.Util;

public class ContainerFluidMonitor extends FCContainerMonitor<IAEFluidStack> {
Expand Down Expand Up @@ -126,46 +126,51 @@ public ItemStack transferStackInSlot(final EntityPlayer p, final int idx) {
protected void processItemList() {
if (!this.fluids.isEmpty()) {
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();
final SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate(true);

SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate();
for (final IAEFluidStack is : this.fluids) {
final IAEFluidStack send = monitorCache.findPrecise(is);
if (send == null) {
is.setStackSize(0);
piu.appendFluid(is);
} else {
piu.appendFluid(send);
}
}

if (!piu.isEmpty()) {
this.fluids.resetStatus();

for (final Object c : this.crafters) {
if (c instanceof EntityPlayer) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
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);
}

}
}
for (final Object c : this.crafters) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
}
}

@Override
protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayer && this.monitor != null) {
SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate(true);
if (Platform.isServer() && c instanceof EntityPlayerMP && this.monitor != null) {
final IItemList<IAEFluidStack> monitorCache = this.monitor.getStorageList();

for (final IAEFluidStack send : monitorCache) {
SPacketMEFluidInvUpdate packet = new SPacketMEFluidInvUpdate();
for (final IAEFluidStack is : monitorCache) {
try {
piu.appendFluid(send);
} catch (final BufferOverflowException boe) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
piu = new SPacketMEInventoryUpdate(true);
piu.appendFluid(send);
packet.appendFluid(is);
} catch (BufferOverflowException e) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
packet = new SPacketMEFluidInvUpdate();
packet.appendFluid(is);
}
}
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.gui.container.base.FCContainerMonitor;
import com.glodblock.github.network.SPacketMEInventoryUpdate;
import com.glodblock.github.network.SPacketMEItemInvUpdate;

public class ContainerItemMonitor extends FCContainerMonitor<IAEItemStack> {

Expand Down Expand Up @@ -95,46 +95,55 @@ protected ContainerItemMonitor(final InventoryPlayer ip, final ITerminalHost mon
protected void processItemList() {
if (!this.items.isEmpty()) {
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();
final SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate();
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
for (final IAEItemStack is : this.items) {
final IAEItemStack send = monitorCache.findPrecise(is);
if (send == null) {
is.setStackSize(0);
piu.appendItem(is);
} else {
piu.appendItem(send);
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 (!piu.isEmpty()) {
this.items.resetStatus();

for (final Object c : this.crafters) {
if (c instanceof EntityPlayer) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
}
for (final Object c : this.crafters) {
if (c instanceof EntityPlayerMP) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
}
this.items.resetStatus();
}
}

@Override
protected void queueInventory(final ICrafting c) {
if (Platform.isServer() && c instanceof EntityPlayer && this.monitor != null) {
SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate();
final IItemList<IAEItemStack> monitorCache = this.monitor.getStorageList();

for (final IAEItemStack send : monitorCache) {
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
for (final IAEItemStack is : monitorCache) {
try {
piu.appendItem(send);
} catch (final BufferOverflowException boe) {
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);

piu = new SPacketMEInventoryUpdate();
piu.appendItem(send);
packet.appendItem(is);
} catch (BufferOverflowException e) {
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
packet = new SPacketMEItemInvUpdate();
packet.appendItem(is);
}
}
FluidCraft.proxy.netHandler.sendTo(piu, (EntityPlayerMP) c);
FluidCraft.proxy.netHandler.sendTo(packet, (EntityPlayerMP) c);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/glodblock/github/common/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Config {
public static boolean removeRecipe;
public static boolean blacklistEssentiaGas;
public static double portableCellBattery;
public static int packetSize;

public static void run() {
loadCategory();
Expand Down Expand Up @@ -51,7 +52,9 @@ private static void loadProperty() {
"Blacklist Essentia Gas from Thaumic Energistics, so they won't be stored in Fluid Storage Cells.");
portableCellBattery = Config.get("Fluid Craft for AE2", "Portable Fluid Cell Battery Capacity", 20000D)
.getDouble();

packetSize = Config.get("Fluid Craft for AE2", "packetSize", 256, "Number of items to be sent per packet")
.getInt();
if (packetSize <= 0) packetSize = 256;
if (Config.hasChanged()) Config.save();
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/glodblock/github/loader/ChannelLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public void run() {
Side.SERVER);
FluidCraft.proxy.netHandler
.registerMessage(new CPacketEncodePattern.Handler(), CPacketEncodePattern.class, id++, Side.SERVER);
FluidCraft.proxy.netHandler
.registerMessage(new SPacketMEItemInvUpdate.Handler(), SPacketMEItemInvUpdate.class, id++, Side.CLIENT);
FluidCraft.proxy.netHandler.registerMessage(
new SPacketMEInventoryUpdate.Handler(),
SPacketMEInventoryUpdate.class,
new SPacketMEFluidInvUpdate.Handler(),
SPacketMEFluidInvUpdate.class,
id++,
Side.CLIENT);
FluidCraft.proxy.netHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,27 @@ public void toBytes(ByteBuf buf) {
public static class Handler implements IMessageHandler<CPacketLevelMaintainer, IMessage> {

private void refresh(ContainerLevelMaintainer cca, EntityPlayerMP player) {
SPacketMEInventoryUpdate piu = new SPacketMEInventoryUpdate(false);
SPacketMEItemInvUpdate packet = new SPacketMEItemInvUpdate();
for (int i = 0; i < TileLevelMaintainer.REQ_COUNT; i++) {
IAEItemStack is = cca.getTile().requests.getRequestQtyStack(i);
IAEItemStack is1 = cca.getTile().requests.getRequestBatches().getStack(i);
if (is != null) {
if (is1 != null) {
NBTTagCompound data;
data = is1.getItemStack().getTagCompound();
piu.appendItem(
packet.appendItem(
setTag(
is,
is1.getStackSize(),
i,
data.getBoolean("Enable"),
cca.getTile().requests.getState(i).ordinal()));
} else {
piu.appendItem(setTag(is, 0, i, true, 0));
packet.appendItem(setTag(is, 0, i, true, 0));
}
}
}
FluidCraft.proxy.netHandler.sendTo(piu, player);
FluidCraft.proxy.netHandler.sendTo(packet, player);
}

@Nullable
Expand Down
Loading

0 comments on commit 9c2ef02

Please sign in to comment.