Add config for default ethernet frame TLL. Closes #101.

This commit is contained in:
Florian Nücke
2022-02-13 21:11:23 +01:00
parent 72affc693c
commit 516bd6ca73
3 changed files with 25 additions and 2 deletions

View File

@@ -42,6 +42,8 @@ public final class Config {
@Path("admin") public static UUID fakePlayerUUID = UUID.fromString("e39dd9a7-514f-4a2d-aa5e-b6030621416d");
@Path("admin.network") public static int projectorAverageMaxBytesPerSecond = 160 * 1024;
@Path("admin.virtual_network") public static int ethernetFrameTimeToLive = 12;
@Path("admin.virtual_network") public static int hubEthernetFramesPerTick = 32;
public static boolean computersUseEnergy() {
return computerEnergyPerTick > 0 && computerEnergyStorage > 0;

View File

@@ -4,6 +4,7 @@ package li.cil.oc2.common.blockentity;
import li.cil.oc2.api.capabilities.NetworkInterface;
import li.cil.oc2.client.renderer.NetworkCableRenderer;
import li.cil.oc2.common.Config;
import li.cil.oc2.common.block.NetworkConnectorBlock;
import li.cil.oc2.common.capabilities.Capabilities;
import li.cil.oc2.common.item.Items;
@@ -53,7 +54,6 @@ public final class NetworkConnectorBlockEntity extends ModBlockEntity implements
private static final int RETRY_UNLOADED_CHUNK_INTERVAL = TickUtils.toTicks(Duration.ofSeconds(5));
private static final int MAX_CONNECTION_COUNT = 2;
private static final int MAX_CONNECTION_DISTANCE = 16;
private static final int INITIAL_PACKET_TIME_TO_LIVE = 12;
private static final int BYTES_PER_TICK = 64 * 1024 / TickUtils.toTicks(Duration.ofSeconds(1)); // bytes / sec -> bytes / tick
private static final int MIN_ETHERNET_FRAME_SIZE = 42;
private static final int TTL_COST = 1;
@@ -197,7 +197,7 @@ public final class NetworkConnectorBlockEntity extends ModBlockEntity implements
byte[] frame;
while ((frame = source.readEthernetFrame()) != null && byteBudget > 0) {
byteBudget -= Math.max(frame.length, MIN_ETHERNET_FRAME_SIZE); // Avoid bogus packets messing with us.
networkInterface.writeEthernetFrame(source, frame, INITIAL_PACKET_TIME_TO_LIVE);
networkInterface.writeEthernetFrame(source, frame, Config.ethernetFrameTimeToLive);
}
}

View File

@@ -3,6 +3,7 @@
package li.cil.oc2.common.blockentity;
import li.cil.oc2.api.capabilities.NetworkInterface;
import li.cil.oc2.common.Config;
import li.cil.oc2.common.Constants;
import li.cil.oc2.common.capabilities.Capabilities;
import li.cil.oc2.common.util.LazyOptionalUtils;
@@ -21,6 +22,9 @@ import java.util.stream.Stream;
public final class NetworkHubBlockEntity extends ModBlockEntity implements NetworkInterface {
private static final int TTL_COST = 1;
private int frameCount;
private long lastGameTime;
///////////////////////////////////////////////////////////////////
private final NetworkInterface[] adjacentBlockInterfaces = new NetworkInterface[Constants.BLOCK_FACE_COUNT];
@@ -45,6 +49,23 @@ public final class NetworkHubBlockEntity extends ModBlockEntity implements Netwo
@Override
public void writeEthernetFrame(final NetworkInterface source, final byte[] frame, final int timeToLive) {
if (level == null) {
return;
}
// Give a cap on top of the TLL, just in case trolls intentionally build
// loops that exponentially multiply ethernet frames after people crank up
// the default TTL.
final long gameTime = level.getGameTime();
if (gameTime > lastGameTime) {
lastGameTime = gameTime;
frameCount = 1;
} else if (frameCount > Config.hubEthernetFramesPerTick) {
return;
} else {
frameCount++;
}
getAdjacentInterfaces().forEach(adjacentInterface -> {
if (adjacentInterface != source) {
adjacentInterface.writeEthernetFrame(this, frame, timeToLive - TTL_COST);