From cf59dc97205dafa77f951060510f60fb0ed647c5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 14 Jun 2022 09:47:45 +0200 Subject: [PATCH 1/4] Add MetaInfos --- .../steamwar/network/packets/MetaInfos.java | 23 ++++++++++++++ .../network/packets/NetworkPacket.java | 4 +++ .../network/packets/PacketHandler.java | 31 ++++++++++++++----- 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/de/steamwar/network/packets/MetaInfos.java diff --git a/src/de/steamwar/network/packets/MetaInfos.java b/src/de/steamwar/network/packets/MetaInfos.java new file mode 100644 index 0000000..61632d7 --- /dev/null +++ b/src/de/steamwar/network/packets/MetaInfos.java @@ -0,0 +1,23 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.network.packets; + +public interface MetaInfos { +} diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index b1d6bf3..35c68c4 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -40,6 +40,10 @@ public abstract class NetworkPacket implements Serializable { PacketHandler.handlePacket(deserialize(data)); } + public static void handle(MetaInfos metaInfos, byte[] data) { + PacketHandler.handlePacket(metaInfos, deserialize(data)); + } + @SneakyThrows public static NetworkPacket deserialize(byte[] data) { ByteArrayInputStream bais = new ByteArrayInputStream(data); diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java index f23afe5..11b9bcf 100644 --- a/src/de/steamwar/network/packets/PacketHandler.java +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -36,21 +36,34 @@ public abstract class PacketHandler { private final Map, Method> HANDLER_MAP = new HashMap<>(); public static void handlePacket(NetworkPacket packet) { + handlePacket(null, packet); + } + + public static void handlePacket(MetaInfos metaInfos, NetworkPacket packet) { for (PacketHandler handler : PACKET_HANDLERS) { - handler.handle(packet); + handler.handle(metaInfos, packet); } } protected PacketHandler() { Method[] methods = getClass().getMethods(); for (Method method : methods) { - if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { + Handler handler = method.getAnnotation(Handler.class); + if (handler == null) { continue; } - if (method.isAnnotationPresent(Handler.class)) { - Class packetClass = (Class) method.getParameterTypes()[0]; - HANDLER_MAP.put(packetClass, method); + if (!(method.getParameterCount() > 0 && method.getParameterCount() < 3)) { + continue; } + if (!NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { + continue; + } + if (method.getParameterCount() == 2 && !MetaInfos.class.isAssignableFrom(method.getParameterTypes()[1])) { + continue; + } + + Class packetClass = (Class) method.getParameterTypes()[0]; + HANDLER_MAP.put(packetClass, method); } } @@ -63,12 +76,16 @@ public abstract class PacketHandler { } @SneakyThrows - public void handle(NetworkPacket packet) { + public void handle(MetaInfos metaInfos, NetworkPacket packet) { Method method = HANDLER_MAP.get(packet.getClass()); if (method == null) { return; } - method.invoke(this, packet); + if (method.getParameterCount() == 2) { + method.invoke(this, packet, metaInfos); + } else { + method.invoke(this, packet); + } } @Retention(RetentionPolicy.RUNTIME) From 2ddfa9e80f55ff02d85509aaa28f97ab2dffeeaf Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 14 Jun 2022 13:53:26 +0200 Subject: [PATCH 2/4] Fix stuff --- src/de/steamwar/network/packets/PacketHandler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java index 11b9bcf..333bec6 100644 --- a/src/de/steamwar/network/packets/PacketHandler.java +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -82,7 +82,11 @@ public abstract class PacketHandler { return; } if (method.getParameterCount() == 2) { - method.invoke(this, packet, metaInfos); + if (metaInfos == null || method.getParameterTypes()[1].isAssignableFrom(metaInfos.getClass())) { + method.invoke(this, packet, metaInfos); + } else { + throw new IllegalArgumentException("MetaInfos is not assignable to " + method.getParameterTypes()[1]); + } } else { method.invoke(this, packet); } From ea9a4c2a81805268451a9c113e2a9993b1c5b5ad Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 14 Jun 2022 14:57:39 +0200 Subject: [PATCH 3/4] Optimize meta infos even more --- .../network/packets/NetworkPacket.java | 6 +++- .../network/packets/PacketHandler.java | 36 +++++-------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index 35c68c4..2a50056 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -27,6 +27,8 @@ import java.io.*; @EqualsAndHashCode public abstract class NetworkPacket implements Serializable { + public transient MetaInfos metaInfos; + @SneakyThrows public byte[] serialize() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -41,7 +43,9 @@ public abstract class NetworkPacket implements Serializable { } public static void handle(MetaInfos metaInfos, byte[] data) { - PacketHandler.handlePacket(metaInfos, deserialize(data)); + NetworkPacket networkPacket = deserialize(data); + networkPacket.metaInfos = metaInfos; + PacketHandler.handlePacket(networkPacket); } @SneakyThrows diff --git a/src/de/steamwar/network/packets/PacketHandler.java b/src/de/steamwar/network/packets/PacketHandler.java index 333bec6..671eb46 100644 --- a/src/de/steamwar/network/packets/PacketHandler.java +++ b/src/de/steamwar/network/packets/PacketHandler.java @@ -36,34 +36,22 @@ public abstract class PacketHandler { private final Map, Method> HANDLER_MAP = new HashMap<>(); public static void handlePacket(NetworkPacket packet) { - handlePacket(null, packet); - } - - public static void handlePacket(MetaInfos metaInfos, NetworkPacket packet) { for (PacketHandler handler : PACKET_HANDLERS) { - handler.handle(metaInfos, packet); + handler.handle(packet); } } protected PacketHandler() { Method[] methods = getClass().getMethods(); for (Method method : methods) { - Handler handler = method.getAnnotation(Handler.class); - if (handler == null) { - continue; - } - if (!(method.getParameterCount() > 0 && method.getParameterCount() < 3)) { - continue; - } - if (!NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { - continue; - } - if (method.getParameterCount() == 2 && !MetaInfos.class.isAssignableFrom(method.getParameterTypes()[1])) { + if(method.getParameterCount() != 1 || !NetworkPacket.class.isAssignableFrom(method.getParameterTypes()[0])) { continue; } - Class packetClass = (Class) method.getParameterTypes()[0]; - HANDLER_MAP.put(packetClass, method); + if (method.isAnnotationPresent(Handler.class)) { + Class packetClass = (Class) method.getParameterTypes()[0]; + HANDLER_MAP.put(packetClass, method); + } } } @@ -76,20 +64,12 @@ public abstract class PacketHandler { } @SneakyThrows - public void handle(MetaInfos metaInfos, NetworkPacket packet) { + public void handle(NetworkPacket packet) { Method method = HANDLER_MAP.get(packet.getClass()); if (method == null) { return; } - if (method.getParameterCount() == 2) { - if (metaInfos == null || method.getParameterTypes()[1].isAssignableFrom(metaInfos.getClass())) { - method.invoke(this, packet, metaInfos); - } else { - throw new IllegalArgumentException("MetaInfos is not assignable to " + method.getParameterTypes()[1]); - } - } else { - method.invoke(this, packet); - } + method.invoke(this, packet); } @Retention(RetentionPolicy.RUNTIME) From 62d2b50207001d630b736024eb5a50d997e57c94 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 14 Jun 2022 15:12:07 +0200 Subject: [PATCH 4/4] Optimize meta infos even more --- src/de/steamwar/network/packets/NetworkPacket.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/network/packets/NetworkPacket.java b/src/de/steamwar/network/packets/NetworkPacket.java index 2a50056..14797fc 100644 --- a/src/de/steamwar/network/packets/NetworkPacket.java +++ b/src/de/steamwar/network/packets/NetworkPacket.java @@ -27,7 +27,11 @@ import java.io.*; @EqualsAndHashCode public abstract class NetworkPacket implements Serializable { - public transient MetaInfos metaInfos; + private transient MetaInfos metaInfos; + + public MetaInfos getMetaInfos() { + return metaInfos; + } @SneakyThrows public byte[] serialize() {