Skip to content

Commit

Permalink
Prototype commit fnuecke#3
Browse files Browse the repository at this point in the history
  • Loading branch information
dequbed authored and Kilobyte22 committed Feb 16, 2022
1 parent 6b9b51f commit 5eead9a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/main/java/li/cil/oc2/common/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public void load(CompoundTag tag) {
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;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/li/cil/oc2/common/item/Items.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

///////////////////////////////////////////////////////////////////

Expand Down
49 changes: 31 additions & 18 deletions src/main/java/li/cil/oc2/common/vxlan/TunnelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ public TunnelManager(InetAddress bindHost, short bindPort, InetAddress remoteHos
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();
}
}
Expand Down Expand Up @@ -81,16 +86,24 @@ public static TunnelManager instance() {
}

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) {
Expand Down

0 comments on commit 5eead9a

Please sign in to comment.