Optimize / clean up server disconnect method

This commit is contained in:
md_5 2014-02-08 09:33:05 +11:00
parent f08df9555c
commit 4abffa9f24
3 changed files with 18 additions and 21 deletions

View File

@ -1,7 +1,7 @@
package net.md_5.bungee;
import io.netty.channel.ChannelFutureListener;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@ -41,7 +41,7 @@ public class ServerConnection implements Server
}
@Override
public synchronized void disconnect(String reason)
public void disconnect(String reason)
{
disconnect( TextComponent.fromLegacyText( reason ) );
}
@ -49,20 +49,7 @@ public class ServerConnection implements Server
@Override
public void disconnect(BaseComponent... reason)
{
if ( !ch.isClosed() )
{
// TODO: Can we just use a future here?
unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) );
ch.getHandle().eventLoop().schedule( new Runnable()
{
@Override
public void run()
{
ch.getHandle().close();
}
}, 100, TimeUnit.MILLISECONDS );
}
ch.write( new Kick( ComponentSerializer.toString( reason ) ), ChannelFutureListener.CLOSE );
}
@Override

View File

@ -1,8 +1,6 @@
package net.md_5.bungee;
import com.google.common.base.Preconditions;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import java.util.Objects;
import java.util.Queue;
import lombok.RequiredArgsConstructor;

View File

@ -3,6 +3,8 @@ package net.md_5.bungee.netty;
import net.md_5.bungee.protocol.PacketWrapper;
import com.google.common.base.Preconditions;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.Getter;
@ -34,22 +36,32 @@ public class ChannelWrapper
ch.pipeline().get( MinecraftEncoder.class ).setProtocolVersion( protocol );
}
public synchronized void write(Object packet)
public synchronized void write(Object packet, ChannelFutureListener future)
{
if ( !closed )
{
ChannelFuture listener;
if ( packet instanceof PacketWrapper )
{
( (PacketWrapper) packet ).setReleased( true );
ch.write( ( (PacketWrapper) packet ).buf, ch.voidPromise() );
listener = ch.write( ( (PacketWrapper) packet ).buf, ch.voidPromise() );
} else
{
ch.write( packet, ch.voidPromise() );
listener = ch.write( packet, ch.voidPromise() );
}
if ( future != null )
{
listener.addListener( future );
}
ch.flush();
}
}
public synchronized void write(Object packet)
{
write( packet, null );
}
public synchronized void close()
{
if ( !closed )