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/"); FolderUtil.copyFolder(Minosoft.class.getResource("/assets").toURI(), Config.homeDir + "assets/");
} catch (Exception e) { } catch (Exception e) {
Log.fatal("Error occurred while checking assets: " + e.getLocalizedMessage()); Log.fatal("Error occurred while checking assets: " + e.getLocalizedMessage());
e.printStackTrace();
System.exit(1); System.exit(1);
} }
} }

View File

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

View File

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

View File

@ -38,7 +38,6 @@ import de.bixilon.minosoft.util.DNSUtil;
import de.bixilon.minosoft.util.ServerAddress; import de.bixilon.minosoft.util.ServerAddress;
import org.xbill.DNS.TextParseException; import org.xbill.DNS.TextParseException;
import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
@ -53,7 +52,7 @@ public class Connection {
final int connectionId; final int connectionId;
final Player player; final Player player;
final String hostname; final String hostname;
ArrayList<ServerAddress> addresses; LinkedList<ServerAddress> addresses;
int desiredVersionNumber = -1; int desiredVersionNumber = -1;
ServerAddress address; ServerAddress address;
PluginChannelHandler pluginChannelHandler; PluginChannelHandler pluginChannelHandler;
@ -86,7 +85,7 @@ public class Connection {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
address = addresses.get(0); address = addresses.getFirst();
this.nextReason = reason; this.nextReason = reason;
Log.info(String.format("Trying to connect to %s", address)); Log.info(String.format("Trying to connect to %s", address));
if (protocolId != -1) { if (protocolId != -1) {
@ -116,11 +115,19 @@ public class Connection {
network.connect(address); 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() { public ServerAddress getAddress() {
return address; return address;
} }
public ArrayList<ServerAddress> getAvailableAddresses() { public LinkedList<ServerAddress> getAvailableAddresses() {
return addresses; return addresses;
} }
@ -141,12 +148,7 @@ public class Connection {
ConnectionStates next = ((reason == ConnectionReasons.CONNECT) ? ConnectionStates.LOGIN : ConnectionStates.STATUS); ConnectionStates next = ((reason == ConnectionReasons.CONNECT) ? ConnectionStates.LOGIN : ConnectionStates.STATUS);
if (reason == ConnectionReasons.DNS) { if (reason == ConnectionReasons.DNS) {
// valid hostname found // 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)); 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())); network.sendPacket(new PacketHandshake(address, next, (next == ConnectionStates.STATUS) ? -1 : getVersion().getProtocolVersion()));
@ -211,7 +213,7 @@ public class Connection {
} }
public void handle(ClientboundPacket p) { public void handle(ClientboundPacket p) {
handlingQueue.add(p); handlingQueue.addLast(p);
handleThread.interrupt(); handleThread.interrupt();
} }
@ -241,7 +243,7 @@ public class Connection {
handleThread = new Thread(() -> { handleThread = new Thread(() -> {
while (getConnectionState() != ConnectionStates.DISCONNECTING) { while (getConnectionState() != ConnectionStates.DISCONNECTING) {
while (handlingQueue.size() > 0) { while (handlingQueue.size() > 0) {
ClientboundPacket packet = handlingQueue.get(0); ClientboundPacket packet = handlingQueue.getFirst();
try { try {
packet.log(); packet.log();
packet.handle(getHandler()); packet.handle(getHandler());
@ -253,7 +255,7 @@ public class Connection {
try { try {
// sleep, wait for an interrupt from other thread // sleep, wait for an interrupt from other thread
//noinspection BusyWait //noinspection BusyWait
Thread.sleep(100); Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
} }

View File

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

View File

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

View File

@ -30,20 +30,13 @@ import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater; import java.util.zip.Inflater;
public final class Util { public final class Util {
static final Pattern UUID_FIX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); 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
// 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 UUID uuidFromString(String uuid) { public static UUID uuidFromString(String uuid) {
if (uuid.length() == 36) { if (uuid.length() == 36) {
return UUID.fromString(uuid); 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")); 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)); throw new IllegalArgumentException(String.format("%s is not a valid UUID String", uuid));