From 5eead9a1ee0c40f1b2096a62edff7996ca8715bd Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Sun, 30 Jan 2022 02:02:16 +0100 Subject: [PATCH] Prototype commit #3 --- src/main/java/li/cil/oc2/common/Config.java | 4 +- .../common/blockentity/VxlanBlockEntity.java | 6 +++ .../java/li/cil/oc2/common/item/Items.java | 1 + .../cil/oc2/common/vxlan/TunnelManager.java | 49 ++++++++++++------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/li/cil/oc2/common/Config.java b/src/main/java/li/cil/oc2/common/Config.java index a243ffde..32f178e8 100644 --- a/src/main/java/li/cil/oc2/common/Config.java +++ b/src/main/java/li/cil/oc2/common/Config.java @@ -47,9 +47,9 @@ public final class Config { @Path("vxlan") public static boolean enable = false; @Path("vxlan") public static String remoteHost = "::1"; - @Path("vxlan") public static short remotePort = 4789; + @Path("vxlan") public static int remotePort = 4789; @Path("vxlan") public static String bindHost = "::1"; - @Path("vxlan") public static short bindPort = 4789; + @Path("vxlan") public static int bindPort = 4789; public static boolean computersUseEnergy() { return computerEnergyPerTick > 0 && computerEnergyStorage > 0; diff --git a/src/main/java/li/cil/oc2/common/blockentity/VxlanBlockEntity.java b/src/main/java/li/cil/oc2/common/blockentity/VxlanBlockEntity.java index 2ea8fba6..fc350339 100644 --- a/src/main/java/li/cil/oc2/common/blockentity/VxlanBlockEntity.java +++ b/src/main/java/li/cil/oc2/common/blockentity/VxlanBlockEntity.java @@ -63,6 +63,12 @@ public final class VxlanBlockEntity extends ModBlockEntity implements NetworkInt vti = tag.getInt("vti"); } + @Override + public void saveAdditional(CompoundTag tag) { + super.saveAdditional(tag); + tag.putInt("vti", vti); + } + @Override protected void onUnload(final boolean isRemove) { adjacentBlockInterfaces[0] = null; diff --git a/src/main/java/li/cil/oc2/common/item/Items.java b/src/main/java/li/cil/oc2/common/item/Items.java index 09a630d8..97a19376 100644 --- a/src/main/java/li/cil/oc2/common/item/Items.java +++ b/src/main/java/li/cil/oc2/common/item/Items.java @@ -33,6 +33,7 @@ public final class Items { public static final RegistryObject NETWORK_HUB = register(Blocks.NETWORK_HUB); public static final RegistryObject PROJECTOR = register(Blocks.PROJECTOR); public static final RegistryObject REDSTONE_INTERFACE = register(Blocks.REDSTONE_INTERFACE); + public static final RegistryObject VXLAN_HUB = register(Blocks.VXLAN_HUB); /////////////////////////////////////////////////////////////////// diff --git a/src/main/java/li/cil/oc2/common/vxlan/TunnelManager.java b/src/main/java/li/cil/oc2/common/vxlan/TunnelManager.java index 88fbb3ec..7c7dc375 100644 --- a/src/main/java/li/cil/oc2/common/vxlan/TunnelManager.java +++ b/src/main/java/li/cil/oc2/common/vxlan/TunnelManager.java @@ -23,21 +23,26 @@ public class TunnelManager { this.remotePort = remotePort; this.bindHost = bindHost; this.bindPort = bindPort; - socket = new DatagramSocket(bindPort, bindHost); - socket.connect(remoteHost, remotePort); + if (Config.enable) { + socket = new DatagramSocket(bindPort, bindHost); + socket.connect(remoteHost, remotePort); + } else { + socket = null; + } } public static void initialize() { - if (Config.enable) { - try { - INSTANCE = new TunnelManager( - InetAddress.getByName(Config.bindHost), Config.bindPort, - InetAddress.getByName(Config.remoteHost), Config.remotePort - ); - } catch (SocketException | UnknownHostException e) { - e.printStackTrace(); - } + try { + INSTANCE = new TunnelManager( + InetAddress.getByName(Config.bindHost), (short) Config.bindPort, + InetAddress.getByName(Config.remoteHost), (short) Config.remotePort + ); + } catch (SocketException | UnknownHostException e) { + e.printStackTrace(); + } + + if (Config.enable) { new Thread(() -> INSTANCE.listen()).start(); } } @@ -81,16 +86,24 @@ public class TunnelManager { } public void sendToVti(int vti, byte[] payload) { - byte[] buffer = new byte[payload.length + 8]; + if (socket != null) { + byte[] buffer = new byte[payload.length + 8]; - System.arraycopy(payload, 0, buffer, 8, payload.length); + System.arraycopy(payload, 0, buffer, 8, payload.length); - buffer[0] = 0x08; - buffer[4] = (byte) ((vti >> 16) & 0xff); - buffer[5] = (byte) ((vti >> 8) & 0xff); - buffer[6] = (byte) (vti & 0xff); + buffer[0] = 0x08; + buffer[4] = (byte) ((vti >> 16) & 0xff); + buffer[5] = (byte) ((vti >> 8) & 0xff); + buffer[6] = (byte) (vti & 0xff); - DatagramPacket packet = new DatagramPacket(buffer, buffer.length, this.remoteHost, this.remotePort); + DatagramPacket packet = new DatagramPacket(buffer, buffer.length, this.remoteHost, this.remotePort); + + try { + socket.send(packet); + } catch (IOException e) { + e.printStackTrace(); + } + } } public NetworkInterface registerVti(int vti, NetworkInterface iface) {