Prototype commit #3

This commit is contained in:
Nadja Reitzenstein
2022-01-30 02:02:16 +01:00
committed by Kilobyte22
parent 6b9b51f6c1
commit 5eead9a1ee
4 changed files with 40 additions and 20 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -33,6 +33,7 @@ public final class Items {
public static final RegistryObject<Item> NETWORK_HUB = register(Blocks.NETWORK_HUB);
public static final RegistryObject<Item> PROJECTOR = register(Blocks.PROJECTOR);
public static final RegistryObject<Item> REDSTONE_INTERFACE = register(Blocks.REDSTONE_INTERFACE);
public static final RegistryObject<Item> VXLAN_HUB = register(Blocks.VXLAN_HUB);
///////////////////////////////////////////////////////////////////

View File

@@ -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) {