don't ping server when connecting

This commit is contained in:
Bixilon 2020-08-31 13:58:47 +02:00
parent 7f46603ef0
commit 6875d73e7c
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 52 additions and 48 deletions

View File

@ -127,6 +127,7 @@ public class Minosoft {
FolderUtil.copyFolder(Minosoft.class.getResource("/assets").toURI(), Config.homeDir + "assets/");
} catch (Exception e) {
Log.fatal("Error occurred while checking assets: " + e.getLocalizedMessage());
e.printStackTrace();
System.exit(1);
}
}

View File

@ -57,12 +57,13 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
public MenuItem optionsEdit;
@FXML
public MenuItem optionsDelete;
boolean canConnect = false;
Connection lastPing;
@FXML
private Label serverName;
@FXML
private AnchorPane root;
private Server server;
boolean canConnect = false;
public static ServerListCell newInstance() {
FXMLLoader loader = new FXMLLoader(ServerListCell.class.getResource("/layout/cells/server.fxml"));
@ -104,8 +105,8 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
optionsEdit.setOnAction(e -> edit());
optionsDelete.setOnAction(e -> delete());
Connection connection = new Connection(Connection.lastConnectionId++, server.getAddress(), null);
connection.addPingCallback(ping -> Platform.runLater(() -> {
lastPing = new Connection(Connection.lastConnectionId++, server.getAddress(), null);
lastPing.addPingCallback(ping -> Platform.runLater(() -> {
if (ping == null) {
// Offline
players.setText("");
@ -139,7 +140,7 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
icon.setImage(ping.getFavicon());
}
}));
connection.resolve(ConnectionReasons.PING, server.getDesiredVersion()); // resolve dns address and ping
lastPing.resolve(ConnectionReasons.PING, server.getDesiredVersion()); // resolve dns address and ping
}
setOnMouseClicked(click -> {
if (click.getClickCount() == 2) {
@ -216,11 +217,17 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
}
public void connect() {
if (!canConnect) {
if (!canConnect || lastPing == null) {
return;
}
Connection connection = new Connection(Connection.lastConnectionId++, server.getAddress(), new Player(Minosoft.accountList.get(0)));
connection.resolve(ConnectionReasons.CONNECT, server.getDesiredVersion());
Version version;
if (server.getDesiredVersion() == -1) {
version = lastPing.getVersion();
} else {
version = Versions.getVersionById(server.getDesiredVersion());
}
connection.connect(lastPing.getAddress(), version);
setStyle("-fx-background-color: darkseagreen;");
}
}

View File

@ -17,11 +17,11 @@ import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.game.datatypes.TextComponent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedList;
public class Log {
final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
final static ArrayList<String> queue = new ArrayList<>();
final static LinkedList<String> queue = new LinkedList<>();
static LogLevels level = LogLevels.PROTOCOL;
static Thread logThread;
@ -45,7 +45,7 @@ public class Log {
} else {
builder.append(message);
}
queue.add(builder.toString());
queue.addLast(builder.toString());
logThread.interrupt();
}
@ -55,16 +55,17 @@ public class Log {
while (true) {
while (queue.size() > 0) {
// something to print
System.out.println(queue.get(0));
String message = queue.getFirst();
System.out.println(message);
// ToDo: log to file
queue.remove(0);
queue.remove(message);
}
try {
// wait for interrupt
//noinspection BusyWait
Thread.sleep(100);
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException ignored) {
}
}

View File

@ -38,7 +38,6 @@ import de.bixilon.minosoft.util.DNSUtil;
import de.bixilon.minosoft.util.ServerAddress;
import org.xbill.DNS.TextParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
@ -53,7 +52,7 @@ public class Connection {
final int connectionId;
final Player player;
final String hostname;
ArrayList<ServerAddress> addresses;
LinkedList<ServerAddress> addresses;
int desiredVersionNumber = -1;
ServerAddress address;
PluginChannelHandler pluginChannelHandler;
@ -86,7 +85,7 @@ public class Connection {
throw new RuntimeException(e);
}
}
address = addresses.get(0);
address = addresses.getFirst();
this.nextReason = reason;
Log.info(String.format("Trying to connect to %s", address));
if (protocolId != -1) {
@ -116,11 +115,19 @@ public class Connection {
network.connect(address);
}
public void connect(ServerAddress address, Version version) {
this.address = address;
this.reason = ConnectionReasons.CONNECT;
setVersion(version);
Log.info(String.format("Connecting to server: %s", address));
network.connect(address);
}
public ServerAddress getAddress() {
return address;
}
public ArrayList<ServerAddress> getAvailableAddresses() {
public LinkedList<ServerAddress> getAvailableAddresses() {
return addresses;
}
@ -141,12 +148,7 @@ public class Connection {
ConnectionStates next = ((reason == ConnectionReasons.CONNECT) ? ConnectionStates.LOGIN : ConnectionStates.STATUS);
if (reason == ConnectionReasons.DNS) {
// valid hostname found
if (nextReason == ConnectionReasons.CONNECT) {
// connecting, we must get the version first
reason = ConnectionReasons.GET_VERSION;
} else {
reason = nextReason;
}
reason = nextReason;
Log.info(String.format("Connection to %s seems to be okay, connecting...", address));
}
network.sendPacket(new PacketHandshake(address, next, (next == ConnectionStates.STATUS) ? -1 : getVersion().getProtocolVersion()));
@ -196,14 +198,14 @@ public class Connection {
public void setVersion(Version version) {
this.version = version;
this.customMapping.setVersion(version);
try {
Versions.loadVersionMappings(version.getProtocolVersion());
} catch (Exception e) {
e.printStackTrace();
Log.fatal(String.format("Could not load mapping for %s. Exiting...", version));
System.exit(1);
}
customMapping.setVersion(version);
try {
Versions.loadVersionMappings(version.getProtocolVersion());
} catch (Exception e) {
e.printStackTrace();
Log.fatal(String.format("Could not load mapping for %s. Exiting...", version));
System.exit(1);
}
customMapping.setVersion(version);
}
public PacketHandler getHandler() {
@ -211,7 +213,7 @@ public class Connection {
}
public void handle(ClientboundPacket p) {
handlingQueue.add(p);
handlingQueue.addLast(p);
handleThread.interrupt();
}
@ -241,7 +243,7 @@ public class Connection {
handleThread = new Thread(() -> {
while (getConnectionState() != ConnectionStates.DISCONNECTING) {
while (handlingQueue.size() > 0) {
ClientboundPacket packet = handlingQueue.get(0);
ClientboundPacket packet = handlingQueue.getFirst();
try {
packet.log();
packet.handle(getHandler());
@ -253,7 +255,7 @@ public class Connection {
try {
// sleep, wait for an interrupt from other thread
//noinspection BusyWait
Thread.sleep(100);
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException ignored) {
}
}

View File

@ -83,7 +83,7 @@ public class Network {
}
while (queue.size() > 0) {
ServerboundPacket packet = queue.get(0);
ServerboundPacket packet = queue.getFirst();
packet.log();
queue.remove(packet);
byte[] data = packet.write(connection).getOutBytes();
@ -215,7 +215,7 @@ public class Network {
}
public void sendPacket(ServerboundPacket p) {
queue.add(p);
queue.addLast(p);
socketThread.interrupt();
}

View File

@ -17,13 +17,13 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import org.xbill.DNS.Record;
import org.xbill.DNS.*;
import java.util.ArrayList;
import java.util.LinkedList;
public final class DNSUtil {
public static ArrayList<ServerAddress> getServerAddresses(String hostname) throws TextParseException {
public static LinkedList<ServerAddress> getServerAddresses(String hostname) throws TextParseException {
ServerAddress fallbackAddress = getServerAddress(hostname);
ArrayList<ServerAddress> ret = new ArrayList<>();
LinkedList<ServerAddress> ret = new LinkedList<>();
if (hostname.contains(":")) {
// port provided, skip srv check
ret.add(fallbackAddress);

View File

@ -30,20 +30,13 @@ import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
public final class Util {
static final Pattern UUID_FIX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
// thanks https://www.spigotmc.org/threads/free-code-easily-convert-between-trimmed-and-full-uuids.165615
public static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException ignored) {
}
}
public static final Pattern UUID_FIX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); // thanks https://www.spigotmc.org/threads/free-code-easily-convert-between-trimmed-and-full-uuids.165615
public static UUID uuidFromString(String uuid) {
if (uuid.length() == 36) {
return UUID.fromString(uuid);
} else if (uuid.length() == 32) {
}
if (uuid.length() == 32) {
return UUID.fromString(UUID_FIX.matcher(uuid.replace("-", "")).replaceAll("$1-$2-$3-$4-$5"));
}
throw new IllegalArgumentException(String.format("%s is not a valid UUID String", uuid));