net client

This commit is contained in:
Sen 2025-05-08 13:00:17 +02:00
parent 6b7923cf41
commit 193a12b00f
2 changed files with 45 additions and 63 deletions

View file

@ -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<NioEventLoopGroup> CLIENT_NIO_EVENTLOOP = new LazyLoadBase<NioEventLoopGroup>()
{
protected NioEventLoopGroup load()
{
return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Client IO #%d").setDaemon(true).build());
}
};
private final Queue<FutureTask<?>> tasks = new ArrayDeque<FutureTask<?>>();
private final Map<Integer, Long> 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<Channel>()
{
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));