2011-03-27 22:12:39 +02:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
2011-04-20 22:47:26 +02:00
|
|
|
import java.io.BufferedOutputStream;
|
2011-03-27 22:12:39 +02:00
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.SocketAddress;
|
2011-06-12 00:02:58 +02:00
|
|
|
import java.net.SocketException;
|
2011-03-27 22:12:39 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2011-06-07 07:29:55 +02:00
|
|
|
|
2011-03-27 22:12:39 +02:00
|
|
|
public class NetworkManager {
|
|
|
|
|
|
|
|
public static final Object a = new Object();
|
|
|
|
public static int b;
|
|
|
|
public static int c;
|
2011-05-26 14:48:22 +02:00
|
|
|
private Object g = new Object();
|
2011-05-14 16:29:42 +02:00
|
|
|
public Socket socket; // CraftBukkit - private -> public
|
2011-05-26 14:48:22 +02:00
|
|
|
private final SocketAddress i;
|
2011-04-20 19:05:14 +02:00
|
|
|
private DataInputStream input;
|
|
|
|
private DataOutputStream output;
|
2011-05-26 14:48:22 +02:00
|
|
|
private boolean l = true;
|
2012-06-17 08:25:01 +02:00
|
|
|
private java.util.Queue m = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit - Concurrent linked queue
|
2011-06-27 00:25:01 +02:00
|
|
|
private List highPriorityQueue = Collections.synchronizedList(new ArrayList());
|
|
|
|
private List lowPriorityQueue = Collections.synchronizedList(new ArrayList());
|
2012-01-14 21:03:48 +01:00
|
|
|
private NetHandler packetListener;
|
2012-03-01 11:49:23 +01:00
|
|
|
private boolean q = false;
|
2011-05-26 14:48:22 +02:00
|
|
|
private Thread r;
|
|
|
|
private Thread s;
|
|
|
|
private boolean t = false;
|
|
|
|
private String u = "";
|
|
|
|
private Object[] v;
|
|
|
|
private int w = 0;
|
|
|
|
private int x = 0;
|
|
|
|
public static int[] d = new int[256];
|
|
|
|
public static int[] e = new int[256];
|
|
|
|
public int f = 0;
|
2011-06-27 00:25:01 +02:00
|
|
|
private int lowPriorityQueueDelay = 50;
|
2011-03-27 22:12:39 +02:00
|
|
|
|
|
|
|
public NetworkManager(Socket socket, String s, NetHandler nethandler) {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.socket = socket;
|
2011-05-26 14:48:22 +02:00
|
|
|
this.i = socket.getRemoteSocketAddress();
|
2012-01-14 21:03:48 +01:00
|
|
|
this.packetListener = nethandler;
|
2011-03-27 22:12:39 +02:00
|
|
|
|
2011-06-07 07:29:55 +02:00
|
|
|
// CraftBukkit start - IPv6 stack in Java on BSD/OSX doesn't support setTrafficClass
|
2011-03-27 22:12:39 +02:00
|
|
|
try {
|
|
|
|
socket.setTrafficClass(24);
|
2011-06-12 00:02:58 +02:00
|
|
|
} catch (SocketException e) {}
|
2011-06-07 07:29:55 +02:00
|
|
|
// CraftBukkit end
|
2011-04-20 22:47:26 +02:00
|
|
|
|
2011-06-07 07:29:55 +02:00
|
|
|
try {
|
2011-05-14 16:29:42 +02:00
|
|
|
// CraftBukkit start - cant compile these outside the try
|
2011-06-07 07:29:55 +02:00
|
|
|
socket.setSoTimeout(30000);
|
2012-07-26 08:17:09 +02:00
|
|
|
this.input = new DataInputStream(new java.io.BufferedInputStream(socket.getInputStream(), 2)); // Remove buffered input after 1.3
|
2011-05-28 22:50:08 +02:00
|
|
|
this.output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream(), 5120));
|
2011-06-27 00:25:01 +02:00
|
|
|
} catch (java.io.IOException socketexception) {
|
2011-04-20 22:47:26 +02:00
|
|
|
// CraftBukkit end
|
2011-03-31 22:40:00 +02:00
|
|
|
System.err.println(socketexception.getMessage());
|
|
|
|
}
|
2011-03-27 22:12:39 +02:00
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
/* CraftBukkit start - moved up
|
|
|
|
this.input = new DataInputStream(socket.getInputStream());
|
|
|
|
this.output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream(), 5120));
|
|
|
|
// CraftBukkit end */
|
2011-05-26 14:48:22 +02:00
|
|
|
this.s = new NetworkReaderThread(this, s + " read thread");
|
|
|
|
this.r = new NetworkWriterThread(this, s + " write thread");
|
|
|
|
this.s.start();
|
|
|
|
this.r.start();
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void a(NetHandler nethandler) {
|
2012-01-14 21:03:48 +01:00
|
|
|
this.packetListener = nethandler;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
public void queue(Packet packet) {
|
2012-03-01 11:49:23 +01:00
|
|
|
if (!this.q) {
|
2011-05-26 14:48:22 +02:00
|
|
|
Object object = this.g;
|
2011-03-27 22:12:39 +02:00
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
synchronized (this.g) {
|
|
|
|
this.x += packet.a() + 1;
|
2012-01-14 21:03:48 +01:00
|
|
|
if (packet.lowPriority) {
|
2011-06-27 00:25:01 +02:00
|
|
|
this.lowPriorityQueue.add(packet);
|
2011-03-27 22:12:39 +02:00
|
|
|
} else {
|
2011-06-27 00:25:01 +02:00
|
|
|
this.highPriorityQueue.add(packet);
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
private boolean g() {
|
2011-05-26 16:20:11 +02:00
|
|
|
boolean flag = false;
|
|
|
|
|
2011-03-27 22:12:39 +02:00
|
|
|
try {
|
|
|
|
Object object;
|
2011-06-10 09:42:36 +02:00
|
|
|
Packet packet;
|
2011-05-26 14:48:22 +02:00
|
|
|
int i;
|
|
|
|
int[] aint;
|
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
if (!this.highPriorityQueue.isEmpty() && (this.f == 0 || System.currentTimeMillis() - ((Packet) this.highPriorityQueue.get(0)).timestamp >= (long) this.f)) {
|
2011-06-10 09:42:36 +02:00
|
|
|
object = this.g;
|
|
|
|
synchronized (this.g) {
|
2011-06-27 00:25:01 +02:00
|
|
|
packet = (Packet) this.highPriorityQueue.remove(0);
|
2011-05-26 14:48:22 +02:00
|
|
|
this.x -= packet.a() + 1;
|
2011-06-10 09:42:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2011-06-27 00:25:01 +02:00
|
|
|
if ((flag || this.lowPriorityQueueDelay-- <= 0) && !this.lowPriorityQueue.isEmpty() && (this.highPriorityQueue.isEmpty() || ((Packet) this.highPriorityQueue.get(0)).timestamp > ((Packet) this.lowPriorityQueue.get(0)).timestamp)) {
|
2011-06-10 09:42:36 +02:00
|
|
|
object = this.g;
|
|
|
|
synchronized (this.g) {
|
2011-06-27 00:25:01 +02:00
|
|
|
packet = (Packet) this.lowPriorityQueue.remove(0);
|
2011-05-26 14:48:22 +02:00
|
|
|
this.x -= packet.a() + 1;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
Packet.a(packet, this.output);
|
2011-05-26 14:48:22 +02:00
|
|
|
aint = e;
|
|
|
|
i = packet.b();
|
2011-05-28 22:50:08 +02:00
|
|
|
aint[i] += packet.a() + 1;
|
2011-06-27 00:25:01 +02:00
|
|
|
this.lowPriorityQueueDelay = 0;
|
2011-05-26 16:20:11 +02:00
|
|
|
flag = true;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
2011-05-28 22:50:08 +02:00
|
|
|
|
|
|
|
return flag;
|
2011-03-27 22:12:39 +02:00
|
|
|
} catch (Exception exception) {
|
2011-05-26 14:48:22 +02:00
|
|
|
if (!this.t) {
|
2011-03-27 22:12:39 +02:00
|
|
|
this.a(exception);
|
|
|
|
}
|
2011-05-26 16:20:11 +02:00
|
|
|
|
2011-05-28 22:50:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public void a() {
|
2011-05-28 22:50:08 +02:00
|
|
|
this.s.interrupt();
|
|
|
|
this.r.interrupt();
|
2011-05-26 14:48:22 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
private boolean h() {
|
2011-05-26 16:20:11 +02:00
|
|
|
boolean flag = false;
|
|
|
|
|
2011-03-27 22:12:39 +02:00
|
|
|
try {
|
2012-07-26 08:17:09 +02:00
|
|
|
// 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
|
2012-01-14 21:03:48 +01:00
|
|
|
Packet packet = Packet.a(this.input, this.packetListener.c());
|
2011-03-27 22:12:39 +02:00
|
|
|
|
|
|
|
if (packet != null) {
|
2011-05-26 14:48:22 +02:00
|
|
|
int[] aint = d;
|
|
|
|
int i = packet.b();
|
|
|
|
|
2011-05-28 22:50:08 +02:00
|
|
|
aint[i] += packet.a() + 1;
|
2012-03-01 11:49:23 +01:00
|
|
|
if (!this.q) {
|
|
|
|
this.m.add(packet);
|
|
|
|
}
|
|
|
|
|
2011-05-26 16:20:11 +02:00
|
|
|
flag = true;
|
2011-03-27 22:12:39 +02:00
|
|
|
} else {
|
|
|
|
this.a("disconnect.endOfStream", new Object[0]);
|
|
|
|
}
|
2011-05-28 22:50:08 +02:00
|
|
|
|
|
|
|
return flag;
|
2011-03-27 22:12:39 +02:00
|
|
|
} catch (Exception exception) {
|
2011-05-26 14:48:22 +02:00
|
|
|
if (!this.t) {
|
2011-03-27 22:12:39 +02:00
|
|
|
this.a(exception);
|
|
|
|
}
|
2011-05-26 16:20:11 +02:00
|
|
|
|
2011-05-28 22:50:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void a(Exception exception) {
|
2012-05-13 01:44:32 +02:00
|
|
|
// exception.printStackTrace(); // CraftBukkit - Remove console spam
|
2011-03-27 22:12:39 +02:00
|
|
|
this.a("disconnect.genericReason", new Object[] { "Internal exception: " + exception.toString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void a(String s, Object... aobject) {
|
2011-05-26 14:48:22 +02:00
|
|
|
if (this.l) {
|
|
|
|
this.t = true;
|
|
|
|
this.u = s;
|
|
|
|
this.v = aobject;
|
2011-03-27 22:12:39 +02:00
|
|
|
(new NetworkMasterThread(this)).start();
|
2011-05-26 14:48:22 +02:00
|
|
|
this.l = false;
|
2011-03-27 22:12:39 +02:00
|
|
|
|
|
|
|
try {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.input.close();
|
|
|
|
this.input = null;
|
2011-03-27 22:12:39 +02:00
|
|
|
} catch (Throwable throwable) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.output.close();
|
|
|
|
this.output = null;
|
2011-03-27 22:12:39 +02:00
|
|
|
} catch (Throwable throwable1) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.socket.close();
|
|
|
|
this.socket = null;
|
2011-03-27 22:12:39 +02:00
|
|
|
} catch (Throwable throwable2) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public void b() {
|
|
|
|
if (this.x > 1048576) {
|
2011-03-27 22:12:39 +02:00
|
|
|
this.a("disconnect.overflow", new Object[0]);
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
if (this.m.isEmpty()) {
|
|
|
|
if (this.w++ == 1200) {
|
2011-03-27 22:12:39 +02:00
|
|
|
this.a("disconnect.timeout", new Object[0]);
|
|
|
|
}
|
|
|
|
} else {
|
2011-05-26 14:48:22 +02:00
|
|
|
this.w = 0;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-09-15 02:23:52 +02:00
|
|
|
int i = 1000;
|
2011-03-27 22:12:39 +02:00
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
while (!this.m.isEmpty() && i-- >= 0) {
|
2012-06-17 08:25:01 +02:00
|
|
|
Packet packet = (Packet) this.m.poll(); // CraftBukkit - remove -> poll
|
2011-03-27 22:12:39 +02:00
|
|
|
|
2012-03-01 11:49:23 +01:00
|
|
|
if (!this.q) packet.handle(this.packetListener); // CraftBukkit
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-28 22:50:08 +02:00
|
|
|
this.a();
|
2011-05-26 14:48:22 +02:00
|
|
|
if (this.t && this.m.isEmpty()) {
|
2012-01-14 21:03:48 +01:00
|
|
|
this.packetListener.a(this.u, this.v);
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public SocketAddress getSocketAddress() {
|
2011-05-26 14:48:22 +02:00
|
|
|
return this.i;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public void d() {
|
2012-03-01 11:49:23 +01:00
|
|
|
if (!this.q) {
|
2012-01-12 23:10:13 +01:00
|
|
|
this.a();
|
2012-03-01 11:49:23 +01:00
|
|
|
this.q = true;
|
2012-01-12 23:10:13 +01:00
|
|
|
this.s.interrupt();
|
|
|
|
(new NetworkMonitorThread(this)).start();
|
|
|
|
}
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public int e() {
|
2011-06-27 00:25:01 +02:00
|
|
|
return this.lowPriorityQueue.size();
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-14 21:03:48 +01:00
|
|
|
public Socket getSocket() {
|
2011-09-15 02:23:52 +02:00
|
|
|
return this.socket;
|
|
|
|
}
|
|
|
|
|
2011-03-27 22:12:39 +02:00
|
|
|
static boolean a(NetworkManager networkmanager) {
|
2011-05-26 14:48:22 +02:00
|
|
|
return networkmanager.l;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static boolean b(NetworkManager networkmanager) {
|
2012-03-01 11:49:23 +01:00
|
|
|
return networkmanager.q;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 16:20:11 +02:00
|
|
|
static boolean c(NetworkManager networkmanager) {
|
2011-09-15 02:23:52 +02:00
|
|
|
return networkmanager.h();
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 16:20:11 +02:00
|
|
|
static boolean d(NetworkManager networkmanager) {
|
2011-09-15 02:23:52 +02:00
|
|
|
return networkmanager.g();
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-28 22:50:08 +02:00
|
|
|
static DataOutputStream e(NetworkManager networkmanager) {
|
|
|
|
return networkmanager.output;
|
2011-05-26 14:48:22 +02:00
|
|
|
}
|
|
|
|
|
2011-05-28 22:50:08 +02:00
|
|
|
static boolean f(NetworkManager networkmanager) {
|
|
|
|
return networkmanager.t;
|
2011-05-26 14:48:22 +02:00
|
|
|
}
|
|
|
|
|
2011-05-28 22:50:08 +02:00
|
|
|
static void a(NetworkManager networkmanager, Exception exception) {
|
|
|
|
networkmanager.a(exception);
|
2011-05-26 14:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Thread g(NetworkManager networkmanager) {
|
|
|
|
return networkmanager.s;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
static Thread h(NetworkManager networkmanager) {
|
|
|
|
return networkmanager.r;
|
2011-03-27 22:12:39 +02:00
|
|
|
}
|
|
|
|
}
|