#2041: More robust closing code
This commit is contained in:
		| @@ -52,17 +52,7 @@ public class ServerConnection implements Server | ||||
|     { | ||||
|         Preconditions.checkArgument( reason.length == 0, "Server cannot have disconnect reason" ); | ||||
|  | ||||
|         if ( !ch.isClosed() ) | ||||
|         { | ||||
|             ch.getHandle().eventLoop().schedule( new Runnable() | ||||
|             { | ||||
|                 @Override | ||||
|                 public void run() | ||||
|                 { | ||||
|                     ch.getHandle().close(); | ||||
|                 } | ||||
|             }, 100, TimeUnit.MILLISECONDS ); | ||||
|         } | ||||
|         ch.delayedClose( null ); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -368,15 +368,7 @@ public final class UserConnection implements ProxiedPlayer | ||||
|                 getName(), BaseComponent.toLegacyText( reason ) | ||||
|             } ); | ||||
|  | ||||
|             ch.delayedClose( new Runnable() | ||||
|             { | ||||
|  | ||||
|                 @Override | ||||
|                 public void run() | ||||
|                 { | ||||
|                     unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) ); | ||||
|                 } | ||||
|             } ); | ||||
|             ch.delayedClose( new Kick( ComponentSerializer.toString( reason ) ) ); | ||||
|  | ||||
|             if ( server != null ) | ||||
|             { | ||||
| @@ -614,7 +606,7 @@ public final class UserConnection implements ProxiedPlayer | ||||
|  | ||||
|     public void setCompressionThreshold(int compressionThreshold) | ||||
|     { | ||||
|         if ( ch.getHandle().isActive() && this.compressionThreshold == -1 && compressionThreshold >= 0 ) | ||||
|         if ( !ch.isClosing() && this.compressionThreshold == -1 && compressionThreshold >= 0 ) | ||||
|         { | ||||
|             this.compressionThreshold = compressionThreshold; | ||||
|             unsafe.sendPacket( new SetCompression( compressionThreshold ) ); | ||||
|   | ||||
| @@ -140,8 +140,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection | ||||
|     public void handle(LegacyHandshake legacyHandshake) throws Exception | ||||
|     { | ||||
|         this.legacy = true; | ||||
|         ch.getHandle().writeAndFlush( bungee.getTranslation( "outdated_client" ) ); | ||||
|         ch.close(); | ||||
|         ch.close( bungee.getTranslation( "outdated_client" ) ); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -492,7 +491,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection | ||||
|                     @Override | ||||
|                     public void run() | ||||
|                     { | ||||
|                         if ( ch.getHandle().isActive() ) | ||||
|                         if ( !ch.isClosing() ) | ||||
|                         { | ||||
|                             UserConnection userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this ); | ||||
|                             userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() ); | ||||
| @@ -538,18 +537,13 @@ public class InitialHandler extends PacketHandler implements PendingConnection | ||||
|     @Override | ||||
|     public void disconnect(final BaseComponent... reason) | ||||
|     { | ||||
|         ch.delayedClose( new Runnable() | ||||
|         if ( thisState != State.STATUS && thisState != State.PING ) | ||||
|         { | ||||
|  | ||||
|             @Override | ||||
|             public void run() | ||||
|             { | ||||
|                 if ( thisState != State.STATUS && thisState != State.PING ) | ||||
|                 { | ||||
|                     unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) ); | ||||
|                 } | ||||
|             } | ||||
|         } ); | ||||
|             ch.delayedClose( new Kick( ComponentSerializer.toString( reason ) ) ); | ||||
|         } else | ||||
|         { | ||||
|             ch.close(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -2,6 +2,7 @@ package net.md_5.bungee.netty; | ||||
|  | ||||
| import com.google.common.base.Preconditions; | ||||
| import io.netty.channel.Channel; | ||||
| import io.netty.channel.ChannelFutureListener; | ||||
| import io.netty.channel.ChannelHandler; | ||||
| import io.netty.channel.ChannelHandlerContext; | ||||
| import java.util.concurrent.TimeUnit; | ||||
| @@ -12,6 +13,7 @@ import net.md_5.bungee.protocol.MinecraftDecoder; | ||||
| import net.md_5.bungee.protocol.MinecraftEncoder; | ||||
| import net.md_5.bungee.protocol.PacketWrapper; | ||||
| import net.md_5.bungee.protocol.Protocol; | ||||
| import net.md_5.bungee.protocol.packet.Kick; | ||||
|  | ||||
| public class ChannelWrapper | ||||
| { | ||||
| @@ -56,19 +58,37 @@ public class ChannelWrapper | ||||
|     } | ||||
|  | ||||
|     public void close() | ||||
|     { | ||||
|         close( null ); | ||||
|     } | ||||
|  | ||||
|     public void close(Object packet) | ||||
|     { | ||||
|         if ( !closed ) | ||||
|         { | ||||
|             closed = closing = true; | ||||
|             ch.flush(); | ||||
|             ch.close(); | ||||
|  | ||||
|             if ( packet != null && ch.isActive() ) | ||||
|             { | ||||
|                 ch.writeAndFlush( packet ).addListeners( ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, ChannelFutureListener.CLOSE ); | ||||
|                 ch.eventLoop().schedule( new Runnable() | ||||
|                 { | ||||
|                     @Override | ||||
|                     public void run() | ||||
|                     { | ||||
|                         ch.close(); | ||||
|                     } | ||||
|                 }, 250, TimeUnit.MILLISECONDS ); | ||||
|             } else | ||||
|             { | ||||
|                 ch.flush(); | ||||
|                 ch.close(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public void delayedClose(final Runnable runnable) | ||||
|     public void delayedClose(final Kick kick) | ||||
|     { | ||||
|         Preconditions.checkArgument( runnable != null, "runnable" ); | ||||
|  | ||||
|         if ( !closing ) | ||||
|         { | ||||
|             closing = true; | ||||
| @@ -82,13 +102,7 @@ public class ChannelWrapper | ||||
|                 @Override | ||||
|                 public void run() | ||||
|                 { | ||||
|                     try | ||||
|                     { | ||||
|                         runnable.run(); | ||||
|                     } finally | ||||
|                     { | ||||
|                         ChannelWrapper.this.close(); | ||||
|                     } | ||||
|                     close( kick ); | ||||
|                 } | ||||
|             }, 250, TimeUnit.MILLISECONDS ); | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 md_5
					md_5