#2041: More robust closing code

This commit is contained in:
md_5 2017-01-16 10:11:17 +11:00
parent c1bdbef9cf
commit 81de9d5a63
4 changed files with 37 additions and 47 deletions

View File

@ -52,17 +52,7 @@ public class ServerConnection implements Server
{ {
Preconditions.checkArgument( reason.length == 0, "Server cannot have disconnect reason" ); Preconditions.checkArgument( reason.length == 0, "Server cannot have disconnect reason" );
if ( !ch.isClosed() ) ch.delayedClose( null );
{
ch.getHandle().eventLoop().schedule( new Runnable()
{
@Override
public void run()
{
ch.getHandle().close();
}
}, 100, TimeUnit.MILLISECONDS );
}
} }
@Override @Override

View File

@ -368,15 +368,7 @@ public final class UserConnection implements ProxiedPlayer
getName(), BaseComponent.toLegacyText( reason ) getName(), BaseComponent.toLegacyText( reason )
} ); } );
ch.delayedClose( new Runnable() ch.delayedClose( new Kick( ComponentSerializer.toString( reason ) ) );
{
@Override
public void run()
{
unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) );
}
} );
if ( server != null ) if ( server != null )
{ {
@ -614,7 +606,7 @@ public final class UserConnection implements ProxiedPlayer
public void setCompressionThreshold(int compressionThreshold) 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; this.compressionThreshold = compressionThreshold;
unsafe.sendPacket( new SetCompression( compressionThreshold ) ); unsafe.sendPacket( new SetCompression( compressionThreshold ) );

View File

@ -140,8 +140,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
public void handle(LegacyHandshake legacyHandshake) throws Exception public void handle(LegacyHandshake legacyHandshake) throws Exception
{ {
this.legacy = true; this.legacy = true;
ch.getHandle().writeAndFlush( bungee.getTranslation( "outdated_client" ) ); ch.close( bungee.getTranslation( "outdated_client" ) );
ch.close();
} }
@Override @Override
@ -492,7 +491,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public void run() public void run()
{ {
if ( ch.getHandle().isActive() ) if ( !ch.isClosing() )
{ {
UserConnection userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this ); UserConnection userCon = new UserConnection( bungee, ch, getName(), InitialHandler.this );
userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() ); userCon.setCompressionThreshold( BungeeCord.getInstance().config.getCompressionThreshold() );
@ -538,18 +537,13 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public void disconnect(final BaseComponent... reason) public void disconnect(final BaseComponent... reason)
{ {
ch.delayedClose( new Runnable() if ( thisState != State.STATUS && thisState != State.PING )
{ {
ch.delayedClose( new Kick( ComponentSerializer.toString( reason ) ) );
@Override } else
public void run() {
{ ch.close();
if ( thisState != State.STATUS && thisState != State.PING ) }
{
unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) );
}
}
} );
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.netty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.util.concurrent.TimeUnit; 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.MinecraftEncoder;
import net.md_5.bungee.protocol.PacketWrapper; import net.md_5.bungee.protocol.PacketWrapper;
import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.packet.Kick;
public class ChannelWrapper public class ChannelWrapper
{ {
@ -56,19 +58,37 @@ public class ChannelWrapper
} }
public void close() public void close()
{
close( null );
}
public void close(Object packet)
{ {
if ( !closed ) if ( !closed )
{ {
closed = closing = true; 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 ) if ( !closing )
{ {
closing = true; closing = true;
@ -82,13 +102,7 @@ public class ChannelWrapper
@Override @Override
public void run() public void run()
{ {
try close( kick );
{
runnable.run();
} finally
{
ChannelWrapper.this.close();
}
} }
}, 250, TimeUnit.MILLISECONDS ); }, 250, TimeUnit.MILLISECONDS );
} }