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.Futures;
import common.future.ListenableFuture; import common.future.ListenableFuture;
import common.future.ListenableFutureTask; import common.future.ListenableFutureTask;
import common.future.ThreadFactoryBuilder;
import common.init.BlockRegistry; import common.init.BlockRegistry;
import common.init.Config; import common.init.Config;
import common.init.EntityRegistry; import common.init.EntityRegistry;
@ -129,6 +130,10 @@ import common.model.ParticleType;
import common.nbt.NBTTagCompound; import common.nbt.NBTTagCompound;
import common.network.IThreadListener; import common.network.IThreadListener;
import common.network.NetConnection; 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.network.NetHandler.ThreadQuickExitException;
import common.packet.CPacketAction; import common.packet.CPacketAction;
import common.packet.CPacketCheat; import common.packet.CPacketCheat;
@ -151,6 +156,7 @@ import common.util.ExtMath;
import common.util.Facing; import common.util.Facing;
import common.util.FileUtils; import common.util.FileUtils;
import common.util.HitPosition; import common.util.HitPosition;
import common.util.LazyLoadBase;
import common.util.Util; import common.util.Util;
import common.util.HitPosition.ObjectType; import common.util.HitPosition.ObjectType;
import common.world.Chunk; import common.world.Chunk;
@ -159,6 +165,15 @@ import common.world.Region;
import common.world.State; import common.world.State;
import common.world.World; import common.world.World;
import common.world.WorldClient; 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 ... Een net ganz funktionierndes Programm ...
@ -247,6 +262,13 @@ public class Game implements IThreadListener, IClient {
} }
public static final int LOG_BUFFER = 32768; 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 Queue<FutureTask<?>> tasks = new ArrayDeque<FutureTask<?>>();
private final Map<Integer, Long> bars = Maps.newTreeMap(); private final Map<Integer, Long> bars = Maps.newTreeMap();
@ -446,13 +468,35 @@ public class Game implements IThreadListener, IClient {
return INSTANCE; 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) { public void connect(String address, int port, String user, String pass, String access) {
this.displayGuiScreen(GuiLoading.makeWaitTask("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port + " ...")); this.displayGuiScreen(GuiLoading.makeWaitTask("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port + " ..."));
Log.JNI.info("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port); Log.JNI.info("Verbinde zu " + (address == null ? "localhost" : address) + ":" + port);
NetConnection connection = null; NetConnection connection = null;
try 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.setNetHandler(new ClientLoginHandler(connection, this));
connection.sendPacket(new HPacketHandshake(Config.PROTOCOL)); connection.sendPacket(new HPacketHandshake(Config.PROTOCOL));
connection.sendPacket(new LPacketPasswordResponse(user, access, pass)); connection.sendPacket(new LPacketPasswordResponse(user, access, pass));

View file

@ -1,31 +1,18 @@
package common.network; package common.network;
import java.net.InetAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import common.future.ThreadFactoryBuilder;
import common.log.Log; import common.log.Log;
import common.network.NetHandler.ThreadQuickExitException; import common.network.NetHandler.ThreadQuickExitException;
import common.util.LazyLoadBase;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelException;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; 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.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.handler.timeout.TimeoutException;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
@ -35,13 +22,6 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
{ {
private static final Pattern IP_REPLACER = Pattern.compile("([0-9]*)\\.([0-9]*)\\.[0-9]*\\.[0-9]*"); private static final Pattern IP_REPLACER = Pattern.compile("([0-9]*)\\.([0-9]*)\\.[0-9]*\\.[0-9]*");
public static final AttributeKey<PacketRegistry> ATTR_STATE = AttributeKey.<PacketRegistry>valueOf("protocol"); public static final AttributeKey<PacketRegistry> ATTR_STATE = AttributeKey.<PacketRegistry>valueOf("protocol");
public 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<NetConnection.InboundHandlerTuplePacketListener> outboundPacketsQueue = new ConcurrentLinkedQueue<InboundHandlerTuplePacketListener>(); private final Queue<NetConnection.InboundHandlerTuplePacketListener> outboundPacketsQueue = new ConcurrentLinkedQueue<InboundHandlerTuplePacketListener>();
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
@ -290,48 +270,6 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
this.terminationReason = message; 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 <? extends SocketChannel > oclass;
LazyLoadBase <? extends EventLoopGroup > 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<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(oclass)).connect(address, serverPort).syncUninterruptibly();
return networkmanager;
}
public boolean isChannelOpen() public boolean isChannelOpen()
{ {