Better web support, fixed application hanging in background
This commit is contained in:
@@ -546,7 +546,6 @@ public class Control extends Module{
|
||||
|
||||
@Override
|
||||
public void update(){
|
||||
//UCore.log(input.placeMode, input.breakMode, input.lastPlaceMode);
|
||||
|
||||
if(Gdx.input != proxy){
|
||||
Gdx.input = proxy;
|
||||
|
||||
@@ -11,7 +11,6 @@ import io.anuke.ucore.scene.style.Drawable;
|
||||
import io.anuke.ucore.scene.ui.Dialog;
|
||||
import io.anuke.ucore.scene.ui.ScrollPane;
|
||||
import io.anuke.ucore.scene.ui.TextButton;
|
||||
import io.anuke.ucore.scene.ui.TextField.TextFieldFilter.DigitsOnlyFilter;
|
||||
import io.anuke.ucore.scene.ui.layout.Table;
|
||||
import io.anuke.ucore.util.Bundles;
|
||||
import io.anuke.ucore.util.Strings;
|
||||
@@ -40,12 +39,13 @@ public class JoinDialog extends FloatingDialog {
|
||||
}).size(240f, 54f).get(), 100);
|
||||
|
||||
join.content().row();
|
||||
/*
|
||||
join.content().add("$text.server.port").left();
|
||||
Mindustry.platforms.addDialog(join.content()
|
||||
.addField(Settings.getString("port"), new DigitsOnlyFilter(), text ->{
|
||||
Settings.putString("port", text);
|
||||
Settings.save();
|
||||
}).size(240f, 54f).get());
|
||||
}).size(240f, 54f).get());*/
|
||||
join.buttons().defaults().size(140f, 60f).pad(4f);
|
||||
join.buttons().addButton("$text.cancel", join::hide);
|
||||
join.buttons().addButton("$text.ok", () -> {
|
||||
@@ -56,14 +56,14 @@ public class JoinDialog extends FloatingDialog {
|
||||
setupRemote();
|
||||
refreshRemote();
|
||||
}else{
|
||||
renaming.port = Strings.parseInt(Settings.getString("port"));
|
||||
//renaming.port = Strings.parseInt(Settings.getString("port"));
|
||||
renaming.ip = Settings.getString("ip");
|
||||
saveServers();
|
||||
setupRemote();
|
||||
refreshRemote();
|
||||
}
|
||||
join.hide();
|
||||
}).disabled(b -> Settings.getString("ip").isEmpty() || Strings.parseInt(Settings.getString("port")) == Integer.MIN_VALUE || Net.active());
|
||||
}).disabled(b -> Settings.getString("ip").isEmpty() || Net.active());
|
||||
|
||||
join.shown(() -> {
|
||||
join.getTitleLabel().setText(renaming != null ? "$text.server.edit" : "$text.server.add");
|
||||
@@ -83,8 +83,8 @@ public class JoinDialog extends FloatingDialog {
|
||||
//why are java lambdas this bad
|
||||
TextButton[] buttons = {null};
|
||||
|
||||
TextButton button = buttons[0] = remote.addButton("[accent]"+server.ip + " / " + server.port, "clear", () -> {
|
||||
if(!buttons[0].childrenPressed()) connect(server.ip, server.port);
|
||||
TextButton button = buttons[0] = remote.addButton("[accent]"+server.ip, "clear", () -> {
|
||||
if(!buttons[0].childrenPressed()) connect(server.ip, Vars.port);
|
||||
}).width(w).height(120f).pad(4f).get();
|
||||
|
||||
button.getLabel().setWrap(true);
|
||||
@@ -145,10 +145,12 @@ public class JoinDialog extends FloatingDialog {
|
||||
}
|
||||
|
||||
void refreshLocal(){
|
||||
local.clear();
|
||||
local.background("button");
|
||||
local.label(() -> "[accent]" + Bundles.get("text.hosts.discovering") + new String(new char[(int)(Timers.time() / 10) % 4]).replace("\0", ".")).pad(10f);
|
||||
Net.discoverServers(this::addLocalHosts);
|
||||
if(!Vars.gwt) {
|
||||
local.clear();
|
||||
local.background("button");
|
||||
local.label(() -> "[accent]" + Bundles.get("text.hosts.discovering") + new String(new char[(int) (Timers.time() / 10) % 4]).replace("\0", ".")).pad(10f);
|
||||
Net.discoverServers(this::addLocalHosts);
|
||||
}
|
||||
}
|
||||
|
||||
void setup(){
|
||||
@@ -201,7 +203,7 @@ public class JoinDialog extends FloatingDialog {
|
||||
button.add("[lightgray]" + (a.players != 1 ? Bundles.format("text.players", a.players) :
|
||||
Bundles.format("text.players.single", a.players)));
|
||||
button.row();
|
||||
button.add("[lightgray]" + a.address + " / " + Vars.port).pad(4).left();
|
||||
button.add("[lightgray]" + a.address).pad(4).left();
|
||||
|
||||
local.row();
|
||||
local.background((Drawable) null);
|
||||
|
||||
@@ -15,7 +15,9 @@ import io.anuke.mindustry.net.Packet;
|
||||
import io.anuke.mindustry.net.Packets.Connect;
|
||||
import io.anuke.mindustry.net.Packets.Disconnect;
|
||||
import io.anuke.mindustry.net.Registrator;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.function.Consumer;
|
||||
import io.anuke.ucore.util.Strings;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -103,6 +105,35 @@ public class WebsocketClient implements ClientProvider {
|
||||
@Override
|
||||
public void pingHost(String address, int port, Consumer<Host> valid, Consumer<IOException> failed) {
|
||||
failed.accept(new IOException());
|
||||
Websocket socket = new Websocket("ws://" + address + ":" + Vars.webPort);
|
||||
final boolean[] accepted = {false};
|
||||
socket.addListener(new WebsocketListener() {
|
||||
@Override
|
||||
public void onClose() {
|
||||
if(!accepted[0]) failed.accept(new IOException("Failed to connect to host."));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String msg) {
|
||||
String[] text = msg.split("\\|");
|
||||
Host host = new Host(text[1], address, Strings.parseInt(text[0]));
|
||||
valid.accept(host);
|
||||
accepted[0] = true;
|
||||
socket.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen() {
|
||||
socket.send("_ping_");
|
||||
}
|
||||
});
|
||||
socket.open();
|
||||
Timers.runTask(60f*5, () -> {
|
||||
if(!accepted[0]){
|
||||
failed.accept(new IOException("Failed to connect to host."));
|
||||
socket.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -37,11 +37,11 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
public class KryoServer implements ServerProvider {
|
||||
final boolean debug = false;
|
||||
final Server server;
|
||||
final SocketServer webServer;
|
||||
final ByteSerializer serializer = new ByteSerializer();
|
||||
final ByteBuffer buffer = ByteBuffer.allocate(4096);
|
||||
final CopyOnWriteArrayList<KryoConnection> connections = new CopyOnWriteArrayList<>();
|
||||
final Array<KryoConnection> array = new Array<>();
|
||||
SocketServer webServer;
|
||||
|
||||
int lastconnection = 0;
|
||||
|
||||
@@ -54,7 +54,6 @@ public class KryoServer implements ServerProvider {
|
||||
datagramChannel.send(buffer, fromAddress);
|
||||
return true;
|
||||
});
|
||||
webServer = new SocketServer(Vars.webPort);
|
||||
|
||||
Listener listener = new Listener(){
|
||||
|
||||
@@ -136,6 +135,7 @@ public class KryoServer implements ServerProvider {
|
||||
public void host(int port) throws IOException {
|
||||
lastconnection = 0;
|
||||
server.bind(port, port);
|
||||
webServer = new SocketServer(Vars.webPort);
|
||||
webServer.start();
|
||||
|
||||
Thread thread = new Thread(() -> {
|
||||
@@ -153,14 +153,22 @@ public class KryoServer implements ServerProvider {
|
||||
public void close() {
|
||||
UCore.setPrivate(server, "shutdown", true);
|
||||
|
||||
new Thread(() ->{
|
||||
Thread thread = new Thread(() ->{
|
||||
try {
|
||||
server.close();
|
||||
webServer.stop();
|
||||
webServer.stop(1); //please die, right now
|
||||
//kill them all
|
||||
for(Thread worker : Thread.getAllStackTraces().keySet()){
|
||||
if(worker.getName().contains("WebSocketWorker")){
|
||||
worker.interrupt();
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
|
||||
}
|
||||
}).run();
|
||||
});
|
||||
thread.setDaemon(true);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -247,7 +255,13 @@ public class KryoServer implements ServerProvider {
|
||||
public void dispose(){
|
||||
try {
|
||||
server.dispose();
|
||||
webServer.stop();
|
||||
webServer.stop(1);
|
||||
//kill them all
|
||||
for(Thread thread : Thread.getAllStackTraces().keySet()){
|
||||
if(thread.getName().contains("WebSocketWorker")){
|
||||
thread.interrupt();
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -372,15 +386,19 @@ public class KryoServer implements ServerProvider {
|
||||
@Override
|
||||
public void onMessage(WebSocket conn, String message) {
|
||||
try {
|
||||
if(debug) UCore.log("Got message: " + message);
|
||||
KryoConnection k = getBySocket(conn);
|
||||
if (k == null) return;
|
||||
if(message.equals("_ping_")){
|
||||
conn.send(connections.size() + "|" + Vars.player.name);
|
||||
}else {
|
||||
if (debug) UCore.log("Got message: " + message);
|
||||
KryoConnection k = getBySocket(conn);
|
||||
if (k == null) return;
|
||||
|
||||
byte[] out = Base64Coder.decode(message);
|
||||
if(debug) UCore.log("Decoded: " + Arrays.toString(out));
|
||||
ByteBuffer buffer = ByteBuffer.wrap(out);
|
||||
Object o = serializer.read(buffer);
|
||||
Net.handleServerReceived(o, k.id);
|
||||
byte[] out = Base64Coder.decode(message);
|
||||
if (debug) UCore.log("Decoded: " + Arrays.toString(out));
|
||||
ByteBuffer buffer = ByteBuffer.wrap(out);
|
||||
Object o = serializer.read(buffer);
|
||||
Net.handleServerReceived(o, k.id);
|
||||
}
|
||||
}catch (Exception e){
|
||||
UCore.log("Error reading message!");
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user