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

124 Zeilen
3.9 KiB
Java

package net.minecraft.server;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class DedicatedServerConnectionThread extends Thread {
2013-03-13 23:33:27 +01:00
private final List a = Collections.synchronizedList(new ArrayList());
private final HashMap b = new HashMap();
private int c = 0;
private final ServerSocket d;
private ServerConnection e;
private final InetAddress f;
private final int g;
long connectionThrottle; // CraftBukkit
public DedicatedServerConnectionThread(ServerConnection serverconnection, InetAddress inetaddress, int i) throws IOException { // CraftBukkit - added throws
super("Listen thread");
2013-03-13 23:33:27 +01:00
this.e = serverconnection;
this.g = i;
this.d = new ServerSocket(i, 0, inetaddress);
this.f = inetaddress == null ? this.d.getInetAddress() : inetaddress;
this.d.setPerformancePreferences(0, 2, 1);
}
public void a() {
2013-03-13 23:33:27 +01:00
List list = this.a;
2013-03-13 23:33:27 +01:00
synchronized (this.a) {
for (int i = 0; i < this.a.size(); ++i) {
PendingConnection pendingconnection = (PendingConnection) this.a.get(i);
try {
2012-12-20 05:03:52 +01:00
pendingconnection.c();
} catch (Exception exception) {
2012-12-20 05:03:52 +01:00
pendingconnection.disconnect("Internal server error");
2013-03-13 23:33:27 +01:00
this.e.d().getLogger().warning("Failed to handle packet for " + pendingconnection.getName() + ": " + exception, (Throwable) exception);
}
2013-03-13 23:33:27 +01:00
if (pendingconnection.b) {
this.a.remove(i--);
}
2012-12-20 05:03:52 +01:00
pendingconnection.networkManager.a();
}
}
}
public void run() {
2013-03-13 23:33:27 +01:00
while (this.e.a) {
try {
2013-03-13 23:33:27 +01:00
Socket socket = this.d.accept();
2013-03-20 21:09:23 +01:00
// CraftBukkit start - connection throttle
InetAddress address = socket.getInetAddress();
long currentTime = System.currentTimeMillis();
2013-03-13 23:33:27 +01:00
if (((MinecraftServer) this.e.d()).server == null) {
socket.close();
continue;
}
2013-03-13 23:33:27 +01:00
connectionThrottle = ((MinecraftServer) this.e.d()).server.getConnectionThrottle();
2013-03-13 23:33:27 +01:00
synchronized (this.b) {
2013-03-20 21:09:23 +01:00
if (this.b.containsKey(address) && !"127.0.0.1".equals(address.getHostAddress()) && currentTime - ((Long) this.b.get(address)).longValue() < connectionThrottle) {
this.b.put(address, Long.valueOf(currentTime));
socket.close();
continue;
}
2013-03-20 21:09:23 +01:00
this.b.put(address, Long.valueOf(currentTime));
}
2013-03-20 21:09:23 +01:00
// CraftBukkit end
2013-03-13 23:33:27 +01:00
PendingConnection pendingconnection = new PendingConnection(this.e.d(), socket, "Connection #" + this.c++);
2012-12-20 05:03:52 +01:00
this.a(pendingconnection);
} catch (IOException ioexception) {
2013-03-13 23:33:27 +01:00
this.e.d().getLogger().warning("DSCT: " + ioexception.getMessage()); // CraftBukkit
}
}
2013-03-13 23:33:27 +01:00
this.e.d().getLogger().info("Closing listening thread");
}
2012-12-20 05:03:52 +01:00
private void a(PendingConnection pendingconnection) {
if (pendingconnection == null) {
throw new IllegalArgumentException("Got null pendingconnection!");
} else {
2013-03-13 23:33:27 +01:00
List list = this.a;
2013-03-13 23:33:27 +01:00
synchronized (this.a) {
this.a.add(pendingconnection);
}
}
}
public void a(InetAddress inetaddress) {
if (inetaddress != null) {
2013-03-13 23:33:27 +01:00
HashMap hashmap = this.b;
2013-03-13 23:33:27 +01:00
synchronized (this.b) {
this.b.remove(inetaddress);
}
}
}
public void b() {
try {
2013-03-13 23:33:27 +01:00
this.d.close();
} catch (Throwable throwable) {
;
}
}
}