From a51ce916ce984bbc338fd4ae23761caef8386180 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 13 Oct 2012 09:26:29 +1100 Subject: [PATCH] Retry this mess of connections again. --- .../net/md_5/bungee/ServerConnection.java | 2 +- .../java/net/md_5/bungee/UserConnection.java | 27 ++++++++++--------- .../md_5/bungee/command/CommandServer.java | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/md_5/bungee/ServerConnection.java b/src/main/java/net/md_5/bungee/ServerConnection.java index 7168b3872..fa276c3da 100644 --- a/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/src/main/java/net/md_5/bungee/ServerConnection.java @@ -69,7 +69,7 @@ public class ServerConnection extends GenericConnection { throw ex; } catch (Exception ex) { InetSocketAddress def = BungeeCord.instance.config.getServer(null); - if (retry && address != def) { + if (retry && !address.equals(def)) { return connect(name, def, handshake, false); } else { throw new RuntimeException("Could not connect to target server"); diff --git a/src/main/java/net/md_5/bungee/UserConnection.java b/src/main/java/net/md_5/bungee/UserConnection.java index 4e3da51bd..8814ce712 100644 --- a/src/main/java/net/md_5/bungee/UserConnection.java +++ b/src/main/java/net/md_5/bungee/UserConnection.java @@ -28,6 +28,7 @@ public class UserConnection extends GenericConnection implements CommandSender { private Packet10HeldItem heldItem; private int clientEntityId; private int serverEntityId; + private volatile boolean reconnecting; public UserConnection(Socket socket, PacketInputStream in, OutputStream out, Packet2Handshake handshake) { super(socket, in, out); @@ -43,6 +44,13 @@ public class UserConnection extends GenericConnection implements CommandSender { private void connect(String name, InetSocketAddress serverAddr) { try { + reconnecting = true; + + if (server != null) { + out.write(new Packet9Respawn((byte) 1, (byte) 0, (byte) 0, (short) 256, "DEFAULT").getPacket()); + out.write(new Packet9Respawn((byte) -1, (byte) 0, (byte) 0, (short) 256, "DEFAULT").getPacket()); + } + ServerConnection newServer = ServerConnection.connect(name, serverAddr, handshake, server == null); if (server == null) { clientEntityId = newServer.loginPacket.entityId; @@ -50,14 +58,14 @@ public class UserConnection extends GenericConnection implements CommandSender { upBridge = new UpstreamBridge(); upBridge.start(); } else { - downBridge.alive = false; try { + downBridge.interrupt(); downBridge.join(); } catch (InterruptedException ie) { } + server.disconnect("Quitting"); - out.write(new Packet9Respawn((byte) 1, (byte) 0, (byte) 0, (short) 256, "DEFAULT").getPacket()); - out.write(new Packet9Respawn((byte) -1, (byte) 0, (byte) 0, (short) 256, "DEFAULT").getPacket()); + Packet1Login login = newServer.loginPacket; serverEntityId = login.entityId; out.write(new Packet9Respawn(login.dimension, login.difficulty, login.gameMode, (short) 256, login.levelType).getPacket()); @@ -66,17 +74,14 @@ public class UserConnection extends GenericConnection implements CommandSender { newServer.out.write(heldItem.getPacket()); } } + reconnecting = false; downBridge = new DownstreamBridge(); server = newServer; downBridge.start(); } catch (KickException ex) { destory(ex.getMessage()); } catch (Exception ex) { - if (server == null) { - destory("Could not connect to server"); - } else { - packetQueue.add(new Packet3Chat(ChatColor.YELLOW + "The server you selected is not up at the moment.")); - } + destory("Could not connect to server"); } } @@ -135,8 +140,6 @@ public class UserConnection extends GenericConnection implements CommandSender { private class DownstreamBridge extends Thread { - private volatile boolean alive = true; - public DownstreamBridge() { super("Downstream Bridge - " + username); } @@ -144,7 +147,7 @@ public class UserConnection extends GenericConnection implements CommandSender { @Override public void run() { try { - while (alive) { + while (!reconnecting) { byte[] packet = server.in.readPacket(); boolean sendPacket = true; @@ -154,7 +157,7 @@ public class UserConnection extends GenericConnection implements CommandSender { if (message.tag.equals("RubberBand")) { String server = new String(message.data); connect(server); - sendPacket = false; + break; } } diff --git a/src/main/java/net/md_5/bungee/command/CommandServer.java b/src/main/java/net/md_5/bungee/command/CommandServer.java index 78112e7ae..891be44a0 100644 --- a/src/main/java/net/md_5/bungee/command/CommandServer.java +++ b/src/main/java/net/md_5/bungee/command/CommandServer.java @@ -6,7 +6,7 @@ import net.md_5.bungee.ChatColor; import net.md_5.bungee.UserConnection; /** - * Command to list and switch a player between availible servers. + * Command to list and switch a player between available servers. */ public class CommandServer extends Command {