small network code cleanup

This commit is contained in:
Sen 2025-05-24 20:42:53 +02:00
parent bd4b8d427a
commit d6d934f1f3
2 changed files with 203 additions and 264 deletions

View file

@ -23,8 +23,7 @@ import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
public class NetConnection extends SimpleChannelInboundHandler<Packet>
{
public class NetConnection extends SimpleChannelInboundHandler<Packet> {
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");
@ -37,44 +36,36 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
private String exitReason;
private boolean disconnected;
public void channelActive(ChannelHandlerContext context) throws Exception
{
public void channelActive(ChannelHandlerContext context) throws Exception {
super.channelActive(context);
this.channel = context.channel();
this.address = this.channel.remoteAddress();
try
{
try {
this.setConnectionState(PacketRegistry.HANDSHAKE);
}
catch (Throwable t)
{
catch(Throwable t) {
Log.JNI.error(t, "Fehler beim Aufbauen der Verbindung für Handshake");
}
}
public void setConnectionState(PacketRegistry newState)
{
public void setConnectionState(PacketRegistry newState) {
this.channel.attr(ATTR_STATE).set(newState);
this.channel.config().setAutoRead(true);
// Log.debug("Automatisches Lesen eingeschaltet");
}
public void channelInactive(ChannelHandlerContext context) throws Exception
{
public void channelInactive(ChannelHandlerContext context) throws Exception {
this.closeChannel("Ende der Datenübertragung");
}
public void exceptionCaught(ChannelHandlerContext context, Throwable throwable) throws Exception
{
public void exceptionCaught(ChannelHandlerContext context, Throwable throwable) throws Exception {
String comp;
if (throwable instanceof TimeoutException)
{
if(throwable instanceof TimeoutException) {
comp = "Zeitüberschreitung";
}
else
{
else {
comp = "Interner Fehler: " + throwable;
if(!(throwable instanceof ClosedChannelException))
Log.SYSTEM.error(throwable, "Fehler in der Verbindung mit %s", this.getCutAddress());
@ -83,149 +74,118 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
this.closeChannel(comp);
}
protected void channelRead0(ChannelHandlerContext context, Packet packet) throws Exception
{
if (this.channel.isOpen())
{
try
{
protected void channelRead0(ChannelHandlerContext context, Packet packet) throws Exception {
if(this.channel.isOpen()) {
try {
packet.processPacket(this.handler);
}
catch (ThreadQuickExitException e)
{
catch(ThreadQuickExitException e) {
;
}
}
}
public void setNetHandler(NetHandler handler)
{
if (handler == null) {
public void setNetHandler(NetHandler handler) {
if(handler == null) {
throw new NullPointerException("Handler ist Null");
}
// Log.debug("Setze Handler von " + this + " auf " + handler);
this.handler = handler;
}
public void sendPacket(Packet packetIn)
{
if (this.isChannelOpen())
{
public void sendPacket(Packet packet) {
if(this.isChannelOpen()) {
this.flushOutboundQueue();
this.dispatchPacket(packetIn, null);
this.dispatchPacket(packet, null);
}
else
{
else {
this.lock.writeLock().lock();
try
{
this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packetIn, null));
try {
this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packet, null));
}
finally
{
finally {
this.lock.writeLock().unlock();
}
}
}
public void sendPacket(Packet packetIn, GenericFutureListener <? extends Future <? super Void >> listener)
{
if (this.isChannelOpen())
{
public void sendPacket(Packet packet, GenericFutureListener<? extends Future<? super Void>> listener) {
if(this.isChannelOpen()) {
this.flushOutboundQueue();
this.dispatchPacket(packetIn, new GenericFutureListener[] {listener});
this.dispatchPacket(packet, new GenericFutureListener[] {listener});
}
else
{
else {
this.lock.writeLock().lock();
try
{
this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packetIn, listener));
try {
this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packet, listener));
}
finally
{
finally {
this.lock.writeLock().unlock();
}
}
}
private void dispatchPacket(final Packet inPacket, final GenericFutureListener <? extends Future <? super Void >> [] futureListeners)
{
final PacketRegistry state = PacketRegistry.getType(inPacket);
private void dispatchPacket(final Packet packet, final GenericFutureListener<? extends Future<? super Void>>[] listeners) {
final PacketRegistry state = PacketRegistry.getType(packet);
final PacketRegistry current = this.channel.attr(ATTR_STATE).get();
if (current != state)
{
if(current != state) {
// Log.debug("Automatisches Lesen ausgeschaltet");
this.channel.config().setAutoRead(false);
}
if (this.channel.eventLoop().inEventLoop())
{
if (state != current)
{
if(this.channel.eventLoop().inEventLoop()) {
if(state != current) {
this.setConnectionState(state);
}
ChannelFuture channelfuture = this.channel.writeAndFlush(inPacket);
ChannelFuture future = this.channel.writeAndFlush(packet);
if (futureListeners != null)
{
channelfuture.addListeners(futureListeners);
if(listeners != null) {
future.addListeners(listeners);
}
channelfuture.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
future.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
else
{
this.channel.eventLoop().execute(new Runnable()
{
public void run()
{
if (state != current)
{
else {
this.channel.eventLoop().execute(new Runnable() {
public void run() {
if(state != current) {
NetConnection.this.setConnectionState(state);
}
ChannelFuture channelfuture1 = NetConnection.this.channel.writeAndFlush(inPacket);
ChannelFuture future = NetConnection.this.channel.writeAndFlush(packet);
if (futureListeners != null)
{
channelfuture1.addListeners(futureListeners);
if(listeners != null) {
future.addListeners(listeners);
}
channelfuture1.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
future.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
});
}
}
private void flushOutboundQueue()
{
if (this.channel != null && this.channel.isOpen())
{
private void flushOutboundQueue() {
if(this.channel != null && this.channel.isOpen()) {
this.lock.readLock().lock();
try
{
while (!this.queue.isEmpty())
{
try {
while(!this.queue.isEmpty()) {
NetConnection.InboundHandlerTuplePacketListener entry = this.queue.poll();
if(entry != null) // NPE Fix
this.dispatchPacket(entry.packet, entry.listeners);
}
}
finally
{
finally {
this.lock.readLock().unlock();
}
}
}
public void processReceivedPackets()
{
public void processReceivedPackets() {
this.flushOutboundQueue();
this.handler.update();
if(this.channel != null)
@ -236,78 +196,60 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
return this.address == null ? "?.?.*.*" : IP_REPLACER.matcher(this.address.toString()).replaceAll("$1.$2.*.*");
}
public void closeChannel(String message)
{
if (this.channel.isOpen())
{
public void closeChannel(String message) {
if(this.channel.isOpen()) {
this.channel.close().awaitUninterruptibly();
this.exitReason = message;
}
}
public boolean isChannelOpen()
{
public boolean isChannelOpen() {
return this.channel != null && this.channel.isOpen();
}
public boolean hasNoChannel()
{
public boolean hasNoChannel() {
return this.channel == null;
}
public void disableAutoRead()
{
public void disableAutoRead() {
this.channel.config().setAutoRead(false);
}
public void setCompressionTreshold(int treshold)
{
if (treshold >= 0)
{
if (this.channel.pipeline().get("decompress") instanceof CompressionDecoder)
{
public void setCompressionTreshold(int treshold) {
if(treshold >= 0) {
if(this.channel.pipeline().get("decompress") instanceof CompressionDecoder) {
((CompressionDecoder)this.channel.pipeline().get("decompress")).setTreshold(treshold);
}
else
{
else {
this.channel.pipeline().addBefore("decoder", "decompress", new CompressionDecoder(treshold));
}
if (this.channel.pipeline().get("compress") instanceof CompressionEncoder)
{
if(this.channel.pipeline().get("compress") instanceof CompressionEncoder) {
((CompressionEncoder)this.channel.pipeline().get("compress")).setTreshold(treshold);
}
else
{
else {
this.channel.pipeline().addBefore("encoder", "compress", new CompressionEncoder(treshold));
}
}
else
{
if (this.channel.pipeline().get("decompress") instanceof CompressionDecoder)
{
else {
if(this.channel.pipeline().get("decompress") instanceof CompressionDecoder) {
this.channel.pipeline().remove("decompress");
}
if (this.channel.pipeline().get("compress") instanceof CompressionEncoder)
{
if(this.channel.pipeline().get("compress") instanceof CompressionEncoder) {
this.channel.pipeline().remove("compress");
}
}
}
public void checkDisconnected()
{
if (this.channel != null && !this.channel.isOpen())
{
if (!this.disconnected)
{
public void checkDisconnected() {
if(this.channel != null && !this.channel.isOpen()) {
if(!this.disconnected) {
this.disconnected = true;
if(this.handler != null)
this.handler.onDisconnect(this.exitReason != null ? this.exitReason : "Verbindung getrennt");
}
else
{
else {
Log.JNI.warn("handleDisconnection() zweifach aufgerufen");
}
}
@ -318,13 +260,11 @@ public class NetConnection extends SimpleChannelInboundHandler<Packet>
this.channel.pipeline().addBefore("prepender", "encrypt", new EncryptionEncoder(EncryptUtil.createCipher(Cipher.ENCRYPT_MODE, key)));
}
private static class InboundHandlerTuplePacketListener
{
private static class InboundHandlerTuplePacketListener {
private final Packet packet;
private final GenericFutureListener <? extends Future <? super Void >> [] listeners;
private final GenericFutureListener<? extends Future<? super Void>>[] listeners;
public InboundHandlerTuplePacketListener(Packet packet, GenericFutureListener <? extends Future <? super Void >> listener)
{
public InboundHandlerTuplePacketListener(Packet packet, GenericFutureListener<? extends Future<? super Void>> listener) {
this.packet = packet;
this.listeners = listener == null ? null : new GenericFutureListener[] {listener};
}

View file

@ -95,8 +95,7 @@ import common.packet.SPacketTrades;
import common.packet.SPacketUpdateHealth;
import common.packet.SPacketWorld;
public enum PacketRegistry
{
public enum PacketRegistry {
HANDSHAKE {{
this.client(HPacketHandshake.class);
}},