This commit is contained in:
md_5 2013-06-20 19:23:30 +10:00
parent 0503dd73fd
commit 715a18a7d9

View File

@ -1,4 +1,4 @@
From 184b38aa96c6eb71116c452bff126cd7df9db814 Mon Sep 17 00:00:00 2001
From da93c0bd48b4e34c4561ba975e7561cf60be9ef0 Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Tue, 23 Apr 2013 11:47:32 +1000
Subject: [PATCH] Netty
@ -236,14 +236,13 @@ index 2567bde..7f5f3ea 100644
}
diff --git a/src/main/java/org/spigotmc/MultiplexingServerConnection.java b/src/main/java/org/spigotmc/MultiplexingServerConnection.java
new file mode 100644
index 0000000..c8ea80a
index 0000000..386c2f8
--- /dev/null
+++ b/src/main/java/org/spigotmc/MultiplexingServerConnection.java
@@ -0,0 +1,126 @@
@@ -0,0 +1,136 @@
+package org.spigotmc;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
@ -257,41 +256,38 @@ index 0000000..c8ea80a
+import net.minecraft.server.ServerConnection;
+import org.bukkit.Bukkit;
+
+public class MultiplexingServerConnection extends ServerConnection {
+public class MultiplexingServerConnection extends ServerConnection
+{
+
+ private static final boolean NETTY_DISABLED = Boolean.getBoolean("org.spigotmc.netty.disabled");
+ private final Collection<ServerConnection> children = new HashSet<ServerConnection>();
+ private final List<PendingConnection> pending = Collections.synchronizedList(new ArrayList<PendingConnection>());
+ private final List<PendingConnection> pending = Collections.synchronizedList( new ArrayList<PendingConnection>() );
+ private final HashMap<InetAddress, Long> throttle = new HashMap<InetAddress, Long>();
+
+ public MultiplexingServerConnection(MinecraftServer ms) {
+ super(ms);
+ public MultiplexingServerConnection(MinecraftServer ms)
+ {
+ super( ms );
+
+ // Add primary connection
+ start(ms.server.getIp(), ms.server.getPort());
+ // Add all other connections
+ for (InetSocketAddress address : ms.server.getSecondaryHosts()) {
+ start(address.getAddress().getHostAddress(), address.getPort());
+ }
+ }
+
+ private void start(String ipAddress, int port) {
+ try {
+ // Calculate address, can't use isEmpty due to Java 5
+ InetAddress socketAddress = (ipAddress.length() == 0) ? null : InetAddress.getByName(ipAddress);
+ // Say hello to the log
+ d().getLogger().info("Starting listener #" + children.size() + " on " + (socketAddress == null ? "*" : ipAddress) + ":" + port);
+ // Start connection: Netty / non Netty
+ ServerConnection listener = (NETTY_DISABLED) ? new DedicatedServerConnection(d(), socketAddress, port) : new org.spigotmc.netty.NettyServerConnection(d(), socketAddress, port);
+ // Register with other connections
+ children.add(listener);
+ // Gotta catch em all
+ } catch (Throwable t) {
+ // Just print some info to the log
+ t.printStackTrace();
+ d().getLogger().warning("**** FAILED TO BIND TO PORT!");
+ d().getLogger().warning("The exception was: {0}", t);
+ d().getLogger().warning("Perhaps a server is already running on that port?");
+ for ( SpigotConfig.Listener listener : SpigotConfig.listeners )
+ {
+ try
+ {
+ // Calculate address, can't use isEmpty due to Java 5
+ InetAddress socketAddress = ( listener.host.length() == 0 ) ? null : InetAddress.getByName( listener.host );
+ // Say hello to the log
+ d().getLogger().info( "Starting listener #" + children.size() + " on " + ( socketAddress == null ? "*" : listener.host ) + ":" + listener.port );
+ // Start connection: Netty / non Netty
+ ServerConnection l = ( listener.netty ) ? new DedicatedServerConnection( d(), socketAddress, listener.port ) : new org.spigotmc.netty.NettyServerConnection( d(), socketAddress, listener.port );
+ // Register with other connections
+ children.add( l );
+ // Gotta catch em all
+ } catch ( Throwable t )
+ {
+ // Just print some info to the log
+ t.printStackTrace();
+ d().getLogger().warning( "**** FAILED TO BIND TO PORT!" );
+ d().getLogger().warning( "The exception was: {0}", t );
+ d().getLogger().warning( "Perhaps a server is already running on that port?" );
+ }
+ }
+ }
+
@ -299,8 +295,10 @@ index 0000000..c8ea80a
+ * close.
+ */
+ @Override
+ public void a() {
+ for (ServerConnection child : children) {
+ public void a()
+ {
+ for ( ServerConnection child : children )
+ {
+ child.a();
+ }
+ }
@ -310,20 +308,25 @@ index 0000000..c8ea80a
+ * called from the main server thread a few times a tick.
+ */
+ @Override
+ public void b() {
+ public void b()
+ {
+ super.b(); // pulse PlayerConnections
+ for (int i = 0; i < pending.size(); ++i) {
+ PendingConnection connection = pending.get(i);
+ for ( int i = 0; i < pending.size(); ++i )
+ {
+ PendingConnection connection = pending.get( i );
+
+ try {
+ try
+ {
+ connection.c();
+ } catch (Exception ex) {
+ connection.disconnect("Internal server error");
+ Bukkit.getServer().getLogger().log(Level.WARNING, "Failed to handle packet: " + ex, ex);
+ } catch ( Exception ex )
+ {
+ connection.disconnect( "Internal server error" );
+ Bukkit.getServer().getLogger().log( Level.WARNING, "Failed to handle packet: " + ex, ex );
+ }
+
+ if (connection.b) {
+ pending.remove(i--);
+ if ( connection.b )
+ {
+ pending.remove( i-- );
+ }
+ }
+ }
@ -334,10 +337,13 @@ index 0000000..c8ea80a
+ *
+ * @param address the address to remove
+ */
+ public void unThrottle(InetAddress address) {
+ if (address != null) {
+ synchronized (throttle) {
+ throttle.remove(address);
+ public void unThrottle(InetAddress address)
+ {
+ if ( address != null )
+ {
+ synchronized ( throttle )
+ {
+ throttle.remove( address );
+ }
+ }
+ }
@ -348,24 +354,89 @@ index 0000000..c8ea80a
+ * @param address
+ * @return Whether they must be disconnected
+ */
+ public boolean throttle(InetAddress address) {
+ public boolean throttle(InetAddress address)
+ {
+ long currentTime = System.currentTimeMillis();
+ synchronized (throttle) {
+ Long value = throttle.get(address);
+ if (value != null && !address.isLoopbackAddress() && currentTime - value < d().server.getConnectionThrottle()) {
+ throttle.put(address, currentTime);
+ synchronized ( throttle )
+ {
+ Long value = throttle.get( address );
+ if ( value != null && !address.isLoopbackAddress() && currentTime - value < d().server.getConnectionThrottle() )
+ {
+ throttle.put( address, currentTime );
+ return true;
+ }
+
+ throttle.put(address, currentTime);
+ throttle.put( address, currentTime );
+ }
+ return false;
+ }
+
+ public void register(PendingConnection conn) {
+ pending.add(conn);
+ public void register(PendingConnection conn)
+ {
+ pending.add( conn );
+ }
+}
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
index 2fe3a5d..90d3193 100644
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
@@ -6,6 +6,11 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -130,4 +135,44 @@ public class SpigotConfig
restartScript = getString( "settings.restart-script", restartScript );
WatchdogThread.doStart( timeoutTime, restartOnCrash );
}
+
+ public static class Listener
+ {
+
+ public String host;
+ public int port;
+ public boolean netty;
+ public long connectionThrottle;
+
+ public Listener(String host, int port, boolean netty, long connectionThrottle)
+ {
+ this.host = host;
+ this.port = port;
+ this.netty = netty;
+ this.connectionThrottle = connectionThrottle;
+ }
+ }
+ public static List<Listener> listeners = new ArrayList<Listener>();
+ private void listeners()
+ {
+ Map<String, Object> def = new HashMap<String, Object>();
+ def.put( "host", "default" );
+ def.put( "port", "default" );
+ def.put( "netty", true );
+ def.put( "throttle", "default" );
+
+ config.addDefault( "listeners", Collections.singletonList( def ) );
+ for ( Map<String, Object> info : (List<Map<String, Object>>) config.getList( "listeners" ) )
+ {
+ String host = (String) info.get( "host" );
+ if ( "default".equals( host ) )
+ {
+ host = Bukkit.getIp();
+ }
+ int port = ( info.get( "port" ) instanceof Integer ) ? (Integer) info.get( "port" ) : Bukkit.getPort();
+ boolean netty = (Boolean) info.get( "netty" );
+ long connectionThrottle = ( info.get( "throttle" ) instanceof Number ) ? ( (Number) info.get( "throttle" ) ).longValue() : Bukkit.getConnectionThrottle();
+ this.listeners.add( new Listener( host, port, netty, connectionThrottle ) );
+ }
+ }
}
diff --git a/src/main/java/org/spigotmc/netty/CipherBase.java b/src/main/java/org/spigotmc/netty/CipherBase.java
new file mode 100644
index 0000000..c75a60f