diff --git a/common/src/common/network/NetConnection.java b/common/src/common/network/NetConnection.java index 216b3cb..c430fdb 100755 --- a/common/src/common/network/NetConnection.java +++ b/common/src/common/network/NetConnection.java @@ -23,310 +23,250 @@ import io.netty.util.AttributeKey; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; -public class NetConnection extends SimpleChannelInboundHandler -{ +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"); - - private final Queue queue = new ConcurrentLinkedQueue(); - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - - private Channel channel; - private SocketAddress address; - private NetHandler handler; - private String exitReason; - private boolean disconnected; - - public void channelActive(ChannelHandlerContext context) throws Exception - { - super.channelActive(context); - this.channel = context.channel(); - this.address = this.channel.remoteAddress(); - try - { - this.setConnectionState(PacketRegistry.HANDSHAKE); - } - catch (Throwable t) - { - Log.JNI.error(t, "Fehler beim Aufbauen der Verbindung für Handshake"); - } - } + private final Queue queue = new ConcurrentLinkedQueue(); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - public void setConnectionState(PacketRegistry newState) - { - this.channel.attr(ATTR_STATE).set(newState); - this.channel.config().setAutoRead(true); + private Channel channel; + private SocketAddress address; + private NetHandler handler; + private String exitReason; + private boolean disconnected; + + public void channelActive(ChannelHandlerContext context) throws Exception { + super.channelActive(context); + this.channel = context.channel(); + this.address = this.channel.remoteAddress(); + + try { + this.setConnectionState(PacketRegistry.HANDSHAKE); + } + catch(Throwable t) { + Log.JNI.error(t, "Fehler beim Aufbauen der Verbindung für Handshake"); + } + } + + 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 - { - this.closeChannel("Ende der Datenübertragung"); - } + public void channelInactive(ChannelHandlerContext context) throws Exception { + this.closeChannel("Ende der Datenübertragung"); + } - public void exceptionCaught(ChannelHandlerContext context, Throwable throwable) throws Exception - { - String comp; + public void exceptionCaught(ChannelHandlerContext context, Throwable throwable) throws Exception { + String comp; - if (throwable instanceof TimeoutException) - { - comp = "Zeitüberschreitung"; - } - else - { - comp = "Interner Fehler: " + throwable; - if(!(throwable instanceof ClosedChannelException)) - Log.SYSTEM.error(throwable, "Fehler in der Verbindung mit %s", this.getCutAddress()); - } + if(throwable instanceof TimeoutException) { + comp = "Zeitüberschreitung"; + } + else { + comp = "Interner Fehler: " + throwable; + if(!(throwable instanceof ClosedChannelException)) + Log.SYSTEM.error(throwable, "Fehler in der Verbindung mit %s", this.getCutAddress()); + } - this.closeChannel(comp); - } + this.closeChannel(comp); + } - protected void channelRead0(ChannelHandlerContext context, Packet packet) throws Exception - { - if (this.channel.isOpen()) - { - try - { - packet.processPacket(this.handler); - } - catch (ThreadQuickExitException e) - { - ; - } - } - } + protected void channelRead0(ChannelHandlerContext context, Packet packet) throws Exception { + if(this.channel.isOpen()) { + try { + packet.processPacket(this.handler); + } + 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; - } + this.handler = handler; + } - public void sendPacket(Packet packetIn) - { - if (this.isChannelOpen()) - { - this.flushOutboundQueue(); - this.dispatchPacket(packetIn, null); - } - else - { - this.lock.writeLock().lock(); + public void sendPacket(Packet packet) { + if(this.isChannelOpen()) { + this.flushOutboundQueue(); + this.dispatchPacket(packet, null); + } + else { + this.lock.writeLock().lock(); - try - { - this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packetIn, null)); - } - finally - { - this.lock.writeLock().unlock(); - } - } - } + try { + this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packet, null)); + } + finally { + this.lock.writeLock().unlock(); + } + } + } - public void sendPacket(Packet packetIn, GenericFutureListener > listener) - { - if (this.isChannelOpen()) - { - this.flushOutboundQueue(); - this.dispatchPacket(packetIn, new GenericFutureListener[] {listener}); - } - else - { - this.lock.writeLock().lock(); + public void sendPacket(Packet packet, GenericFutureListener> listener) { + if(this.isChannelOpen()) { + this.flushOutboundQueue(); + this.dispatchPacket(packet, new GenericFutureListener[] {listener}); + } + else { + this.lock.writeLock().lock(); - try - { - this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packetIn, listener)); - } - finally - { - this.lock.writeLock().unlock(); - } - } - } + try { + this.queue.add(new NetConnection.InboundHandlerTuplePacketListener(packet, listener)); + } + finally { + this.lock.writeLock().unlock(); + } + } + } - private void dispatchPacket(final Packet inPacket, final GenericFutureListener > [] futureListeners) - { - final PacketRegistry state = PacketRegistry.getType(inPacket); - final PacketRegistry current = this.channel.attr(ATTR_STATE).get(); + private void dispatchPacket(final Packet packet, final GenericFutureListener>[] 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); - } + this.channel.config().setAutoRead(false); + } - if (this.channel.eventLoop().inEventLoop()) - { - if (state != current) - { - this.setConnectionState(state); - } + 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); - } - else - { - this.channel.eventLoop().execute(new Runnable() - { - public void run() - { - if (state != current) - { - NetConnection.this.setConnectionState(state); - } + future.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + } + 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()) - { - this.lock.readLock().lock(); + private void flushOutboundQueue() { + if(this.channel != null && this.channel.isOpen()) { + this.lock.readLock().lock(); - try - { - while (!this.queue.isEmpty()) - { - NetConnection.InboundHandlerTuplePacketListener entry = this.queue.poll(); - if(entry != null) // NPE Fix - this.dispatchPacket(entry.packet, entry.listeners); - } - } - finally - { - this.lock.readLock().unlock(); - } - } - } + try { + while(!this.queue.isEmpty()) { + NetConnection.InboundHandlerTuplePacketListener entry = this.queue.poll(); + if(entry != null) // NPE Fix + this.dispatchPacket(entry.packet, entry.listeners); + } + } + finally { + this.lock.readLock().unlock(); + } + } + } + + public void processReceivedPackets() { + this.flushOutboundQueue(); + this.handler.update(); + if(this.channel != null) + this.channel.flush(); + } - public void processReceivedPackets() - { - this.flushOutboundQueue(); - this.handler.update(); - if(this.channel != null) - this.channel.flush(); - } - public String getCutAddress() { return this.address == null ? "?.?.*.*" : IP_REPLACER.matcher(this.address.toString()).replaceAll("$1.$2.*.*"); } - public void closeChannel(String message) - { - if (this.channel.isOpen()) - { - this.channel.close().awaitUninterruptibly(); - this.exitReason = message; - } - } + public void closeChannel(String message) { + if(this.channel.isOpen()) { + this.channel.close().awaitUninterruptibly(); + this.exitReason = message; + } + } - public boolean isChannelOpen() - { - return this.channel != null && this.channel.isOpen(); - } + public boolean isChannelOpen() { + return this.channel != null && this.channel.isOpen(); + } - public boolean hasNoChannel() - { - return this.channel == null; - } + public boolean hasNoChannel() { + return this.channel == null; + } - public void disableAutoRead() - { - this.channel.config().setAutoRead(false); - } + public void disableAutoRead() { + this.channel.config().setAutoRead(false); + } - 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 - { - this.channel.pipeline().addBefore("decoder", "decompress", new CompressionDecoder(treshold)); - } + 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 { + this.channel.pipeline().addBefore("decoder", "decompress", new CompressionDecoder(treshold)); + } - if (this.channel.pipeline().get("compress") instanceof CompressionEncoder) - { - ((CompressionEncoder)this.channel.pipeline().get("compress")).setTreshold(treshold); - } - else - { - this.channel.pipeline().addBefore("encoder", "compress", new CompressionEncoder(treshold)); - } - } - else - { - if (this.channel.pipeline().get("decompress") instanceof CompressionDecoder) - { - this.channel.pipeline().remove("decompress"); - } + if(this.channel.pipeline().get("compress") instanceof CompressionEncoder) { + ((CompressionEncoder)this.channel.pipeline().get("compress")).setTreshold(treshold); + } + else { + this.channel.pipeline().addBefore("encoder", "compress", new CompressionEncoder(treshold)); + } + } + else { + if(this.channel.pipeline().get("decompress") instanceof CompressionDecoder) { + this.channel.pipeline().remove("decompress"); + } - if (this.channel.pipeline().get("compress") instanceof CompressionEncoder) - { - this.channel.pipeline().remove("compress"); - } - } - } + 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) - { - this.disconnected = true; - if(this.handler != null) - this.handler.onDisconnect(this.exitReason != null ? this.exitReason : "Verbindung getrennt"); - } - else - { - Log.JNI.warn("handleDisconnection() zweifach aufgerufen"); - } - } - } - - public void startEncryption(SecretKey key) { - this.channel.pipeline().addBefore("splitter", "decrypt", new EncryptionDecoder(EncryptUtil.createCipher(Cipher.DECRYPT_MODE, key))); - this.channel.pipeline().addBefore("prepender", "encrypt", new EncryptionEncoder(EncryptUtil.createCipher(Cipher.ENCRYPT_MODE, key))); - } + 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 { + Log.JNI.warn("handleDisconnection() zweifach aufgerufen"); + } + } + } - private static class InboundHandlerTuplePacketListener - { - private final Packet packet; - private final GenericFutureListener > [] listeners; + public void startEncryption(SecretKey key) { + this.channel.pipeline().addBefore("splitter", "decrypt", new EncryptionDecoder(EncryptUtil.createCipher(Cipher.DECRYPT_MODE, key))); + this.channel.pipeline().addBefore("prepender", "encrypt", new EncryptionEncoder(EncryptUtil.createCipher(Cipher.ENCRYPT_MODE, key))); + } - public InboundHandlerTuplePacketListener(Packet packet, GenericFutureListener > listener) - { - this.packet = packet; - this.listeners = listener == null ? null : new GenericFutureListener[] {listener}; - } - } + private static class InboundHandlerTuplePacketListener { + private final Packet packet; + private final GenericFutureListener>[] listeners; + + public InboundHandlerTuplePacketListener(Packet packet, GenericFutureListener> listener) { + this.packet = packet; + this.listeners = listener == null ? null : new GenericFutureListener[] {listener}; + } + } } diff --git a/common/src/common/network/PacketRegistry.java b/common/src/common/network/PacketRegistry.java index a1930de..e896dbb 100755 --- a/common/src/common/network/PacketRegistry.java +++ b/common/src/common/network/PacketRegistry.java @@ -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); }},