diff --git a/client/src/client/Game.java b/client/src/client/Game.java index f754b50..abdb07b 100755 --- a/client/src/client/Game.java +++ b/client/src/client/Game.java @@ -109,6 +109,7 @@ import common.entity.types.IEntityFX; import common.future.Futures; import common.future.ListenableFuture; import common.future.ListenableFutureTask; +import common.future.ThreadFactoryBuilder; import common.init.BlockRegistry; import common.init.Config; import common.init.EntityRegistry; @@ -129,6 +130,10 @@ import common.model.ParticleType; import common.nbt.NBTTagCompound; import common.network.IThreadListener; import common.network.NetConnection; +import common.network.PacketDecoder; +import common.network.PacketEncoder; +import common.network.PacketPrepender; +import common.network.PacketSplitter; import common.network.NetHandler.ThreadQuickExitException; import common.packet.CPacketAction; import common.packet.CPacketCheat; @@ -151,6 +156,7 @@ import common.util.ExtMath; import common.util.Facing; import common.util.FileUtils; import common.util.HitPosition; +import common.util.LazyLoadBase; import common.util.Util; import common.util.HitPosition.ObjectType; import common.world.Chunk; @@ -159,6 +165,15 @@ import common.world.Region; import common.world.State; import common.world.World; import common.world.WorldClient; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelException; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.timeout.ReadTimeoutHandler; /* Een net ganz funktionierndes Programm ... @@ -247,6 +262,13 @@ public class Game implements IThreadListener, IClient { } public static final int LOG_BUFFER = 32768; + private static final LazyLoadBase CLIENT_NIO_EVENTLOOP = new LazyLoadBase() + { + protected NioEventLoopGroup load() + { + return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Client IO #%d").setDaemon(true).build()); + } + }; private final Queue> tasks = new ArrayDeque>(); private final Map bars = Maps.newTreeMap(); @@ -446,13 +468,35 @@ public class Game implements IThreadListener, IClient { return INSTANCE; } + private static NetConnection createNetworkManagerAndConnect(InetAddress address, int serverPort) + { + final NetConnection networkmanager = new NetConnection(); + ((Bootstrap)((Bootstrap)((Bootstrap)(new Bootstrap()).group(CLIENT_NIO_EVENTLOOP.getValue())).handler(new ChannelInitializer() + { + protected void initChannel(Channel p_initChannel_1_) throws Exception + { + try + { + p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true)); + } + catch (ChannelException var3) + { + ; + } + + p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(30))).addLast((String)"splitter", (ChannelHandler)(new PacketSplitter())).addLast((String)"decoder", (ChannelHandler)(new PacketDecoder(false))).addLast((String)"prepender", (ChannelHandler)(new PacketPrepender())).addLast((String)"encoder", (ChannelHandler)(new PacketEncoder(true))).addLast((String)"packet_handler", (ChannelHandler)networkmanager); + } + })).channel(NioSocketChannel.class)).connect(address, serverPort).syncUninterruptibly(); + return networkmanager; + } + public void connect(String address, int port, String user, String pass, String access) { this.displayGuiScreen(GuiLoading.makeWaitTask("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port + " ...")); Log.JNI.info("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port); NetConnection connection = null; try { - connection = NetConnection.createNetworkManagerAndConnect(address == null ? InetAddress.getLoopbackAddress() : InetAddress.getByName(IDN.toASCII(address)), port); + connection = createNetworkManagerAndConnect(address == null ? InetAddress.getLoopbackAddress() : InetAddress.getByName(IDN.toASCII(address)), port); connection.setNetHandler(new ClientLoginHandler(connection, this)); connection.sendPacket(new HPacketHandshake(Config.PROTOCOL)); connection.sendPacket(new LPacketPasswordResponse(user, access, pass)); diff --git a/common/src/common/network/NetConnection.java b/common/src/common/network/NetConnection.java index 89c055a..a77835e 100755 --- a/common/src/common/network/NetConnection.java +++ b/common/src/common/network/NetConnection.java @@ -1,31 +1,18 @@ package common.network; -import java.net.InetAddress; import java.net.SocketAddress; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.regex.Pattern; -import common.future.ThreadFactoryBuilder; import common.log.Log; import common.network.NetHandler.ThreadQuickExitException; -import common.util.LazyLoadBase; -import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; -import io.netty.channel.ChannelException; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; import io.netty.channel.SimpleChannelInboundHandler; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.TimeoutException; import io.netty.util.AttributeKey; import io.netty.util.concurrent.Future; @@ -35,13 +22,6 @@ public class NetConnection extends SimpleChannelInboundHandler { private static final Pattern IP_REPLACER = Pattern.compile("([0-9]*)\\.([0-9]*)\\.[0-9]*\\.[0-9]*"); public static final AttributeKey ATTR_STATE = AttributeKey.valueOf("protocol"); - public static final LazyLoadBase CLIENT_NIO_EVENTLOOP = new LazyLoadBase() - { - protected NioEventLoopGroup load() - { - return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Client IO #%d").setDaemon(true).build()); - } - }; private final Queue outboundPacketsQueue = new ConcurrentLinkedQueue(); private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); @@ -290,48 +270,6 @@ public class NetConnection extends SimpleChannelInboundHandler this.terminationReason = message; } } - - /** - * Create a new NetworkManager from the server host and connect it to the server - * - * @param address The address of the server - * @param serverPort The server port - */ - public static NetConnection createNetworkManagerAndConnect(InetAddress address, int serverPort) - { - final NetConnection networkmanager = new NetConnection(); - Class oclass; - LazyLoadBase lazyloadbase; - -// if (Epoll.isAvailable()) -// { -// oclass = EpollSocketChannel.class; -// lazyloadbase = CLIENT_EPOLL_EVENTLOOP; -// } -// else -// { - oclass = NioSocketChannel.class; - lazyloadbase = CLIENT_NIO_EVENTLOOP; -// } - - ((Bootstrap)((Bootstrap)((Bootstrap)(new Bootstrap()).group((EventLoopGroup)lazyloadbase.getValue())).handler(new ChannelInitializer() - { - protected void initChannel(Channel p_initChannel_1_) throws Exception - { - try - { - p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true)); - } - catch (ChannelException var3) - { - ; - } - - p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(30))).addLast((String)"splitter", (ChannelHandler)(new PacketSplitter())).addLast((String)"decoder", (ChannelHandler)(new PacketDecoder(false))).addLast((String)"prepender", (ChannelHandler)(new PacketPrepender())).addLast((String)"encoder", (ChannelHandler)(new PacketEncoder(true))).addLast((String)"packet_handler", (ChannelHandler)networkmanager); - } - })).channel(oclass)).connect(address, serverPort).syncUninterruptibly(); - return networkmanager; - } public boolean isChannelOpen() {