diff --git a/android/build.gradle b/android/build.gradle index 75e513de2f..0f61c32912 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -26,7 +26,6 @@ repositories{ dependencies{ implementation project(":core") - implementation project(":net") implementation arcModule("backends:backend-android") natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi" diff --git a/android/src/io/anuke/mindustry/AndroidLauncher.java b/android/src/io/anuke/mindustry/AndroidLauncher.java index 7198a9d39b..53f7e79df8 100644 --- a/android/src/io/anuke/mindustry/AndroidLauncher.java +++ b/android/src/io/anuke/mindustry/AndroidLauncher.java @@ -17,8 +17,6 @@ import io.anuke.arc.util.*; import io.anuke.arc.util.serialization.*; import io.anuke.mindustry.game.Saves.*; import io.anuke.mindustry.io.*; -import io.anuke.mindustry.net.*; -import io.anuke.mindustry.net.Net.*; import io.anuke.mindustry.ui.dialogs.*; import java.io.*; @@ -41,11 +39,6 @@ public class AndroidLauncher extends AndroidApplication{ initialize(new ClientLauncher(){ - @Override - public NetProvider getNet(){ - return new ArcNetImpl(); - } - @Override public void hide(){ moveTaskToBack(true); diff --git a/build.gradle b/build.gradle index 16baf53f6f..427025f4ef 100644 --- a/build.gradle +++ b/build.gradle @@ -151,7 +151,6 @@ project(":desktop"){ dependencies{ compile project(":core") - compile project(":net") if(debugged()) compile project(":debug") @@ -189,7 +188,6 @@ project(":ios"){ dependencies{ compile project(":core") - compile project(":net") compileOnly project(":annotations") compile arcModule("backends:backend-robovm") @@ -254,7 +252,6 @@ project(":server"){ dependencies{ compile project(":core") - compile project(":net") compile arcModule("backends:backend-headless") } } @@ -299,14 +296,6 @@ project(":annotations"){ } } -project(":net"){ - apply plugin: "java" - - dependencies{ - compile project(":core") - } -} - task deployAll{ task cleanDeployOutput{ doFirst{ diff --git a/core/src/io/anuke/mindustry/core/Platform.java b/core/src/io/anuke/mindustry/core/Platform.java index a54fccf3c8..71ac3ba836 100644 --- a/core/src/io/anuke/mindustry/core/Platform.java +++ b/core/src/io/anuke/mindustry/core/Platform.java @@ -7,6 +7,7 @@ import io.anuke.arc.function.*; import io.anuke.arc.math.*; import io.anuke.arc.scene.ui.*; import io.anuke.arc.util.serialization.*; +import io.anuke.mindustry.net.*; import io.anuke.mindustry.net.Net.*; import io.anuke.mindustry.ui.dialogs.*; @@ -15,7 +16,9 @@ import static io.anuke.mindustry.Vars.mobile; public interface Platform{ /** Get the networking implementation.*/ - NetProvider getNet(); + default NetProvider getNet(){ + return new ArcNetImpl(); + } /** Steam: Update lobby visibility.*/ default void updateLobby(){ diff --git a/net/src/io/anuke/mindustry/net/ArcNetImpl.java b/core/src/io/anuke/mindustry/net/ArcNetImpl.java similarity index 79% rename from net/src/io/anuke/mindustry/net/ArcNetImpl.java rename to core/src/io/anuke/mindustry/net/ArcNetImpl.java index db0edd6fa2..1c924b60c6 100644 --- a/net/src/io/anuke/mindustry/net/ArcNetImpl.java +++ b/core/src/io/anuke/mindustry/net/ArcNetImpl.java @@ -4,6 +4,7 @@ import io.anuke.arc.*; import io.anuke.arc.collection.*; import io.anuke.arc.function.*; import io.anuke.arc.net.*; +import io.anuke.arc.net.FrameworkMessage.*; import io.anuke.arc.util.*; import io.anuke.arc.util.async.*; import io.anuke.arc.util.pooling.*; @@ -271,6 +272,16 @@ public class ArcNetImpl implements NetProvider{ return null; } + private void handleException(Exception e){ + if(e instanceof ArcNetException){ + Core.app.post(() -> net.showError(new IOException("mismatch"))); + }else if(e instanceof ClosedChannelException){ + Core.app.post(() -> net.showError(new IOException("alreadyconnected"))); + }else{ + Core.app.post(() -> net.showError(e)); + } + } + class ArcConnection extends NetConnection{ public final Connection connection; @@ -333,13 +344,81 @@ public class ArcNetImpl implements NetProvider{ } } - private void handleException(Exception e){ - if(e instanceof ArcNetException){ - Core.app.post(() -> net.showError(new IOException("mismatch"))); - }else if(e instanceof ClosedChannelException){ - Core.app.post(() -> net.showError(new IOException("alreadyconnected"))); - }else{ - Core.app.post(() -> net.showError(e)); + @SuppressWarnings("unchecked") + public static class PacketSerializer implements NetSerializer{ + + @Override + public void write(ByteBuffer byteBuffer, Object o){ + if(o instanceof FrameworkMessage){ + byteBuffer.put((byte)-2); //code for framework message + writeFramework(byteBuffer, (FrameworkMessage)o); + }else{ + if(!(o instanceof Packet)) + throw new RuntimeException("All sent objects must implement be Packets! Class: " + o.getClass()); + byte id = Registrator.getID(o.getClass()); + if(id == -1) + throw new RuntimeException("Unregistered class: " + o.getClass()); + byteBuffer.put(id); + ((Packet)o).write(byteBuffer); + } + } + + @Override + public Object read(ByteBuffer byteBuffer){ + byte id = byteBuffer.get(); + if(id == -2){ + return readFramework(byteBuffer); + }else{ + Packet packet = Pools.obtain((Class)Registrator.getByID(id).type, (Supplier)Registrator.getByID(id).constructor); + packet.read(byteBuffer); + return packet; + } + } + + public void writeFramework(ByteBuffer buffer, FrameworkMessage message){ + if(message instanceof Ping){ + Ping p = (Ping)message; + buffer.put((byte)0); + buffer.putInt(p.id); + buffer.put(p.isReply ? 1 : (byte)0); + }else if(message instanceof DiscoverHost){ + buffer.put((byte)1); + }else if(message instanceof KeepAlive){ + buffer.put((byte)2); + }else if(message instanceof RegisterUDP){ + RegisterUDP p = (RegisterUDP)message; + buffer.put((byte)3); + buffer.putInt(p.connectionID); + }else if(message instanceof RegisterTCP){ + RegisterTCP p = (RegisterTCP)message; + buffer.put((byte)4); + buffer.putInt(p.connectionID); + } + } + + public FrameworkMessage readFramework(ByteBuffer buffer){ + byte id = buffer.get(); + + if(id == 0){ + Ping p = new Ping(); + p.id = buffer.getInt(); + p.isReply = buffer.get() == 1; + return p; + }else if(id == 1){ + return new DiscoverHost(); + }else if(id == 2){ + return new KeepAlive(); + }else if(id == 3){ + RegisterUDP p = new RegisterUDP(); + p.connectionID = buffer.getInt(); + return p; + }else if(id == 4){ + RegisterTCP p = new RegisterTCP(); + p.connectionID = buffer.getInt(); + return p; + }else{ + throw new RuntimeException("Unknown framework message!"); + } } } diff --git a/core/src/io/anuke/mindustry/net/Net.java b/core/src/io/anuke/mindustry/net/Net.java index 6d23d5f7f4..b4d9ec152c 100644 --- a/core/src/io/anuke/mindustry/net/Net.java +++ b/core/src/io/anuke/mindustry/net/Net.java @@ -297,7 +297,7 @@ public class Net{ tcp, udp } - /** Client implementation. */ + /** Networking implementation. */ public interface NetProvider{ /** Connect to a server. */ void connectClient(String ip, int port, Runnable success) throws IOException; diff --git a/core/src/io/anuke/mindustry/net/NetConnection.java b/core/src/io/anuke/mindustry/net/NetConnection.java index 36bde0afbb..66d3c4b25a 100644 --- a/core/src/io/anuke/mindustry/net/NetConnection.java +++ b/core/src/io/anuke/mindustry/net/NetConnection.java @@ -25,7 +25,6 @@ public abstract class NetConnection{ public boolean hasConnected, hasBegunConnecting; public float viewWidth, viewHeight, viewX, viewY; - /** Assigns this connection a unique ID. No two connections will ever have the same ID.*/ public NetConnection(String address){ this.address = address; } diff --git a/desktop/src/io/anuke/mindustry/desktop/steam/SteamCoreNetImpl.java b/desktop/src/io/anuke/mindustry/desktop/steam/SteamCoreNetImpl.java index 82f6b9cbb3..05453c7635 100644 --- a/desktop/src/io/anuke/mindustry/desktop/steam/SteamCoreNetImpl.java +++ b/desktop/src/io/anuke/mindustry/desktop/steam/SteamCoreNetImpl.java @@ -13,6 +13,7 @@ import io.anuke.mindustry.game.EventType.*; import io.anuke.mindustry.game.Version; import io.anuke.mindustry.game.*; import io.anuke.mindustry.net.*; +import io.anuke.mindustry.net.ArcNetImpl.*; import io.anuke.mindustry.net.Net.*; import io.anuke.mindustry.net.Packets.*; diff --git a/ios/src/io/anuke/mindustry/IOSLauncher.java b/ios/src/io/anuke/mindustry/IOSLauncher.java index c860cb09b4..11f3a57375 100644 --- a/ios/src/io/anuke/mindustry/IOSLauncher.java +++ b/ios/src/io/anuke/mindustry/IOSLauncher.java @@ -10,8 +10,6 @@ import io.anuke.arc.util.io.*; import io.anuke.mindustry.game.EventType.*; import io.anuke.mindustry.game.Saves.*; import io.anuke.mindustry.io.*; -import io.anuke.mindustry.net.*; -import io.anuke.mindustry.net.Net.*; import org.robovm.apple.foundation.*; import org.robovm.apple.uikit.*; import org.robovm.objc.block.*; @@ -38,11 +36,6 @@ public class IOSLauncher extends IOSApplication.Delegate{ IOSApplicationConfiguration config = new IOSApplicationConfiguration(); return new IOSApplication(new ClientLauncher(){ - @Override - public NetProvider getNet(){ - return new ArcNetImpl(); - } - @Override public void showFileChooser(boolean open, String extension, Consumer cons){ UIDocumentBrowserViewController cont = new UIDocumentBrowserViewController(NSArray.fromStrings("public.archive")); diff --git a/net/build.gradle b/net/build.gradle deleted file mode 100644 index 57fced1dd2..0000000000 --- a/net/build.gradle +++ /dev/null @@ -1,4 +0,0 @@ -apply plugin: "java" - -sourceCompatibility = 1.8 -sourceSets.main.java.srcDirs = ["src/"] diff --git a/net/src/io/anuke/mindustry/net/PacketSerializer.java b/net/src/io/anuke/mindustry/net/PacketSerializer.java deleted file mode 100644 index ae24e240e3..0000000000 --- a/net/src/io/anuke/mindustry/net/PacketSerializer.java +++ /dev/null @@ -1,86 +0,0 @@ -package io.anuke.mindustry.net; - -import io.anuke.arc.function.*; -import io.anuke.arc.net.*; -import io.anuke.arc.net.FrameworkMessage.*; -import io.anuke.arc.util.pooling.*; - -import java.nio.*; - -@SuppressWarnings("unchecked") -public class PacketSerializer implements NetSerializer{ - - @Override - public void write(ByteBuffer byteBuffer, Object o){ - if(o instanceof FrameworkMessage){ - byteBuffer.put((byte)-2); //code for framework message - writeFramework(byteBuffer, (FrameworkMessage)o); - }else{ - if(!(o instanceof Packet)) - throw new RuntimeException("All sent objects must implement be Packets! Class: " + o.getClass()); - byte id = Registrator.getID(o.getClass()); - if(id == -1) - throw new RuntimeException("Unregistered class: " + o.getClass()); - byteBuffer.put(id); - ((Packet)o).write(byteBuffer); - } - } - - @Override - public Object read(ByteBuffer byteBuffer){ - byte id = byteBuffer.get(); - if(id == -2){ - return readFramework(byteBuffer); - }else{ - Packet packet = Pools.obtain((Class)Registrator.getByID(id).type, (Supplier)Registrator.getByID(id).constructor); - packet.read(byteBuffer); - return packet; - } - } - - public static void writeFramework(ByteBuffer buffer, FrameworkMessage message){ - if(message instanceof Ping){ - Ping p = (Ping)message; - buffer.put((byte)0); - buffer.putInt(p.id); - buffer.put(p.isReply ? 1 : (byte)0); - }else if(message instanceof DiscoverHost){ - buffer.put((byte)1); - }else if(message instanceof KeepAlive){ - buffer.put((byte)2); - }else if(message instanceof RegisterUDP){ - RegisterUDP p = (RegisterUDP)message; - buffer.put((byte)3); - buffer.putInt(p.connectionID); - }else if(message instanceof RegisterTCP){ - RegisterTCP p = (RegisterTCP)message; - buffer.put((byte)4); - buffer.putInt(p.connectionID); - } - } - - public static FrameworkMessage readFramework(ByteBuffer buffer){ - byte id = buffer.get(); - - if(id == 0){ - Ping p = new Ping(); - p.id = buffer.getInt(); - p.isReply = buffer.get() == 1; - return p; - }else if(id == 1){ - return new DiscoverHost(); - }else if(id == 2){ - return new KeepAlive(); - }else if(id == 3){ - RegisterUDP p = new RegisterUDP(); - p.connectionID = buffer.getInt(); - return p; - }else if(id == 4){ - RegisterTCP p = new RegisterTCP(); - p.connectionID = buffer.getInt(); - return p; - }else{ - throw new RuntimeException("Unknown framework message!"); - } - } -} diff --git a/server/src/io/anuke/mindustry/server/ServerLauncher.java b/server/src/io/anuke/mindustry/server/ServerLauncher.java index 17ea3027ff..fcf6a52077 100644 --- a/server/src/io/anuke/mindustry/server/ServerLauncher.java +++ b/server/src/io/anuke/mindustry/server/ServerLauncher.java @@ -3,6 +3,7 @@ package io.anuke.mindustry.server; import io.anuke.arc.backends.headless.*; import io.anuke.mindustry.*; +import io.anuke.mindustry.core.*; import io.anuke.mindustry.net.*; import static io.anuke.mindustry.Vars.platform; @@ -11,7 +12,7 @@ public class ServerLauncher{ public static void main(String[] args){ try{ - Vars.platform = ArcNetImpl::new; + Vars.platform = new Platform(){}; Vars.net = new Net(platform.getNet()); new HeadlessApplication(new MindustryServer(args), null, throwable -> CrashSender.send(throwable, f -> {})); }catch(Throwable t){ diff --git a/settings.gradle b/settings.gradle index 0d91e99761..e81f9cf42c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ -include 'desktop', 'core', 'net', 'server', 'ios', 'annotations', 'tools', 'tests' +include 'desktop', 'core', 'server', 'ios', 'annotations', 'tools', 'tests' def use = { String name -> include(name)