From a43d621c0138110d2ec362b2eaa8f938f241bb49 Mon Sep 17 00:00:00 2001 From: feildmaster Date: Sun, 29 Jul 2012 02:33:13 -0500 Subject: Update CraftBukkit to Minecraft 1.3.1 --- .../java/net/minecraft/server/NetworkManager.java | 300 ++++++++++++--------- 1 file changed, 173 insertions(+), 127 deletions(-) (limited to 'src/main/java/net/minecraft/server/NetworkManager.java') diff --git a/src/main/java/net/minecraft/server/NetworkManager.java b/src/main/java/net/minecraft/server/NetworkManager.java index 58a4f813..21cd9f2a 100644 --- a/src/main/java/net/minecraft/server/NetworkManager.java +++ b/src/main/java/net/minecraft/server/NetworkManager.java @@ -5,56 +5,60 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket; import java.net.SocketAddress; -import java.net.SocketException; +import java.security.PrivateKey; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import javax.crypto.SecretKey; -public class NetworkManager { +import java.io.IOException; // CraftBukkit - public static final Object a = new Object(); - public static int b; - public static int c; - private Object g = new Object(); +public class NetworkManager implements INetworkManager { + + public static AtomicInteger a = new AtomicInteger(); + public static AtomicInteger b = new AtomicInteger(); + private Object h = new Object(); public Socket socket; // CraftBukkit - private -> public - private final SocketAddress i; - private DataInputStream input; - private DataOutputStream output; - private boolean l = true; - private java.util.Queue m = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit - Concurrent linked queue + private final SocketAddress j; + private volatile DataInputStream input; + private volatile DataOutputStream output; + private volatile boolean m = true; + private volatile boolean n = false; + private java.util.Queue inboundQueue = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit - Concurrent linked queue private List highPriorityQueue = Collections.synchronizedList(new ArrayList()); private List lowPriorityQueue = Collections.synchronizedList(new ArrayList()); private NetHandler packetListener; - private boolean q = false; - private Thread r; - private Thread s; - private boolean t = false; - private String u = ""; - private Object[] v; - private int w = 0; + private boolean s = false; + private Thread t; + private Thread u; + private String v = ""; + private Object[] w; private int x = 0; + private int y = 0; + public static int[] c = new int[256]; public static int[] d = new int[256]; - public static int[] e = new int[256]; - public int f = 0; + public int e = 0; + boolean f = false; + boolean g = false; + private SecretKey z = null; + private PrivateKey A = null; private int lowPriorityQueueDelay = 50; - public NetworkManager(Socket socket, String s, NetHandler nethandler) { + public NetworkManager(Socket socket, String s, NetHandler nethandler, PrivateKey privatekey) { + this.A = privatekey; this.socket = socket; - this.i = socket.getRemoteSocketAddress(); + this.j = socket.getRemoteSocketAddress(); this.packetListener = nethandler; - // CraftBukkit start - IPv6 stack in Java on BSD/OSX doesn't support setTrafficClass - try { - socket.setTrafficClass(24); - } catch (SocketException e) {} - // CraftBukkit end - try { - // CraftBukkit start - cant compile these outside the try socket.setSoTimeout(30000); - this.input = new DataInputStream(new java.io.BufferedInputStream(socket.getInputStream(), 2)); // Remove buffered input after 1.3 + socket.setTrafficClass(24); + // CraftBukkit start - initialize these in try-catch + this.input = new DataInputStream(socket.getInputStream()); this.output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream(), 5120)); - } catch (java.io.IOException socketexception) { + } catch (IOException socketexception) { // CraftBukkit end System.err.println(socketexception.getMessage()); } @@ -63,10 +67,10 @@ public class NetworkManager { this.input = new DataInputStream(socket.getInputStream()); this.output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream(), 5120)); // CraftBukkit end */ - this.s = new NetworkReaderThread(this, s + " read thread"); - this.r = new NetworkWriterThread(this, s + " write thread"); - this.s.start(); - this.r.start(); + this.u = new NetworkReaderThread(this, s + " read thread"); + this.t = new NetworkWriterThread(this, s + " write thread"); + this.u.start(); + this.t.start(); } public void a(NetHandler nethandler) { @@ -74,11 +78,11 @@ public class NetworkManager { } public void queue(Packet packet) { - if (!this.q) { - Object object = this.g; + if (!this.s) { + Object object = this.h; - synchronized (this.g) { - this.x += packet.a() + 1; + synchronized (this.h) { + this.y += packet.a() + 1; if (packet.lowPriority) { this.lowPriorityQueue.add(packet); } else { @@ -88,48 +92,49 @@ public class NetworkManager { } } - private boolean g() { + private boolean h() { boolean flag = false; try { - Object object; Packet packet; int i; int[] aint; - if (!this.highPriorityQueue.isEmpty() && (this.f == 0 || System.currentTimeMillis() - ((Packet) this.highPriorityQueue.get(0)).timestamp >= (long) this.f)) { - object = this.g; - synchronized (this.g) { - packet = (Packet) this.highPriorityQueue.remove(0); - this.x -= packet.a() + 1; + if (this.e == 0 || System.currentTimeMillis() - ((Packet) this.highPriorityQueue.get(0)).timestamp >= (long) this.e) { + packet = this.a(false); + if (packet != null) { + Packet.a(packet, this.output); + if (packet instanceof Packet252KeyResponse && !this.g) { + if (!this.packetListener.a()) { + this.z = ((Packet252KeyResponse) packet).d(); + } + + this.k(); + } + + aint = d; + i = packet.k(); + aint[i] += packet.a() + 1; + flag = true; } - - Packet.a(packet, this.output); - aint = e; - i = packet.b(); - aint[i] += packet.a() + 1; - flag = true; } - // CraftBukkit - don't allow low priority packet to be sent unless it was placed in the queue before the first packet on the high priority queue + // CraftBukkit - don't allow low priority packet to be sent unless it was placed in the queue before the first packet on the high priority queue TODO: is this still right? if ((flag || this.lowPriorityQueueDelay-- <= 0) && !this.lowPriorityQueue.isEmpty() && (this.highPriorityQueue.isEmpty() || ((Packet) this.highPriorityQueue.get(0)).timestamp > ((Packet) this.lowPriorityQueue.get(0)).timestamp)) { - object = this.g; - synchronized (this.g) { - packet = (Packet) this.lowPriorityQueue.remove(0); - this.x -= packet.a() + 1; + packet = this.a(true); + if (packet != null) { + Packet.a(packet, this.output); + aint = d; + i = packet.k(); + aint[i] += packet.a() + 1; + this.lowPriorityQueueDelay = 0; + flag = true; } - - Packet.a(packet, this.output); - aint = e; - i = packet.b(); - aint[i] += packet.a() + 1; - this.lowPriorityQueueDelay = 0; - flag = true; } return flag; } catch (Exception exception) { - if (!this.t) { + if (!this.n) { this.a(exception); } @@ -137,41 +142,81 @@ public class NetworkManager { } } + private Packet a(boolean flag) { + Packet packet = null; + List list = flag ? this.lowPriorityQueue : this.highPriorityQueue; + Object object = this.h; + + synchronized (this.h) { + while (!list.isEmpty() && packet == null) { + packet = (Packet) list.remove(0); + this.y -= packet.a() + 1; + if (this.a(packet, flag)) { + packet = null; + } + } + + return packet; + } + } + + private boolean a(Packet packet, boolean flag) { + if (!packet.e()) { + return false; + } else { + List list = flag ? this.lowPriorityQueue : this.highPriorityQueue; + Iterator iterator = list.iterator(); + + Packet packet1; + + do { + if (!iterator.hasNext()) { + return false; + } + + packet1 = (Packet) iterator.next(); + } while (packet1.k() != packet.k()); + + return packet.a(packet1); + } + } + public void a() { - this.s.interrupt(); - this.r.interrupt(); + if (this.u != null) { + this.u.interrupt(); + } + + if (this.t != null) { + this.t.interrupt(); + } } - private boolean h() { + private boolean i() { boolean flag = false; try { - // CraftBukkit start - 1.3 detection - this.input.mark(2); - if (this.input.read() == 2 && this.input.read() != 0) { - Packet.a(this.input, 16); - Packet.a(this.input, 255); - this.input.readInt(); - - if (this.q) { - return true; - } - - this.m.clear(); - this.m.add(new Packet2Handshake(null)); - return true; - } - this.input.reset(); - // CraftBukkit end - Packet packet = Packet.a(this.input, this.packetListener.c()); + Packet packet = Packet.a(this.input, this.packetListener.a()); if (packet != null) { - int[] aint = d; - int i = packet.b(); + if (packet instanceof Packet252KeyResponse && !this.f) { + if (this.packetListener.a()) { + this.z = ((Packet252KeyResponse) packet).a(this.A); + } + + this.j(); + } + + int[] aint = c; + int i = packet.k(); aint[i] += packet.a() + 1; - if (!this.q) { - this.m.add(packet); + if (!this.s) { + if (packet.a_() && this.packetListener.b()) { + this.x = 0; + packet.handle(this.packetListener); + } else { + this.inboundQueue.add(packet); + } } flag = true; @@ -181,7 +226,7 @@ public class NetworkManager { return flag; } catch (Exception exception) { - if (!this.t) { + if (!this.n) { this.a(exception); } @@ -195,76 +240,77 @@ public class NetworkManager { } public void a(String s, Object... aobject) { - if (this.l) { - this.t = true; - this.u = s; - this.v = aobject; + if (this.m) { + this.n = true; + this.v = s; + this.w = aobject; + this.m = false; (new NetworkMasterThread(this)).start(); - this.l = false; try { this.input.close(); this.input = null; - } catch (Throwable throwable) { - ; - } - - try { this.output.close(); this.output = null; - } catch (Throwable throwable1) { - ; - } - - try { this.socket.close(); this.socket = null; - } catch (Throwable throwable2) { + } catch (Throwable throwable) { ; } } } public void b() { - if (this.x > 1048576) { + if (this.y > 2097152) { this.a("disconnect.overflow", new Object[0]); } - if (this.m.isEmpty()) { - if (this.w++ == 1200) { + if (this.inboundQueue.isEmpty()) { + if (this.x++ == 1200) { this.a("disconnect.timeout", new Object[0]); } } else { - this.w = 0; + this.x = 0; } int i = 1000; - while (!this.m.isEmpty() && i-- >= 0) { - Packet packet = (Packet) this.m.poll(); // CraftBukkit - remove -> poll + while (!this.inboundQueue.isEmpty() && i-- >= 0) { + Packet packet = (Packet) this.inboundQueue.poll(); // CraftBukkit - remove -> poll - if (!this.q) packet.handle(this.packetListener); // CraftBukkit + packet.handle(this.packetListener); } this.a(); - if (this.t && this.m.isEmpty()) { - this.packetListener.a(this.u, this.v); + if (this.n && this.inboundQueue.isEmpty()) { + this.packetListener.a(this.v, this.w); } } public SocketAddress getSocketAddress() { - return this.i; + return this.j; } public void d() { - if (!this.q) { + if (!this.s) { this.a(); - this.q = true; - this.s.interrupt(); + this.s = true; + this.u.interrupt(); (new NetworkMonitorThread(this)).start(); } } + private void j() throws IOException { // CraftBukkit - throws IOException + this.f = true; + this.input = new DataInputStream(MinecraftEncryption.a(this.z, this.socket.getInputStream())); + } + + private void k() throws IOException { // CraftBukkit - throws IOException + this.output.flush(); + this.g = true; + this.output = new DataOutputStream(new BufferedOutputStream(MinecraftEncryption.a(this.z, this.socket.getOutputStream()), 5120)); + } + public int e() { return this.lowPriorityQueue.size(); } @@ -274,19 +320,19 @@ public class NetworkManager { } static boolean a(NetworkManager networkmanager) { - return networkmanager.l; + return networkmanager.m; } static boolean b(NetworkManager networkmanager) { - return networkmanager.q; + return networkmanager.s; } static boolean c(NetworkManager networkmanager) { - return networkmanager.h(); + return networkmanager.i(); } static boolean d(NetworkManager networkmanager) { - return networkmanager.g(); + return networkmanager.h(); } static DataOutputStream e(NetworkManager networkmanager) { @@ -294,7 +340,7 @@ public class NetworkManager { } static boolean f(NetworkManager networkmanager) { - return networkmanager.t; + return networkmanager.n; } static void a(NetworkManager networkmanager, Exception exception) { @@ -302,10 +348,10 @@ public class NetworkManager { } static Thread g(NetworkManager networkmanager) { - return networkmanager.s; + return networkmanager.u; } static Thread h(NetworkManager networkmanager) { - return networkmanager.r; + return networkmanager.t; } } -- cgit v1.2.3