Non-blocking connect / Localized connect error messages
This commit is contained in:
@@ -269,10 +269,9 @@ public class Drone extends FlyingUnit implements BuilderTrait{
|
||||
}
|
||||
|
||||
private void notifyPlaced(BuildEntity entity, boolean isBreaking){
|
||||
float timeToBuild = entity.buildCost;
|
||||
float dist = Math.min(entity.distanceTo(x, y) - placeDistance, 0);
|
||||
|
||||
if(!state.is(build) && dist / type.maxVelocity < timeToBuild * 0.9f){
|
||||
if(!state.is(build) && dist / type.maxVelocity < entity.buildCost * 0.9f){
|
||||
target = entity;
|
||||
this.isBreaking = isBreaking;
|
||||
setState(build);
|
||||
|
||||
@@ -17,13 +17,14 @@ import io.anuke.mindustry.net.Streamable.StreamBuilder;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.function.BiConsumer;
|
||||
import io.anuke.ucore.function.Consumer;
|
||||
import io.anuke.ucore.util.Bundles;
|
||||
import io.anuke.ucore.util.Log;
|
||||
import io.anuke.ucore.util.Pooling;
|
||||
import io.anuke.ucore.util.Threads;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static io.anuke.mindustry.Vars.headless;
|
||||
import static io.anuke.mindustry.Vars.ui;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class Net{
|
||||
private static boolean server;
|
||||
@@ -46,15 +47,41 @@ public class Net{
|
||||
return serverProvider != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a network error.
|
||||
*/
|
||||
public static void showError(String text){
|
||||
/**Display a network error. Call on the graphics thread.*/
|
||||
public static void showError(Throwable e){
|
||||
|
||||
if(!headless){
|
||||
ui.showError(text);
|
||||
}else{
|
||||
Log.err(text);
|
||||
Threads.assertGraphics();
|
||||
|
||||
Throwable t = e;
|
||||
while(t.getCause() != null){
|
||||
t = t.getCause();
|
||||
}
|
||||
|
||||
String error = t.getMessage() == null ? "" : t.getMessage().toLowerCase();
|
||||
String type = error.getClass().toString().toLowerCase();
|
||||
|
||||
if(error.equals("mismatch")){
|
||||
error = Bundles.get("text.error.mismatch");
|
||||
}else if(error.contains("port out of range") || error.contains("invalid argument") || (error.contains("invalid") && error.contains("address"))){
|
||||
error = Bundles.get("text.error.invalidaddress");
|
||||
}else if(error.contains("connection refused") || error.contains("route to host") || type.contains("unknownhost")){
|
||||
error = Bundles.get("text.error.unreachable");
|
||||
}else if(type.contains("timeout")){
|
||||
error = Bundles.get("text.error.timeout");
|
||||
}else if(!error.isEmpty()){
|
||||
error = Bundles.get("text.error.any");
|
||||
}
|
||||
|
||||
ui.showText("", Bundles.format("text.connectfail", error));
|
||||
ui.loadfrag.hide();
|
||||
|
||||
if(Net.client()){
|
||||
netClient.disconnectQuietly();
|
||||
}
|
||||
}
|
||||
|
||||
Log.err(e);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,14 +104,18 @@ public class Net{
|
||||
/**
|
||||
* Connect to an address.
|
||||
*/
|
||||
public static void connect(String ip, int port) throws IOException{
|
||||
lastIP = ip + ":" + port;
|
||||
if(!active){
|
||||
clientProvider.connect(ip, port);
|
||||
active = true;
|
||||
server = false;
|
||||
}else{
|
||||
throw new IOException("Already connected!");
|
||||
public static void connect(String ip, int port, Runnable success){
|
||||
try{
|
||||
lastIP = ip + ":" + port;
|
||||
if(!active){
|
||||
clientProvider.connect(ip, port, success);
|
||||
active = true;
|
||||
server = false;
|
||||
}else{
|
||||
throw new IOException("Already connected!");
|
||||
}
|
||||
}catch(IOException e){
|
||||
showError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,7 +377,7 @@ public class Net{
|
||||
/**Client implementation.*/
|
||||
public interface ClientProvider{
|
||||
/**Connect to a server.*/
|
||||
void connect(String ip, int port) throws IOException;
|
||||
void connect(String ip, int port, Runnable success) throws IOException;
|
||||
|
||||
/**Send an object to the server.*/
|
||||
void send(Object object, SendMode mode);
|
||||
|
||||
@@ -18,7 +18,6 @@ import io.anuke.ucore.scene.ui.layout.Cell;
|
||||
import io.anuke.ucore.scene.ui.layout.Table;
|
||||
import io.anuke.ucore.scene.utils.UIUtils;
|
||||
import io.anuke.ucore.util.Bundles;
|
||||
import io.anuke.ucore.util.Log;
|
||||
import io.anuke.ucore.util.Strings;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
@@ -300,34 +299,11 @@ public class JoinDialog extends FloatingDialog{
|
||||
});
|
||||
|
||||
Timers.runTask(2f, () -> {
|
||||
try{
|
||||
Vars.netClient.beginConnecting();
|
||||
Net.connect(ip, port);
|
||||
Vars.netClient.beginConnecting();
|
||||
Net.connect(ip, port, () -> {
|
||||
hide();
|
||||
add.hide();
|
||||
}catch(Exception e){
|
||||
Throwable t = e;
|
||||
while(t.getCause() != null){
|
||||
t = t.getCause();
|
||||
}
|
||||
//TODO localize
|
||||
String error = t.getMessage() == null ? "" : t.getMessage().toLowerCase();
|
||||
if(error.contains("connection refused")){
|
||||
error = "connection refused";
|
||||
}else if(error.contains("port out of range")){
|
||||
error = "invalid port!";
|
||||
}else if(error.contains("invalid argument")){
|
||||
error = "invalid IP or port!";
|
||||
}else if(t.getClass().toString().toLowerCase().contains("sockettimeout")){
|
||||
error = "timed out!\nmake sure the host has port forwarding set up,\nand that the address is correct!";
|
||||
}else{
|
||||
error = Strings.parseException(e, false);
|
||||
}
|
||||
ui.showError(Bundles.format("text.connectfail", error));
|
||||
ui.loadfrag.hide();
|
||||
|
||||
Log.err(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user