Merged net module
This commit is contained in:
@@ -26,7 +26,6 @@ repositories{
|
|||||||
|
|
||||||
dependencies{
|
dependencies{
|
||||||
implementation project(":core")
|
implementation project(":core")
|
||||||
implementation project(":net")
|
|
||||||
|
|
||||||
implementation arcModule("backends:backend-android")
|
implementation arcModule("backends:backend-android")
|
||||||
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
|
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ import io.anuke.arc.util.*;
|
|||||||
import io.anuke.arc.util.serialization.*;
|
import io.anuke.arc.util.serialization.*;
|
||||||
import io.anuke.mindustry.game.Saves.*;
|
import io.anuke.mindustry.game.Saves.*;
|
||||||
import io.anuke.mindustry.io.*;
|
import io.anuke.mindustry.io.*;
|
||||||
import io.anuke.mindustry.net.*;
|
|
||||||
import io.anuke.mindustry.net.Net.*;
|
|
||||||
import io.anuke.mindustry.ui.dialogs.*;
|
import io.anuke.mindustry.ui.dialogs.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -41,11 +39,6 @@ public class AndroidLauncher extends AndroidApplication{
|
|||||||
|
|
||||||
initialize(new ClientLauncher(){
|
initialize(new ClientLauncher(){
|
||||||
|
|
||||||
@Override
|
|
||||||
public NetProvider getNet(){
|
|
||||||
return new ArcNetImpl();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void hide(){
|
public void hide(){
|
||||||
moveTaskToBack(true);
|
moveTaskToBack(true);
|
||||||
|
|||||||
11
build.gradle
11
build.gradle
@@ -151,7 +151,6 @@ project(":desktop"){
|
|||||||
|
|
||||||
dependencies{
|
dependencies{
|
||||||
compile project(":core")
|
compile project(":core")
|
||||||
compile project(":net")
|
|
||||||
|
|
||||||
if(debugged()) compile project(":debug")
|
if(debugged()) compile project(":debug")
|
||||||
|
|
||||||
@@ -189,7 +188,6 @@ project(":ios"){
|
|||||||
|
|
||||||
dependencies{
|
dependencies{
|
||||||
compile project(":core")
|
compile project(":core")
|
||||||
compile project(":net")
|
|
||||||
compileOnly project(":annotations")
|
compileOnly project(":annotations")
|
||||||
|
|
||||||
compile arcModule("backends:backend-robovm")
|
compile arcModule("backends:backend-robovm")
|
||||||
@@ -254,7 +252,6 @@ project(":server"){
|
|||||||
|
|
||||||
dependencies{
|
dependencies{
|
||||||
compile project(":core")
|
compile project(":core")
|
||||||
compile project(":net")
|
|
||||||
compile arcModule("backends:backend-headless")
|
compile arcModule("backends:backend-headless")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -299,14 +296,6 @@ project(":annotations"){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
project(":net"){
|
|
||||||
apply plugin: "java"
|
|
||||||
|
|
||||||
dependencies{
|
|
||||||
compile project(":core")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
task deployAll{
|
task deployAll{
|
||||||
task cleanDeployOutput{
|
task cleanDeployOutput{
|
||||||
doFirst{
|
doFirst{
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import io.anuke.arc.function.*;
|
|||||||
import io.anuke.arc.math.*;
|
import io.anuke.arc.math.*;
|
||||||
import io.anuke.arc.scene.ui.*;
|
import io.anuke.arc.scene.ui.*;
|
||||||
import io.anuke.arc.util.serialization.*;
|
import io.anuke.arc.util.serialization.*;
|
||||||
|
import io.anuke.mindustry.net.*;
|
||||||
import io.anuke.mindustry.net.Net.*;
|
import io.anuke.mindustry.net.Net.*;
|
||||||
import io.anuke.mindustry.ui.dialogs.*;
|
import io.anuke.mindustry.ui.dialogs.*;
|
||||||
|
|
||||||
@@ -15,7 +16,9 @@ import static io.anuke.mindustry.Vars.mobile;
|
|||||||
public interface Platform{
|
public interface Platform{
|
||||||
|
|
||||||
/** Get the networking implementation.*/
|
/** Get the networking implementation.*/
|
||||||
NetProvider getNet();
|
default NetProvider getNet(){
|
||||||
|
return new ArcNetImpl();
|
||||||
|
}
|
||||||
|
|
||||||
/** Steam: Update lobby visibility.*/
|
/** Steam: Update lobby visibility.*/
|
||||||
default void updateLobby(){
|
default void updateLobby(){
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import io.anuke.arc.*;
|
|||||||
import io.anuke.arc.collection.*;
|
import io.anuke.arc.collection.*;
|
||||||
import io.anuke.arc.function.*;
|
import io.anuke.arc.function.*;
|
||||||
import io.anuke.arc.net.*;
|
import io.anuke.arc.net.*;
|
||||||
|
import io.anuke.arc.net.FrameworkMessage.*;
|
||||||
import io.anuke.arc.util.*;
|
import io.anuke.arc.util.*;
|
||||||
import io.anuke.arc.util.async.*;
|
import io.anuke.arc.util.async.*;
|
||||||
import io.anuke.arc.util.pooling.*;
|
import io.anuke.arc.util.pooling.*;
|
||||||
@@ -271,6 +272,16 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
return null;
|
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{
|
class ArcConnection extends NetConnection{
|
||||||
public final Connection connection;
|
public final Connection connection;
|
||||||
|
|
||||||
@@ -333,13 +344,81 @@ public class ArcNetImpl implements NetProvider{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleException(Exception e){
|
@SuppressWarnings("unchecked")
|
||||||
if(e instanceof ArcNetException){
|
public static class PacketSerializer implements NetSerializer{
|
||||||
Core.app.post(() -> net.showError(new IOException("mismatch")));
|
|
||||||
}else if(e instanceof ClosedChannelException){
|
@Override
|
||||||
Core.app.post(() -> net.showError(new IOException("alreadyconnected")));
|
public void write(ByteBuffer byteBuffer, Object o){
|
||||||
}else{
|
if(o instanceof FrameworkMessage){
|
||||||
Core.app.post(() -> net.showError(e));
|
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<Packet>)Registrator.getByID(id).type, (Supplier<Packet>)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!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,7 +297,7 @@ public class Net{
|
|||||||
tcp, udp
|
tcp, udp
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Client implementation. */
|
/** Networking implementation. */
|
||||||
public interface NetProvider{
|
public interface NetProvider{
|
||||||
/** Connect to a server. */
|
/** Connect to a server. */
|
||||||
void connectClient(String ip, int port, Runnable success) throws IOException;
|
void connectClient(String ip, int port, Runnable success) throws IOException;
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ public abstract class NetConnection{
|
|||||||
public boolean hasConnected, hasBegunConnecting;
|
public boolean hasConnected, hasBegunConnecting;
|
||||||
public float viewWidth, viewHeight, viewX, viewY;
|
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){
|
public NetConnection(String address){
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import io.anuke.mindustry.game.EventType.*;
|
|||||||
import io.anuke.mindustry.game.Version;
|
import io.anuke.mindustry.game.Version;
|
||||||
import io.anuke.mindustry.game.*;
|
import io.anuke.mindustry.game.*;
|
||||||
import io.anuke.mindustry.net.*;
|
import io.anuke.mindustry.net.*;
|
||||||
|
import io.anuke.mindustry.net.ArcNetImpl.*;
|
||||||
import io.anuke.mindustry.net.Net.*;
|
import io.anuke.mindustry.net.Net.*;
|
||||||
import io.anuke.mindustry.net.Packets.*;
|
import io.anuke.mindustry.net.Packets.*;
|
||||||
|
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import io.anuke.arc.util.io.*;
|
|||||||
import io.anuke.mindustry.game.EventType.*;
|
import io.anuke.mindustry.game.EventType.*;
|
||||||
import io.anuke.mindustry.game.Saves.*;
|
import io.anuke.mindustry.game.Saves.*;
|
||||||
import io.anuke.mindustry.io.*;
|
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.foundation.*;
|
||||||
import org.robovm.apple.uikit.*;
|
import org.robovm.apple.uikit.*;
|
||||||
import org.robovm.objc.block.*;
|
import org.robovm.objc.block.*;
|
||||||
@@ -38,11 +36,6 @@ public class IOSLauncher extends IOSApplication.Delegate{
|
|||||||
IOSApplicationConfiguration config = new IOSApplicationConfiguration();
|
IOSApplicationConfiguration config = new IOSApplicationConfiguration();
|
||||||
return new IOSApplication(new ClientLauncher(){
|
return new IOSApplication(new ClientLauncher(){
|
||||||
|
|
||||||
@Override
|
|
||||||
public NetProvider getNet(){
|
|
||||||
return new ArcNetImpl();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showFileChooser(boolean open, String extension, Consumer<FileHandle> cons){
|
public void showFileChooser(boolean open, String extension, Consumer<FileHandle> cons){
|
||||||
UIDocumentBrowserViewController cont = new UIDocumentBrowserViewController(NSArray.fromStrings("public.archive"));
|
UIDocumentBrowserViewController cont = new UIDocumentBrowserViewController(NSArray.fromStrings("public.archive"));
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
apply plugin: "java"
|
|
||||||
|
|
||||||
sourceCompatibility = 1.8
|
|
||||||
sourceSets.main.java.srcDirs = ["src/"]
|
|
||||||
@@ -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<Packet>)Registrator.getByID(id).type, (Supplier<Packet>)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!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,7 @@ package io.anuke.mindustry.server;
|
|||||||
|
|
||||||
import io.anuke.arc.backends.headless.*;
|
import io.anuke.arc.backends.headless.*;
|
||||||
import io.anuke.mindustry.*;
|
import io.anuke.mindustry.*;
|
||||||
|
import io.anuke.mindustry.core.*;
|
||||||
import io.anuke.mindustry.net.*;
|
import io.anuke.mindustry.net.*;
|
||||||
|
|
||||||
import static io.anuke.mindustry.Vars.platform;
|
import static io.anuke.mindustry.Vars.platform;
|
||||||
@@ -11,7 +12,7 @@ public class ServerLauncher{
|
|||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
try{
|
try{
|
||||||
Vars.platform = ArcNetImpl::new;
|
Vars.platform = new Platform(){};
|
||||||
Vars.net = new Net(platform.getNet());
|
Vars.net = new Net(platform.getNet());
|
||||||
new HeadlessApplication(new MindustryServer(args), null, throwable -> CrashSender.send(throwable, f -> {}));
|
new HeadlessApplication(new MindustryServer(args), null, throwable -> CrashSender.send(throwable, f -> {}));
|
||||||
}catch(Throwable t){
|
}catch(Throwable t){
|
||||||
|
|||||||
@@ -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 ->
|
def use = { String name ->
|
||||||
include(name)
|
include(name)
|
||||||
|
|||||||
Reference in New Issue
Block a user