Paper/src/main/java/net/minecraft/server/NetworkManager.java

263 Zeilen
7.1 KiB
Java

2011-03-27 22:12:39 +02:00
package net.minecraft.server;
2011-04-20 22:47:26 +02:00
import java.io.BufferedInputStream;
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-05-14 16:29:42 +02:00
import java.io.IOException; // CraftBukkit - instead of SocketException
2011-03-27 22:12:39 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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;
private DataInputStream input;
private DataOutputStream output;
2011-05-26 14:48:22 +02:00
private boolean l = true;
2011-03-27 22:12:39 +02:00
private List m = Collections.synchronizedList(new ArrayList());
2011-05-26 14:48:22 +02:00
private List n = Collections.synchronizedList(new ArrayList());
private List o = Collections.synchronizedList(new ArrayList());
private NetHandler p;
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 int x = 0;
private transient boolean y = false;
public static int[] d = new int[256];
public static int[] e = new int[256];
public int f = 0;
private int z = 50;
2011-03-27 22:12:39 +02:00
public NetworkManager(Socket socket, String s, NetHandler nethandler) {
this.socket = socket;
2011-05-26 14:48:22 +02:00
this.i = socket.getRemoteSocketAddress();
this.p = nethandler;
2011-03-27 22:12:39 +02:00
try {
2011-04-20 22:47:26 +02:00
socket.setSoTimeout(30000);
2011-03-27 22:12:39 +02:00
socket.setTrafficClass(24);
2011-04-20 22:47:26 +02:00
2011-05-14 16:29:42 +02:00
// CraftBukkit start - cant compile these outside the try
this.input = new DataInputStream(socket.getInputStream());
this.output = new DataOutputStream(socket.getOutputStream());
2011-04-20 22:47:26 +02:00
// CraftBukkit end
} catch (IOException socketexception) {
2011-03-31 22:40:00 +02:00
System.err.println(socketexception.getMessage());
}
2011-03-27 22:12:39 +02:00
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) {
2011-05-26 14:48:22 +02:00
this.p = nethandler;
2011-03-27 22:12:39 +02:00
}
public void a(Packet packet) {
2011-05-26 14:48:22 +02:00
if (!this.q) {
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;
2011-03-27 22:12:39 +02:00
if (packet.k) {
2011-05-26 14:48:22 +02:00
this.o.add(packet);
2011-03-27 22:12:39 +02:00
} else {
2011-05-26 14:48:22 +02:00
this.n.add(packet);
2011-03-27 22:12:39 +02:00
}
}
}
}
2011-05-26 14:48:22 +02:00
private void f() {
2011-03-27 22:12:39 +02:00
try {
Object object;
Packet packet;
2011-05-26 14:48:22 +02:00
int i;
int[] aint;
if (!this.n.isEmpty() && (this.f == 0 || System.currentTimeMillis() - ((Packet) this.n.get(0)).timestamp >= (long) this.f)) {
object = this.g;
synchronized (this.g) {
packet = (Packet) this.n.remove(0);
this.x -= packet.a() + 1;
2011-03-27 22:12:39 +02:00
}
Packet.a(packet, this.output);
2011-05-26 14:48:22 +02:00
aint = e;
i = packet.b();
aint[i] += packet.a();
2011-03-27 22:12:39 +02:00
}
2011-05-26 14:48:22 +02:00
// Craftbukkit - we used to enforce package priorities, we don't now. Do we need to redo this? - TODO
if (this.z-- <= 0 && !this.o.isEmpty() && (this.f == 0 || System.currentTimeMillis() - ((Packet) this.o.get(0)).timestamp >= (long) this.f)) {
object = this.g;
synchronized (this.g) {
packet = (Packet) this.o.remove(0);
this.x -= packet.a() + 1;
2011-03-27 22:12:39 +02:00
}
Packet.a(packet, this.output);
2011-05-26 14:48:22 +02:00
aint = e;
i = packet.b();
aint[i] += packet.a();
this.z = 50;
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 14:48:22 +02:00
public void a() {
this.y = true;
}
private void g() {
2011-03-27 22:12:39 +02:00
try {
2011-05-26 14:48:22 +02:00
Packet packet = Packet.a(this.input, this.p.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();
aint[i] += packet.a();
this.m.add(packet);
2011-03-27 22:12:39 +02:00
} else {
this.a("disconnect.endOfStream", new Object[0]);
}
} 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);
}
}
}
private void a(Exception exception) {
exception.printStackTrace();
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 {
this.input.close();
this.input = null;
2011-03-27 22:12:39 +02:00
} catch (Throwable throwable) {
;
}
try {
this.output.close();
this.output = null;
2011-03-27 22:12:39 +02:00
} catch (Throwable throwable1) {
;
}
try {
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
}
int i = 100;
2011-05-26 14:48:22 +02:00
while (!this.m.isEmpty() && i-- >= 0) {
Packet packet = (Packet) this.m.remove(0);
2011-03-27 22:12:39 +02:00
2011-05-26 14:48:22 +02:00
packet.a(this.p);
2011-03-27 22:12:39 +02:00
}
2011-05-26 14:48:22 +02:00
if (this.t && this.m.isEmpty()) {
this.p.a(this.u, this.v);
2011-03-27 22:12:39 +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() {
this.a();
this.q = true;
this.s.interrupt();
2011-03-27 22:12:39 +02:00
(new ThreadMonitorConnection(this)).start();
}
2011-05-26 14:48:22 +02:00
public int e() {
return this.o.size();
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) {
2011-05-26 14:48:22 +02:00
return networkmanager.q;
2011-03-27 22:12:39 +02:00
}
static void c(NetworkManager networkmanager) {
2011-05-26 14:48:22 +02:00
networkmanager.g();
2011-03-27 22:12:39 +02:00
}
static void d(NetworkManager networkmanager) {
2011-05-26 14:48:22 +02:00
networkmanager.f();
2011-03-27 22:12:39 +02:00
}
2011-05-26 14:48:22 +02:00
static boolean e(NetworkManager networkmanager) {
return networkmanager.y;
}
static boolean a(NetworkManager networkmanager, boolean flag) {
return networkmanager.y = flag;
}
static DataOutputStream f(NetworkManager networkmanager) {
return networkmanager.output;
}
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
}
}